001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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.domain;
028:
029: import java.util.ArrayList;
030: import java.util.Collection;
031: import java.util.List;
032: import java.util.Set;
033:
034: import org.cougaar.core.blackboard.ChangeEnvelopeTuple;
035: import org.cougaar.core.blackboard.Directive;
036: import org.cougaar.core.blackboard.DirectiveMessage;
037: import org.cougaar.core.blackboard.EnvelopeTuple;
038: import org.cougaar.core.component.BindingSite;
039: import org.cougaar.core.component.Component;
040: import org.cougaar.core.component.ServiceBroker;
041: import org.cougaar.core.mts.MessageAddress;
042: import org.cougaar.core.service.LoggingService;
043: import org.cougaar.util.GenericStateModelAdapter;
044:
045: /**
046: * This component is an optional base class for {@link Domain}
047: * implementations.
048: */
049: public abstract class DomainAdapter extends GenericStateModelAdapter
050: implements Component, Domain {
051:
052: private BindingSite bindingSite;
053: private ServiceBroker sb;
054: private LoggingService logger;
055: private XPlanService xplanService;
056: private DomainRegistryService domainRegistryService;
057:
058: private final List myEnvelopeLPs = new ArrayList();
059: private final List myMessageLPs = new ArrayList();
060: private final List myRestartLPs = new ArrayList();
061: private final List myABAChangeLPs = new ArrayList();
062:
063: private Factory myFactory;
064: private XPlan myXPlan;
065:
066: /** returns the Domain name */
067: public abstract String getDomainName();
068:
069: public void setBindingSite(BindingSite bindingSite) {
070: this .bindingSite = bindingSite;
071: }
072:
073: public void setServiceBroker(ServiceBroker sb) {
074: this .sb = sb;
075: }
076:
077: protected BindingSite getBindingSite() {
078: return bindingSite;
079: }
080:
081: protected ServiceBroker getServiceBroker() {
082: return sb;
083: }
084:
085: protected XPlan getXPlanForDomain(String domainName) {
086: return xplanService.getXPlan(domainName);
087: }
088:
089: protected XPlan getXPlanForDomain(Class domainClass) {
090: return xplanService.getXPlan(domainClass);
091: }
092:
093: /** returns the LoggingService */
094: public LoggingService getLoggingService() {
095: return logger;
096: }
097:
098: /** returns the Factory for this Domain. */
099: public Factory getFactory() {
100: return myFactory;
101: }
102:
103: /**
104: * @return the XPlan instance for the domain - instance may be
105: * be shared among domains
106: */
107: public XPlan getXPlan() {
108: return myXPlan;
109: }
110:
111: public void load() {
112: super .load();
113:
114: LoggingService ls = (LoggingService) sb.getService(this ,
115: LoggingService.class, null);
116: if (ls != null) {
117: logger = ls;
118: }
119:
120: xplanService = (XPlanService) sb.getService(this ,
121: XPlanService.class, null);
122: if (xplanService == null) {
123: throw new RuntimeException("Unable to obtain XPlanService");
124: }
125:
126: domainRegistryService = (DomainRegistryService) sb.getService(
127: this , DomainRegistryService.class, null);
128: if (domainRegistryService == null) {
129: throw new RuntimeException(
130: "Unable to obtain DomainRegistryService");
131: }
132:
133: loadFactory();
134: loadXPlan();
135: loadLPs();
136:
137: domainRegistryService.registerDomain(this );
138: }
139:
140: public void unload() {
141: super .unload();
142:
143: if (domainRegistryService != null) {
144: domainRegistryService.unregisterDomain(this );
145: sb.releaseService(this , DomainRegistryService.class,
146: domainRegistryService);
147: domainRegistryService = null;
148: }
149:
150: if (xplanService != null) {
151: sb.releaseService(this , XPlanService.class, xplanService);
152: xplanService = null;
153: }
154:
155: if (logger != LoggingService.NULL) {
156: sb.releaseService(this , LoggingService.class, logger);
157: logger = LoggingService.NULL;
158: }
159: }
160:
161: /** invoke the MessageLogicProviders for this domain */
162: public void invokeMessageLogicProviders(DirectiveMessage message) {
163: Directive[] directives = message.getDirectives();
164: for (int index = 0; index < directives.length; index++) {
165: Directive directive = directives[index];
166: Collection changeReports = null;
167: if (directive instanceof DirectiveMessage.DirectiveWithChangeReports) {
168: DirectiveMessage.DirectiveWithChangeReports dd = (DirectiveMessage.DirectiveWithChangeReports) directive;
169: changeReports = dd.getChangeReports();
170: directive = dd.getDirective();
171: }
172:
173: synchronized (myMessageLPs) {
174: for (int lpIndex = 0; lpIndex < myMessageLPs.size(); lpIndex++) {
175: ((MessageLogicProvider) myMessageLPs.get(lpIndex))
176: .execute(directive, changeReports);
177: }
178: }
179: }
180: }
181:
182: /** invoke the EnvelopeLogicProviders for this domain */
183: public void invokeEnvelopeLogicProviders(EnvelopeTuple tuple,
184: boolean isPersistenceEnvelope) {
185: Collection changeReports = null;
186: if (tuple instanceof ChangeEnvelopeTuple) {
187: changeReports = ((ChangeEnvelopeTuple) tuple)
188: .getChangeReports();
189: }
190:
191: synchronized (myEnvelopeLPs) {
192: for (int lpIndex = 0; lpIndex < myEnvelopeLPs.size(); lpIndex++) {
193: EnvelopeLogicProvider lp = (EnvelopeLogicProvider) myEnvelopeLPs
194: .get(lpIndex);
195: if (isPersistenceEnvelope
196: && !(lp instanceof LogicProviderNeedingPersistenceEnvelopes)) {
197: continue; // This lp does not want contents of PersistenceEnvelopes
198: }
199: try {
200: lp.execute(tuple, changeReports);
201: } catch (Exception e) {
202: e.printStackTrace();
203: }
204: }
205: }
206: }
207:
208: /** invoke the RestartLogicProviders for this domain */
209: public void invokeRestartLogicProviders(MessageAddress cid) {
210: synchronized (myRestartLPs) {
211: for (int index = 0; index < myRestartLPs.size(); index++) {
212: try {
213: ((RestartLogicProvider) myRestartLPs.get(index))
214: .restart(cid);
215: } catch (RuntimeException e) {
216: e.printStackTrace();
217: }
218: }
219: }
220: }
221:
222: public void invokeABAChangeLogicProviders(Set communities) {
223: synchronized (myABAChangeLPs) {
224: for (int index = 0; index < myABAChangeLPs.size(); index++) {
225: try {
226: ((ABAChangeLogicProvider) myABAChangeLPs.get(index))
227: .abaChange(communities);
228: } catch (RuntimeException e) {
229: e.printStackTrace();
230: }
231: }
232: }
233: }
234:
235: /**
236: * setParameter - Should only be used by the binding utilities when
237: * loading a domain from a ComponentDescription. Used as a sanity check
238: * to ensure that the specified domain name matches the domain name for
239: * the class.
240: *
241: * @param o Expecting a List containing one non-null String that
242: * specifies the domainName.
243: */
244: public void setParameter(Object o) {
245: String domainName = (String) (((List) o).get(0));
246: if (domainName == null) {
247: throw new IllegalArgumentException("Null domain name");
248: }
249:
250: if (!domainName.equals(getDomainName())) {
251: System.err.println("Invalid Domain name parameter - "
252: + " specified " + domainName + " should be "
253: + getDomainName());
254: }
255: }
256:
257: /** Add a LogicProvider to the set maintained for this Domain. */
258: protected void addLogicProvider(LogicProvider lp) {
259: if (lp instanceof MessageLogicProvider) {
260: myMessageLPs.add(lp);
261: }
262: if (lp instanceof EnvelopeLogicProvider) {
263: myEnvelopeLPs.add(lp);
264: }
265: if (lp instanceof RestartLogicProvider) {
266: myRestartLPs.add(lp);
267: }
268: if (lp instanceof ABAChangeLogicProvider) {
269: myABAChangeLPs.add(lp);
270: }
271:
272: lp.init();
273: }
274:
275: protected final List getEnvelopeLPs() {
276: return myEnvelopeLPs;
277: }
278:
279: protected final List getMessageLPs() {
280: return myMessageLPs;
281: }
282:
283: protected final List getRestartLPs() {
284: return myRestartLPs;
285: }
286:
287: protected final List getABAChangeLPs() {
288: return myABAChangeLPs;
289: }
290:
291: /**
292: * Load the Factory for this Domain. Should call setFactory() to set the
293: * factory for this Domain
294: */
295: abstract protected void loadFactory();
296:
297: /**
298: * Load the XPLan for this Domain. Should call setXPlan() to set the XPlan
299: * for this Domain
300: */
301: abstract protected void loadXPlan();
302:
303: /**
304: * Load the LogicProviders for this Domain. Should call addLogicProvider() to
305: * add each LogicProvider to the set maintained for this Domain.
306: */
307: abstract protected void loadLPs();
308:
309: /** set the factory for this Domain */
310: protected void setFactory(Factory factory) {
311: if ((myFactory != null) && logger.isDebugEnabled()) {
312: // Should we even allow this?
313: logger.debug("DomainAdapter: resetting Factory");
314: }
315:
316: myFactory = factory;
317: }
318:
319: /** set the XPlan for this Domain */
320: protected void setXPlan(XPlan xPlan) {
321: if ((myXPlan != null) && logger.isDebugEnabled()) {
322: // Should we even allow this?
323: logger.debug("DomainAdapter: resetting XPlan");
324: }
325:
326: myXPlan = xPlan;
327: }
328: }
|