001: /*
002: * Copyright James Leigh (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.repository.event.base;
007:
008: import java.io.File;
009: import java.util.Set;
010: import java.util.concurrent.CopyOnWriteArraySet;
011:
012: import org.openrdf.repository.Repository;
013: import org.openrdf.repository.RepositoryException;
014: import org.openrdf.repository.base.RepositoryWrapper;
015: import org.openrdf.repository.event.NotifyingRepository;
016: import org.openrdf.repository.event.NotifyingRepositoryConnection;
017: import org.openrdf.repository.event.RepositoryConnectionListener;
018: import org.openrdf.repository.event.RepositoryListener;
019:
020: /**
021: * This notifying decorator allows listeners to register with the repository or
022: * connection and be notified when events occur.
023: *
024: * @author James Leigh
025: * @author Herko ter Horst
026: * @author Arjohn Kampman
027: * @see NotifyingRepositoryConnectionWrapper
028: */
029: public class NotifyingRepositoryWrapper extends RepositoryWrapper
030: implements NotifyingRepository {
031:
032: /*-----------*
033: * Variables *
034: *-----------*/
035:
036: private boolean activated;
037:
038: private boolean defaultReportDeltas = false;
039:
040: private Set<RepositoryListener> listeners = new CopyOnWriteArraySet<RepositoryListener>();
041:
042: private Set<RepositoryConnectionListener> conListeners = new CopyOnWriteArraySet<RepositoryConnectionListener>();
043:
044: /*--------------*
045: * Constructors *
046: *--------------*/
047:
048: public NotifyingRepositoryWrapper() {
049: super ();
050: }
051:
052: public NotifyingRepositoryWrapper(Repository delegate) {
053: super (delegate);
054: }
055:
056: public NotifyingRepositoryWrapper(Repository delegate,
057: boolean defaultReportDeltas) {
058: this (delegate);
059: setDefaultReportDeltas(defaultReportDeltas);
060: }
061:
062: /*---------*
063: * Methods *
064: *---------*/
065:
066: public boolean getDefaultReportDeltas() {
067: return defaultReportDeltas;
068: }
069:
070: public void setDefaultReportDeltas(boolean defaultReportDeltas) {
071: this .defaultReportDeltas = defaultReportDeltas;
072: }
073:
074: /**
075: * Registers a <tt>RepositoryListener</tt> that will receive notifications
076: * of operations that are performed on this repository.
077: */
078: public void addRepositoryListener(RepositoryListener listener) {
079: listeners.add(listener);
080: activated = true;
081: }
082:
083: /**
084: * Removes a registered <tt>RepositoryListener</tt> from this repository.
085: */
086: public void removeRepositoryListener(RepositoryListener listener) {
087: listeners.remove(listener);
088: activated = !listeners.isEmpty();
089: }
090:
091: /**
092: * Registers a <tt>RepositoryConnectionListener</tt> that will receive
093: * notifications of operations that are performed on any< connections that
094: * are created by this repository.
095: */
096: public void addRepositoryConnectionListener(
097: RepositoryConnectionListener listener) {
098: conListeners.add(listener);
099: }
100:
101: /**
102: * Removes a registered <tt>RepositoryConnectionListener</tt> from this
103: * repository.
104: */
105: public void removeRepositoryConnectinoListener(
106: RepositoryConnectionListener listener) {
107: conListeners.remove(listener);
108: }
109:
110: @Override
111: public NotifyingRepositoryConnection getConnection()
112: throws RepositoryException {
113: NotifyingRepositoryConnection con = new NotifyingRepositoryConnectionWrapper(
114: this , super .getConnection(), getDefaultReportDeltas());
115:
116: if (activated) {
117: for (RepositoryListener listener : listeners) {
118: listener.getConnection(this , con);
119: }
120: }
121:
122: for (RepositoryConnectionListener l : conListeners) {
123: con.addRepositoryConnectionListener(l);
124: }
125:
126: return con;
127: }
128:
129: @Override
130: public void initialize() throws RepositoryException {
131: super .initialize();
132:
133: if (activated) {
134: for (RepositoryListener listener : listeners) {
135: listener.initialize(this );
136: }
137: }
138: }
139:
140: @Override
141: public void setDataDir(File dataDir) {
142: super .setDataDir(dataDir);
143:
144: if (activated) {
145: for (RepositoryListener listener : listeners) {
146: listener.setDataDir(this , dataDir);
147: }
148: }
149: }
150:
151: @Override
152: public void shutDown() throws RepositoryException {
153: super .shutDown();
154:
155: if (activated) {
156: for (RepositoryListener listener : listeners) {
157: listener.shutDown(this);
158: }
159: }
160: }
161: }
|