KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > jdbc > JDBCDatabaseEngine


1 package com.sslexplorer.jdbc;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.sql.Connection JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.util.Calendar JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 import com.sslexplorer.boot.Util;
15 import com.sslexplorer.vfs.webdav.DAVUtilities;
16
17 /**
18  * <p>
19  * Title:
20  * </p>
21  *
22  * <p>
23  * Description:
24  * </p>
25  *
26  * <p>
27  * Copyright: Copyright (c) 2004
28  * </p>
29  *
30  * <p>
31  * Company:
32  * </p>
33  *
34  * @author not attributable
35  */

36 public abstract class JDBCDatabaseEngine {
37
38     Log log = LogFactory.getLog(JDBCDatabaseEngine.class);
39     Connection JavaDoc conn;
40     Properties JavaDoc SQL;
41     String JavaDoc vendor;
42     String JavaDoc db;
43     String JavaDoc driver;
44     String JavaDoc alias;
45     String JavaDoc username;
46     String JavaDoc password;
47     
48     // database instance name, such as myOracleDB
49
// the connection pool for that database instance
50
private JDBCConnectionImpl.JDBCPool connectionPool;
51
52     public JDBCDatabaseEngine(String JavaDoc vendor, String JavaDoc driver) {
53         this.vendor = vendor;
54         this.driver = driver;
55     }
56
57     public abstract String JavaDoc getURL();
58
59     public String JavaDoc getDatabase() {
60         return db;
61     }
62     
63     public String JavaDoc getAlias() {
64         return alias;
65     }
66
67     public void init(String JavaDoc alias, String JavaDoc db, String JavaDoc username, String JavaDoc password, ClassLoader JavaDoc classLoader) throws SQLException JavaDoc, ClassNotFoundException JavaDoc {
68         init(alias, db, username, password, null, classLoader);
69     }
70
71     /**
72      * Initialize the DB connection using a specified resource path. This
73      * location must contain both the default SQL resources and any vendor
74      * specific resources.
75      *
76      * @param alias alias
77      * @param db String
78      * @param username String
79      * @param password String
80      * @param defaultResourcePath String
81      * @param classLoader class loader to load resources with
82      * @throws SQLException
83      * @throws ClassNotFoundException
84      */

85     public void init(String JavaDoc alias, String JavaDoc db, String JavaDoc username, String JavaDoc password, String JavaDoc defaultResourcePath, ClassLoader JavaDoc classLoader) throws SQLException JavaDoc,
86                     ClassNotFoundException JavaDoc {
87         try {
88
89             this.db = db;
90             this.alias = alias;
91             this.username = username;
92             this.password = password;
93             
94             /**
95              * Load the SQL resources. This code assumes that if a default
96              * resource path is supplied, for example "/com/sslexplorer/xtra"
97              * then both generic SQL resources and database vendor specific
98              * resource files are located in the same package.
99              *
100              * Otherwise this code will load default resources from the
101              * com.sslexplorer.core.jdbc package and vendor specific resources
102              * from the package that contains the vendor implementation class.
103              */

104
105             String JavaDoc defaultResources;
106             if (defaultResourcePath == null)
107                 defaultResources = "/prepared/" + alias + ".properties";
108             else
109                 defaultResources = defaultResourcePath + "/" + alias + ".properties";
110
111             /**
112              * Get the database specific resources
113              */

114             String JavaDoc dbResources;
115
116             if (defaultResourcePath == null)
117                 dbResources = "/prepared/" + vendor.toLowerCase() + "/" + alias + ".properties";
118             else
119                 dbResources = defaultResourcePath + "/" + vendor.toLowerCase() + "/" + alias + ".properties";
120             
121             /**
122              * Load the default resources into our properties object
123              */

124             SQL = new Properties JavaDoc();
125             InputStream JavaDoc in = classLoader == null ? getClass().getResourceAsStream(defaultResources)
126                                 : classLoader.getResourceAsStream(DAVUtilities.stripLeadingSlash(defaultResources));
127             if(in != null) {
128                 try {
129                     SQL.load(in);
130                 }
131                 finally {
132                     in.close();
133                 }
134             }
135
136             /**
137              * If we have any database specific then override
138              */

139             in = classLoader == null ? getClass().getResourceAsStream(dbResources)
140                     : classLoader.getResourceAsStream(DAVUtilities.stripLeadingSlash(dbResources));
141             if (in != null) {
142                 try {
143                     SQL.load(in);
144                 }
145                 finally {
146                     in.close();
147                 }
148             }
149             connectionPool = JDBCConnectionImpl.JDBCPool.getInstance();
150             String JavaDoc url = getURL();
151             if (log.isInfoEnabled())
152                 log.info("Aliasing database " + alias + " to " + db + " using driver " + driver + " and URL " + url);
153             connectionPool.createImpl(alias + db, driver, getURL(), username, password);
154         } catch (IOException JavaDoc ex) {
155             if (log.isInfoEnabled())
156                 log.info("Failed to load database resources for " + db, ex);
157             throw new SQLException JavaDoc("Failed to load database resources for " + db);
158         }
159     }
160
161     public JDBCPreparedStatement getStatement(String JavaDoc key) throws SQLException JavaDoc, ClassNotFoundException JavaDoc {
162         return getStatement(null, key);
163     }
164
165     public JDBCPreparedStatement getStatement(JDBCPreparedStatement ps, String JavaDoc key) throws SQLException JavaDoc, ClassNotFoundException JavaDoc {
166
167         if (SQL.containsKey(key)) {
168             String JavaDoc sql = SQL.getProperty(key);
169             if (log.isDebugEnabled())
170                 log.debug("Aquiring statement for " + key + " = '" + sql + "'");
171             return aquirePreparedStatement(key, sql, ps);
172         }
173
174         throw new SQLException JavaDoc("Unable to locate database resource " + key + " in " + getAlias());
175     }
176
177     public JDBCConnectionImpl aquireConnection() throws SQLException JavaDoc, ClassNotFoundException JavaDoc {
178         return connectionPool.acquireImpl(alias + db);
179     }
180
181     public void releaseConnection(JDBCConnectionImpl con) throws SQLException JavaDoc {
182         connectionPool.releaseImpl(con);
183     }
184
185     JDBCPreparedStatement aquirePreparedStatement(String JavaDoc key, String JavaDoc sql, JDBCPreparedStatement ps) throws SQLException JavaDoc, ClassNotFoundException JavaDoc {
186         return new JDBCPreparedStatement(key, sql, this, ps == null ? aquireConnection() : ps.getConnection(), ps != null);
187     }
188
189     public void releasePreparedStatement(JDBCPreparedStatement ps) throws SQLException JavaDoc {
190         ps.releasePreparedStatement();
191     }
192
193     // send a request to the database and return the result
194
public ResultSet JavaDoc executeQuery(String JavaDoc sqlString) throws SQLException JavaDoc, ClassNotFoundException JavaDoc {
195         JDBCConnectionImpl impl = connectionPool.acquireImpl(alias + db);
196         try {
197             ResultSet JavaDoc rs = impl.executeQuery(sqlString);
198             return rs;
199         } finally {
200             connectionPool.releaseImpl(impl);
201         }
202     }
203
204     public void execute(String JavaDoc sqlString) throws SQLException JavaDoc, ClassNotFoundException JavaDoc {
205         JDBCConnectionImpl impl = connectionPool.acquireImpl(alias + db);
206         try {
207             impl.execute(sqlString);
208         } finally {
209             connectionPool.releaseImpl(impl);
210         }
211     }
212
213     public boolean isDatabaseExists() {
214         return true;
215     }
216     
217     public long getLastInsertIdLong(JDBCPreparedStatement ps, String JavaDoc key) throws Exception JavaDoc {
218         String JavaDoc sql = SQL.getProperty(key);
219         if (log.isDebugEnabled())
220             log.debug("Aquiring statement for " + key + " = '" + sql + "'");
221         ps.reprepare(key, sql);
222         ResultSet JavaDoc rs = ps.executeQuery();
223         try {
224             if (!rs.next()) {
225                 throw new Exception JavaDoc("Failed to select last inserted ID from table");
226             }
227             return rs.getLong(1);
228         }
229         finally {
230             rs.close();
231         }
232     }
233
234     public int getLastInsertId(JDBCPreparedStatement ps, String JavaDoc key) throws Exception JavaDoc {
235         String JavaDoc sql = SQL.getProperty(key);
236         if (log.isDebugEnabled())
237             log.debug("Aquiring statement for " + key + " = '" + sql + "'");
238         ps.reprepare(key, sql);
239         ResultSet JavaDoc rs = ps.executeQuery();
240         try {
241             if (!rs.next()) {
242                 throw new Exception JavaDoc("Failed to select last inserted ID from table");
243             }
244             return rs.getInt(1);
245         }
246         finally {
247             rs.close();
248         }
249     }
250
251     public abstract String JavaDoc formatTimestamp(Calendar JavaDoc c);
252
253     public void stop() {
254         if(connectionPool != null) {
255             connectionPool.closeAll();
256         }
257     }
258 }
Popular Tags