001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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:
027: package org.cougaar.core.blackboard;
028:
029: import java.util.Collection;
030:
031: import org.cougaar.core.component.ServiceBroker;
032: import org.cougaar.core.component.ServiceProvider;
033: import org.cougaar.core.service.BlackboardMetricsService;
034: import org.cougaar.core.service.BlackboardQueryService;
035: import org.cougaar.core.service.BlackboardService;
036: import org.cougaar.util.UnaryPredicate;
037:
038: /**
039: * The service provider for the {@link BlackboardService},
040: * {@link BlackboardQueryService}, and {@link
041: * BlackboardMetricsService}.
042: * <p>
043: * All operations are backed by the
044: * {@link org.cougaar.core.blackboard.Distributor}.
045: */
046: public class BlackboardServiceProvider implements ServiceProvider {
047:
048: private Distributor distributor;
049:
050: public BlackboardServiceProvider(Distributor distributor) {
051: super ();
052: this .distributor = distributor;
053: }
054:
055: public Object getService(BlackboardClient bbclient) {
056: return new BlackboardServiceImpl(bbclient);
057: }
058:
059: public Object getService(ServiceBroker sb, Object requestor,
060: Class serviceClass) {
061: if (serviceClass == BlackboardService.class) {
062: return new BlackboardServiceImpl(
063: (BlackboardClient) requestor);
064: } else if (serviceClass == BlackboardMetricsService.class) {
065: return getBlackboardMetricsService();
066: } else if (serviceClass == BlackboardQueryService.class) {
067: return new BlackboardQueryServiceImpl(requestor);
068: } else {
069: throw new IllegalArgumentException(
070: "BlackboardServiceProvider does not provide a service for: "
071: + serviceClass);
072: }
073: }
074:
075: public void releaseService(ServiceBroker sb, Object requestor,
076: Class serviceClass, Object service) {
077: // ?? each client will get its own subscriber - how can we clean them up??
078: }
079:
080: // only need one instance of this service.
081: private BlackboardMetricsService bbmetrics = null;
082:
083: private BlackboardMetricsService getBlackboardMetricsService() {
084: if (bbmetrics == null) {
085: bbmetrics = new BlackboardMetricsServiceImpl();
086: }
087: return bbmetrics;
088: }
089:
090: /** BlackboardService is a Subscriber */
091: private final class BlackboardServiceImpl extends Subscriber
092: implements BlackboardService {
093: BlackboardServiceImpl(BlackboardClient client) {
094: super (client, distributor);
095: }
096: }
097:
098: /** The implementation of BlackboardMetricsService */
099: private final class BlackboardMetricsServiceImpl implements
100: BlackboardMetricsService {
101: public int getBlackboardCount() {
102: return distributor.getBlackboardSize();
103: }
104:
105: public int getBlackboardCount(Class cl) {
106: return distributor.getBlackboardCount(cl);
107: }
108:
109: public int getBlackboardCount(UnaryPredicate predicate) {
110: return distributor.getBlackboardCount(predicate);
111: }
112: }
113:
114: /** The implementation of BlackboardQueryService */
115: private final class BlackboardQueryServiceImpl implements
116: BlackboardQueryService {
117: private BlackboardQueryServiceImpl(Object requestor) {
118: // ignore the requestor (for now).
119: }
120:
121: public final Collection query(UnaryPredicate isMember) {
122: QuerySubscription qs = new QuerySubscription(isMember);
123: //qs.setSubscriber(null); // ignored
124: distributor.fillQuery(qs);
125: return qs.getCollection();
126: }
127: }
128: }
|