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.util.Set;
009: import java.util.concurrent.CopyOnWriteArraySet;
010:
011: import org.openrdf.model.Resource;
012: import org.openrdf.model.URI;
013: import org.openrdf.model.Value;
014: import org.openrdf.repository.Repository;
015: import org.openrdf.repository.RepositoryConnection;
016: import org.openrdf.repository.RepositoryException;
017: import org.openrdf.repository.base.RepositoryConnectionWrapper;
018: import org.openrdf.repository.event.InterceptingRepositoryConnection;
019: import org.openrdf.repository.event.RepositoryConnectionInterceptor;
020:
021: /**
022: * Wrapper that notifies interceptors of events on RepositoryConnections before
023: * they happen. Any interceptor can block the operation by returning true from
024: * the relevant notification method. To do so will also cause the notification
025: * process to stop, i.e. no other interceptors will be notified. The order in
026: * which interceptors are notified is unspecified.
027: *
028: * @author Herko ter Horst
029: * @see InterceptingRepositoryWrapper
030: */
031: public class InterceptingRepositoryConnectionWrapper extends
032: RepositoryConnectionWrapper implements
033: InterceptingRepositoryConnection {
034:
035: /*-----------*
036: * Variables *
037: *-----------*/
038:
039: private boolean activated;
040:
041: private Set<RepositoryConnectionInterceptor> interceptors = new CopyOnWriteArraySet<RepositoryConnectionInterceptor>();
042:
043: /*--------------*
044: * Construcotrs *
045: *--------------*/
046:
047: public InterceptingRepositoryConnectionWrapper(
048: Repository repository, RepositoryConnection connection) {
049: super (repository, connection);
050: }
051:
052: /*---------*
053: * Methods *
054: *---------*/
055:
056: /**
057: * Registers a <tt>RepositoryConnectionInterceptor</tt> that will receive
058: * notifications of operations that are performed on this connection.
059: */
060: public void addRepositoryConnectionInterceptor(
061: RepositoryConnectionInterceptor interceptor) {
062: interceptors.add(interceptor);
063: activated = true;
064: }
065:
066: /**
067: * Removes a registered <tt>RepositoryConnectionInterceptor</tt> from this
068: * connection.
069: */
070: public void removeRepositoryConnectionInterceptor(
071: RepositoryConnectionInterceptor interceptor) {
072: interceptors.remove(interceptor);
073: activated = !interceptors.isEmpty();
074: }
075:
076: @Override
077: protected boolean isDelegatingAdd() {
078: return !activated;
079: }
080:
081: @Override
082: protected boolean isDelegatingRemove() {
083: return !activated;
084: }
085:
086: @Override
087: public void addWithoutCommit(Resource subject, URI predicate,
088: Value object, Resource... contexts)
089: throws RepositoryException {
090: if (!getDelegate().hasStatement(subject, predicate, object,
091: false, contexts)) {
092: boolean denied = false;
093: if (activated) {
094: for (RepositoryConnectionInterceptor interceptor : interceptors) {
095: denied = interceptor.add(this , subject, predicate,
096: object, contexts);
097: if (denied) {
098: break;
099: }
100: }
101: }
102: if (!denied) {
103: getDelegate().add(subject, predicate, object, contexts);
104: }
105: }
106: }
107:
108: @Override
109: public void clear(Resource... contexts) throws RepositoryException {
110: boolean denied = false;
111: if (activated) {
112: for (RepositoryConnectionInterceptor interceptor : interceptors) {
113: denied = interceptor.clear(this , contexts);
114: if (denied) {
115: break;
116: }
117: }
118: }
119: if (!denied) {
120: getDelegate().clear(contexts);
121: }
122: }
123:
124: @Override
125: public void close() throws RepositoryException {
126: boolean denied = false;
127: if (activated) {
128: for (RepositoryConnectionInterceptor interceptor : interceptors) {
129: denied = interceptor.close(this );
130: if (denied) {
131: break;
132: }
133: }
134: }
135: if (!denied) {
136: super .close();
137: }
138: }
139:
140: @Override
141: public void commit() throws RepositoryException {
142: boolean denied = false;
143: if (activated) {
144: for (RepositoryConnectionInterceptor interceptor : interceptors) {
145: denied = interceptor.commit(this );
146: if (denied) {
147: break;
148: }
149: }
150: }
151: if (!denied) {
152: getDelegate().commit();
153: }
154: }
155:
156: @Override
157: public void removeWithoutCommit(Resource subject, URI predicate,
158: Value object, Resource... contexts)
159: throws RepositoryException {
160: boolean denied = false;
161: if (activated) {
162: for (RepositoryConnectionInterceptor interceptor : interceptors) {
163: denied = interceptor.remove(this , subject, predicate,
164: object, contexts);
165: if (denied) {
166: break;
167: }
168: }
169: }
170: if (!denied) {
171: getDelegate().remove(subject, predicate, object, contexts);
172:
173: }
174: }
175:
176: @Override
177: public void removeNamespace(String prefix)
178: throws RepositoryException {
179: boolean denied = false;
180: if (activated) {
181: for (RepositoryConnectionInterceptor interceptor : interceptors) {
182: denied = interceptor.removeNamespace(this , prefix);
183: if (denied) {
184: break;
185: }
186: }
187: }
188: if (!denied) {
189: getDelegate().removeNamespace(prefix);
190: }
191: }
192:
193: @Override
194: public void clearNamespaces() throws RepositoryException {
195: boolean denied = false;
196: if (activated) {
197: for (RepositoryConnectionInterceptor interceptor : interceptors) {
198: denied = interceptor.clearNamespaces(this );
199: if (denied) {
200: break;
201: }
202: }
203: }
204: if (!denied) {
205: getDelegate().clearNamespaces();
206: }
207: }
208:
209: @Override
210: public void rollback() throws RepositoryException {
211: boolean denied = false;
212: if (activated) {
213: for (RepositoryConnectionInterceptor interceptor : interceptors) {
214: denied = interceptor.rollback(this );
215: if (denied) {
216: break;
217: }
218: }
219: }
220: if (!denied) {
221: getDelegate().rollback();
222: }
223: }
224:
225: @Override
226: public void setAutoCommit(boolean autoCommit)
227: throws RepositoryException {
228: boolean denied = false;
229: boolean wasAutoCommit = isAutoCommit();
230: if (activated && wasAutoCommit != autoCommit) {
231: for (RepositoryConnectionInterceptor interceptor : interceptors) {
232: denied = interceptor.setAutoCommit(this , autoCommit);
233: if (denied) {
234: break;
235: }
236: }
237: }
238: if (!denied) {
239: getDelegate().setAutoCommit(autoCommit);
240:
241: }
242: }
243:
244: @Override
245: public void setNamespace(String prefix, String name)
246: throws RepositoryException {
247: boolean denied = false;
248: if (activated) {
249: for (RepositoryConnectionInterceptor interceptor : interceptors) {
250: denied = interceptor.setNamespace(this, prefix, name);
251: if (denied) {
252: break;
253: }
254: }
255: }
256: if (!denied) {
257: getDelegate().setNamespace(prefix, name);
258: }
259: }
260: }
|