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.tools.csmart.experiment;
028:
029: import org.cougaar.core.component.ComponentDescription;
030: import org.cougaar.tools.csmart.core.cdata.AgentComponentData;
031: import org.cougaar.tools.csmart.core.cdata.ComponentData;
032: import org.cougaar.tools.csmart.core.cdata.GenericComponentData;
033: import org.cougaar.tools.csmart.core.db.DBConflictHandler;
034: import org.cougaar.tools.csmart.society.AgentComponent;
035: import org.cougaar.tools.csmart.society.SocietyComponent;
036: import org.cougaar.tools.csmart.society.cdata.AgentCDataComponent;
037: import org.cougaar.tools.csmart.society.cdata.SocietyCDataComponent;
038: import org.cougaar.tools.csmart.ui.console.CSMARTConsoleModel;
039: import org.cougaar.tools.csmart.util.ReadOnlyProperties;
040: import org.xml.sax.Attributes;
041: import org.xml.sax.SAXException;
042: import org.xml.sax.SAXParseException;
043: import org.xml.sax.helpers.DefaultHandler;
044:
045: import javax.swing.*;
046: import javax.xml.parsers.SAXParser;
047: import javax.xml.parsers.SAXParserFactory;
048: import java.awt.*;
049: import java.io.*;
050: import java.net.InetAddress;
051: import java.net.UnknownHostException;
052: import java.util.HashMap;
053:
054: /**
055: * org.cougaar.tools.csmart.experiment
056: *
057: */
058: public class XMLExperiment extends ExperimentBase {
059: private File file;
060:
061: private ComponentData experiment;
062: private String societyFileName;
063: private Component parent;
064: private ProgressMonitorInputStream monitor = null;
065:
066: public XMLExperiment(File file, Component parent) {
067: super (file.getName());
068: this .file = file;
069: this .parent = parent;
070: createLogger();
071: setDefaultNodeArguments();
072: }
073:
074: public ComponentData getSocietyComponentData() {
075: return experiment;
076: }
077:
078: public String getSocietyFileName() {
079: return societyFileName;
080: }
081:
082: public void dumpINIFiles() {
083: }
084:
085: public void dumpHNA() {
086: }
087:
088: public void save(DBConflictHandler ch) {
089: }
090:
091: public ComponentData getExperiment() {
092: return experiment;
093: }
094:
095: public void doParse(boolean validate) throws Exception {
096: MyHandler handler = new MyHandler();
097: SAXParserFactory factory = SAXParserFactory.newInstance();
098: factory.setValidating(validate);
099: factory.setNamespaceAware(true);
100: SAXParser saxParser = factory.newSAXParser();
101:
102: String text = ((factory.isValidating()) ? "Parsing and Validating "
103: : "Parsing ");
104: monitor = new ProgressMonitorInputStream(parent, text
105: + file.getName(), new FileInputStream(file));
106: InputStream in = new BufferedInputStream(monitor);
107: if (in != null) {
108: saxParser.parse(in, handler);
109: } else {
110: log.error("Unable to open " + file.getName()
111: + " for XML initialization");
112: throw new IOException("Upable to open stream for "
113: + file.getName());
114: }
115:
116: SocietyComponent soc = new SocietyCDataComponent(experiment,
117: null);
118: soc.initProperties();
119: setSocietyComponent(soc);
120: }
121:
122: protected void setDefaultNodeArguments() {
123: defaultNodeArguments = new ReadOnlyProperties();
124: createObserver();
125: // defaultNodeArguments.put(PERSISTENCE_ENABLE, PERSISTENCE_DFLT);
126: // // By default we clear any existing persistence deltas when we
127: // // start a run.
128: // defaultNodeArguments.put(PERSIST_CLEAR, PERSIST_CLEAR_DFLT);
129: // defaultNodeArguments.put(TIMEZONE, TIMEZONE_DFLT);
130: // defaultNodeArguments.put(AGENT_STARTTIME, AGENT_STARTTIME_DFLT);
131: // defaultNodeArguments.put(COMPLAININGLP_LEVEL, COMPLAININGLP_LEVEL_DFLT);
132: // defaultNodeArguments.put(CONTROL_PORT, Integer.toString(APP_SERVER_DEFAULT_PORT));
133:
134: // // By default, we tell the AppServer to ignore connection errors
135: // // if CSMART dies, so that the society does _not_ die.
136: // defaultNodeArguments.put(AS_SWALLOW_ERRORS, AS_SWALLOW_ERRORS_DFLT);
137:
138: // // Class of Node to run. This is the first argument to the BOOTSTRAP_CLASS
139: // // below.
140: // defaultNodeArguments.put(CSMARTConsoleModel.COMMAND_ARGUMENTS, DEFAULT_NODE_CLASS);
141:
142: // // Class of bootstrapper to use. The actual class being executed
143: // defaultNodeArguments.put(BOOTSTRAP_CLASS, DEFAULT_BOOTSTRAP_CLASS);
144:
145: // try {
146: // defaultNodeArguments.put(ENV_DISPLAY, InetAddress.getLocalHost().getHostName() + ":0.0");
147: // } catch (UnknownHostException uhe) {
148: // if (log.isErrorEnabled()) {
149: // log.error("UnknownHost Exception", uhe);
150: // }
151: // }
152: }
153:
154: // private static String insertionPointContainer(String insertionPoint) {
155: // return insertionPoint.substring(0, insertionPoint.lastIndexOf('.'));
156: // }
157:
158: private class MyHandler extends DefaultHandler {
159:
160: boolean this Node = false;
161: CharArrayWriter chars;
162: ComponentData host;
163: ComponentData node;
164: ComponentData agent;
165: ComponentData component;
166: ComponentData parent;
167: ComponentData current;
168: ComponentData childless;
169: HostComponent hostComp = null;
170: NodeComponent nodeComp = null;
171:
172: private final String stdPriority = ComponentDescription
173: .priorityToString(ComponentDescription.PRIORITY_STANDARD);
174:
175: public void startElement(String namespaceURI, String localName,
176: String qName, Attributes atts) throws SAXException {
177: if (localName.equals("society")) {
178: experiment = new GenericComponentData();
179: experiment.setName(atts.getValue("name"));
180: if (log.isDebugEnabled()) {
181: log.debug("started element for society: "
182: + experiment.getName());
183: }
184: experiment.setType(ComponentData.SOCIETY);
185: experiment.setClassName("");
186: experiment.setParent(null);
187: experiment.setOwner(null);
188: current = experiment;
189: parent = experiment;
190: }
191: if (localName.equals("host")) {
192: hostComp = XMLExperiment.this .addHost(atts
193: .getValue("name"));
194: host = new GenericComponentData();
195: host.setName(atts.getValue("name"));
196: if (log.isDebugEnabled()) {
197: log.debug("started element for host: "
198: + host.getName());
199: }
200: host.setType(ComponentData.HOST);
201: host.setParent(experiment);
202: host.setOwner(null);
203: current = host;
204: parent = host;
205: }
206: if (localName.equals("node")) {
207: nodeComp = XMLExperiment.this .addNode(atts
208: .getValue("name"));
209: node = new GenericComponentData();
210: node.setName(atts.getValue("name"));
211: if (log.isDebugEnabled())
212: log.debug("started element for node: "
213: + node.getName());
214:
215: node.setType(ComponentData.NODE);
216: node.setParent(host);
217: node.setOwner(null);
218: current = node;
219: parent = node;
220: }
221: if (localName.equals("agent")) {
222: agent = new AgentComponentData();
223: agent.setName(atts.getValue("name"));
224: if (log.isDebugEnabled())
225: log.debug("started element for agent "
226: + agent.getName());
227:
228: agent.setClassName(atts.getValue("class"));
229: agent.setType(GenericComponentData
230: .getCanonicalType(atts
231: .getValue("insertionpoint")));
232: agent.setPriority(stdPriority);
233: agent.setParent(node);
234: current = agent;
235: parent = agent;
236: } else if (localName.equals("component")) {
237: component = new GenericComponentData();
238: component.setName(atts.getValue("name"));
239: if (log.isDebugEnabled()) {
240: log.debug("Have component: "
241: + atts.getValue("name"));
242: }
243:
244: component.setClassName(atts.getValue("class"));
245: component.setPriority(atts.getValue("priority"));
246: component.setParent(parent);
247: component.setType(GenericComponentData
248: .getCanonicalType(atts
249: .getValue("insertionpoint")));
250: current = component;
251: } else if (localName.equals("argument")) {
252: if (log.isDebugEnabled()) {
253: log.debug("Have an argument");
254: }
255: chars = new CharArrayWriter();
256: } else if (localName.equals("facet")) {
257: childless = new GenericComponentData();
258: HashMap map = new HashMap(2);
259: if (atts.getValue("superior_org_id") != null) {
260: map.put("superior_org_id", atts
261: .getValue("superior_org_id"));
262: }
263: if (atts.getValue("subordinate_org_id") != null) {
264: map.put("subordinate_org_id", atts
265: .getValue("subordinate_org_id"));
266: }
267: childless.addParameter(map);
268: childless.setType("facet");
269: childless.setParent(current);
270: childless.setClassName("");
271: } else if (localName.equals("class")
272: || localName.equals("prog_parameter")
273: || localName.equals("vm_parameter")
274: || localName.equals("env_parameter")) {
275: childless = new GenericComponentData();
276: childless.setType(localName);
277: childless.setName(localName);
278: chars = new CharArrayWriter();
279: }
280: }
281:
282: /**
283: * @see org.xml.sax.ContentHandler#characters(char[], int, int)
284: */
285: public void characters(char[] ch, int start, int length)
286: throws SAXException {
287: if (chars != null)
288: chars.write(ch, start, length);
289: }
290:
291: /**
292: * @see org.xml.sax.ContentHandler#endElement(String, String, String)
293: */
294: public void endElement(String namespaceURI, String localName,
295: String qName) throws SAXException {
296: if (localName.equals("argument")) {
297: current.addParameter(chars.toString().trim());
298: chars = null;
299: } else if (localName.equals("class")
300: || localName.equals("prog_parameter")
301: || localName.equals("vm_parameter")) {
302: GenericComponentData gcd = new GenericComponentData();
303: gcd.setName(chars.toString().trim());
304: gcd.setType(localName);
305: addToNode(localName, chars.toString().trim());
306: current.addChild(gcd);
307: current.setParent(agent);
308: current = parent;
309: } else if (localName.equals("facet")) {
310: parent.addChild(childless);
311: } else if (localName.equals("component")) {
312: parent.addChild(component);
313: } else if (localName.equals("agent")) {
314: node.addChild(agent);
315: AgentComponent ac = new AgentCDataComponent(agent);
316: ac.initProperties();
317: nodeComp.addAgent(ac);
318: if (log.isDebugEnabled())
319: log.debug("finished a agent");
320: } else if (localName.equals("node")) {
321: host.addChild(node);
322: hostComp.addNode(nodeComp);
323: if (log.isDebugEnabled())
324: log.debug("finished a node");
325: nodeComp = null;
326: } else if (localName.equals("host")) {
327: experiment.addChild(host);
328: hostComp = null;
329: if (log.isDebugEnabled()) {
330: log.debug("finished a host");
331: }
332: } else if (localName.equals("society")) {
333: if (log.isDebugEnabled()) {
334: log.debug("finished a society");
335: }
336: }
337: }
338:
339: private void addToNode(String type, String value) {
340: if (type.equals("class")) {
341: nodeComp.addProperty(
342: CSMARTConsoleModel.COMMAND_ARGUMENTS, value);
343: defaultNodeArguments.put(
344: CSMARTConsoleModel.COMMAND_ARGUMENTS, value);
345: System.out.println("Adding: "
346: + CSMARTConsoleModel.COMMAND_ARGUMENTS + " "
347: + value);
348: } else if (type.equals("prog_parameter")) {
349: nodeComp.addProperty(
350: CSMARTConsoleModel.COMMAND_ARGUMENTS, value);
351: defaultNodeArguments.put(
352: CSMARTConsoleModel.COMMAND_ARGUMENTS, value);
353: System.out.println("Adding: "
354: + CSMARTConsoleModel.COMMAND_ARGUMENTS + " "
355: + value);
356: } else if (type.equals("vm_parameter")) {
357: if (value.indexOf("=") != -1) {
358: String name = value
359: .substring(2, value.indexOf("="));
360: String val = value
361: .substring((value.indexOf("=") + 1));
362: if (name.equals("org.cougaar.society.file")) {
363: XMLExperiment.this .societyFileName = val;
364: }
365: nodeComp.addArgument(name, val);
366: defaultNodeArguments.put(name, val);
367: System.out.println("Adding: " + name + " " + val);
368: } else {
369: nodeComp.addArgument(value, "");
370: defaultNodeArguments.put(value, "");
371: System.out.println("Adding: " + value);
372: }
373: } else if (type.equals("env_parameter")) {
374: nodeComp.addProperty(
375: CSMARTConsoleModel.COMMAND_ARGUMENTS, value);
376: defaultNodeArguments.put(
377: CSMARTConsoleModel.COMMAND_ARGUMENTS, value);
378: System.out.println("Adding: "
379: + CSMARTConsoleModel.COMMAND_ARGUMENTS + " "
380: + value);
381: }
382: }
383:
384: /**
385: * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
386: */
387: public void error(SAXParseException exception)
388: throws SAXException {
389: log.error("Error parsing the file", exception);
390: super .error(exception);
391: }
392:
393: /**
394: * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
395: */
396: public void warning(SAXParseException exception)
397: throws SAXException {
398: log.warn("Warning parsing the file", exception);
399: super.warning(exception);
400: }
401:
402: }
403: }
|