001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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.db;
027:
028: import org.cougaar.tools.csmart.core.cdata.ComponentData;
029: import org.cougaar.tools.csmart.core.db.DBUtils;
030: import org.cougaar.tools.csmart.core.property.BaseComponent;
031: import org.cougaar.tools.csmart.core.property.ConfigurableComponentPropertyAdapter;
032: import org.cougaar.tools.csmart.core.property.Property;
033: import org.cougaar.tools.csmart.core.property.PropertyEvent;
034: import org.cougaar.tools.csmart.society.AgentBase;
035: import org.cougaar.tools.csmart.society.AgentComponent;
036: import org.cougaar.tools.csmart.society.BinderBase;
037: import org.cougaar.tools.csmart.society.ComponentBase;
038: import org.cougaar.tools.csmart.society.ContainerBase;
039: import org.cougaar.tools.csmart.society.PluginBase;
040: import org.cougaar.tools.csmart.ui.viewer.CSMART;
041: import org.cougaar.util.DBConnectionPool;
042: import org.cougaar.util.DBProperties;
043: import org.cougaar.util.log.Logger;
044:
045: import java.io.IOException;
046: import java.io.ObjectInputStream;
047: import java.io.Serializable;
048: import java.sql.Connection;
049: import java.sql.ResultSet;
050: import java.sql.Statement;
051: import java.util.ArrayList;
052: import java.util.HashMap;
053: import java.util.List;
054: import java.util.Map;
055:
056: /**
057: * A single Agent in a <code>SocietyDBComponent</code>,
058: * i.e. one that was created from the configuration Database.
059: * @see org.cougaar.tools.csmart.core.db.CMT
060: * @see SocietyDBComponent
061: */
062: public class AgentDBComponent extends AgentBase implements
063: AgentComponent, Serializable {
064:
065: /** Agent Property Name **/
066: public static final String PROP_AGENT_NAME = "Agent Name";
067:
068: /** Assembly ID Property Name **/
069: public static final String PROP_ASSEMBLY_ID = "Assembly ID";
070:
071: // /** Component ID Property Name **/
072: // public static final String PROP_COMPONENT_ID = "Component ID";
073:
074: /** Component Category Property Name **/
075: public static final String PROP_COMPONENT_CATEGORY = "Component Category";
076:
077: private static final String QUERY_AGENT_DATA = "queryAgentData";
078: private static final String QUERY_PLUGIN_NAME = "queryPluginNames";
079: private static final String QUERY_PLUGIN_ARGS = "queryComponentArgs";
080:
081: private transient Logger log;
082:
083: // The tree will not display in the builder
084: // if there are no properties so put it's name.
085: private Property propName;
086: // private Property propComponentID;
087:
088: private String assemblyID;
089:
090: private transient DBProperties dbp;
091: private Map substitutions = new HashMap();
092:
093: /**
094: * Creates a new <code>AgentDBComponent</code> instance.
095: *
096: */
097: public AgentDBComponent() {
098: this ("Name", null);
099: }
100:
101: /**
102: * Creates a new <code>AgentDBComponent</code> instance.
103: *
104: * @param name Name of the new Component
105: */
106: public AgentDBComponent(String name) {
107: super (name);
108: this .assemblyID = null;
109: createLogger();
110: }
111:
112: /**
113: * Creates a new <code>AgentDBComponent</code> instance.
114: * This component is created from the database assembly
115: * that is passed in as a parameter.
116: *
117: * @param name Name of the new component
118: * @param assemblyID String Assembly ID for the component
119: */
120: public AgentDBComponent(String name, String assemblyID) {
121: super (name);
122: this .assemblyID = assemblyID;
123: createLogger();
124: }
125:
126: private void createLogger() {
127: log = CSMART.createLogger(this .getClass().getName());
128: }
129:
130: /**
131: * Initialize all local properties.
132: *
133: */
134: public void initProperties() {
135: super .initProperties();
136: // String componentID = name;
137: // String componentCategory = "agent";
138: propName = addProperty(PROP_AGENT_NAME, getShortName(),
139: new ConfigurableComponentPropertyAdapter() {
140: public void propertyValueChanged(PropertyEvent e) {
141: String old = (String) e.getPreviousValue();
142: String newVal = (String) e.getProperty()
143: .getValue();
144: if (log.isDebugEnabled())
145: log.debug("Agent name changed. old = "
146: + old + ", new = " + newVal);
147: if (newVal == null || newVal.trim().equals(""))
148: propName.setValue(old);
149: else if (!newVal.equals(old)) {
150: AgentDBComponent.this .setName(newVal);
151: fireModification();
152: }
153: }
154: });
155:
156: // propComponentID = addProperty(PROP_COMPONENT_ID, componentID,
157: // new ConfigurableComponentPropertyAdapter() {
158: // public void propertyValueChanged(PropertyEvent e) {
159: // }
160: // });
161:
162: try {
163: initDBProperties();
164: } catch (IOException ioe) {
165: if (log.isErrorEnabled()) {
166: log.error("Exception initializing from db "
167: + getShortName() + ": ", ioe);
168: }
169: }
170:
171: // Get the Agent class from the DB
172: initAgentClass();
173:
174: addBinders();
175: addPlugins();
176: addComponents();
177:
178: if (DBUtils.agentHasAssetData(getShortName(), assemblyID)) {
179: addAssetData();
180: }
181: }
182:
183: private void initDBProperties() throws IOException {
184: dbp = DBProperties.readQueryFile(DBUtils.QUERY_FILE, "csmart");
185: }
186:
187: // Get the class (ie, SimpleAgent) for the Agent from the DB
188: private void initAgentClass() {
189: String aClass = null;
190: // queryAgentClass - takes :assembly_id: and :agent_name, gives just class
191: substitutions.put(":agent_name", getShortName());
192: substitutions.put(":assembly_id:", assemblyID);
193: try {
194: Connection conn = DBUtils.getConnection();
195: try {
196: Statement stmt = conn.createStatement();
197: String query = DBUtils.getQuery("queryAgentClass",
198: substitutions);
199: ResultSet rs = stmt.executeQuery(query);
200: if (rs.next()) {
201: aClass = rs.getString(1);
202: }
203: rs.close();
204: stmt.close();
205:
206: } finally {
207: conn.close();
208: }
209: } catch (Exception e) {
210: if (log.isErrorEnabled()) {
211: log.error("Exception getting ag class for ag "
212: + getShortName(), e);
213: }
214: }
215:
216: if (aClass != null && !aClass.trim().equals(""))
217: setAgentClassName(aClass);
218: }
219:
220: private void readObject(ObjectInputStream ois) throws IOException,
221: ClassNotFoundException {
222: ois.defaultReadObject();
223: initDBProperties();
224: createLogger();
225: }
226:
227: protected void addPlugins() {
228: ContainerBase container = new ContainerBase("Plugins");
229: container.initProperties();
230: addChild(container);
231:
232: // Grab all non-CMT assemblies. Why?
233: // String assemblyMatch = DBUtils.getListMatch(assemblyID, "CMT");
234: String assemblyMatch = DBUtils.getListMatch(assemblyID);
235: if (assemblyMatch != null) {
236: substitutions.put(":assemblyMatch", assemblyMatch);
237: substitutions.put(":agent_name", getShortName());
238: }
239:
240: // Get Plugin Names, class names, and parameters
241: try {
242: Connection conn = DBUtils.getConnection();
243: String query = "";
244: substitutions.put(":comp_type:", "= '"
245: + ComponentData.PLUGIN + "'");
246: try {
247: Statement stmt = conn.createStatement();
248: query = dbp.getQuery(QUERY_PLUGIN_NAME, substitutions);
249: ResultSet rs = stmt.executeQuery(query);
250: while (rs.next()) {
251: String pluginClassName = rs.getString(1);
252: String pluginName = rs.getString(4);
253: String priority = rs.getString(6).intern();
254: PluginBase plugin = new PluginBase(pluginName,
255: pluginClassName, priority,
256: ComponentData.PLUGIN);
257: plugin.initProperties();
258: String alibId = rs.getString(2);
259: String libId = rs.getString(3);
260: plugin.setAlibID(alibId);
261: plugin.setLibID(libId);
262: substitutions.put(":comp_alib_id", alibId);
263: substitutions.put(":comp_id", rs.getString(3));
264: Statement stmt2 = conn.createStatement();
265: String query2 = dbp.getQuery(QUERY_PLUGIN_ARGS,
266: substitutions);
267: ResultSet rs2 = stmt2.executeQuery(query2);
268: while (rs2.next()) {
269: String arg = rs2.getString(1);
270: plugin.addParameter(arg);
271: }
272: rs2.close();
273: stmt2.close();
274: container.addChild(plugin);
275: } // end of loop over plugins to add
276: rs.close();
277: stmt.close();
278: } finally {
279: conn.close();
280: }
281: } catch (Exception e) {
282: if (log.isErrorEnabled()) {
283: log.error("Exception", e);
284: }
285: throw new RuntimeException("Error" + e);
286: }
287: }
288:
289: protected void addBinders() {
290: ContainerBase container = new ContainerBase("Binders");
291: container.initProperties();
292: addChild(container);
293:
294: // Grab all non-CMT assemblies. Why?
295: // String assemblyMatch = DBUtils.getListMatch(assemblyID, "CMT");
296: String assemblyMatch = DBUtils.getListMatch(assemblyID);
297: if (assemblyMatch != null) {
298: substitutions.put(":assemblyMatch", assemblyMatch);
299: substitutions.put(":agent_name", getShortName());
300: }
301:
302: // Get Binder Names, class names, and parameters
303: try {
304: Connection conn = DBUtils.getConnection();
305: String query = "";
306: substitutions.put(":comp_type:", "= '"
307: + ComponentData.AGENTBINDER + "'");
308: try {
309: Statement stmt = conn.createStatement();
310: query = dbp.getQuery(QUERY_PLUGIN_NAME, substitutions);
311: // if(log.isDebugEnabled()) {
312: // log.debug("Query: " + query);
313: // }
314:
315: ResultSet rs = stmt.executeQuery(query);
316: while (rs.next()) {
317: String binderClassName = rs.getString(1);
318: String binderName = rs.getString(4);
319: String priority = rs.getString(6).intern();
320: BinderBase binder = new BinderBase(binderName,
321: binderClassName, priority,
322: ComponentData.AGENTBINDER);
323: binder.initProperties();
324: //binder.setBinderType(ComponentData.AGENTBINDER);
325: String alibId = rs.getString(2);
326: String libId = rs.getString(3);
327: binder.setAlibID(alibId);
328: binder.setLibID(libId);
329:
330: substitutions.put(":comp_alib_id", alibId);
331: substitutions.put(":comp_id", rs.getString(3));
332: Statement stmt2 = conn.createStatement();
333: String query2 = dbp.getQuery(QUERY_PLUGIN_ARGS,
334: substitutions);
335: ResultSet rs2 = stmt2.executeQuery(query2);
336: while (rs2.next()) {
337: String arg = rs2.getString(1);
338: binder.addParameter(arg);
339: }
340: rs2.close();
341: stmt2.close();
342: container.addChild(binder);
343: } // end of loop over binders to add
344: rs.close();
345: stmt.close();
346: } finally {
347: conn.close();
348: }
349: } catch (Exception e) {
350: if (log.isErrorEnabled()) {
351: log.error("Exception: ", e);
352: }
353: throw new RuntimeException("Error:" + e);
354: }
355: }
356:
357: protected void addComponents() {
358: ContainerBase container = new ContainerBase("Other Components");
359: container.initProperties();
360: addChild(container);
361:
362: // Grab all non-CMT assemblies. Why?
363: // String assemblyMatch = DBUtils.getListMatch(assemblyID, "CMT");
364: String assemblyMatch = DBUtils.getListMatch(assemblyID);
365: if (assemblyMatch != null) {
366: substitutions.put(":assemblyMatch", assemblyMatch);
367: substitutions.put(":agent_name", getShortName());
368: }
369:
370: // Get Binder Names, class names, and parameters
371: try {
372: Connection conn = DBUtils.getConnection();
373: String query = "";
374:
375: // FIXME!!!
376: List types = new ArrayList();
377: types.add(ComponentData.SOCIETY);
378: types.add(ComponentData.NODE);
379: types.add(ComponentData.HOST);
380: types.add(ComponentData.NODEBINDER);
381: types.add(ComponentData.NODEAGENT);
382: types.add(ComponentData.AGENTBINDER);
383: types.add(ComponentData.AGENT);
384: types.add(ComponentData.PLUGIN);
385: types.add(ComponentData.RECIPE);
386: String ctypematch = "NOT " + DBUtils.getListMatch(types);
387: substitutions.put(":comp_type:", ctypematch);
388:
389: try {
390: Statement stmt = conn.createStatement();
391: query = dbp.getQuery(QUERY_PLUGIN_NAME, substitutions);
392: // if(log.isDebugEnabled()) {
393: // log.debug("Query: " + query);
394: // }
395:
396: ResultSet rs = stmt.executeQuery(query);
397: while (rs.next()) {
398: String binderClassName = rs.getString(1);
399: String binderName = rs.getString(4);
400: String priority = rs.getString(6).intern();
401: String type = rs.getString(5);
402: ComponentBase binder = new ComponentBase(
403: binderName, binderClassName, priority, type);
404: binder.initProperties();
405: // binder.setComponentType(rs.getString(5));
406: String alibId = rs.getString(2);
407: String libId = rs.getString(3);
408: binder.setAlibID(alibId);
409: binder.setLibID(libId);
410:
411: substitutions.put(":comp_alib_id", alibId);
412: substitutions.put(":comp_id", rs.getString(3));
413: Statement stmt2 = conn.createStatement();
414: String query2 = dbp.getQuery(QUERY_PLUGIN_ARGS,
415: substitutions);
416: ResultSet rs2 = stmt2.executeQuery(query2);
417: while (rs2.next()) {
418: String arg = rs2.getString(1);
419: binder.addParameter(arg);
420: }
421: rs2.close();
422: stmt2.close();
423: container.addChild(binder);
424: } // end of loop over binders to add
425: rs.close();
426: stmt.close();
427: } finally {
428: conn.close();
429: }
430: } catch (Exception e) {
431: if (log.isErrorEnabled()) {
432: log.error("Exception: ", e);
433: }
434: throw new RuntimeException("Error: " + e);
435: }
436: }
437:
438: protected void addAssetData() {
439: BaseComponent asset = (BaseComponent) new AssetDBComponent(
440: getShortName(), assemblyID);
441: asset.initProperties();
442: addChild(asset);
443: }
444: }
|