001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.tools.csmart.society.file;
027:
028: import org.cougaar.core.component.ComponentDescription;
029: import org.cougaar.tools.csmart.core.cdata.ComponentConnector;
030: import org.cougaar.tools.csmart.core.property.BaseComponent;
031: import org.cougaar.tools.csmart.society.AgentBase;
032: import org.cougaar.tools.csmart.society.AgentComponent;
033: import org.cougaar.tools.csmart.society.BinderBase;
034: import org.cougaar.tools.csmart.society.ComponentBase;
035: import org.cougaar.tools.csmart.society.ContainerBase;
036: import org.cougaar.tools.csmart.society.PluginBase;
037: import org.cougaar.tools.csmart.ui.viewer.CSMART;
038: import org.cougaar.tools.csmart.ui.viewer.SocietyFinder;
039: import org.cougaar.util.log.Logger;
040:
041: import java.util.Iterator;
042:
043: /**
044: * Basic component to hold the definition of a society
045: * read in from Files.
046: */
047: public class AgentFileComponent extends AgentBase implements
048: AgentComponent {
049:
050: private transient Logger log;
051: private String filename; // that defines this agent
052:
053: /**
054: * Creates a new <code>AgentFileComponent</code> instance.
055: *
056: * @param name name of the agent
057: * @param filename complete pathname of file that defines agent
058: * @param classname Classname for the agent
059: */
060: public AgentFileComponent(String name, String filename,
061: String classname) {
062: super (name);
063: this .classname = classname;
064: this .filename = filename;
065: createLogger();
066: }
067:
068: private void createLogger() {
069: log = CSMART.createLogger(this .getClass().getName());
070: }
071:
072: public void initProperties() {
073: super .initProperties();
074:
075: addPlugins();
076: addBinders();
077: addComponents();
078: addAssetData();
079: }
080:
081: protected void addAssetData() {
082: String assetFile = filename.substring(0, filename
083: .lastIndexOf('.'));
084: assetFile = assetFile + "-prototype-ini.dat";
085: if (log.isDebugEnabled()) {
086: log.debug("Trying asset file " + assetFile);
087: }
088: // Check to see if the file exists before we do anything!
089: if (SocietyFinder.getInstance().locateFile(assetFile) != null) {
090: BaseComponent asset = (BaseComponent) new AssetFileComponent(
091: filename, getShortName());
092: asset.initProperties();
093: addChild(asset);
094: }
095: }
096:
097: protected void addPlugins() {
098: ContainerBase container = new ContainerBase("Plugins");
099: container.initProperties();
100: addChild(container);
101:
102: ComponentDescription[] desc = ComponentConnector
103: .parseFile(filename);
104: if (desc == null) {
105: if (log.isWarnEnabled()) {
106: log.warn("No data found in file " + filename);
107: }
108: return;
109: }
110: for (int i = 0; i < desc.length; i++) {
111: StringBuffer name = new StringBuffer(desc[i].getName());
112: StringBuffer className = new StringBuffer(desc[i]
113: .getClassname());
114: String insertionPoint = desc[i].getInsertionPoint();
115: String priority = desc[i].priorityToString(desc[i]
116: .getPriority());
117: // if(log.isDebugEnabled()) {
118: // log.debug("Insertion Point: " + insertionPoint);
119: // }
120:
121: if (insertionPoint.endsWith("Plugin")) {
122: int start = 0;
123: if ((start = name.indexOf("OrgRTData")) != -1) {
124: name.delete(start, start + 2);
125: start = className.indexOf("RT");
126: className.delete(start, start + 2);
127: }
128:
129: // HACK!
130: // When dumping INIs we add an extra parameter to the GLSInitServlet,
131: // so strip it off here
132: boolean isGLS = false;
133: if (className.indexOf("GLSInitServlet") != -1) {
134: isGLS = true;
135: }
136:
137: int index = name.lastIndexOf(".");
138: if (index != -1)
139: name.delete(0, index + 1);
140: PluginBase plugin = new PluginBase(name.substring(0),
141: className.substring(0), priority);
142: plugin.initProperties();
143: Iterator iter = ComponentConnector
144: .getPluginProps(desc[i]);
145: while (iter.hasNext()) {
146: if (isGLS) {
147: String param = (String) iter.next();
148: if (param.startsWith("exptid="))
149: continue;
150: else
151: plugin.addParameter(param);
152: } else
153: plugin.addParameter((String) iter.next());
154: }
155: container.addChild(plugin);
156: }
157: }
158: }
159:
160: protected void addBinders() {
161: ContainerBase container = new ContainerBase("Binders");
162: container.initProperties();
163: addChild(container);
164:
165: ComponentDescription[] desc = ComponentConnector
166: .parseFile(filename);
167: if (desc == null) {
168: if (log.isWarnEnabled()) {
169: log.warn("No data found in file " + filename);
170: }
171: return;
172: }
173: for (int i = 0; i < desc.length; i++) {
174: String name = desc[i].getName();
175: String insertionPoint = desc[i].getInsertionPoint();
176: // if(log.isDebugEnabled()) {
177: // log.debug("Insertion Point: " + insertionPoint);
178: // }
179:
180: if (insertionPoint.endsWith("Binder")) {
181: if (log.isDebugEnabled()) {
182: log.debug("Create Binder: " + name);
183: }
184: int index = name.lastIndexOf('.');
185: if (index != -1)
186: name = name.substring(index + 1);
187: BinderBase binder = new BinderBase(name, desc[i]
188: .getClassname(), desc[i]
189: .priorityToString(desc[i].getPriority()),
190: insertionPoint);
191: binder.initProperties();
192:
193: // FIXME: Must I change ComponentConnector in some way here?
194: Iterator iter = ComponentConnector
195: .getPluginProps(desc[i]);
196: while (iter.hasNext())
197: binder.addParameter((String) iter.next());
198: container.addChild(binder);
199: }
200: }
201: }
202:
203: protected void addComponents() {
204: ContainerBase container = new ContainerBase("Other Components");
205: container.initProperties();
206: addChild(container);
207:
208: ComponentDescription[] desc = ComponentConnector
209: .parseFile(filename);
210: if (desc == null) {
211: if (log.isWarnEnabled()) {
212: log.warn("No data found in file " + filename);
213: }
214: return;
215: }
216: for (int i = 0; i < desc.length; i++) {
217: String name = desc[i].getName();
218: String insertionPoint = desc[i].getInsertionPoint();
219: // if(log.isDebugEnabled()) {
220: // log.debug("Insertion Point: " + insertionPoint);
221: // }
222:
223: if (!insertionPoint.endsWith(".Binder")
224: && !insertionPoint.endsWith(".Plugin")) {
225: if (log.isDebugEnabled()) {
226: log.debug("Create Component: " + name);
227: }
228: int index = name.lastIndexOf('.');
229: if (index != -1)
230: name = name.substring(index + 1);
231: ComponentBase binder = new ComponentBase(name, desc[i]
232: .getClassname(), desc[i]
233: .priorityToString(desc[i].getPriority()),
234: insertionPoint);
235: binder.initProperties();
236:
237: // FIXME: Must I change ComponentConnector in some way here?
238: Iterator iter = ComponentConnector
239: .getPluginProps(desc[i]);
240: while (iter.hasNext())
241: binder.addParameter((String) iter.next());
242: container.addChild(binder);
243: }
244: }
245: }
246:
247: } // end of AgentFileComponent
|