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.mlm.plugin.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.Collection;
034:
035: import org.cougaar.core.service.LoggingService;
036: import org.cougaar.util.DBConnectionPool;
037: import org.cougaar.util.DBProperties;
038: import org.cougaar.util.Parameters;
039:
040: public abstract class NewQueryHandler {
041: protected DBProperties dbp;
042: protected NewOplanPlugin plugin;
043: protected String database, username, password;
044: protected LoggingService logger;
045:
046: protected NewQueryHandler(DBProperties adbp, NewOplanPlugin plugin) {
047: dbp = adbp;
048: this .plugin = plugin;
049: this .logger = plugin.getLoggingService();
050: try {
051: String dbtype = dbp.getDBType();
052: insureDriverClass(dbtype);
053: database = dbp.getProperty("database");
054: username = dbp.getProperty("username");
055: password = dbp.getProperty("password");
056: } catch (Exception e) {
057: throw new RuntimeException("SQL Driver problem", e);
058: }
059: }
060:
061: private void insureDriverClass(String dbtype) throws SQLException,
062: ClassNotFoundException {
063: String driverParam = "driver." + dbtype;
064: String driverClass = Parameters.findParameter(driverParam);
065: if (driverClass == null) {
066: // this is likely a "cougaar.rc" problem.
067: // Parameters should be modified to help generate this exception:
068: throw new SQLException("Unable to find driver class for \""
069: + driverParam + "\" -- check your \"cougaar.rc\"");
070: }
071: Class.forName(driverClass);
072: }
073:
074: protected String getString(ResultSet rs, int column)
075: throws SQLException {
076: Object o = rs.getObject(column);
077: if (o instanceof String) {
078: return (String) o;
079: } else {
080: if (o == null)
081: return new String();
082: else {
083: try {
084: return new String((byte[]) o, "US-ASCII");
085: } catch (java.io.UnsupportedEncodingException e) {
086: SQLException sqle = new SQLException(e.getMessage());
087: sqle.initCause(e);
088: throw sqle;
089: }
090: }
091: }
092: }
093:
094: protected abstract Collection executeQueries(Statement stmt)
095: throws SQLException;
096:
097: public final Collection readCollection() throws SQLException {
098: try {
099: Connection conn = DBConnectionPool.getConnection(database,
100: username, password);
101: try {
102: Statement stmt = conn.createStatement();
103: try {
104: return executeQueries(stmt);
105: } finally {
106: stmt.close();
107: }
108: } finally {
109: conn.close();
110: }
111: } catch (SQLException sqle) {
112: throw sqle;
113: } catch (Exception e) {
114: SQLException sqle = new SQLException(
115: "Other error doing queries");
116: sqle.initCause(e);
117: throw sqle;
118: }
119: }
120:
121: public final Object readObject() throws SQLException {
122: Collection c = readCollection();
123: if (c.isEmpty())
124: return null;
125: return c.iterator().next();
126: }
127: }
|