001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.core.examples;
027:
028: import java.util.*;
029: import org.cougaar.util.*;
030: import org.cougaar.core.component.*;
031: import org.cougaar.core.agent.*;
032: import org.cougaar.core.domain.*;
033: import org.cougaar.core.blackboard.*;
034: import org.cougaar.core.persist.*;
035: import org.cougaar.core.blackboard.*;
036: import org.cougaar.core.service.BlackboardService;
037:
038: /** A plugin's view of its parent component (Container).
039: * Add a line like the following to a cluster.ini file:
040: * <pre>
041: * Node.AgentManager.Agent.PluginManager.Binder = org.cougaar.core.examples.PluginServiceFilter
042: * </pre>
043:
044: **/
045: public class PluginServiceFilter extends ServiceFilter {
046: // This method specifies the Binder to use (defined later)
047: protected Class getBinderClass(Object child) {
048: return PluginServiceFilterBinder.class;
049: }
050:
051: // This is a "Wrapper" binder which installs a service filter for plugins
052: public static class PluginServiceFilterBinder extends
053: ServiceFilterBinder {
054: public PluginServiceFilterBinder(BinderFactory bf, Object child) {
055: super (bf, child);
056: }
057:
058: // this method specifies a binder proxy to use, so as to avoid exposing the binder
059: // itself to the lower level objects.
060: protected ContainerAPI createContainerProxy() {
061: return new ServiceFilterContainerProxy();
062: }
063:
064: // this method installs the "filtering" service broker
065: protected ServiceBroker createFilteringServiceBroker(
066: ServiceBroker sb) {
067: return new PluginFilteringServiceBroker(sb);
068: }
069:
070: // this class catches requests for blackboard services, and
071: // installs its own service proxy.
072: protected class PluginFilteringServiceBroker extends
073: FilteringServiceBroker {
074: public PluginFilteringServiceBroker(ServiceBroker sb) {
075: super (sb);
076: }
077:
078: // here's where we catch the service request for Blackboard and proxy the
079: // returned service. See FilteringServiceBroker for more options.
080: protected Object getServiceProxy(Object service,
081: Class serviceClass, Object client) {
082: if (service instanceof BlackboardService) {
083: return new BlackboardServiceProxy(
084: (BlackboardService) service, client);
085: }
086: return null;
087: }
088: }
089: }
090:
091: // this class is a proxy for the blackboard service which audits subscription
092: // requests.
093: private static class BlackboardServiceProxy extends
094: BlackboardServiceDelegate {
095: private final Object client;
096:
097: public BlackboardServiceProxy(BlackboardService bs,
098: Object client) {
099: super (bs);
100: this .client = client;
101: }
102:
103: public Subscriber getSubscriber() {
104: System.err.println("Warning: " + client
105: + " is calling BlackboardService.getSubscriber()!");
106: return super .getSubscriber();
107: }
108:
109: public Subscription subscribe(UnaryPredicate isMember) {
110: System.err.println("BlackboardService.subscribe("
111: + isMember + ") called by: " + client);
112: return super .subscribe(isMember);
113: }
114: }
115:
116: // dumb delegate, could be promoted to a reusable public class
117: private static class BlackboardServiceDelegate implements
118: BlackboardService {
119: private final BlackboardService bs;
120:
121: public BlackboardServiceDelegate(BlackboardService bs) {
122: this .bs = bs;
123: }
124:
125: public Subscriber getSubscriber() {
126: return bs.getSubscriber();
127: }
128:
129: public Subscription subscribe(UnaryPredicate isMember) {
130: return bs.subscribe(isMember);
131: }
132:
133: public Subscription subscribe(UnaryPredicate isMember,
134: Collection realCollection) {
135: return bs.subscribe(isMember, realCollection);
136: }
137:
138: public Subscription subscribe(UnaryPredicate isMember,
139: boolean isIncremental) {
140: return bs.subscribe(isMember, isIncremental);
141: }
142:
143: public Subscription subscribe(UnaryPredicate isMember,
144: Collection realCollection, boolean isIncremental) {
145: return bs
146: .subscribe(isMember, realCollection, isIncremental);
147: }
148:
149: public Subscription subscribe(Subscription subscription) {
150: return bs.subscribe(subscription);
151: }
152:
153: public Collection query(UnaryPredicate isMember) {
154: return bs.query(isMember);
155: }
156:
157: public void unsubscribe(Subscription subscription) {
158: bs.unsubscribe(subscription);
159: }
160:
161: public int getSubscriptionCount() {
162: return bs.getSubscriptionCount();
163: }
164:
165: public int getSubscriptionSize() {
166: return bs.getSubscriptionSize();
167: }
168:
169: public int getPublishAddedCount() {
170: return bs.getPublishAddedCount();
171: }
172:
173: public int getPublishChangedCount() {
174: return bs.getPublishChangedCount();
175: }
176:
177: public int getPublishRemovedCount() {
178: return bs.getPublishRemovedCount();
179: }
180:
181: public boolean haveCollectionsChanged() {
182: return bs.haveCollectionsChanged();
183: }
184:
185: public void publishAdd(Object o) {
186: bs.publishAdd(o);
187: }
188:
189: public void publishRemove(Object o) {
190: bs.publishRemove(o);
191: }
192:
193: public void publishChange(Object o) {
194: bs.publishChange(o);
195: }
196:
197: public void publishChange(Object o, Collection changes) {
198: bs.publishChange(o, changes);
199: }
200:
201: public void openTransaction() {
202: bs.openTransaction();
203: }
204:
205: public boolean tryOpenTransaction() {
206: return bs.tryOpenTransaction();
207: }
208:
209: public void closeTransaction() throws SubscriberException {
210: bs.closeTransaction();
211: }
212:
213: public void closeTransactionDontReset()
214: throws SubscriberException {
215: bs.closeTransactionDontReset();
216: }
217:
218: /** @deprecated Use {@link #closeTransactionDontReset closeTransactionDontReset}
219: **/
220: public void closeTransaction(boolean resetp)
221: throws SubscriberException {
222: bs.closeTransaction(resetp);
223: }
224:
225: public boolean isTransactionOpen() {
226: return bs.isTransactionOpen();
227: }
228:
229: public void signalClientActivity() {
230: bs.signalClientActivity();
231: }
232:
233: public SubscriptionWatcher registerInterest(
234: SubscriptionWatcher w) {
235: return bs.registerInterest(w);
236: }
237:
238: public SubscriptionWatcher registerInterest() {
239: return bs.registerInterest();
240: }
241:
242: public void unregisterInterest(SubscriptionWatcher w)
243: throws SubscriberException {
244: bs.unregisterInterest(w);
245: }
246:
247: public void setShouldBePersisted(boolean value) {
248: bs.setShouldBePersisted(value);
249: }
250:
251: public boolean shouldBePersisted() {
252: return bs.shouldBePersisted();
253: }
254:
255: public void persistNow()
256: throws org.cougaar.core.persist.PersistenceNotEnabledException {
257: bs.persistNow();
258: }
259:
260: public boolean didRehydrate() {
261: return bs.didRehydrate();
262: }
263:
264: public Persistence getPersistence() {
265: return bs.getPersistence();
266: }
267: }
268: }
|