001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2007 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.node;
028:
029: import java.util.Arrays;
030: import java.util.Collections;
031: import java.util.List;
032: import org.cougaar.core.component.ComponentDescription;
033: import org.cougaar.core.component.ComponentSupport;
034: import org.cougaar.core.component.ServiceBroker;
035: import org.cougaar.core.component.ServiceProvider;
036: import org.cougaar.core.mts.MessageAddress;
037: import org.cougaar.core.service.LoggingService;
038: import org.cougaar.util.Arguments;
039:
040: /**
041: * This component is a base class for {@link ComponentInitializerService}
042: * override implementations.
043: *
044: * @see #overrideComponentDescriptions
045: */
046: public abstract class ConfiguratorBase extends ComponentSupport {
047:
048: protected Arguments args = Arguments.EMPTY_INSTANCE;
049:
050: protected MessageAddress localNode;
051: private ComponentInitializerService root_cis;
052: private ServiceBroker rootsb;
053:
054: protected LoggingService log;
055:
056: private ServiceProvider sp;
057:
058: public void setParameter(Object o) {
059: args = new Arguments(o);
060: }
061:
062: public void setLoggingService(LoggingService log) {
063: this .log = log;
064: }
065:
066: public void setNodeControlService(NodeControlService ncs) {
067: if (ncs != null) {
068: rootsb = ncs.getRootServiceBroker();
069: }
070: }
071:
072: public void setComponentInitializerService(
073: ComponentInitializerService cis) {
074: root_cis = cis;
075: }
076:
077: public void load() {
078: super .load();
079:
080: localNode = find_local_node();
081:
082: // override the top-level comp-init service
083: //
084: // note that we can add this service at the root, even though we were able
085: // to obtained it. This isn't a conflict because the initial service is
086: // advertised *above* the root, in the Node, otherwise our call to
087: // "addService" would fail.
088: sp = new MySP();
089: rootsb.addService(ComponentInitializerService.class, sp);
090: }
091:
092: public void unload() {
093: if (sp != null) {
094: rootsb.revokeService(ComponentInitializerService.class, sp);
095: sp = null;
096: }
097: super .unload();
098: }
099:
100: protected ComponentDescription[] getComponentDescriptions(
101: String agentName, String insertionPoint)
102: throws ComponentInitializerService.InitializerException {
103: ComponentDescription[] orig = null;
104: ComponentDescription[] ret = null;
105: Exception e = null;
106: try {
107: orig = root_cis.getComponentDescriptions(agentName,
108: insertionPoint);
109: ret = overrideComponentDescriptions(agentName,
110: insertionPoint, orig);
111: } catch (Exception ex) {
112: e = ex;
113: }
114: if (e != null) {
115: if (e instanceof ComponentInitializerService.InitializerException) {
116: throw (ComponentInitializerService.InitializerException) e;
117: } else if (e instanceof RuntimeException) {
118: throw (RuntimeException) e;
119: } else {
120: throw new RuntimeException(null, e);
121: }
122: }
123: return ret;
124: }
125:
126: /**
127: * Define this method to override each agent's configuration.
128: * <p>
129: * @return a new list of components, or the passed-in components
130: * for no override.
131: */
132: protected abstract ComponentDescription[] overrideComponentDescriptions(
133: String agentName, String insertionPoint,
134: ComponentDescription[] components);
135:
136: // TODO move to ComponentDescription
137: protected static ComponentDescription newComponentDescription(
138: String classname) {
139: return newComponentDescription(classname, null);
140: }
141:
142: protected static ComponentDescription newComponentDescription(
143: String classname, Object parameter) {
144: return newComponentDescription(
145: "Node.AgentManager.Agent.PluginManager.Plugin",
146: classname, parameter);
147: }
148:
149: protected static ComponentDescription newComponentDescription(
150: String insertionPoint, String classname, Object parameter) {
151: List args = (parameter == null ? Collections.EMPTY_LIST
152: : parameter instanceof String ? Collections
153: .singletonList(parameter)
154: : parameter instanceof String[] ? Arrays
155: .asList((String[]) parameter)
156: : (List) parameter);
157: String name;
158: {
159: StringBuffer buf = new StringBuffer();
160: buf.append(classname);
161: buf.append("_").append(insertionPoint).append("(");
162: for (int i = 0; i < args.size(); i++) {
163: if (i > 0) {
164: buf.append(",");
165: }
166: buf.append(args.get(i));
167: }
168: buf.append(")");
169: name = buf.toString();
170: }
171: return new ComponentDescription(name, insertionPoint,
172: classname, null, // codebase
173: (args.isEmpty() ? null : args), null, // certificate
174: null, // lease
175: null, // policy
176: ComponentDescription.PRIORITY_COMPONENT);
177: }
178:
179: protected static String toXML(ComponentDescription cd,
180: String indent, boolean verbose) {
181: StringBuffer buf = new StringBuffer();
182: buf.append(indent).append("<component");
183: if (verbose) {
184: buf.append(indent).append(" name='");
185: buf.append(cd.getName());
186: buf.append("'").append(indent).append(" ");
187: }
188: buf.append(" class='");
189: buf.append(cd.getClassname());
190: buf.append("'");
191: int p = cd.getPriority();
192: if (verbose || p != ComponentDescription.PRIORITY_COMPONENT) {
193: buf.append(indent).append(" ");
194: buf.append("priority='");
195: buf.append(ComponentDescription.priorityToString(p));
196: buf.append("'");
197: }
198: String ip = cd.getInsertionPoint();
199: if (verbose
200: || !ip
201: .equals("Node.AgentManager.Agent.PluginManager.Plugin")) {
202: buf.append(indent).append(" ");
203: buf.append("insertionpoint='");
204: buf.append(cd.getInsertionPoint());
205: buf.append("'");
206: }
207: buf.append(">");
208: Object o = cd.getParameter();
209: if (o instanceof List) {
210: List args = (List) o;
211: for (int i = 0; i < args.size(); i++) {
212: buf.append(indent).append(" <argument>");
213: buf.append(indent).append(" ").append(args.get(i));
214: buf.append(indent).append(" </argument>");
215: }
216: }
217: buf.append(indent).append("</component>");
218: return buf.toString();
219: }
220:
221: protected boolean includesDefaultComponents() {
222: return root_cis.includesDefaultComponents();
223: }
224:
225: private MessageAddress find_local_node() {
226: NodeIdentificationService nis = (NodeIdentificationService) getServiceBroker()
227: .getService(this , NodeIdentificationService.class, null);
228: if (nis == null) {
229: return null;
230: }
231: MessageAddress ret = nis.getMessageAddress();
232: getServiceBroker().releaseService(this ,
233: NodeIdentificationService.class, nis);
234: return ret;
235: }
236:
237: private class MySP implements ServiceProvider {
238: private final ComponentInitializerService cis = new ComponentInitializerService() {
239: public ComponentDescription[] getComponentDescriptions(
240: String agentName, String insertionPoint)
241: throws InitializerException {
242: return ConfiguratorBase.this .getComponentDescriptions(
243: agentName, insertionPoint);
244: }
245:
246: public boolean includesDefaultComponents() {
247: return ConfiguratorBase.this
248: .includesDefaultComponents();
249: }
250: };
251:
252: public Object getService(ServiceBroker sb, Object requestor,
253: Class serviceClass) {
254: return (ComponentInitializerService.class
255: .isAssignableFrom(serviceClass) ? cis : null);
256: }
257:
258: public void releaseService(ServiceBroker sb, Object requestor,
259: Class serviceClass, Object service) {
260: }
261: }
262: }
|