From 8a9844b2afc4e30e60759c03a1428dc99a13619e Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Thu, 24 Jun 2010 11:12:40 -0700 Subject: [PATCH] Redo event listeners to be more generic Replace the old crude event listener system with a much more generic implementation, patterned after the event dispatch techniques used in Google Web Toolkit 1.5 and later. Each event delivers to an interface that defines a single method, and the event itself is what performs the delivery in a type-safe way through its own dispatch method. Listeners are registered in a generic listener list, indexed by the interface they implement and wish to receive an event for. Delivery of events is performed by looping through all listeners implementing the event's corresponding listener interface, and using the event's own dispatch method to deliver the event. This is the classical "double dispatch" pattern for event delivery. Listeners can be unregistered by invoking remove() on their registration handle. This change therefore requires application code to track the handle if it wishes to remove the listener at a later point in time. Event delivery is now exposed as a generic public method on the Repository class, making it easier for any type of message to be sent out to any type of listener that has registered, without needing to pre-arrange for type-safe fireFoo() methods. New event types can be added in the future simply by defining a new RepositoryEvent subclass and a corresponding RepositoryListener interface that it dispatches to. By always adding new events through a new interface, we never need to worry about defining an Adapter to provide default no-op implementations of new event methods. Change-Id: I651417b3098b9afc93d91085e9f0b2265df8fc81 Signed-off-by: Shawn O. Pearce --- org.eclipse.jgit/META-INF/MANIFEST.MF | 1 + .../{lib => events}/IndexChangedEvent.java | 28 ++-- .../IndexChangedListener.java} | 25 ++-- .../ListenerHandle.java} | 40 +++--- .../org/eclipse/jgit/events/ListenerList.java | 130 ++++++++++++++++++ .../eclipse/jgit/events/RefsChangedEvent.java | 57 ++++++++ .../RefsChangedListener.java} | 31 ++--- .../eclipse/jgit/events/RepositoryEvent.java | 95 +++++++++++++ .../{lib => events}/RepositoryListener.java | 28 +--- .../src/org/eclipse/jgit/lib/GitIndex.java | 5 +- .../org/eclipse/jgit/lib/RefDirectory.java | 3 +- .../src/org/eclipse/jgit/lib/Repository.java | 98 ++++--------- 12 files changed, 371 insertions(+), 170 deletions(-) rename org.eclipse.jgit/src/org/eclipse/jgit/{lib => events}/IndexChangedEvent.java (74%) rename org.eclipse.jgit/src/org/eclipse/jgit/{lib/RepositoryAdapter.java => events/IndexChangedListener.java} (84%) rename org.eclipse.jgit/src/org/eclipse/jgit/{lib/RefsChangedEvent.java => events/ListenerHandle.java} (75%) create mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/events/ListenerList.java create mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/events/RefsChangedEvent.java rename org.eclipse.jgit/src/org/eclipse/jgit/{lib/RepositoryChangedEvent.java => events/RefsChangedListener.java} (78%) create mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/events/RepositoryEvent.java rename org.eclipse.jgit/src/org/eclipse/jgit/{lib => events}/RepositoryListener.java (77%) diff --git a/org.eclipse.jgit/META-INF/MANIFEST.MF b/org.eclipse.jgit/META-INF/MANIFEST.MF index 258e6781b..81441dbdb 100644 --- a/org.eclipse.jgit/META-INF/MANIFEST.MF +++ b/org.eclipse.jgit/META-INF/MANIFEST.MF @@ -9,6 +9,7 @@ Export-Package: org.eclipse.jgit;version="0.9.0", org.eclipse.jgit.api;version="0.9.0", org.eclipse.jgit.diff;version="0.9.0", org.eclipse.jgit.dircache;version="0.9.0", + org.eclipse.jgit.events;version="0.9.0", org.eclipse.jgit.errors;version="0.9.0", org.eclipse.jgit.fnmatch;version="0.9.0", org.eclipse.jgit.lib;version="0.9.0", diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/IndexChangedEvent.java b/org.eclipse.jgit/src/org/eclipse/jgit/events/IndexChangedEvent.java similarity index 74% rename from org.eclipse.jgit/src/org/eclipse/jgit/lib/IndexChangedEvent.java rename to org.eclipse.jgit/src/org/eclipse/jgit/events/IndexChangedEvent.java index c866db531..a54288ee9 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/IndexChangedEvent.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/events/IndexChangedEvent.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008, Robin Rosenberg + * Copyright (C) 2010, Google Inc. * and other copyright owners as documented in the project's IP log. * * This program and the accompanying materials are made available @@ -41,27 +41,17 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.eclipse.jgit.lib; +package org.eclipse.jgit.events; -/** - * This class passes information about a changed Git index to a - * {@link RepositoryListener} - * - * Currently only a reference to the repository is passed. - */ -public class IndexChangedEvent extends RepositoryChangedEvent { - /** - * Create an event describing index changes in a repository. - * - * @param repository - * the repository whose index (DirCache) recently changed. - */ - public IndexChangedEvent(final Repository repository) { - super(repository); +/** Describes a change to one or more paths in the index file. */ +public class IndexChangedEvent extends RepositoryEvent { + @Override + public Class getListenerType() { + return IndexChangedListener.class; } @Override - public String toString() { - return "IndexChangedEvent[" + getRepository() + "]"; + public void dispatch(IndexChangedListener listener) { + listener.onIndexChanged(this); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryAdapter.java b/org.eclipse.jgit/src/org/eclipse/jgit/events/IndexChangedListener.java similarity index 84% rename from org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryAdapter.java rename to org.eclipse.jgit/src/org/eclipse/jgit/events/IndexChangedListener.java index e43c33ad7..d41ef74ee 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryAdapter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/events/IndexChangedListener.java @@ -41,20 +41,15 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.eclipse.jgit.lib; - -/** - * A default {@link RepositoryListener} that does nothing except invoke an - * optional general method for any repository change. - */ -public class RepositoryAdapter implements RepositoryListener { - - public void indexChanged(final IndexChangedEvent e) { - // Empty - } - - public void refsChanged(final RefsChangedEvent e) { - // Empty - } +package org.eclipse.jgit.events; +/** Receives {@link IndexChangedEvent}s. */ +public interface IndexChangedListener extends RepositoryListener { + /** + * Invoked when any change is made to the index. + * + * @param event + * information about the changes. + */ + void onIndexChanged(IndexChangedEvent event); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefsChangedEvent.java b/org.eclipse.jgit/src/org/eclipse/jgit/events/ListenerHandle.java similarity index 75% rename from org.eclipse.jgit/src/org/eclipse/jgit/lib/RefsChangedEvent.java rename to org.eclipse.jgit/src/org/eclipse/jgit/events/ListenerHandle.java index 705c6138e..ef90b2205 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefsChangedEvent.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/events/ListenerHandle.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008, Robin Rosenberg + * Copyright (C) 2010, Google Inc. * and other copyright owners as documented in the project's IP log. * * This program and the accompanying materials are made available @@ -41,27 +41,31 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.eclipse.jgit.lib; +package org.eclipse.jgit.events; -/** - * This class passes information about a changed Git index to a - * {@link RepositoryListener} - * - * Currently only a reference to the repository is passed. - */ -public class RefsChangedEvent extends RepositoryChangedEvent { - /** - * Create an event describing reference changes in a repository. - * - * @param repository - * the repository whose references recently changed. - */ - public RefsChangedEvent(final Repository repository) { - super(repository); +/** Tracks a previously registered {@link RepositoryListener}. */ +public class ListenerHandle { + private final ListenerList parent; + + final Class type; + + final RepositoryListener listener; + + ListenerHandle(ListenerList parent, + Class type, + RepositoryListener listener) { + this.parent = parent; + this.type = type; + this.listener = listener; + } + + /** Remove the listener and stop receiving events. */ + public void remove() { + parent.remove(this); } @Override public String toString() { - return "RefsChangedEvent[" + getRepository() + "]"; + return type.getSimpleName() + "[" + listener + "]"; } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/events/ListenerList.java b/org.eclipse.jgit/src/org/eclipse/jgit/events/ListenerList.java new file mode 100644 index 000000000..24e2d4da0 --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/events/ListenerList.java @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2010, Google Inc. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - 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. + * + * - Neither the name of the Eclipse Foundation, Inc. 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 COPYRIGHT OWNER 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. + */ + +package org.eclipse.jgit.events; + +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CopyOnWriteArrayList; + +/** Manages a thread-safe list of {@link RepositoryListener}s. */ +public class ListenerList { + private final ConcurrentMap, CopyOnWriteArrayList> lists = new ConcurrentHashMap, CopyOnWriteArrayList>(); + + /** + * Register an IndexChangedListener. + * + * @param listener + * the listener implementation. + * @return handle to later remove the listener. + */ + public ListenerHandle addIndexChangedListener(IndexChangedListener listener) { + return addListener(IndexChangedListener.class, listener); + } + + /** + * Register a RefsChangedListener. + * + * @param listener + * the listener implementation. + * @return handle to later remove the listener. + */ + public ListenerHandle addRefsChangedListener(RefsChangedListener listener) { + return addListener(RefsChangedListener.class, listener); + } + + /** + * Add a listener to the list. + * + * @param + * the type of listener being registered. + * @param type + * type of listener being registered. + * @param listener + * the listener instance. + * @return a handle to later remove the registration, if desired. + */ + public ListenerHandle addListener( + Class type, T listener) { + ListenerHandle handle = new ListenerHandle(this, type, listener); + add(handle); + return handle; + } + + /** + * Dispatch an event to all interested listeners. + *

+ * Listeners are selected by the type of listener the event delivers to. + * + * @param event + * the event to deliver. + */ + @SuppressWarnings("unchecked") + public void dispatch(RepositoryEvent event) { + List list = lists.get(event.getListenerType()); + if (list != null) { + for (ListenerHandle handle : list) + event.dispatch(handle.listener); + } + } + + private void add(ListenerHandle handle) { + List list = lists.get(handle.type); + if (list == null) { + CopyOnWriteArrayList newList; + + newList = new CopyOnWriteArrayList(); + list = lists.putIfAbsent(handle.type, newList); + if (list == null) + list = newList; + } + list.add(handle); + } + + void remove(ListenerHandle handle) { + List list = lists.get(handle.type); + if (list != null) + list.remove(handle); + } +} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/events/RefsChangedEvent.java b/org.eclipse.jgit/src/org/eclipse/jgit/events/RefsChangedEvent.java new file mode 100644 index 000000000..36af3f8b7 --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/events/RefsChangedEvent.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2010, Google Inc. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - 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. + * + * - Neither the name of the Eclipse Foundation, Inc. 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 COPYRIGHT OWNER 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. + */ + +package org.eclipse.jgit.events; + +/** Describes a change to one or more references of a repository. */ +public class RefsChangedEvent extends RepositoryEvent { + @Override + public Class getListenerType() { + return RefsChangedListener.class; + } + + @Override + public void dispatch(RefsChangedListener listener) { + listener.onRefsChanged(this); + } +} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryChangedEvent.java b/org.eclipse.jgit/src/org/eclipse/jgit/events/RefsChangedListener.java similarity index 78% rename from org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryChangedEvent.java rename to org.eclipse.jgit/src/org/eclipse/jgit/events/RefsChangedListener.java index 495049ce7..9c0f4ed58 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryChangedEvent.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/events/RefsChangedListener.java @@ -41,30 +41,15 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.eclipse.jgit.lib; - -/** - * This class passes information about changed refs to a - * {@link RepositoryListener} - * - * Currently only a reference to the repository is passed. - */ -public class RepositoryChangedEvent { - private final Repository repository; - - RepositoryChangedEvent(final Repository repository) { - this.repository = repository; - } +package org.eclipse.jgit.events; +/** Receives {@link RefsChangedEvent}s. */ +public interface RefsChangedListener extends RepositoryListener { /** - * @return the repository that was changed + * Invoked when any reference changes. + * + * @param event + * information about the changes. */ - public Repository getRepository() { - return repository; - } - - @Override - public String toString() { - return "RepositoryChangedEvent[" + repository + "]"; - } + void onRefsChanged(RefsChangedEvent event); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/events/RepositoryEvent.java b/org.eclipse.jgit/src/org/eclipse/jgit/events/RepositoryEvent.java new file mode 100644 index 000000000..fa1b1bd41 --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/events/RepositoryEvent.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2010, Google Inc. + * Copyright (C) 2008, Robin Rosenberg + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - 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. + * + * - Neither the name of the Eclipse Foundation, Inc. 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 COPYRIGHT OWNER 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. + */ + +package org.eclipse.jgit.events; + +import org.eclipse.jgit.lib.Repository; + +/** + * Describes a modification made to a repository. + * + * @param + * type of listener this event dispatches to. + */ +public abstract class RepositoryEvent { + private volatile Repository repository; + + /** + * Set the repository this event occurred on. + *

+ * This method should only be invoked once on each event object, and is + * automatically set by {@link Repository#fireEvent(RepositoryEvent)}. + * + * @param r + * the repository. + */ + public void setRepository(Repository r) { + if (repository == null) + repository = r; + } + + /** @return the repository that was changed. */ + public Repository getRepository() { + return repository; + } + + /** @return type of listener this event dispatches to. */ + public abstract Class getListenerType(); + + /** + * Dispatch this event to the given listener. + * + * @param listener + * listener that wants this event. + */ + public abstract void dispatch(T listener); + + @Override + public String toString() { + String type = getClass().getSimpleName(); + if (repository == null) + return type; + return type + "[" + repository + "]"; + } +} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryListener.java b/org.eclipse.jgit/src/org/eclipse/jgit/events/RepositoryListener.java similarity index 77% rename from org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryListener.java rename to org.eclipse.jgit/src/org/eclipse/jgit/events/RepositoryListener.java index 0473093e2..4f951e5f8 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryListener.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/events/RepositoryListener.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008, Robin Rosenberg + * Copyright (C) 2010, Google Inc. * and other copyright owners as documented in the project's IP log. * * This program and the accompanying materials are made available @@ -41,29 +41,9 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.eclipse.jgit.lib; +package org.eclipse.jgit.events; -/** - * A RepositoryListener gets notification about changes in refs or repository. - *

- * It currently does not get notification about which items are - * changed. - */ +/** A listener can register for event delivery. */ public interface RepositoryListener { - /** - * Invoked when a ref changes - * - * @param e - * information about the changes. - */ - void refsChanged(RefsChangedEvent e); - - /** - * Invoked when the index changes - * - * @param e - * information about the changes. - */ - void indexChanged(IndexChangedEvent e); - + // Empty marker interface; see extensions for actual methods. } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java index 42c16cb10..929cd2d2e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java @@ -71,6 +71,7 @@ import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.NotSupportedException; +import org.eclipse.jgit.events.IndexChangedEvent; import org.eclipse.jgit.util.RawParseUtils; /** @@ -155,7 +156,7 @@ public boolean isChanged() { public void rereadIfNecessary() throws IOException { if (cacheFile.exists() && cacheFile.lastModified() != lastCacheTime) { read(); - db.fireIndexChanged(); + db.fireEvent(new IndexChangedEvent()); } } @@ -307,7 +308,7 @@ public void write() throws IOException { changed = false; statDirty = false; lastCacheTime = cacheFile.lastModified(); - db.fireIndexChanged(); + db.fireEvent(new IndexChangedEvent()); } finally { if (!lock.delete()) throw new IOException( diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDirectory.java index 0cbcf2b5c..13e9c22d9 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDirectory.java @@ -75,6 +75,7 @@ import org.eclipse.jgit.JGitText; import org.eclipse.jgit.errors.ObjectWritingException; +import org.eclipse.jgit.events.RefsChangedEvent; import org.eclipse.jgit.revwalk.RevObject; import org.eclipse.jgit.revwalk.RevTag; import org.eclipse.jgit.revwalk.RevWalk; @@ -829,7 +830,7 @@ private void fireRefsChanged() { final int last = lastNotifiedModCnt.get(); final int curr = modCnt.get(); if (last != curr && lastNotifiedModCnt.compareAndSet(last, curr)) - parent.fireRefsChanged(); + parent.fireEvent(new RefsChangedEvent()); } /** diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java index 7b32d69a0..2d99f6587 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java @@ -49,7 +49,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -57,7 +56,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.Vector; import java.util.concurrent.atomic.AtomicInteger; import org.eclipse.jgit.JGitText; @@ -65,6 +63,8 @@ import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.RevisionSyntaxException; +import org.eclipse.jgit.events.ListenerList; +import org.eclipse.jgit.events.RepositoryEvent; import org.eclipse.jgit.revwalk.RevBlob; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevObject; @@ -82,6 +82,13 @@ * This class is thread-safe. */ public abstract class Repository { + private static final ListenerList globalListeners = new ListenerList(); + + /** @return the global listener list observing all events in this JVM. */ + public static ListenerList getGlobalListenerList() { + return globalListeners; + } + private final AtomicInteger useCnt = new AtomicInteger(1); /** Metadata directory holding the repository's critical files. */ @@ -92,8 +99,7 @@ public abstract class Repository { private GitIndex index; - private final List listeners = new Vector(); // thread safe - static private final List allListeners = new Vector(); // thread safe + private final ListenerList myListeners = new ListenerList(); /** If not bare, the top level directory of the working files. */ private final File workTree; @@ -114,6 +120,26 @@ protected Repository(final BaseRepositoryBuilder options) { indexFile = options.getIndexFile(); } + /** @return listeners observing only events on this repository. */ + public ListenerList getListenerList() { + return myListeners; + } + + /** + * Fire an event to all registered listeners. + *

+ * The source repository of the event is automatically set to this + * repository, before the event is delivered to any listeners. + * + * @param event + * the event to deliver. + */ + public void fireEvent(RepositoryEvent event) { + event.setRepository(this); + myListeners.dispatch(event); + globalListeners.dispatch(event); + } + /** * Create a new Git repository. *

@@ -1027,70 +1053,6 @@ public File getWorkTree() throws IllegalStateException { return workTree; } - /** - * Register a {@link RepositoryListener} which will be notified - * when ref changes are detected. - * - * @param l - */ - public void addRepositoryChangedListener(final RepositoryListener l) { - listeners.add(l); - } - - /** - * Remove a registered {@link RepositoryListener} - * @param l - */ - public void removeRepositoryChangedListener(final RepositoryListener l) { - listeners.remove(l); - } - - /** - * Register a global {@link RepositoryListener} which will be notified - * when a ref changes in any repository are detected. - * - * @param l - */ - public static void addAnyRepositoryChangedListener(final RepositoryListener l) { - allListeners.add(l); - } - - /** - * Remove a globally registered {@link RepositoryListener} - * @param l - */ - public static void removeAnyRepositoryChangedListener(final RepositoryListener l) { - allListeners.remove(l); - } - - void fireRefsChanged() { - final RefsChangedEvent event = new RefsChangedEvent(this); - List all; - synchronized (listeners) { - all = new ArrayList(listeners); - } - synchronized (allListeners) { - all.addAll(allListeners); - } - for (final RepositoryListener l : all) { - l.refsChanged(event); - } - } - - void fireIndexChanged() { - final IndexChangedEvent event = new IndexChangedEvent(this); - List all; - synchronized (listeners) { - all = new ArrayList(listeners); - } - synchronized (allListeners) { - all.addAll(allListeners); - } - for (final RepositoryListener l : all) { - l.indexChanged(event); - } - } - /** * Force a scan for changed refs. *