001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (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.InterceptingRepository;
016: import org.openrdf.repository.event.InterceptingRepositoryConnection;
017: import org.openrdf.repository.event.RepositoryInterceptor;
018:
019: /**
020: * Wrapper that notifies interceptors of events on Repositories before they
021: * happen. Any interceptor can block the operation by returning true from the
022: * relevant notification method. To do so will also cause the notification
023: * process to stop, i.e. no other interceptors will be notified. The order in
024: * which interceptors are notified is unspecified.
025: *
026: * @author Herko ter Horst
027: * @see InterceptingRepositoryConnectionWrapper
028: */
029: public class InterceptingRepositoryWrapper extends RepositoryWrapper
030: implements InterceptingRepository {
031:
032: /*-----------*
033: * Variables *
034: *-----------*/
035:
036: private boolean activated;
037:
038: private Set<RepositoryInterceptor> interceptors = new CopyOnWriteArraySet<RepositoryInterceptor>();
039:
040: /*--------------*
041: * Constructors *
042: *--------------*/
043:
044: public InterceptingRepositoryWrapper() {
045: super ();
046: }
047:
048: public InterceptingRepositoryWrapper(Repository delegate) {
049: super (delegate);
050: }
051:
052: /*---------*
053: * Methods *
054: *---------*/
055:
056: /**
057: * Registers a <tt>RepositoryInterceptor</tt> that will receive
058: * notifications of operations that are performed on this repository.
059: */
060: public void addRepositoryInterceptor(
061: RepositoryInterceptor interceptor) {
062: interceptors.add(interceptor);
063: activated = true;
064: }
065:
066: /**
067: * Removes a registered <tt>RepositoryInterceptor</tt> from this
068: * repository.
069: */
070: public void removeRepositoryInterceptor(
071: RepositoryInterceptor interceptor) {
072: interceptors.remove(interceptor);
073: activated = !interceptors.isEmpty();
074: }
075:
076: @Override
077: public InterceptingRepositoryConnection getConnection()
078: throws RepositoryException {
079: InterceptingRepositoryConnection conn = new InterceptingRepositoryConnectionWrapper(
080: this , super .getConnection());
081:
082: if (activated) {
083: boolean denied = false;
084:
085: for (RepositoryInterceptor interceptor : interceptors) {
086: denied = interceptor.getConnection(this , conn);
087: if (!denied) {
088: break;
089: }
090: }
091: if (!denied) {
092: conn = null;
093: }
094: }
095:
096: return conn;
097: }
098:
099: @Override
100: public void initialize() throws RepositoryException {
101: boolean denied = false;
102: if (activated) {
103: for (RepositoryInterceptor interceptor : interceptors) {
104: denied = interceptor.initialize(this );
105: if (denied) {
106: break;
107: }
108: }
109: }
110: if (!denied) {
111: super .initialize();
112: }
113: }
114:
115: @Override
116: public void setDataDir(File dataDir) {
117: boolean denied = false;
118: if (activated) {
119: for (RepositoryInterceptor interceptor : interceptors) {
120: denied = interceptor.setDataDir(this , dataDir);
121: if (denied) {
122: break;
123: }
124: }
125: }
126: if (!denied) {
127: super .setDataDir(dataDir);
128: }
129: }
130:
131: @Override
132: public void shutDown() throws RepositoryException {
133: boolean denied = false;
134: if (activated) {
135: for (RepositoryInterceptor interceptor : interceptors) {
136: denied = interceptor.shutDown(this);
137: if (denied) {
138: break;
139: }
140: }
141: }
142: if (!denied) {
143: super.shutDown();
144: }
145: }
146: }
|