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.planning.ldm;
028:
029: import java.sql.Connection;
030: import java.sql.ResultSet;
031: import java.sql.SQLException;
032: import java.sql.Statement;
033: import java.util.ArrayList;
034: import java.util.List;
035: import java.util.Map;
036:
037: import org.cougaar.core.component.ServiceBroker;
038: import org.cougaar.core.component.ServiceProvider;
039: import org.cougaar.core.node.DBInitializerService;
040: import org.cougaar.planning.plugin.asset.AssetDataDBReader;
041: import org.cougaar.planning.plugin.asset.AssetDataReader;
042: import org.cougaar.planning.service.AssetInitializerService;
043: import org.cougaar.util.log.Logger;
044: import org.cougaar.util.log.Logging;
045:
046: /**
047: * Implementation of AssetInitializerServiceProvider that reads
048: * initialization information from a database.
049: **/
050: public class DBAssetInitializerServiceProvider implements
051: ServiceProvider {
052:
053: private final DBInitializerService dbInit;
054: private final Logger logger;
055:
056: public DBAssetInitializerServiceProvider(DBInitializerService dbInit) {
057: if (dbInit == null)
058: throw new IllegalArgumentException(
059: "Null DBInitializerService passed to DBAssetInitializerServiceProvider constructor.");
060:
061: this .dbInit = dbInit;
062: this .logger = Logging.getLogger(getClass());
063: }
064:
065: public Object getService(ServiceBroker sb, Object requestor,
066: Class serviceClass) {
067: if (serviceClass != AssetInitializerService.class) {
068: throw new IllegalArgumentException(getClass()
069: + " does not furnish " + serviceClass);
070: }
071: return new AssetInitializerServiceImpl();
072: }
073:
074: public void releaseService(ServiceBroker sb, Object requestor,
075: Class serviceClass, Object service) {
076: }
077:
078: private class AssetInitializerServiceImpl implements
079: AssetInitializerService {
080:
081: public String getAgentPrototype(String agentName)
082: throws InitializerException {
083: if (logger.isDebugEnabled()) {
084: logger.debug("In getAgentPrototype");
085: }
086: Map substitutions = dbInit.createSubstitutions();
087: substitutions.put(":agent_name:", agentName);
088: try {
089: Connection conn = dbInit.getConnection();
090: try {
091: Statement stmt = conn.createStatement();
092: String query = dbInit.getQuery(
093: "queryAgentPrototype", substitutions);
094: ResultSet rs = dbInit.executeQuery(stmt, query);
095: if (rs.next()) {
096: String result = dbInit.getNonNullString(rs, 1,
097: query);
098: if (rs.next())
099: throw new InitializerException(
100: "Multiple prototypes for "
101: + agentName);
102: return result;
103: }
104: throw new InitializerException("No prototype for "
105: + agentName);
106: } finally {
107: conn.close();
108: }
109: } catch (SQLException e) {
110: throw new InitializerException("getAgentPrototype("
111: + agentName + ")", e);
112: }
113: }
114:
115: public String[] getAgentPropertyGroupNames(String agentName)
116: throws InitializerException {
117: if (logger.isDebugEnabled()) {
118: logger.debug("In getAgentPropGroupNames");
119: }
120: Map substitutions = dbInit.createSubstitutions();
121: substitutions.put(":agent_name:", agentName);
122: try {
123: Connection conn = dbInit.getConnection();
124: try {
125: Statement stmt = conn.createStatement();
126: String query = dbInit.getQuery("queryAgentPGNames",
127: substitutions);
128: ResultSet rs = dbInit.executeQuery(stmt, query);
129: List result = new ArrayList();
130: while (rs.next()) {
131: result.add(dbInit
132: .getNonNullString(rs, 1, query));
133: }
134: rs.close();
135: stmt.close();
136: return (String[]) result.toArray(new String[result
137: .size()]);
138: } finally {
139: conn.close();
140: }
141: } catch (SQLException e) {
142: throw new InitializerException(
143: "getAgentPropertyGroupNames(" + agentName + ")",
144: e);
145: }
146: }
147:
148: /**
149: * Return values for all properties of a property group as an
150: * array of Object arrays. For each property an array of Objects
151: * has the property's name, type, and value or array of values.
152: * All non-array objects are Strings. If the value is an array it
153: * is an array of Strings
154: **/
155: public Object[][] getAgentProperties(String agentName,
156: String pgName) throws InitializerException {
157: if (logger.isDebugEnabled()) {
158: logger.debug("In getAgentProperties");
159: }
160: try {
161: Connection conn = dbInit.getConnection();
162: Map substitutions = dbInit.createSubstitutions();
163: substitutions.put(":agent_name:", agentName);
164: substitutions.put(":pg_name:", pgName);
165: try {
166: Statement stmt = conn.createStatement();
167: String query = dbInit.getQuery(
168: "queryLibProperties", substitutions);
169: ResultSet rs = dbInit.executeQuery(stmt, query);
170: List result = new ArrayList();
171: while (rs.next()) {
172: String attributeName = dbInit.getNonNullString(
173: rs, 1, query);
174: String attributeType = dbInit.getNonNullString(
175: rs, 2, query);
176: boolean collection = !rs.getString(3).equals(
177: "SINGLE");
178: Object attributeId = rs.getString(4);
179: Statement stmt2 = conn.createStatement();
180: substitutions.put(":pg_attribute_id:",
181: attributeId);
182: String query2 = dbInit.getQuery(
183: "queryAgentProperties", substitutions);
184: ResultSet rs2 = dbInit.executeQuery(stmt2,
185: query2);
186: Object value;
187: if (collection) {
188: List values = new ArrayList();
189: while (rs2.next()) {
190: String v = dbInit.getNonNullString(rs2,
191: 1, query2);
192: values.add(v);
193: }
194: value = values.toArray(new String[values
195: .size()]);
196: } else if (rs2.next()) {
197: value = dbInit.getNonNullString(rs2, 1,
198: query2);
199: if (rs2.next())
200: throw new InitializerException(
201: "Multiple values for "
202: + attributeId);
203: } else {
204: continue; // Skip missing properties
205: // throw new InitializerException("No value for " + attributeId);
206: }
207: Object[] e = { attributeName, attributeType,
208: value };
209: result.add(e);
210: rs2.close();
211: stmt2.close();
212: }
213: rs.close();
214: stmt.close();
215: return (Object[][]) result
216: .toArray(new Object[result.size()][]);
217: } finally {
218: conn.close();
219: }
220: } catch (SQLException e) {
221: throw new InitializerException("getAgentProperties("
222: + agentName + ", " + pgName + ")", e);
223: }
224: }
225:
226: /**
227: * Get the relationships of an agent. Each relationship is
228: * represented by a 6-tuple of the roleName, itemId, typeId,
229: * otherAgentId, start time, and end time.
230: **/
231: public String[][] getAgentRelationships(String agentName)
232: throws InitializerException {
233: try {
234: Connection conn = dbInit.getConnection();
235: Map substitutions = dbInit.createSubstitutions();
236: substitutions.put(":agent_name:", agentName);
237: try {
238: Statement stmt = conn.createStatement();
239: String query = dbInit.getQuery(
240: "queryAgentRelation", substitutions);
241: ResultSet rs = dbInit.executeQuery(stmt, query);
242: List result = new ArrayList();
243: while (rs.next()) {
244: String[] v = {
245: dbInit.getNonNullString(rs, 1, query),
246: dbInit.getNonNullString(rs, 2, query),
247: dbInit.getNonNullString(rs, 3, query),
248: dbInit.getNonNullString(rs, 4, query),
249: rs.getString(5), rs.getString(6), };
250: result.add(v);
251: }
252: rs.close();
253: stmt.close();
254: String[][] ary = (String[][]) result
255: .toArray(new String[result.size()][]);
256: if (false) {
257: StringBuffer buf = new StringBuffer();
258: buf
259: .append(System
260: .getProperty("line.separator"));
261: for (int i = 0; i < ary.length; i++) {
262: String[] ary2 = ary[i];
263: buf.append("Relationship of ");
264: buf.append(agentName);
265: buf.append(": ");
266: for (int j = 0; j < ary2.length; j++) {
267: if (j > 0)
268: buf.append('\t');
269: buf.append(ary2[j]);
270: }
271: buf.append(System
272: .getProperty("line.separator"));
273: }
274: System.out.println(buf);
275: }
276: return ary;
277: } finally {
278: conn.close();
279: }
280: } catch (SQLException e) {
281: throw new InitializerException("getAgentRelationships("
282: + agentName + ")", e);
283: }
284: }
285:
286: public AssetDataReader getAssetDataReader() {
287: return new AssetDataDBReader(this );
288: }
289:
290: public Object[] translateAttributeValue(String type, String key)
291: throws InitializerException {
292: try {
293: return dbInit.translateAttributeValue(type, key);
294: } catch (SQLException e) {
295: throw new InitializerException(
296: "translateAttributeValue(" + type + ", " + key
297: + ")", e);
298: }
299: }
300: }
301: }
|