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.plugin;
028:
029: import org.cougaar.core.agent.Agent;
030: import org.cougaar.core.component.ComponentDescriptions;
031: import org.cougaar.core.component.ComponentRuntimeException;
032: import org.cougaar.core.component.ContainerSupport;
033: import org.cougaar.core.component.ServiceBroker;
034: import org.cougaar.core.mts.MessageAddress;
035: import org.cougaar.core.node.ComponentInitializerService;
036: import org.cougaar.core.service.AgentIdentificationService;
037: import org.cougaar.core.service.LoggingService;
038: import org.cougaar.util.ConfigFinder;
039:
040: /**
041: * This component is a container for {@link ComponentPlugin} and
042: * {@link org.cougaar.core.servlet.ComponentServlet} components.
043: */
044: public class PluginManager extends ContainerSupport {
045: /** The insertion point for a PluginManager, defined relative to its parent, Agent. */
046: public static final String INSERTION_POINT = Agent.INSERTION_POINT
047: + ".PluginManager";
048:
049: private LoggingService logger;
050: private MessageAddress agentId;
051: private AgentIdentificationService agentIdService;
052:
053: public void setLoggingService(LoggingService logger) {
054: this .logger = logger;
055: }
056:
057: public void setAgentIdentificationService(
058: AgentIdentificationService ais) {
059: this .agentIdService = ais;
060: if (ais != null) {
061: this .agentId = ais.getMessageAddress();
062: // } else { // Revocation
063: }
064: }
065:
066: protected String specifyContainmentPoint() {
067: return INSERTION_POINT;
068: }
069:
070: protected ComponentDescriptions findInitialComponentDescriptions() {
071: String cname = agentId.toString();
072: ServiceBroker sb = getServiceBroker();
073: ComponentInitializerService cis = (ComponentInitializerService) sb
074: .getService(this , ComponentInitializerService.class,
075: null);
076: try {
077: return new ComponentDescriptions(cis
078: .getComponentDescriptions(cname,
079: specifyContainmentPoint()));
080: } catch (ComponentInitializerService.InitializerException cise) {
081: if (logger.isInfoEnabled()) {
082: logger.info("\nUnable to add " + cname + "'s plugins ",
083: cise);
084: }
085: return null;
086: } finally {
087: sb.releaseService(this , ComponentInitializerService.class,
088: cis);
089: }
090: }
091:
092: public boolean add(Object o) {
093: try {
094: if (logger.isInfoEnabled()) {
095: logger.info("Agent " + agentId + " adding plugin " + o);
096: }
097: boolean result = super .add(o);
098: if (logger.isInfoEnabled()) {
099: logger.info("Agent " + agentId + " added plugin " + o);
100: }
101: return result;
102: } catch (ComponentRuntimeException cre) {
103: Throwable cause = cre.getCause();
104: if (cause == null)
105: cause = cre;
106: logger.error("Failed to add " + o + " to " + this , cause);
107: throw cre;
108: } catch (RuntimeException re) {
109: //logger.error("Failed to add "+o+" to "+this, re);
110: throw re;
111: }
112: }
113:
114: public void unload() {
115: super .unload();
116:
117: // release services
118: ServiceBroker sb = getServiceBroker();
119: if (agentIdService != null) {
120: sb.releaseService(this , AgentIdentificationService.class,
121: agentIdService);
122: agentIdService = null;
123: }
124: if (logger != null) {
125: sb.releaseService(this , LoggingService.class, logger);
126: logger = null;
127: }
128: }
129:
130: //
131: // other services
132: //
133:
134: public MessageAddress getMessageAddress() {
135: return agentId;
136: }
137:
138: public MessageAddress getAgentIdentifier() {
139: return agentId;
140: }
141:
142: public ConfigFinder getConfigFinder() {
143: return ConfigFinder.getInstance(); // FIXME replace with service
144: }
145:
146: public String toString() {
147: return agentId + "/PluginManager";
148: }
149:
150: }
|