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.core.agent.Agent;
029: import org.cougaar.tools.csmart.core.db.DBUtils;
030: import org.cougaar.tools.csmart.core.property.ModificationEvent;
031: import org.cougaar.tools.csmart.society.SocietyBase;
032: import org.cougaar.tools.csmart.ui.viewer.CSMART;
033: import org.cougaar.util.log.Logger;
034:
035: import java.io.IOException;
036: import java.io.ObjectInputStream;
037: import java.io.Serializable;
038: import java.sql.Connection;
039: import java.sql.ResultSet;
040: import java.sql.Statement;
041: import java.util.HashMap;
042: import java.util.Map;
043:
044: /**
045: * A Society created from the CFW portion of the CSMART configuration
046: * database.
047: * @see org.cougaar.tools.csmart.core.db.CMT
048: */
049: public class SocietyDBComponent extends SocietyBase implements
050: Serializable {
051: protected static final String DESCRIPTION_RESOURCE_NAME = "/org/cougaar/tools/csmart/society/society-base-description.html";
052: protected static final String BACKUP_DESCRIPTION = "A Society created from the database: Agents, Binders, Plugins, etc.";
053:
054: private static final String QUERY_AGENT_NAMES = "queryAgentNames";
055:
056: private Map substitutions;
057: private transient Logger log;
058:
059: public SocietyDBComponent(String name) {
060: super (name);
061: createLogger();
062: }
063:
064: public SocietyDBComponent(String name, String assemblyId) {
065: super (name);
066: this .assemblyId = assemblyId;
067: createLogger();
068: }
069:
070: private void createLogger() {
071: log = CSMART.createLogger(this .getClass().getName());
072: }
073:
074: public void initProperties() {
075: Map substitutions = new HashMap();
076: if (assemblyId != null) {
077: substitutions.put(":assemblyMatch", DBUtils
078: .getListMatch(assemblyId));
079: substitutions
080: .put(":insertion_point", Agent.INSERTION_POINT);
081:
082: // FIXME:
083: // It would be really nice to be able to handle Binders and other non-Agent
084: // top-level things
085:
086: try {
087: Connection conn = DBUtils.getConnection();
088: try {
089: Statement stmt = conn.createStatement();
090: String query = DBUtils.getQuery(QUERY_AGENT_NAMES,
091: substitutions);
092: ResultSet rs = stmt.executeQuery(query);
093: while (rs.next()) {
094: String agentName = DBUtils.getNonNullString(rs,
095: 1, query);
096: AgentDBComponent agent = new AgentDBComponent(
097: agentName, assemblyId);
098: agent.initProperties();
099: addChild(agent);
100: }
101: rs.close();
102: stmt.close();
103: } finally {
104: conn.close();
105: }
106: } catch (Exception e) {
107: if (log.isErrorEnabled()) {
108: log.error("Exception", e);
109: }
110: throw new RuntimeException("Error" + e);
111: }
112: // After reading the soc from the DB, it is not modified.
113: // modified = false;
114: modified = false;
115: fireModification(new ModificationEvent(this , SOCIETY_SAVED));
116: }
117: }
118:
119: // public void modified(ModificationEvent e) {
120: // fireModification();
121: // }
122:
123: public void setName(String newName) {
124: super .setName(newName);
125: fireModification();
126: }
127:
128: /**
129: * Returns whether the society is self terminating or must
130: * be manually terminated.
131: * Self terminating nodes cause the app-server to send back
132: * a "process-destroyed" message when the node terminates.
133: * @return true if society is self terminating
134: */
135: public boolean isSelfTerminating() {
136: return false;
137: }
138:
139: // public ModifiableComponent copy(String name) {
140: // // FIXME: I'd like to do the normal copy,
141: // // but my children only get created in initproperties
142: // // if I have an assemblyId, and by reading from the DB, so I don't
143: // // know if it would work
144: // //ModifiableComponent component = super.copy(name);
145: // if (log.isDebugEnabled()) {
146: // log.debug("Copying society " + this.getSocietyName() + " with assembly " + getAssemblyId() + " into new name " + name);
147: // }
148: // String assemblyID = getAssemblyId();
149: // ModifiableComponent sc = new SocietyDBComponent(name, assemblyID);
150: // sc.initProperties();
151:
152: // // If I re-init from DB, it is not modified, per se.
153: // // But we're putting it under a new assembly ID, and not saving it
154: // // so to that extent, it is modified.
155: // // Otherwise, set to the old value (= this.modified)
156: // ((SocietyBase)sc).modified = true;
157: // ((SocietyBase)sc).oldAssemblyId = assemblyID;
158: // ((SocietyBase)sc).setAssemblyId(null); // so it will save under new ID
159:
160: // return sc;
161: // }
162:
163: private void readObject(ObjectInputStream ois) throws IOException,
164: ClassNotFoundException {
165: ois.defaultReadObject();
166: createLogger();
167: }
168:
169: }
|