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.planning.ldm;
028:
029: import java.util.List;
030:
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.domain.Factory;
035: import org.cougaar.core.mts.MessageAddress;
036: import org.cougaar.core.service.AgentIdentificationService;
037: import org.cougaar.core.service.DomainService;
038: import org.cougaar.core.service.UIDServer;
039: import org.cougaar.core.service.UIDService;
040: import org.cougaar.planning.ldm.asset.Asset;
041: import org.cougaar.planning.ldm.asset.PropertyGroup;
042: import org.cougaar.planning.service.LDMService;
043: import org.cougaar.planning.service.PrototypeRegistryService;
044: import org.cougaar.util.GenericStateModelAdapter;
045:
046: /**
047: * The LDMServiceComponent is a provider class for the LDM
048: * service within an agent.
049: */
050: public final class LDMServiceComponent extends GenericStateModelAdapter
051: implements Component {
052: private ServiceBroker sb;
053:
054: private LDMServesPlugin lsp;
055:
056: private MessageAddress agentId;
057: private AgentIdentificationService ais;
058: private DomainService ds;
059: private PrototypeRegistryService prs;
060: private UIDService uids;
061:
062: private LDMService ldmS;
063: private LDMServiceProvider ldmSP;
064:
065: public void setServiceBroker(ServiceBroker sb) {
066: this .sb = sb;
067: }
068:
069: public void setAgentIdentificationService(
070: AgentIdentificationService ais) {
071: this .ais = ais;
072: if (ais == null) {
073: // Revocation
074: } else {
075: this .agentId = ais.getMessageAddress();
076: }
077: }
078:
079: public void setPrototypeRegistryService(PrototypeRegistryService prs) {
080: this .prs = prs;
081: }
082:
083: public void setUIDService(UIDService uids) {
084: this .uids = uids;
085: }
086:
087: public void load() {
088: super .load();
089:
090: // ensure services
091: Class missingServiceClass = (ais == null ? AgentIdentificationService.class
092: : prs == null ? PrototypeRegistryService.class
093: : uids == null ? UIDService.class : null);
094: if (missingServiceClass != null) {
095: throw new RuntimeException("Missing service: "
096: + missingServiceClass.getName());
097: }
098:
099: // create a single per-agent ldm service instance
100: this .ldmS = new LDMServiceImpl();
101:
102: // create and advertise our service
103: this .ldmSP = new LDMServiceProvider();
104: sb.addService(LDMService.class, ldmSP);
105: }
106:
107: /**
108: * Work-around an awkward domain/ldm load order issue.
109: * <p>
110: * The PlanningDomain needs the LDMService to bind Assets, but our
111: * LDMService needs the DomainService to do factory lookup.
112: * Fortunately the DomainManager advertises the DomainService
113: * prior to loading the domains, so this *should* work out okay.
114: */
115: private DomainService getDomainService() {
116: if (ds == null) {
117: ds = (DomainService) sb.getService(this ,
118: DomainService.class, null);
119: }
120: return ds;
121: }
122:
123: public void unload() {
124: // revoke our service
125: if (ldmSP != null) {
126: sb.revokeService(LDMService.class, ldmSP);
127: ldmSP = null;
128: }
129: if (uids != null) {
130: sb.releaseService(this , UIDService.class, uids);
131: uids = null;
132: }
133: if (prs != null) {
134: sb
135: .releaseService(this ,
136: PrototypeRegistryService.class, prs);
137: prs = null;
138: }
139: if (ds != null) {
140: sb.releaseService(this , DomainService.class, ds);
141: ds = null;
142: }
143: if (ais != null) {
144: sb.releaseService(this , AgentIdentificationService.class,
145: ais);
146: ais = null;
147: }
148: super .unload();
149: }
150:
151: private class LDMServiceProvider implements ServiceProvider {
152: public Object getService(ServiceBroker sb, Object requestor,
153: Class serviceClass) {
154: if (LDMService.class.isAssignableFrom(serviceClass)) {
155: return ldmS;
156: } else {
157: return null;
158: }
159: }
160:
161: public void releaseService(ServiceBroker sb, Object requestor,
162: Class serviceClass, Object service) {
163: }
164: }
165:
166: private class LDMServiceImpl implements LDMService {
167: private PlanningFactory pf;
168: private LDMServesPlugin lsp;
169:
170: public MessageAddress getMessageAddress() {
171: return agentId;
172: }
173:
174: public LDMServesPlugin getLDM() {
175: if (lsp == null) {
176: lsp = new LDMAdapter();
177: }
178: return lsp;
179: }
180:
181: public UIDServer getUIDServer() {
182: return uids;
183: }
184:
185: public PlanningFactory getFactory() {
186: if (pf == null) {
187: pf = (PlanningFactory) getFactory("planning");
188: }
189: return pf;
190: }
191:
192: public Factory getFactory(String s) {
193: return getDomainService().getFactory(s);
194: }
195:
196: public Factory getFactory(Class cl) {
197: return getDomainService().getFactory(cl);
198: }
199:
200: public List getFactories() {
201: return getDomainService().getFactories();
202: }
203:
204: // standin API for LDMService called by PluginBinder for temporary support
205: public void addPrototypeProvider(PrototypeProvider plugin) {
206: prs.addPrototypeProvider(plugin);
207: }
208:
209: public void addPropertyProvider(PropertyProvider plugin) {
210: prs.addPropertyProvider(plugin);
211: }
212:
213: public void addLatePropertyProvider(LatePropertyProvider plugin) {
214: prs.addLatePropertyProvider(plugin);
215: }
216: }
217:
218: private class LDMAdapter implements LDMServesPlugin {
219: private PlanningFactory pf;
220:
221: public Asset getPrototype(String aTypeName) {
222: return prs.getPrototype(aTypeName);
223: }
224:
225: public Asset getPrototype(String aTypeName, Class anAssetClass) {
226: return prs.getPrototype(aTypeName, anAssetClass);
227: }
228:
229: public boolean isPrototypeCached(String aTypeName) {
230: return prs.isPrototypeCached(aTypeName);
231: }
232:
233: public ClassLoader getLDMClassLoader() {
234: return getClass().getClassLoader();
235: }
236:
237: public Factory getFactory(Class domainClass) {
238: return getDomainService().getFactory(domainClass);
239: }
240:
241: public Factory getFactory(String domainName) {
242: return getDomainService().getFactory(domainName);
243: }
244:
245: public int getCachedPrototypeCount() {
246: return prs.getCachedPrototypeCount();
247: }
248:
249: public int getPropertyProviderCount() {
250: return prs.getPropertyProviderCount();
251: }
252:
253: public int getPrototypeProviderCount() {
254: return prs.getPrototypeProviderCount();
255: }
256:
257: public LDMServesPlugin getLDM() {
258: return this ;
259: }
260:
261: public MessageAddress getMessageAddress() {
262: return agentId;
263: }
264:
265: public PlanningFactory getFactory() {
266: if (pf == null) {
267: pf = (PlanningFactory) getFactory("planning");
268: }
269: return pf;
270: }
271:
272: public PropertyGroup lateFillPropertyGroup(Asset anAsset,
273: Class pg, long time) {
274: return prs.lateFillPropertyGroup(anAsset, pg, time);
275: }
276:
277: public UIDServer getUIDServer() {
278: return uids;
279: }
280:
281: public void addLatePropertyProvider(LatePropertyProvider lpp) {
282: prs.addLatePropertyProvider(lpp);
283: }
284:
285: public void addPropertyProvider(PropertyProvider prov) {
286: prs.addPropertyProvider(prov);
287: }
288:
289: public void addPrototypeProvider(PrototypeProvider prov) {
290: prs.addPrototypeProvider(prov);
291: }
292:
293: public void cachePrototype(String aTypeName, Asset aPrototype) {
294: prs.cachePrototype(aTypeName, aPrototype);
295: }
296:
297: public void fillProperties(Asset anAsset) {
298: prs.fillProperties(anAsset);
299: }
300: }
301: }
|