KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > database > StandAloneConnection


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2006 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 package org.openbravo.database;
13
14 import javax.servlet.ServletException JavaDoc;
15
16 import org.apache.xerces.parsers.DOMParser;
17 import org.xml.sax.*;
18 import org.w3c.dom.*;
19
20 import java.io.*;
21 import java.sql.*;
22 import org.openbravo.exception.*;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import org.apache.log4j.Logger;
27
28 public class StandAloneConnection implements ConnectionProvider {
29   public static ConnectionPool myPool;
30   protected static Hashtable JavaDoc<String JavaDoc, ConnectionPool> pools = new Hashtable JavaDoc<String JavaDoc, ConnectionPool>();
31   String JavaDoc strDriver;
32   String JavaDoc strURL;
33   Connection connection;
34   static Logger log4j = Logger.getLogger(StandAloneConnection.class);
35   static String JavaDoc sqlDateFormat;
36
37   public void createPool(String JavaDoc poolDir) {
38     try {
39
40       DOMParser parser = new DOMParser();
41       try {
42         parser.parse(poolDir + "/XmlPool.xml");
43       } catch (SAXException se) {
44         se.printStackTrace();
45       } catch (IOException ioe) {
46         ioe.printStackTrace();
47       }
48       Document xmlPool = parser.getDocument();
49       NodeList nodeList = xmlPool.getElementsByTagName("pool");
50       log4j.info("pool elements: " + nodeList.getLength());
51       Node child = null;
52       for (int i=0;i<nodeList.getLength();i++) {
53         Node pool = nodeList.item(i); // We obtain the pool
54
NamedNodeMap atributos = pool.getAttributes(); // We get its attributes to obtain its name
55
String JavaDoc poolName = atributos.item(0).getNodeValue(); // We get the name of the pool, which is the first and unique attribute of the pool
56
log4j.info("Pool name: " + poolName);
57
58         /* We obtain the different nodes of the correspondant pool, which are the pool's configuration parameters, each one separated by two nodes
59         */

60         child = pool.getFirstChild();
61         child = child.getNextSibling();
62         String JavaDoc dbDriver = ((Text)child.getFirstChild()).getData();
63         log4j.info("Driver: " + dbDriver);
64         child = child.getNextSibling();
65         child = child.getNextSibling();
66         String JavaDoc dbServer = ((Text)child.getFirstChild()).getData();
67         log4j.info("Server: " + dbServer);
68         child = child.getNextSibling();
69         child = child.getNextSibling();
70         Text textDbLogin = (Text)child.getFirstChild();
71         String JavaDoc dbLogin;
72         if (textDbLogin != null) {
73           dbLogin = textDbLogin.getData();
74           log4j.info("Login: " + dbLogin);
75         } else
76           dbLogin = null;
77         child = child.getNextSibling();
78         child = child.getNextSibling();
79         Text textDbPassword = (Text)child.getFirstChild();
80         String JavaDoc dbPassword;
81         if (textDbPassword != null) {
82           dbPassword = textDbPassword.getData();
83           log4j.info("Password: " + dbPassword);
84         } else
85           dbPassword = null;
86         child = child.getNextSibling();
87         child = child.getNextSibling();
88         int minConns = Integer.valueOf(((Text)child.getFirstChild()).getData()).intValue();
89         log4j.info("Min conns: " + minConns);
90         child = child.getNextSibling();
91         child = child.getNextSibling();
92         int maxConns = Integer.valueOf(((Text)child.getFirstChild()).getData()).intValue();
93         log4j.info("Max conns: " + maxConns);
94         child = child.getNextSibling();
95         child = child.getNextSibling();
96         double maxConnTime = Double.valueOf(((Text)child.getFirstChild()).getData()).doubleValue();
97         log4j.info("maxConnTime: " + Double.toString(maxConnTime));
98         child = child.getNextSibling();
99         child = child.getNextSibling();
100         String JavaDoc dbSessionConfig = ((Text)child.getFirstChild()).getData();
101         log4j.info("dbSessionConfig: " + dbSessionConfig);
102         child = child.getNextSibling();
103         child = child.getNextSibling();
104         String JavaDoc rdbms = ((Text)child.getFirstChild()).getData();
105         log4j.info("rdbms: " + rdbms);
106
107         readProperties(poolDir+"/Openbravo.properties");
108
109         ConnectionPool myLocalPool = new ConnectionPool(dbDriver,dbServer,dbLogin,dbPassword, minConns,maxConns, maxConnTime,dbSessionConfig, rdbms);
110
111         if (myLocalPool==null)
112           log4j.error("Initialization failed on pool: " + i);
113         pools.put(poolName, myLocalPool);
114         if (i==0) {
115           myPool = myLocalPool;
116         }
117       }
118     }
119     catch (Exception JavaDoc e) {
120       e.printStackTrace();
121     }
122   }
123
124   // methods of Connection provider
125
private ConnectionPool getPool(String JavaDoc poolName) throws PoolNotFoundException {
126     ConnectionPool myPool = pools.get(poolName);
127     if (myPool == null)
128       throw new PoolNotFoundException(poolName + " not found");
129     else
130       return myPool;
131   }
132
133   private ConnectionPool getPool() throws PoolNotFoundException {
134     if (myPool == null)
135       throw new PoolNotFoundException("Default pool not found");
136     else
137       return myPool;
138   }
139
140   public Connection getConnection() {
141     try {
142       return (getPool().getConnection());
143     }
144     catch (Exception JavaDoc ignored){}
145     return null;
146   }
147
148   public String JavaDoc getRDBMS() {
149     try {
150       return (getPool().getRDBMS());
151     }
152     catch (Exception JavaDoc ignored){}
153     return null;
154   }
155
156   public Connection getTransactionConnection() {
157     try {
158       Connection conn = getPool().getConnection();
159       conn.setAutoCommit(false);
160       return conn;
161     } catch (PoolNotFoundException ignored){
162     } catch (SQLException e) {
163       e.printStackTrace();
164     }
165     return null;
166   }
167
168   public void releaseCommitConnection(Connection conn) {
169     try {
170       conn.commit();
171       myPool.releaseConnection(conn);
172       return;
173     } catch (SQLException e) {
174       e.printStackTrace();
175     }
176     return;
177   }
178
179   public void releaseRollbackConnection(Connection conn) {
180     try {
181       conn.rollback();
182       myPool.releaseConnection(conn);
183       return;
184     } catch (SQLException e) {
185       e.printStackTrace();
186     }
187     return;
188   }
189
190   public PreparedStatement getPreparedStatement(String JavaDoc poolName, String JavaDoc strSql) {
191     try {
192       return (getPool(poolName).getPreparedStatement(strSql));
193     }
194     catch (PoolNotFoundException ignored){}
195     return null;
196   }
197
198   public PreparedStatement getPreparedStatement(String JavaDoc strSql) {
199     try {
200       return (getPool().getPreparedStatement(strSql));
201     }
202     catch (PoolNotFoundException ignored){}
203     return null;
204   }
205
206   public PreparedStatement getPreparedStatement(Connection conn, String JavaDoc strSql) {
207     try {
208       return (getPool().getPreparedStatement(conn, strSql));
209     }
210     catch (PoolNotFoundException ignored){}
211     return null;
212   }
213
214   public void releasePreparedStatement(PreparedStatement preparedStatement){
215     try {
216       Connection conn = preparedStatement.getConnection();
217       preparedStatement.close();
218       myPool.releaseConnection(conn);
219     }
220     catch (SQLException e) {
221       e.printStackTrace();
222     }
223   }
224
225   public Statement getStatement(String JavaDoc poolName) {
226     try {
227       return (getPool(poolName).getStatement());
228     }
229     catch (PoolNotFoundException ignored){}
230     return null;
231   }
232
233   public Statement getStatement() {
234     try {
235       return (getPool().getStatement());
236     }
237     catch (PoolNotFoundException ignored){}
238     return null;
239   }
240
241   public Statement getStatement(Connection conn) {
242     try {
243       return (getPool().getStatement(conn));
244     }
245     catch (PoolNotFoundException ignored){}
246     return null;
247   }
248
249   public void releaseStatement(Statement statement){
250     try {
251       Connection conn = statement.getConnection();
252       statement.close();
253       myPool.releaseConnection(conn);
254     }
255     catch (SQLException e) {
256       e.printStackTrace();
257     }
258   }
259
260   public void releaseTransactionalStatement(Statement statement){
261     try {
262       statement.close();
263     }
264     catch (SQLException e) {
265       e.printStackTrace();
266     }
267   }
268
269   public void releaseTransactionalPreparedStatement(PreparedStatement preparedStatement){
270     try {
271       preparedStatement.close();
272     }
273     catch (SQLException e) {
274       e.printStackTrace();
275     }
276   }
277
278   public CallableStatement getCallableStatement(String JavaDoc poolName, String JavaDoc strSql) {
279     try {
280       return (getPool(poolName).getCallableStatement(strSql));
281     }
282     catch (PoolNotFoundException ignored){}
283     return null;
284   }
285
286   public CallableStatement getCallableStatement(String JavaDoc strSql) {
287     try {
288       return (getPool().getCallableStatement(strSql));
289     }
290     catch (PoolNotFoundException ignored){}
291     return null;
292   }
293
294   public CallableStatement getCallableStatement(Connection conn, String JavaDoc strSql) {
295     try {
296       return (getPool().getCallableStatement(conn, strSql));
297     }
298     catch (PoolNotFoundException ignored){}
299     return null;
300   }
301
302   public void releaseCallableStatement(CallableStatement callableStatement){
303     try {
304       Connection conn = callableStatement.getConnection();
305       callableStatement.close();
306       myPool.releaseConnection(conn);
307     }
308     catch (SQLException e) {
309       e.printStackTrace();
310     }
311   }
312
313   public void connect() throws ClassNotFoundException JavaDoc, SQLException {
314     log4j.info("Driver loading: " + strDriver);
315     Class.forName(strDriver);
316     log4j.info("Driver loaded");
317     log4j.info("Connection with: "+ strURL);
318     connection=DriverManager.getConnection(strURL);
319     log4j.info("connect made");
320   }
321
322   public void closeConnection() {
323     try {
324       connection.close();
325     } catch(SQLException e){
326       log4j.error("SQL error in closeConnection: " + e);
327     }
328   }
329
330   public static int dynamicQuery(ConnectionProvider conn, String JavaDoc strSql) throws ServletException JavaDoc {
331     Statement stmt = null;
332     int updateCount = 0;
333
334     try {
335       stmt = conn.getStatement();
336       updateCount = stmt.executeUpdate(strSql);
337     } catch(Exception JavaDoc e){
338       log4j.error("SQL error in query: " + strSql + "\nException:"+ e);
339       throw new ServletException JavaDoc(e.toString());
340     } finally {
341       try {
342         conn.releaseStatement(stmt);
343       } catch(Exception JavaDoc ignored) {}
344     }
345     return(updateCount);
346   }
347
348   public static String JavaDoc preformatedColumn(String JavaDoc strValue, String JavaDoc type) {
349     StringBuffer JavaDoc script = new StringBuffer JavaDoc();
350     script.append("(CASE ").append(strValue).append(" WHEN NULL THEN 'NULL' ELSE ");
351     if (strValue==null || strValue.length()==0) return "NULL";
352     else if (type.equalsIgnoreCase("NUMBER")) script.append("TO_CHAR(" + strValue + ")");
353     else if (type.equalsIgnoreCase("DATE")) script.append("TO_CHAR(" + strValue + ", '"+sqlDateFormat+" HH24:MI:SS')");
354     else script.append(strValue);
355     script.append(" END)");
356     return script.toString();
357   }
358
359   public static String JavaDoc formatedColumn(String JavaDoc strValue, String JavaDoc type) {
360     if (strValue==null || strValue.length()==0) return "NULL";
361     else if (strValue.equalsIgnoreCase("NULL") || type.equalsIgnoreCase("NUMBER")) return strValue;
362     else if (type.equalsIgnoreCase("DATE")) return "TO_DATE('" + strValue + "', '"+sqlDateFormat+" HH24:MI:SS')";
363     else {
364       strValue = strValue.replace("'", "''");
365       strValue = strValue.replace("\r","' || chr(10) || '");
366       strValue = strValue.replace("\n","' || chr(13) || '");
367       return "'" + strValue + "'";
368     }
369   }
370
371   public void readProperties(String JavaDoc strFileProperties) {
372     // Read properties file.
373
Properties JavaDoc properties = new Properties JavaDoc();
374     try {
375       log4j.info("strFileProperties: " + strFileProperties);
376       properties.load(new FileInputStream(strFileProperties));
377       sqlDateFormat = properties.getProperty("dateFormat.sql");
378       log4j.info("sqlDateFormat: " + sqlDateFormat);
379     } catch (IOException e) {
380       // catch possible io errors from readLine()
381
System.out.println("Uh oh, got an IOException error!");
382       e.printStackTrace();
383     }
384   }
385
386   public static void destroy() {
387     myPool.destroy();
388   }
389 }
390
Popular Tags