Browse Source

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>
stable-0.9
Shawn O. Pearce 15 years ago
parent
commit
8a9844b2af
  1. 1
      org.eclipse.jgit/META-INF/MANIFEST.MF
  2. 31
      org.eclipse.jgit/src/org/eclipse/jgit/events/IndexChangedEvent.java
  3. 25
      org.eclipse.jgit/src/org/eclipse/jgit/events/IndexChangedListener.java
  4. 40
      org.eclipse.jgit/src/org/eclipse/jgit/events/ListenerHandle.java
  5. 130
      org.eclipse.jgit/src/org/eclipse/jgit/events/ListenerList.java
  6. 57
      org.eclipse.jgit/src/org/eclipse/jgit/events/RefsChangedEvent.java
  7. 26
      org.eclipse.jgit/src/org/eclipse/jgit/events/RefsChangedListener.java
  8. 50
      org.eclipse.jgit/src/org/eclipse/jgit/events/RepositoryEvent.java
  9. 49
      org.eclipse.jgit/src/org/eclipse/jgit/events/RepositoryListener.java
  10. 5
      org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java
  11. 3
      org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDirectory.java
  12. 98
      org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java

1
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.api;version="0.9.0",
org.eclipse.jgit.diff;version="0.9.0", org.eclipse.jgit.diff;version="0.9.0",
org.eclipse.jgit.dircache;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.errors;version="0.9.0",
org.eclipse.jgit.fnmatch;version="0.9.0", org.eclipse.jgit.fnmatch;version="0.9.0",
org.eclipse.jgit.lib;version="0.9.0", org.eclipse.jgit.lib;version="0.9.0",

31
org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryChangedEvent.java → org.eclipse.jgit/src/org/eclipse/jgit/events/IndexChangedEvent.java

@ -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. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
@ -41,30 +41,17 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package org.eclipse.jgit.lib; package org.eclipse.jgit.events;
/** /** Describes a change to one or more paths in the index file. */
* This class passes information about changed refs to a public class IndexChangedEvent extends RepositoryEvent<IndexChangedListener> {
* {@link RepositoryListener} @Override
* public Class<IndexChangedListener> getListenerType() {
* Currently only a reference to the repository is passed. return IndexChangedListener.class;
*/
public class RepositoryChangedEvent {
private final Repository repository;
RepositoryChangedEvent(final Repository repository) {
this.repository = repository;
}
/**
* @return the repository that was changed
*/
public Repository getRepository() {
return repository;
} }
@Override @Override
public String toString() { public void dispatch(IndexChangedListener listener) {
return "RepositoryChangedEvent[" + repository + "]"; listener.onIndexChanged(this);
} }
} }

25
org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryAdapter.java → org.eclipse.jgit/src/org/eclipse/jgit/events/IndexChangedListener.java

@ -41,20 +41,15 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package org.eclipse.jgit.lib; package org.eclipse.jgit.events;
/**
* 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
}
/** 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);
} }

40
org.eclipse.jgit/src/org/eclipse/jgit/lib/RefsChangedEvent.java → org.eclipse.jgit/src/org/eclipse/jgit/events/ListenerHandle.java

@ -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. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
@ -41,27 +41,31 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package org.eclipse.jgit.lib; package org.eclipse.jgit.events;
/** /** Tracks a previously registered {@link RepositoryListener}. */
* This class passes information about a changed Git index to a public class ListenerHandle {
* {@link RepositoryListener} private final ListenerList parent;
*
* Currently only a reference to the repository is passed. final Class<? extends RepositoryListener> type;
*/
public class RefsChangedEvent extends RepositoryChangedEvent { final RepositoryListener listener;
/**
* Create an event describing reference changes in a repository. ListenerHandle(ListenerList parent,
* Class<? extends RepositoryListener> type,
* @param repository RepositoryListener listener) {
* the repository whose references recently changed. this.parent = parent;
*/ this.type = type;
public RefsChangedEvent(final Repository repository) { this.listener = listener;
super(repository); }
/** Remove the listener and stop receiving events. */
public void remove() {
parent.remove(this);
} }
@Override @Override
public String toString() { public String toString() {
return "RefsChangedEvent[" + getRepository() + "]"; return type.getSimpleName() + "[" + listener + "]";
} }
} }

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

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

26
org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryListener.java → org.eclipse.jgit/src/org/eclipse/jgit/events/RefsChangedListener.java

@ -41,29 +41,15 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package org.eclipse.jgit.lib; package org.eclipse.jgit.events;
/** /** Receives {@link RefsChangedEvent}s. */
* A RepositoryListener gets notification about changes in refs or repository. public interface RefsChangedListener extends RepositoryListener {
* <p>
* It currently does <em>not</em> get notification about which items are
* changed.
*/
public interface RepositoryListener {
/** /**
* Invoked when a ref changes * Invoked when any reference changes.
* *
* @param e * @param event
* information about the changes. * information about the changes.
*/ */
void refsChanged(RefsChangedEvent e); void onRefsChanged(RefsChangedEvent event);
/**
* Invoked when the index changes
*
* @param e
* information about the changes.
*/
void indexChanged(IndexChangedEvent e);
} }

50
org.eclipse.jgit/src/org/eclipse/jgit/lib/IndexChangedEvent.java → org.eclipse.jgit/src/org/eclipse/jgit/events/RepositoryEvent.java

@ -1,4 +1,5 @@
/* /*
* Copyright (C) 2010, Google Inc.
* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com> * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
* and other copyright owners as documented in the project's IP log. * and other copyright owners as documented in the project's IP log.
* *
@ -41,27 +42,54 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package org.eclipse.jgit.lib; package org.eclipse.jgit.events;
import org.eclipse.jgit.lib.Repository;
/** /**
* This class passes information about a changed Git index to a * Describes a modification made to a repository.
* {@link RepositoryListener}
* *
* Currently only a reference to the repository is passed. * @param <T>
* type of listener this event dispatches to.
*/ */
public class IndexChangedEvent extends RepositoryChangedEvent { public abstract class RepositoryEvent<T extends RepositoryListener> {
private volatile Repository repository;
/** /**
* Create an event describing index changes in a 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 repository * @param r
* the repository whose index (DirCache) recently changed. * the repository.
*/ */
public IndexChangedEvent(final Repository repository) { public void setRepository(Repository r) {
super(repository); 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 @Override
public String toString() { public String toString() {
return "IndexChangedEvent[" + getRepository() + "]"; String type = getClass().getSimpleName();
if (repository == null)
return type;
return type + "[" + repository + "]";
} }
} }

49
org.eclipse.jgit/src/org/eclipse/jgit/events/RepositoryListener.java

@ -0,0 +1,49 @@
/*
* 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;
/** A listener can register for event delivery. */
public interface RepositoryListener {
// Empty marker interface; see extensions for actual methods.
}

5
org.eclipse.jgit/src/org/eclipse/jgit/lib/GitIndex.java

@ -71,6 +71,7 @@ import org.eclipse.jgit.JGitText;
import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.NotSupportedException; import org.eclipse.jgit.errors.NotSupportedException;
import org.eclipse.jgit.events.IndexChangedEvent;
import org.eclipse.jgit.util.RawParseUtils; import org.eclipse.jgit.util.RawParseUtils;
/** /**
@ -155,7 +156,7 @@ public class GitIndex {
public void rereadIfNecessary() throws IOException { public void rereadIfNecessary() throws IOException {
if (cacheFile.exists() && cacheFile.lastModified() != lastCacheTime) { if (cacheFile.exists() && cacheFile.lastModified() != lastCacheTime) {
read(); read();
db.fireIndexChanged(); db.fireEvent(new IndexChangedEvent());
} }
} }
@ -307,7 +308,7 @@ public class GitIndex {
changed = false; changed = false;
statDirty = false; statDirty = false;
lastCacheTime = cacheFile.lastModified(); lastCacheTime = cacheFile.lastModified();
db.fireIndexChanged(); db.fireEvent(new IndexChangedEvent());
} finally { } finally {
if (!lock.delete()) if (!lock.delete())
throw new IOException( throw new IOException(

3
org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDirectory.java

@ -75,6 +75,7 @@ import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jgit.JGitText; import org.eclipse.jgit.JGitText;
import org.eclipse.jgit.errors.ObjectWritingException; import org.eclipse.jgit.errors.ObjectWritingException;
import org.eclipse.jgit.events.RefsChangedEvent;
import org.eclipse.jgit.revwalk.RevObject; import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevTag; import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
@ -829,7 +830,7 @@ public class RefDirectory extends RefDatabase {
final int last = lastNotifiedModCnt.get(); final int last = lastNotifiedModCnt.get();
final int curr = modCnt.get(); final int curr = modCnt.get();
if (last != curr && lastNotifiedModCnt.compareAndSet(last, curr)) if (last != curr && lastNotifiedModCnt.compareAndSet(last, curr))
parent.fireRefsChanged(); parent.fireEvent(new RefsChangedEvent());
} }
/** /**

98
org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java

@ -49,7 +49,6 @@ package org.eclipse.jgit.lib;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -57,7 +56,6 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jgit.JGitText; import org.eclipse.jgit.JGitText;
@ -65,6 +63,8 @@ import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.RevisionSyntaxException; 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.RevBlob;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevObject; import org.eclipse.jgit.revwalk.RevObject;
@ -82,6 +82,13 @@ import org.eclipse.jgit.util.RawParseUtils;
* This class is thread-safe. * This class is thread-safe.
*/ */
public abstract class Repository { 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); private final AtomicInteger useCnt = new AtomicInteger(1);
/** Metadata directory holding the repository's critical files. */ /** Metadata directory holding the repository's critical files. */
@ -92,8 +99,7 @@ public abstract class Repository {
private GitIndex index; private GitIndex index;
private final List<RepositoryListener> listeners = new Vector<RepositoryListener>(); // thread safe private final ListenerList myListeners = new ListenerList();
static private final List<RepositoryListener> allListeners = new Vector<RepositoryListener>(); // thread safe
/** If not bare, the top level directory of the working files. */ /** If not bare, the top level directory of the working files. */
private final File workTree; private final File workTree;
@ -114,6 +120,26 @@ public abstract class Repository {
indexFile = options.getIndexFile(); 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. * Create a new Git repository.
* <p> * <p>
@ -1027,70 +1053,6 @@ public abstract class Repository {
return workTree; 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. * Force a scan for changed refs.
* *

Loading…
Cancel
Save