mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 07:55:47 +01:00
rebuild repository hook api
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
* @since 1.6
|
||||
*/
|
||||
public abstract class AbstractRepositoryHookEvent implements RepositoryHookEvent
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Repository getRepository()
|
||||
{
|
||||
return repository;
|
||||
}
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*/
|
||||
@Override
|
||||
public void setRepository(Repository repository)
|
||||
{
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private Repository repository;
|
||||
}
|
||||
@@ -35,14 +35,21 @@ package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.HandlerEvent;
|
||||
import sonia.scm.util.AssertUtil;
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.EnumMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -52,16 +59,76 @@ import java.util.Set;
|
||||
public abstract class AbstractRepositoryManager implements RepositoryManager
|
||||
{
|
||||
|
||||
/** the logger for AbstractRepositoryManager */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(AbstractRepositoryManager.class);
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param hook
|
||||
* @param repository
|
||||
* @param changesets
|
||||
* @param event
|
||||
*/
|
||||
protected abstract void firePostReceiveEvent(PostReceiveHook hook,
|
||||
Repository repository, List<Changeset> changesets);
|
||||
protected abstract void fireHookEvent(RepositoryHook hook,
|
||||
RepositoryHookEvent event);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param hook
|
||||
*/
|
||||
@Override
|
||||
public void addHook(RepositoryHook hook)
|
||||
{
|
||||
Collection<RepositoryHookType> types = hook.getTypes();
|
||||
|
||||
if (types != null)
|
||||
{
|
||||
for (RepositoryHookType type : types)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("register {} hook {}", type, hook.getClass());
|
||||
}
|
||||
|
||||
synchronized (AbstractRepositoryManager.class)
|
||||
{
|
||||
List<RepositoryHook> hooks = hookMap.get(type);
|
||||
|
||||
if (hooks == null)
|
||||
{
|
||||
hooks = new ArrayList<RepositoryHook>();
|
||||
hookMap.put(type, hooks);
|
||||
}
|
||||
|
||||
hooks.add(hook);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("could not find any repository type");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param hooks
|
||||
*/
|
||||
@Override
|
||||
public void addHooks(Collection<RepositoryHook> hooks)
|
||||
{
|
||||
for (RepositoryHook hook : hooks)
|
||||
{
|
||||
addHook(hook);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
@@ -87,6 +154,32 @@ public abstract class AbstractRepositoryManager implements RepositoryManager
|
||||
listenerSet.addAll(listeners);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
* @param event
|
||||
*/
|
||||
@Override
|
||||
public void fireHookEvent(Repository repository, RepositoryHookEvent event)
|
||||
{
|
||||
AssertUtil.assertIsNotNull(repository);
|
||||
AssertUtil.assertIsNotNull(event);
|
||||
AssertUtil.assertIsNotNull(event.getType());
|
||||
event.setRepository(repository);
|
||||
|
||||
List<RepositoryHook> hooks = hookMap.get(event.getType());
|
||||
|
||||
if (Util.isNotEmpty(hooks))
|
||||
{
|
||||
for (RepositoryHook hook : hooks)
|
||||
{
|
||||
fireHookEvent(hook, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -94,40 +187,21 @@ public abstract class AbstractRepositoryManager implements RepositoryManager
|
||||
* @param hook
|
||||
*/
|
||||
@Override
|
||||
public void addPostReceiveHook(PostReceiveHook hook)
|
||||
public void removeHook(RepositoryHook hook)
|
||||
{
|
||||
postReceiveHookSet.add(hook);
|
||||
}
|
||||
Collection<RepositoryHookType> types = hook.getTypes();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param hooks
|
||||
*/
|
||||
@Override
|
||||
public void addPostReceiveHooks(Collection<PostReceiveHook> hooks)
|
||||
{
|
||||
postReceiveHookSet.addAll(hooks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
* @param changesets
|
||||
*/
|
||||
@Override
|
||||
public void firePostReceiveEvent(Repository repository,
|
||||
List<Changeset> changesets)
|
||||
{
|
||||
AssertUtil.assertIsNotNull(repository);
|
||||
AssertUtil.assertIsNotEmpty(changesets);
|
||||
|
||||
for (PostReceiveHook hook : postReceiveHookSet)
|
||||
if (types != null)
|
||||
{
|
||||
firePostReceiveEvent(hook, repository, changesets);
|
||||
for (RepositoryHookType type : types)
|
||||
{
|
||||
List<RepositoryHook> hooks = hookMap.get(type);
|
||||
|
||||
if (hooks != null)
|
||||
{
|
||||
hooks.remove(hook);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,18 +217,6 @@ public abstract class AbstractRepositoryManager implements RepositoryManager
|
||||
listenerSet.remove(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param hook
|
||||
*/
|
||||
@Override
|
||||
public void removePostReceiveHook(PostReceiveHook hook)
|
||||
{
|
||||
removePostReceiveHook(hook);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -173,8 +235,9 @@ public abstract class AbstractRepositoryManager implements RepositoryManager
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private Set<PostReceiveHook> postReceiveHookSet =
|
||||
new HashSet<PostReceiveHook>();
|
||||
private Map<RepositoryHookType, List<RepositoryHook>> hookMap =
|
||||
new EnumMap<RepositoryHookType,
|
||||
List<RepositoryHook>>(RepositoryHookType.class);
|
||||
|
||||
/** Field description */
|
||||
private Set<RepositoryListener> listenerSet =
|
||||
|
||||
@@ -33,40 +33,43 @@
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.plugin.ExtensionPoint;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
import sonia.scm.plugin.ExtensionPoint;
|
||||
|
||||
/**
|
||||
* Hook for post receive events.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
* @since 1.6
|
||||
*/
|
||||
@ExtensionPoint
|
||||
public interface PostReceiveHook
|
||||
public interface RepositoryHook
|
||||
{
|
||||
|
||||
/**
|
||||
* This method is invoked after a repository has changed.
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository that has changed
|
||||
* @param changesets which modified the repository
|
||||
* @param event
|
||||
*/
|
||||
public void onPostReceive(Repository repository, List<Changeset> changesets);
|
||||
public void onEvent(RepositoryHookEvent event);
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns true if the hook is executed asynchronous.
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return true if the hook is executed asynchronous
|
||||
* @return
|
||||
*/
|
||||
public boolean isAsynchronous();
|
||||
public Collection<RepositoryHookType> getTypes();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isAsync();
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* 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;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
* @since 1.6
|
||||
*/
|
||||
public interface RepositoryHookEvent
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Collection<Changeset> getChangesets();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Repository getRepository();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public RepositoryHookType getType();
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*/
|
||||
void setRepository(Repository repository);
|
||||
}
|
||||
@@ -36,31 +36,29 @@ package sonia.scm.repository;
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Support for post receive hooks.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
* @since 1.6
|
||||
*/
|
||||
public interface PostReceiveHookSupport
|
||||
public interface RepositoryHookSupport
|
||||
{
|
||||
|
||||
/**
|
||||
* Registers a new {@link PostReceiveHook}.
|
||||
* Registers a new {@link RepositoryHook}.
|
||||
*
|
||||
*
|
||||
* @param hook to register
|
||||
*/
|
||||
public void addPostReceiveHook(PostReceiveHook hook);
|
||||
|
||||
public void addHook(RepositoryHook hook);
|
||||
|
||||
/**
|
||||
* Register a {@link java.util.Collection} of hooks.
|
||||
*
|
||||
*
|
||||
* @param hooks to register
|
||||
*/
|
||||
public void addPostReceiveHooks(Collection<PostReceiveHook> hooks);
|
||||
public void addHooks(Collection<RepositoryHook> hooks);
|
||||
|
||||
/**
|
||||
* Fires a post receive hook event. This methods calls the
|
||||
@@ -69,10 +67,9 @@ public interface PostReceiveHookSupport
|
||||
*
|
||||
*
|
||||
* @param repository that has changed
|
||||
* @param changesets which modified the repository
|
||||
* @param event
|
||||
*/
|
||||
public void firePostReceiveEvent(Repository repository,
|
||||
List<Changeset> changesets);
|
||||
public void fireHookEvent(Repository repository, RepositoryHookEvent event);
|
||||
|
||||
/**
|
||||
* Fires a post receive hook event. This methods calls the
|
||||
@@ -82,33 +79,32 @@ public interface PostReceiveHookSupport
|
||||
*
|
||||
* @param type of the repository
|
||||
* @param name of the repository
|
||||
* @param changesets which modified the repository
|
||||
* @param event
|
||||
*
|
||||
* @throws RepositoryNotFoundException if the repository could not be found.
|
||||
*/
|
||||
public void firePostReceiveEvent(String type, String name,
|
||||
List<Changeset> changesets)
|
||||
public void fireHookEvent(String type, String name, RepositoryHookEvent event)
|
||||
throws RepositoryNotFoundException;
|
||||
|
||||
/**
|
||||
* Fires a post receive hook event. This methods calls the
|
||||
* {@link PostReceiveHook#onPostReceive(Repository, List)} of each registered
|
||||
* {@link PostReceiveHook}.
|
||||
* Fires a hook event. This methods calls the
|
||||
* {@link RepositoryHook#onEvent(RepositoryHookEvent} of each registered
|
||||
* {@link RepositoryHook}.
|
||||
*
|
||||
*
|
||||
* @param id of the repository
|
||||
* @param changesets which modified the repository
|
||||
* @param event
|
||||
*
|
||||
* @throws RepositoryNotFoundException if the repository could not be found
|
||||
*/
|
||||
public void firePostReceiveEvent(String id, List<Changeset> changesets)
|
||||
public void fireHookEvent(String id, RepositoryHookEvent event)
|
||||
throws RepositoryNotFoundException;
|
||||
|
||||
/**
|
||||
* Unregisters the given {@link PostReceiveHook}.
|
||||
* Unregisters the given {@link RepositoryHook}.
|
||||
*
|
||||
*
|
||||
* @param hook to unregister
|
||||
*/
|
||||
public void removePostReceiveHook(PostReceiveHook hook);
|
||||
public void removeHook(RepositoryHook hook);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
* @since 1.6
|
||||
*/
|
||||
public enum RepositoryHookType { POST_RECEIVE }
|
||||
@@ -50,7 +50,7 @@ import java.util.Collection;
|
||||
public interface RepositoryManager
|
||||
extends TypeManager<Repository, RepositoryException>,
|
||||
ListenerSupport<RepositoryListener>, RepositoryBrowserProvider,
|
||||
PostReceiveHookSupport
|
||||
RepositoryHookSupport
|
||||
{
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user