implement new hook api for subversion repositories

This commit is contained in:
Sebastian Sdorra
2013-07-24 21:01:30 +02:00
parent 5bc88aff2e
commit adf863b985
6 changed files with 531 additions and 16 deletions

View File

@@ -62,6 +62,7 @@ import sonia.scm.util.Util;
import java.io.File;
import java.io.IOException;
import sonia.scm.repository.spi.HookEventFacade;
/**
*
@@ -107,7 +108,7 @@ public class SvnRepositoryHandler
*/
@Inject
public SvnRepositoryHandler(StoreFactory storeFactory, FileSystem fileSystem,
RepositoryManager repositoryManager)
HookEventFacade eventFacade)
{
super(storeFactory, fileSystem);
@@ -118,9 +119,9 @@ public class SvnRepositoryHandler
FSRepositoryFactory.setup();
// register hook
if (repositoryManager != null)
if (eventFacade != null)
{
FSHooks.registerHook(new SvnRepositoryHook(repositoryManager, this));
FSHooks.registerHook(new SvnRepositoryHook(eventFacade, this));
}
else if (logger.isWarnEnabled())
{

View File

@@ -46,6 +46,11 @@ import org.tmatesoft.svn.core.internal.io.fs.FSHook;
import org.tmatesoft.svn.core.internal.io.fs.FSHookEvent;
import org.tmatesoft.svn.core.internal.io.fs.FSHooks;
import sonia.scm.repository.spi.AbstractSvnHookChangesetProvider;
import sonia.scm.repository.spi.HookEventFacade;
import sonia.scm.repository.spi.SvnHookContextProvider;
import sonia.scm.repository.spi.SvnPostReceiveHookChangesetProvier;
import sonia.scm.repository.spi.SvnPreReceiveHookChangesetProvier;
import sonia.scm.util.AssertUtil;
import sonia.scm.util.IOUtil;
import sonia.scm.util.Util;
@@ -72,13 +77,14 @@ public class SvnRepositoryHook implements FSHook
* Constructs ...
*
*
* @param repositoryManager
*
* @param hookEventFacade
* @param handler
*/
public SvnRepositoryHook(RepositoryManager repositoryManager,
SvnRepositoryHandler handler)
public SvnRepositoryHook(HookEventFacade hookEventFacade,
SvnRepositoryHandler handler)
{
this.repositoryManager = repositoryManager;
this.hookEventFacade = hookEventFacade;
this.handler = handler;
}
@@ -107,7 +113,8 @@ public class SvnRepositoryHook implements FSHook
{
long revision = Long.parseLong(args[0]);
fireHook(directory, new SvnPostReceiveHookEvent(directory, revision));
fireHook(directory,
new SvnPostReceiveHookChangesetProvier(directory, revision));
}
catch (NumberFormatException ex)
{
@@ -129,7 +136,8 @@ public class SvnRepositoryHook implements FSHook
if (Util.isNotEmpty(tx))
{
fireHook(directory, new SvnPreReceiveHookEvent(directory, tx));
fireHook(directory,
new SvnPreReceiveHookChangesetProvier(directory, tx));
}
else if (logger.isWarnEnabled())
{
@@ -148,20 +156,27 @@ public class SvnRepositoryHook implements FSHook
*
*
* @param directory
* @param hookEvent
* @param changesetProvider
*
* @throws SVNCancelException
*/
private void fireHook(File directory, RepositoryHookEvent hookEvent)
throws SVNCancelException
private void fireHook(File directory,
AbstractSvnHookChangesetProvider changesetProvider)
throws SVNCancelException
{
try
{
String name = getRepositoryName(directory);
name = IOUtil.trimSeperatorChars(name);
repositoryManager.fireHookEvent(SvnRepositoryHandler.TYPE_NAME, name,
hookEvent);
//J-
hookEventFacade.handle(SvnRepositoryHandler.TYPE_NAME, name)
.fireHookEvent(
changesetProvider.getType(),
new SvnHookContextProvider(changesetProvider)
);
//J+
}
catch (Exception ex)
{
@@ -171,7 +186,7 @@ public class SvnRepositoryHook implements FSHook
}
throw new SVNCancelException(
SVNErrorMessage.create(SVNErrorCode.CANCELLED, ex.getMessage()));
SVNErrorMessage.create(SVNErrorCode.CANCELLED, ex.getMessage()));
}
}
@@ -200,5 +215,5 @@ public class SvnRepositoryHook implements FSHook
private SvnRepositoryHandler handler;
/** Field description */
private RepositoryManager repositoryManager;
private HookEventFacade hookEventFacade;
}

View File

@@ -0,0 +1,96 @@
/**
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. 2. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
* nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.collect.ImmutableSet;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.RepositoryHookType;
//~--- JDK imports ------------------------------------------------------------
import java.util.Collections;
/**
*
* @author Sebastian Sdorra
*/
public abstract class AbstractSvnHookChangesetProvider
implements HookChangesetProvider
{
/**
* Method description
*
*
* @return
*/
public abstract RepositoryHookType getType();
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @return
*/
protected abstract Changeset fetchChangeset();
/**
* Method description
*
*
* @param request
*
* @return
*/
@Override
public HookChangesetResponse handleRequest(HookChangesetRequest request)
{
Changeset c = fetchChangeset();
Iterable<Changeset> iterable;
if (c == null)
{
iterable = Collections.EMPTY_SET;
}
else
{
iterable = ImmutableSet.of(c);
}
return new HookChangesetResponse(iterable);
}
}

View File

@@ -0,0 +1,98 @@
/**
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. 2. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
* nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.repository.api.HookFeature;
//~--- JDK imports ------------------------------------------------------------
import java.util.EnumSet;
import java.util.Set;
/**
*
* @author Sebastian Sdorra
*/
public class SvnHookContextProvider extends HookContextProvider
{
/** Field description */
private static final Set<HookFeature> SUPPORTED_FEATURES =
EnumSet.of(HookFeature.CHANGESET_PROVIDER);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param changesetProvider
*/
public SvnHookContextProvider(
AbstractSvnHookChangesetProvider changesetProvider)
{
this.changesetProvider = changesetProvider;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public AbstractSvnHookChangesetProvider getChangesetProvider()
{
return changesetProvider;
}
/**
* Method description
*
*
* @return
*/
@Override
public Set<HookFeature> getSupportedFeatures()
{
return SUPPORTED_FEATURES;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private AbstractSvnHookChangesetProvider changesetProvider;
}

View File

@@ -0,0 +1,150 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.RepositoryHookType;
import sonia.scm.repository.SvnUtil;
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.util.Collection;
/**
*
* @author Sebastian Sdorra
*/
public class SvnPostReceiveHookChangesetProvier
extends AbstractSvnHookChangesetProvider
{
/** the logger for SvnPostReceiveHookChangesetProvier */
private static final Logger logger =
LoggerFactory.getLogger(SvnPostReceiveHookChangesetProvier.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param repositoryDirectory
* @param revision
*/
public SvnPostReceiveHookChangesetProvier(File repositoryDirectory, long revision)
{
this.repositoryDirectory = repositoryDirectory;
this.revision = revision;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public RepositoryHookType getType()
{
return RepositoryHookType.POST_RECEIVE;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
protected Changeset fetchChangeset()
{
Changeset changeset = null;
SVNRepository repository = null;
try
{
repository =
SVNRepositoryFactory.create(SVNURL.fromFile(repositoryDirectory));
Collection<SVNLogEntry> enties = repository.log(new String[] { "" },
null, revision, revision, true, true);
if (Util.isNotEmpty(enties))
{
SVNLogEntry entry = enties.iterator().next();
if (entry != null)
{
changeset = SvnUtil.createChangeset(entry);
}
}
}
catch (SVNException ex)
{
logger.error("could not open repository", ex);
}
finally
{
SvnUtil.closeSession(repository);
}
return changeset;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private File repositoryDirectory;
/** Field description */
private long revision;
}

View File

@@ -0,0 +1,155 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
import org.tmatesoft.svn.core.wc.admin.SVNLookClient;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.RepositoryHookType;
import sonia.scm.repository.SvnModificationHandler;
import sonia.scm.repository.SvnUtil;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
/**
*
* @author Sebastian Sdorra
*/
public class SvnPreReceiveHookChangesetProvier
extends AbstractSvnHookChangesetProvider
{
/** the logger for SvnPreReceiveHookChangesetProvier */
private static final Logger logger =
LoggerFactory.getLogger(SvnPreReceiveHookChangesetProvier.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param repositoryDirectory
* @param transaction
*/
public SvnPreReceiveHookChangesetProvier(File repositoryDirectory,
String transaction)
{
this.repositoryDirectory = repositoryDirectory;
this.transaction = transaction;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public RepositoryHookType getType()
{
return RepositoryHookType.PRE_RECEIVE;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
protected Changeset fetchChangeset()
{
Changeset changeset = null;
SVNClientManager cm = null;
try
{
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
cm = SVNClientManager.newInstance(options);
SVNLookClient clientManager = cm.getLookClient();
SVNLogEntry entry = clientManager.doGetInfo(repositoryDirectory,
transaction);
if (entry != null)
{
changeset = SvnUtil.createChangeset(entry);
clientManager.doGetChanged(repositoryDirectory, transaction,
new SvnModificationHandler(changeset), true);
}
else if (logger.isWarnEnabled())
{
logger.warn("could not find log entry for pre receive hook");
}
}
catch (Exception ex)
{
logger.error(
"could not fetch changesets for transaction ".concat(transaction), ex);
}
finally
{
SvnUtil.dispose(cm);
}
return changeset;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private File repositoryDirectory;
/** Field description */
private String transaction;
}