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.mlm.plugin.xml;
028:
029: import java.io.FileNotFoundException;
030: import java.io.IOException;
031:
032: import org.cougaar.core.service.LoggingService;
033: import org.cougaar.lib.xml.parser.PrototypeParser;
034: import org.cougaar.planning.ldm.PrototypeProvider;
035: import org.cougaar.planning.ldm.asset.Asset;
036: import org.cougaar.planning.plugin.legacy.SimplePlugin;
037: import org.w3c.dom.Document;
038: import org.w3c.dom.Node;
039:
040: /**
041: * <pre>
042: * This plugin is a prototype provider that attempts to supply
043: * prototypes by looking for XML files by name. It finds the prototype
044: * file by using the ConfigFinder mechanism, and parses it using the
045: * toolkit prototype XML Parser. If successful, it will cache and return the
046: * generated prototype.
047: * NOTE : The name of the prototype is modified to
048: * change '/' characters to '-' characters to allow for O/S compatibility:
049: * Example : Prototype "NSN/1234567890123" looks for a
050: * file NSN-1234567890123.xml
051: *
052: * </pre>
053: * @see org.cougaar.lib.xml.parser.PrototypeParser#cachePrototype
054: * @see org.cougaar.util.ConfigFinder#parseXMLConfigFile
055: **/
056: public class XMLPrototypeProviderPlugin extends SimplePlugin implements
057: PrototypeProvider {
058: /**
059: * Rely upon load-time introspection to set these services -
060: * don't worry about revokation.
061: */
062: public final void setLoggingService(LoggingService bs) {
063: logger = bs;
064: prototypeParser = new PrototypeParser(logger);
065: }
066:
067: /**
068: * Get the logging service, for subclass use.
069: */
070: protected LoggingService getLoggingService() {
071: return logger;
072: }
073:
074: /**
075: * While this is a plugin, we only expect it to operate
076: * as a prototype provider. No subscriptions are made here.
077: */
078: public void setupSubscriptions() {
079: // System.out.println("Loading XMLPrototypeProviderPlugin...");
080: }
081:
082: /** no subscriptions -- nothing happens here */
083: public void execute() {
084: }
085:
086: /**
087: * <pre>
088: * Interface required for PrototypeProvider
089: * If possible, Create a prototype from XML file given by name
090: * <aTypeName>.xml and cache and return prototype
091: * Otherwise, return null
092: * </pre>
093: * @param aTypeName name of the Prototype to create
094: * @param ignoredAnAssetClassHint class hint not used, since we read from a file
095: * @return asset prototype if we can find and parse the file, null otherwise
096: */
097: public Asset getPrototype(String aTypeName,
098: Class ignoredAnAssetClassHint) {
099: Asset new_prototype = null;
100: Node node = null;
101:
102: // Find the file named 'aTypeName'.xml and parse it
103: // Using ConfigFileFinder
104: String filename = cleanupPrototypeName(aTypeName) + ".xml";
105: try {
106: Document doc = getConfigFinder().parseXMLConfigFile(
107: filename);
108: if (doc != null) {
109: node = doc.getDocumentElement();
110: }
111: } catch (FileNotFoundException fnfe) {
112: } catch (IOException ie) {
113: logger.error("Exception parsing prototype file : "
114: + filename, ie);
115: }
116:
117: // Create a prototype from the parsed node
118: // Using toolkit XML Asset parser
119: if (node != null) {
120: new_prototype = prototypeParser.cachePrototype(getLDM(),
121: node, true);
122: if (new_prototype != null) {
123: getLDM().cachePrototype(aTypeName, new_prototype);
124: if (logger.isDebugEnabled())
125: logger
126: .debug(".getPrototype : Caching prototype for "
127: + aTypeName);
128: getLDM().fillProperties(new_prototype);
129: }
130: }
131:
132: // Return newly constructed prototype (or null, if none found)
133: return new_prototype;
134: }
135:
136: /** Alter prototype string to change '/' to '-' */
137: private String cleanupPrototypeName(String prototype) {
138: return prototype.replace('/', '-');
139: }
140:
141: protected LoggingService logger;
142: protected PrototypeParser prototypeParser;
143: }
|