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 <spearce@spearce.org>
This commit is contained in:
Shawn O. Pearce 2010-06-24 11:12:40 -07:00
parent 203bd66267
commit 8a9844b2af
12 changed files with 371 additions and 170 deletions

View File

@ -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",

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
* 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<IndexChangedListener> {
@Override
public Class<IndexChangedListener> getListenerType() {
return IndexChangedListener.class;
}
@Override
public String toString() {
return "IndexChangedEvent[" + getRepository() + "]";
public void dispatch(IndexChangedListener listener) {
listener.onIndexChanged(this);
}
}

View File

@ -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);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
* 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<? extends RepositoryListener> type;
final RepositoryListener listener;
ListenerHandle(ListenerList parent,
Class<? extends RepositoryListener> 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 + "]";
}
}

View File

@ -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<Class<? extends RepositoryListener>, CopyOnWriteArrayList<ListenerHandle>> lists = new ConcurrentHashMap<Class<? extends RepositoryListener>, CopyOnWriteArrayList<ListenerHandle>>();
/**
* 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 <T>
* 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 <T extends RepositoryListener> ListenerHandle addListener(
Class<T> type, T listener) {
ListenerHandle handle = new ListenerHandle(this, type, listener);
add(handle);
return handle;
}
/**
* Dispatch an event to all interested listeners.
* <p>
* 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<ListenerHandle> list = lists.get(event.getListenerType());
if (list != null) {
for (ListenerHandle handle : list)
event.dispatch(handle.listener);
}
}
private void add(ListenerHandle handle) {
List<ListenerHandle> list = lists.get(handle.type);
if (list == null) {
CopyOnWriteArrayList<ListenerHandle> newList;
newList = new CopyOnWriteArrayList<ListenerHandle>();
list = lists.putIfAbsent(handle.type, newList);
if (list == null)
list = newList;
}
list.add(handle);
}
void remove(ListenerHandle handle) {
List<ListenerHandle> list = lists.get(handle.type);
if (list != null)
list.remove(handle);
}
}

View File

@ -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<RefsChangedListener> {
@Override
public Class<RefsChangedListener> getListenerType() {
return RefsChangedListener.class;
}
@Override
public void dispatch(RefsChangedListener listener) {
listener.onRefsChanged(this);
}
}

View File

@ -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);
}

View File

@ -0,0 +1,95 @@
/*
* Copyright (C) 2010, Google Inc.
* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
* 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 <T>
* type of listener this event dispatches to.
*/
public abstract class RepositoryEvent<T extends RepositoryListener> {
private volatile Repository repository;
/**
* Set the repository this event occurred on.
* <p>
* 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<T> 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 + "]";
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
* 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.
* <p>
* It currently does <em>not</em> 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.
}

View File

@ -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(

View File

@ -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());
}
/**

View File

@ -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<RepositoryListener> listeners = new Vector<RepositoryListener>(); // thread safe
static private final List<RepositoryListener> allListeners = new Vector<RepositoryListener>(); // 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.
* <p>
* 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.
* <p>
@ -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<RepositoryListener> all;
synchronized (listeners) {
all = new ArrayList<RepositoryListener>(listeners);
}
synchronized (allListeners) {
all.addAll(allListeners);
}
for (final RepositoryListener l : all) {
l.refsChanged(event);
}
}
void fireIndexChanged() {
final IndexChangedEvent event = new IndexChangedEvent(this);
List<RepositoryListener> all;
synchronized (listeners) {
all = new ArrayList<RepositoryListener>(listeners);
}
synchronized (allListeners) {
all.addAll(allListeners);
}
for (final RepositoryListener l : all) {
l.indexChanged(event);
}
}
/**
* Force a scan for changed refs.
*