From c51704f2ee54e633212af95a0d7e5824fe13c90f Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sun, 17 Jul 2011 17:06:11 +0200 Subject: [PATCH] added api for PostReceiveHooks --- .../repository/AbstractRepositoryManager.java | 61 +++++++++++++++ .../repository/PostReceiveHookSupport.java | 76 +++++++++++++++++++ .../scm/repository/RepositoryManager.java | 3 +- .../repository/xml/XmlRepositoryManager.java | 25 ++++++ 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 scm-core/src/main/java/sonia/scm/repository/PostReceiveHookSupport.java diff --git a/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryManager.java b/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryManager.java index 3d5166a291..9f1ae1ce4b 100644 --- a/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryManager.java +++ b/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryManager.java @@ -36,11 +36,13 @@ package sonia.scm.repository; //~--- non-JDK imports -------------------------------------------------------- import sonia.scm.HandlerEvent; +import sonia.scm.util.AssertUtil; //~--- JDK imports ------------------------------------------------------------ import java.util.Collection; import java.util.HashSet; +import java.util.List; import java.util.Set; /** @@ -50,6 +52,17 @@ import java.util.Set; public abstract class AbstractRepositoryManager implements RepositoryManager { + /** + * Method description + * + * + * @param hook + * @param repository + * @param changesets + */ + protected abstract void firePostReceiveEvent(PostReceiveHook hook, + Repository repository, List changesets); + /** * Method description * @@ -74,6 +87,38 @@ public abstract class AbstractRepositoryManager implements RepositoryManager listenerSet.addAll(listeners); } + /** + * Method description + * + * + * @param hook + */ + @Override + public void addPostReceiveHook(PostReceiveHook hook) + { + postReceiveHookSet.add(hook); + } + + /** + * Method description + * + * + * @param repository + * @param changesets + */ + @Override + public void firePostReceiveEvent(Repository repository, + List changesets) + { + AssertUtil.assertIsNotNull(repository); + AssertUtil.assertIsNotEmpty(changesets); + + for (PostReceiveHook hook : postReceiveHookSet) + { + firePostReceiveEvent(hook, repository, changesets); + } + } + /** * Method description * @@ -86,6 +131,18 @@ public abstract class AbstractRepositoryManager implements RepositoryManager listenerSet.remove(listener); } + /** + * Method description + * + * + * @param hook + */ + @Override + public void removePostReceiveHook(PostReceiveHook hook) + { + removePostReceiveHook(hook); + } + /** * Method description * @@ -103,6 +160,10 @@ public abstract class AbstractRepositoryManager implements RepositoryManager //~--- fields --------------------------------------------------------------- + /** Field description */ + private Set postReceiveHookSet = + new HashSet(); + /** Field description */ private Set listenerSet = new HashSet(); diff --git a/scm-core/src/main/java/sonia/scm/repository/PostReceiveHookSupport.java b/scm-core/src/main/java/sonia/scm/repository/PostReceiveHookSupport.java new file mode 100644 index 0000000000..c0782f4e29 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/repository/PostReceiveHookSupport.java @@ -0,0 +1,76 @@ +/** + * 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.List; + +/** + * Support for post receive hooks. + * + * @author Sebastian Sdorra + * @since 1.6 + */ +public interface PostReceiveHookSupport +{ + + /** + * Registers a new {@link PostReceiveHook}. + * + * + * @param hook to register + */ + public void addPostReceiveHook(PostReceiveHook hook); + + /** + * Fires a post receive hook event. This methods calls the + * {@link PostReceiveHook#onPostReceive(Repository, List)} of each registered + * {@link PostReceiveHook}. + * + * + * @param repository that has changed + * @param changesets which modified the repository + */ + public void firePostReceiveEvent(Repository repository, + List changesets); + + /** + * Unregisters the given {@link PostReceiveHook}. + * + * + * @param hook to unregister + */ + public void removePostReceiveHook(PostReceiveHook hook); +} diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryManager.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryManager.java index 9a81c26be6..5bfc6283e1 100644 --- a/scm-core/src/main/java/sonia/scm/repository/RepositoryManager.java +++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryManager.java @@ -49,7 +49,8 @@ import java.util.Collection; */ public interface RepositoryManager extends TypeManager, - ListenerSupport, RepositoryBrowserProvider + ListenerSupport, RepositoryBrowserProvider, + PostReceiveHookSupport { /** diff --git a/scm-webapp/src/main/java/sonia/scm/repository/xml/XmlRepositoryManager.java b/scm-webapp/src/main/java/sonia/scm/repository/xml/XmlRepositoryManager.java index 888eed97f8..d18a66b25b 100644 --- a/scm-webapp/src/main/java/sonia/scm/repository/xml/XmlRepositoryManager.java +++ b/scm-webapp/src/main/java/sonia/scm/repository/xml/XmlRepositoryManager.java @@ -47,9 +47,11 @@ import sonia.scm.HandlerEvent; import sonia.scm.SCMContextProvider; import sonia.scm.Type; import sonia.scm.repository.AbstractRepositoryManager; +import sonia.scm.repository.Changeset; import sonia.scm.repository.ChangesetViewer; import sonia.scm.repository.PermissionType; import sonia.scm.repository.PermissionUtil; +import sonia.scm.repository.PostReceiveHook; import sonia.scm.repository.Repository; import sonia.scm.repository.RepositoryAllreadyExistExeption; import sonia.scm.repository.RepositoryBrowser; @@ -560,6 +562,29 @@ public class XmlRepositoryManager extends AbstractRepositoryManager //~--- methods -------------------------------------------------------------- + /** + * Method description + * + * + * @param hook + * @param repository + * @param changesets + */ + @Override + protected void firePostReceiveEvent(PostReceiveHook hook, + Repository repository, List changesets) + { + if (hook.isAsynchronous()) + { + + // todo + } + else + { + hook.onPostReceive(repository, changesets); + } + } + /** * Method description *