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.planning.plugin.asset.AssetDataFileReader;
029: import org.cougaar.tools.csmart.core.cdata.AgentAssetData;
030: import org.cougaar.tools.csmart.core.cdata.AgentComponentData;
031: import org.cougaar.tools.csmart.core.cdata.AssetDataCallbackImpl;
032: import org.cougaar.tools.csmart.core.cdata.ComponentData;
033: import org.cougaar.tools.csmart.core.cdata.PropGroupData;
034: import org.cougaar.tools.csmart.core.cdata.RelationshipData;
035: import org.cougaar.tools.csmart.core.property.ModifiableConfigurableComponent;
036: import org.cougaar.tools.csmart.core.property.Property;
037: import org.cougaar.tools.csmart.society.AssetComponent;
038: import org.cougaar.tools.csmart.society.ContainerBase;
039: import org.cougaar.tools.csmart.society.PropGroupBase;
040: import org.cougaar.tools.csmart.society.PropGroupComponent;
041: import org.cougaar.tools.csmart.society.RelationshipBase;
042: import org.cougaar.tools.csmart.util.FileParseUtil;
043: import org.cougaar.tools.csmart.util.PrototypeParser;
044: import org.cougaar.util.TimeSpan;
045:
046: import java.io.File;
047: import java.text.DateFormat;
048: import java.text.ParseException;
049: import java.util.Collection;
050: import java.util.Date;
051: import java.util.Iterator;
052:
053: public class AssetFileComponent extends ModifiableConfigurableComponent
054: implements AssetComponent {
055:
056: /** Property Definitions **/
057: // public static final String PROP_TYPE = "Asset Type";
058: // public static final String PROP_TYPE_DESC = "Type of Asset";
059: // public static final String PROP_CLASS = "Asset Class";
060: // public static final String PROP_CLASS_DESC = "Class of the Asset";
061: // public static final String PROP_UID = "UID";
062: // public static final String PROP_UID_DESC = "UID of the Asset";
063: // public static final String PROP_UNITNAME = "Unit Name";
064: // public static final String PROP_UNITNAME_DESC = "Unit Name of the Asset";
065: // public static final String PROP_UIC = "UIC";
066: // public static final String PROP_UIC_DESC = "UIC of the Asset";
067: private Property propAssetClass;
068: private Property propUniqueID;
069: private Property propUnitName;
070: private Property propUIC;
071: private String filename;
072: private String agentName;
073: private int iniFormat;
074:
075: public AssetFileComponent(String filename, String agentName) {
076: super ("AssetData");
077: this .filename = filename;
078: this .agentName = agentName;
079: }
080:
081: /**
082: * Init properties from a file named:
083: * filename base + -prototype-ini.dat
084: */
085: public void initProperties() {
086: // strip off extension if it exists
087: int index = filename.lastIndexOf('.');
088: if (index != -1)
089: filename = filename.substring(0, index);
090: // Since there are two possible types of asset files, we
091: // need to determine which parser to use.
092: // We will determine this by looking for the "UniqueId"
093: // in the <agent>-ini.dat file.
094: AgentAssetData aad = null;
095: if (FileParseUtil
096: .isOldStyleIni(filename + "-prototype-ini.dat")) {
097: if (log.isDebugEnabled()) {
098: log.debug("Using oldStyle Parser");
099: }
100: aad = PrototypeParser.parse(filename);
101: } else {
102: if (log.isDebugEnabled()) {
103: log.debug("Using newStyle Parser");
104: }
105: AssetDataCallbackImpl callback = new AssetDataCallbackImpl(
106: agentName);
107: AssetDataFileReader reader = new AssetDataFileReader();
108: // If this filename is a Windows style filename, then absolute paths wont work.
109: // Try to let the ConfigFinder find it.
110: if (filename.indexOf(':') == 1) {
111: String nfilename = filename.substring(filename
112: .lastIndexOf(File.separatorChar) + 1);
113: if (log.isDebugEnabled()) {
114: log
115: .debug("Agent filename was windows style ("
116: + filename
117: + "), trying short version ("
118: + nfilename
119: + ") & letting ConfigFinder find it. This requires the files be on the ConfigPath");
120: }
121: filename = nfilename;
122: }
123: reader.readAsset(filename, callback);
124: // Warning: the above line will cause a FileNotFoundException
125: // Any time you use a new-style INI
126: // file that is not your config path
127: aad = callback.getAgentAssetData();
128: }
129:
130: if (aad == null) {
131: if (log.isErrorEnabled()) {
132: log
133: .error("Got no AgentAssetData from which to initialize! Perhaps retry with the directory you are loading from on your org.cougaar.config.path?");
134: }
135: return;
136: }
137:
138: this .iniFormat = aad.getIniFormat();
139: propAssetClass = addProperty(PROP_CLASS, aad.getAssetClass());
140: propAssetClass.setToolTip(PROP_CLASS_DESC);
141:
142: String uniqueId = (aad.getUniqueID() != null) ? aad
143: .getUniqueID() : "";
144: propUniqueID = addProperty(PROP_UID, uniqueId);
145: propUniqueID.setToolTip(PROP_UID_DESC);
146:
147: // Unit name is allowed to be null, if it is, give an empty string.
148: String unitname = (aad.getUnitName() == null) ? "" : aad
149: .getUnitName();
150: propUnitName = addProperty(PROP_UNITNAME, unitname);
151: propUnitName.setToolTip(PROP_UNITNAME_DESC);
152:
153: propUIC = addProperty(PROP_UIC, (aad.getUIC() != null) ? aad
154: .getUIC() : "UIC/" + agentName);
155: propUIC.setToolTip(PROP_UIC_DESC);
156:
157: addPropGroups(aad);
158: addRelationships(aad.getRelationshipData());
159: }
160:
161: /**
162: * Add component data for asset properties, relationships,
163: * and property groups.
164: */
165:
166: public ComponentData addComponentData(ComponentData data) {
167: AgentAssetData assetData = new AgentAssetData(
168: (AgentComponentData) data);
169: assetData.setIniFormat(this .iniFormat);
170: assetData.setAssetClass((String) propAssetClass.getValue());
171: assetData.setUniqueID((String) propUniqueID.getValue());
172: assetData.setUnitName((String) propUnitName.getValue());
173: assetData.setUIC((String) propUIC.getValue());
174:
175: // Add Relationships.
176: Iterator iter = ((Collection) getDescendentsOfClass(ContainerBase.class))
177: .iterator();
178: while (iter.hasNext()) {
179: ContainerBase container = (ContainerBase) iter.next();
180: if (container.getShortName().equals("Relationships")) {
181: for (int i = 0; i < container.getChildCount(); i++) {
182: RelationshipBase rel = (RelationshipBase) container
183: .getChild(i);
184: RelationshipData rData = new RelationshipData();
185: rData.setType((String) rel.getProperty(
186: RelationshipBase.PROP_TYPE).getValue());
187: if (this .iniFormat == AgentAssetData.NEW_FORMAT) {
188: // Since role is stored in type in the new format, we must adjust.
189: rData.setRole((String) rel.getProperty(
190: RelationshipBase.PROP_TYPE).getValue());
191: } else {
192: rData.setRole((String) rel.getProperty(
193: RelationshipBase.PROP_ROLE).getValue());
194: }
195:
196: rData.setItemId((String) rel.getProperty(
197: RelationshipBase.PROP_ITEM).getValue());
198: rData.setTypeId((String) rel.getProperty(
199: RelationshipBase.PROP_TYPEID).getValue());
200: rData
201: .setSupported((String) rel.getProperty(
202: RelationshipBase.PROP_SUPPORTED)
203: .getValue());
204: DateFormat df = DateFormat.getInstance();
205: try {
206: Date start = df.parse((String) rel.getProperty(
207: RelationshipBase.PROP_STARTTIME)
208: .getValue());
209: Date end = df.parse((String) rel.getProperty(
210: RelationshipBase.PROP_STOPTIME)
211: .getValue());
212: rData.setStartTime(start.getTime());
213: rData.setEndTime(end.getTime());
214: } catch (ParseException pe) {
215: if (log.isErrorEnabled()) {
216: log
217: .error(
218: "Caught Exception parsing Date, using default dates.",
219: pe);
220: }
221: rData.setStartTime(TimeSpan.MIN_VALUE);
222: rData.setEndTime(TimeSpan.MAX_VALUE);
223: }
224:
225: assetData.addRelationship(rData);
226: }
227: }
228: }
229:
230: // Add Property Groups.
231: iter = ((Collection) getDescendentsOfClass(ContainerBase.class))
232: .iterator();
233: while (iter.hasNext()) {
234: ContainerBase container = (ContainerBase) iter.next();
235: if (container.getShortName().equals("Property Groups")) {
236: for (int i = 0; i < container.getChildCount(); i++) {
237: PropGroupComponent pg = (PropGroupComponent) container
238: .getChild(i);
239: assetData.addPropertyGroup(pg.getPropGroupData());
240: }
241: }
242: }
243:
244: data.addAgentAssetData(assetData);
245: return data;
246: }
247:
248: private void addRelationships(RelationshipData[] rel) {
249: ContainerBase relContainer = new ContainerBase("Relationships");
250: relContainer.initProperties();
251: addChild(relContainer);
252: for (int i = 0; i < rel.length; i++) {
253: RelationshipBase newR = new RelationshipBase(rel[i]);
254: newR.initProperties();
255: relContainer.addChild(newR);
256: }
257: }
258:
259: private void addPropGroups(AgentAssetData aad) {
260: ContainerBase pgContainer = new ContainerBase("Property Groups");
261: pgContainer.initProperties();
262: addChild(pgContainer);
263: Iterator iter = aad.getPropGroupsIterator();
264: while (iter.hasNext()) {
265: PropGroupData pgd = (PropGroupData) iter.next();
266: PropGroupComponent newPG = new PropGroupBase(pgd);
267: if (log.isDebugEnabled()) {
268: log.debug("Adding: " + pgd.getName());
269: }
270: newPG.initProperties();
271: pgContainer.addChild(newPG);
272: }
273: }
274:
275: }
|