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.List;
030: import org.cougaar.core.agent.service.MessageSwitchService;
031: import org.cougaar.core.component.Component;
032: import org.cougaar.core.component.ServiceBroker;
033: import org.cougaar.core.component.ServiceProvider;
034: import org.cougaar.core.component.ServiceRevokedListener;
035: import org.cougaar.core.mts.MessageAddress;
036: import org.cougaar.core.persist.PersistenceObject;
037: import org.cougaar.core.service.BlackboardMetricsService;
038: import org.cougaar.core.service.BlackboardQueryService;
039: import org.cougaar.core.service.BlackboardService;
040: import org.cougaar.util.GenericStateModelAdapter;
041:
042: /**
043: * This component advertises the {@link BlackboardService}
044: * and manages the {@link Blackboard}.
045: */
046: public class StandardBlackboard extends GenericStateModelAdapter
047: implements Component {
048: private ServiceBroker sb = null;
049: private Blackboard bb = null;
050: private Distributor d = null;
051:
052: private MessageSwitchService msgSwitch;
053:
054: private BlackboardForAgentServiceProvider bbAgentSP;
055: private BlackboardServiceProvider bbSP;
056:
057: public void setServiceBroker(ServiceBroker sb) {
058: this .sb = sb;
059: }
060:
061: public PersistenceObject getPersistenceObject() {
062: try {
063: return bb.getPersistenceObject();
064: } catch (Exception e) {
065: throw new RuntimeException(
066: "Unable to capture Blackboard state", e);
067: }
068: }
069:
070: public void load() {
071: super .load();
072:
073: msgSwitch = (MessageSwitchService) sb.getService(this ,
074: MessageSwitchService.class, null);
075: if (msgSwitch == null) {
076: throw new RuntimeException(
077: "Unable to obtain MessageSwitchService, which is required"
078: + " for the blackboard to send messages!");
079: }
080:
081: // create blackboard with optional prior-state
082: bb = new Blackboard(msgSwitch, sb, null);
083: // bb = new Blackboard(msgSwitch, sb, loadState);
084: // loadState = null;
085:
086: bb.init();
087: d = bb.getDistributor();
088: // d.getPersistence().registerServices(sb);
089:
090: bb.connectDomains();
091:
092: // offer hooks back to the Agent
093: bbAgentSP = new BlackboardForAgentServiceProvider(bb);
094: sb.addService(BlackboardForAgent.class, bbAgentSP);
095:
096: //offer Blackboard service and Blackboard metrics service
097: // both use the same service provider
098: bbSP = new BlackboardServiceProvider(bb.getDistributor());
099: sb.addService(BlackboardService.class, bbSP);
100: sb.addService(BlackboardMetricsService.class, bbSP);
101: sb.addService(BlackboardQueryService.class, bbSP);
102:
103: // add services here (none for now)
104: }
105:
106: public void unload() {
107: super .unload();
108:
109: // unload services in reverse order of "load()"
110: sb.revokeService(BlackboardMetricsService.class, bbSP);
111: sb.revokeService(BlackboardService.class, bbSP);
112: sb.revokeService(BlackboardForAgent.class, bbAgentSP);
113: // d.getPersistence().unregisterServices(sb);
114: d.stop();
115: bb.stop();
116: if (msgSwitch != null) {
117: sb.releaseService(this , MessageSwitchService.class,
118: msgSwitch);
119: msgSwitch = null;
120: }
121: }
122:
123: //
124: // blackboardforagent support
125: //
126: private static class BlackboardForAgentServiceProvider implements
127: ServiceProvider {
128: Blackboard blackboard;
129:
130: BlackboardForAgentServiceProvider(Blackboard blackboard) {
131: this .blackboard = blackboard;
132: }
133:
134: public Object getService(ServiceBroker sb, Object requestor,
135: Class serviceClass) {
136: if (serviceClass == BlackboardForAgent.class) {
137: return new BlackboardForAgentImpl(blackboard);
138: }
139: return null;
140: }
141:
142: public void releaseService(ServiceBroker sb, Object requestor,
143: Class serviceClass, Object service) {
144: if (service instanceof BlackboardForAgentImpl) {
145: ((BlackboardForAgentImpl) service).release(blackboard);
146: }
147: }
148: }
149:
150: private static class BlackboardForAgentImpl implements
151: BlackboardForAgent {
152: private Blackboard blackboard;
153:
154: private BlackboardForAgentImpl(Blackboard bb) {
155: this .blackboard = bb;
156: }
157:
158: void release(Blackboard bb) {
159: if (bb == blackboard) {
160: this .blackboard = null;
161: } else {
162: throw new RuntimeException(
163: "Illegal attempt to revoke a " + this + ".");
164: }
165: }
166:
167: // might be better for blackboard to be a message switch handler, eh?
168: public void receiveMessages(List messages) {
169: blackboard.getDistributor().receiveMessages(messages);
170: }
171:
172: public void restartAgent(MessageAddress cid) {
173: blackboard.getDistributor().restartAgent(cid);
174: }
175:
176: public void suspend() {
177: blackboard.getDistributor().suspend();
178: }
179:
180: public void resume() {
181: blackboard.getDistributor().resume();
182: }
183:
184: public PersistenceObject getPersistenceObject() {
185: return blackboard.getDistributor().getPersistenceObject();
186: }
187:
188: public void persistNow() {
189: blackboard.getDistributor().persistNow();
190: }
191: }
192: }
|