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.Collection;
030: import java.util.List;
031: import java.util.Set;
032:
033: import org.cougaar.core.blackboard.ChangeEnvelopeTuple;
034: import org.cougaar.core.blackboard.Directive;
035: import org.cougaar.core.blackboard.DirectiveMessage;
036: import org.cougaar.core.blackboard.EnvelopeTuple;
037: import org.cougaar.core.component.Component;
038: import org.cougaar.core.component.ServiceBroker;
039: import org.cougaar.core.component.ServiceProvider;
040: import org.cougaar.core.component.Service;
041: import org.cougaar.core.mts.MessageAddress;
042: import org.cougaar.core.relay.RelayLP;
043: import org.cougaar.core.service.AgentIdentificationService;
044: import org.cougaar.util.GenericStateModelAdapter;
045:
046: /**
047: * This component is the minimal "root" domain that creates the
048: * basic {@link RootPlan} for other {@link Domain}s and loads the
049: * {@link RelayLP}.
050: * <p>
051: * Instead of using the {@link DomainAdapter} as a base class, this
052: * simple domain directly implements the domain API for efficiency
053: * and illustative purposes.
054: */
055: public final class RootDomain extends GenericStateModelAdapter
056: implements Component, Domain {
057: private ServiceBroker sb;
058: private DomainRegistryService domainRegistryService;
059: private MessageAddress self;
060: private RootPlan rootplan;
061: private RelayLP relayLP;
062:
063: public void setParameter(Object o) {
064: String domainName = (String) (((List) o).get(0));
065: if (!getDomainName().equals(domainName)) {
066: throw new IllegalArgumentException(
067: "Invalid Domain name parameter - " + " specified "
068: + domainName + " should be "
069: + getDomainName());
070: }
071: }
072:
073: public void setServiceBroker(ServiceBroker sb) {
074: this .sb = sb;
075: }
076:
077: public void setAgentIdentificationService(
078: AgentIdentificationService ais) {
079: if (ais != null) {
080: this .self = ais.getMessageAddress();
081: }
082: }
083:
084: private class RootPlanServiceProvider implements ServiceProvider {
085: private Service theInstance = new RootPlanImpl();
086:
087: /* (non-Javadoc)
088: * @see org.cougaar.core.component.ServiceProvider#getService(org.cougaar.core.component.ServiceBroker, java.lang.Object, java.lang.Class)
089: */
090: public Object getService(ServiceBroker sb, Object requestor,
091: Class serviceClass) {
092: return theInstance;
093: }
094:
095: /* (non-Javadoc)
096: * @see org.cougaar.core.component.ServiceProvider#releaseService(org.cougaar.core.component.ServiceBroker, java.lang.Object, java.lang.Class, java.lang.Object)
097: */
098: public void releaseService(ServiceBroker sb, Object requestor,
099: Class serviceClass, Object service) {
100: }
101: }
102:
103: public void load() {
104: super .load();
105: sb.addService(RootPlan.class, new RootPlanServiceProvider());
106: rootplan = (RootPlan) sb.getService(this , RootPlan.class, null);
107: relayLP = new RelayLP(rootplan, self);
108: domainRegistryService = (DomainRegistryService) sb.getService(
109: this , DomainRegistryService.class, null);
110: if (domainRegistryService != null) {
111: domainRegistryService.registerDomain(this );
112: }
113: }
114:
115: public void unload() {
116: super .unload();
117: if (domainRegistryService != null) {
118: domainRegistryService.unregisterDomain(this );
119: sb.releaseService(this , DomainRegistryService.class,
120: domainRegistryService);
121: domainRegistryService = null;
122: }
123: }
124:
125: public String getDomainName() {
126: return "root";
127: }
128:
129: public XPlan getXPlan() {
130: return rootplan;
131: }
132:
133: public Factory getFactory() {
134: return null;
135: }
136:
137: public void invokeMessageLogicProviders(DirectiveMessage message) {
138: Directive[] directives = message.getDirectives();
139: for (int index = 0; index < directives.length; index++) {
140: Directive directive = directives[index];
141: invokeMessageLogicProviders(directive);
142: }
143: }
144:
145: private void invokeMessageLogicProviders(Directive directive) {
146: Collection changeReports = null;
147: if (directive instanceof DirectiveMessage.DirectiveWithChangeReports) {
148: DirectiveMessage.DirectiveWithChangeReports dd = (DirectiveMessage.DirectiveWithChangeReports) directive;
149: changeReports = dd.getChangeReports();
150: directive = dd.getDirective();
151: }
152: relayLP.execute(directive, changeReports);
153: }
154:
155: public void invokeEnvelopeLogicProviders(EnvelopeTuple tuple,
156: boolean isPersistenceEnvelope) {
157: if (isPersistenceEnvelope) {
158: return;
159: }
160: Collection changeReports = null;
161: if (tuple instanceof ChangeEnvelopeTuple) {
162: changeReports = ((ChangeEnvelopeTuple) tuple)
163: .getChangeReports();
164: }
165: relayLP.execute(tuple, changeReports);
166: }
167:
168: public void invokeRestartLogicProviders(MessageAddress cid) {
169: relayLP.restart(cid);
170: }
171:
172: public void invokeABAChangeLogicProviders(Set communities) {
173: relayLP.abaChange(communities);
174: }
175: }
|