001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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: package org.cougaar.core.examples.mobility.script;
027:
028: import java.io.BufferedReader;
029: import java.io.InputStream;
030: import java.io.InputStreamReader;
031: import java.io.IOException;
032: import java.util.Collection;
033: import org.cougaar.core.examples.mobility.ldm.MobilityTestFactory;
034: import org.cougaar.core.examples.mobility.ldm.Proc;
035: import org.cougaar.core.examples.mobility.ldm.Script;
036: import org.cougaar.core.mobility.ldm.MobilityFactory;
037: import org.cougaar.core.plugin.ComponentPlugin;
038: import org.cougaar.core.service.DomainService;
039: import org.cougaar.core.service.LoggingService;
040:
041: /**
042: * This is an optional plugin that launches a script at startup.
043: * <p>
044: * One parameter is expected, to specify for the script filename.
045: * The file is located using the ConfigFinder.
046: * <p>
047: * Upon startup this plugin creates and publish-adds a matching
048: * mobility script and proc.
049: *
050: * @see org.cougaar.core.mobility.ldm.ScriptParser script language
051: */
052: public class LaunchScriptPlugin extends ComponentPlugin {
053:
054: private LoggingService log;
055: private DomainService domain;
056:
057: private MobilityFactory mobilityFactory;
058: private MobilityTestFactory mobilityTestFactory;
059:
060: public void setLoggingService(LoggingService log) {
061: this .log = (log != null ? log : LoggingService.NULL);
062: }
063:
064: public void setDomainService(DomainService domain) {
065: if (domain != null) {
066: this .domain = domain;
067: this .mobilityFactory = (MobilityFactory) domain
068: .getFactory("mobility");
069: if (mobilityFactory == null) {
070: throw new RuntimeException(
071: "Mobility factory (and domain \"mobility\")"
072: + " not enabled");
073: }
074: this .mobilityTestFactory = (MobilityTestFactory) domain
075: .getFactory("mobilityTest");
076: if (mobilityTestFactory == null) {
077: throw new RuntimeException(
078: "Mobility Test factory (and domain"
079: + " \"mobilityTest\") not enabled");
080: }
081: }
082: }
083:
084: public void unload() {
085: if (domain != null) {
086: getServiceBroker().releaseService(this ,
087: DomainService.class, domain);
088: domain = null;
089: }
090: if ((log != null) && (log != LoggingService.NULL)) {
091: getServiceBroker().releaseService(this ,
092: LoggingService.class, log);
093: log = LoggingService.NULL;
094: }
095: super .unload();
096: }
097:
098: protected void setupSubscriptions() {
099: if (blackboard.didRehydrate()) {
100: // already created our script & proc
101: return;
102: }
103:
104: // get the script file name
105: Collection c = getParameters();
106: if (c == null || c.size() != 1) {
107: throw new IllegalArgumentException(
108: "Expecting one plugin parameter for the filename, not "
109: + c);
110: }
111: String file = (String) c.iterator().next();
112:
113: // start the script
114: launchScript(file);
115: }
116:
117: protected void execute() {
118: // never
119: }
120:
121: protected void launchScript(String file) {
122: if (log.isDebugEnabled()) {
123: log.debug("Read script from file " + file);
124: }
125: String content = readFile(file);
126: Script script = mobilityTestFactory.createScript(content);
127: if (log.isDebugEnabled()) {
128: log.debug("Created new script: " + script);
129: }
130: blackboard.publishAdd(script);
131: Proc proc = mobilityTestFactory.createProc(script.getUID());
132: if (log.isDebugEnabled()) {
133: log.debug("Created new proc: " + proc);
134: }
135: blackboard.publishAdd(proc);
136: if (log.isInfoEnabled()) {
137: log.info("Launched script \"" + file + "\" (script: "
138: + script.getUID() + ", proc: " + proc.getUID()
139: + ")");
140: }
141: }
142:
143: protected String readFile(String file) {
144: StringBuffer ret = new StringBuffer();
145: try {
146: InputStream is = getConfigFinder().open(file);
147: BufferedReader br = new BufferedReader(
148: new InputStreamReader(is));
149: char[] buf = new char[1024];
150: while (true) {
151: int len = br.read(buf);
152: if (len < 0) {
153: break;
154: }
155: ret.append(buf, 0, len);
156: }
157: br.close();
158: } catch (IOException ioe) {
159: throw new RuntimeException("Unable to read file \"" + file
160: + "\"", ioe);
161: }
162: return ret.toString();
163: }
164:
165: }
|