KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > wad > WadConnection


1 /*
2  *************************************************************************
3  * The contents of this file are subject to the Openbravo Public License
4  * Version 1.0 (the "License"), being the Mozilla Public License
5  * Version 1.1 with a permitted attribution clause; you may not use this
6  * file except in compliance with the License. You may obtain a copy of
7  * the License at http://www.openbravo.com/legal/license.html
8  * Software distributed under the License is distributed on an "AS IS"
9  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
10  * License for the specific language governing rights and limitations
11  * under the License.
12  * The Original Code is Openbravo ERP.
13  * The Initial Developer of the Original Code is Openbravo SL
14  * All portions are Copyright (C) 2001-2006 Openbravo SL
15  * All Rights Reserved.
16  * Contributor(s): ______________________________________.
17  ************************************************************************
18  */

19 package org.openbravo.wad;
20
21
22 import java.sql.*;
23 import java.io.*;
24 import org.apache.xerces.parsers.DOMParser;
25 import org.xml.sax.*;
26 import org.w3c.dom.*;
27 import org.openbravo.exception.*;
28 import org.openbravo.database.*;
29 import org.apache.log4j.Logger;
30
31 public class WadConnection implements ConnectionProvider {
32   static Logger log4j = Logger.getLogger(WadConnection.class);
33   protected Connection myPool;
34   String JavaDoc defaultPoolName = "";
35   String JavaDoc bbdd = "";
36   String JavaDoc rdbms = "";
37   String JavaDoc contextName = "openbravo";
38
39   public WadConnection (String JavaDoc xmlPoolFile) {
40     if (myPool == null) {
41       try {
42         connect(xmlPoolFile);
43       } catch (Exception JavaDoc e) {
44         e.printStackTrace();
45       }
46     }
47   }
48
49   public void connect(String JavaDoc file) throws ClassNotFoundException JavaDoc, SQLException {
50     if (log4j.isDebugEnabled()) log4j.debug("Creating Connection");
51     try {
52       DOMParser parser = new DOMParser();
53       try {
54           parser.parse((file.toUpperCase().startsWith("FILE:///")?"":"file:///") + file);
55       } catch (SAXException se) {
56         log4j.error(se);
57         throw new SQLException("Couldn't load parse for pool file " + file);
58       } catch (IOException ioe) {
59         log4j.error(ioe);
60         throw new SQLException("Couldn't load pool file " + file + " for input/output operations");
61       }
62       Document xmlPool = parser.getDocument();
63       NodeList nodeList = xmlPool.getElementsByTagName("pool");
64       log4j.debug("Pool's elements: " + nodeList.getLength());
65       Node child = null;
66       for (int i=0;i<nodeList.getLength();i++) {
67         Node pool = nodeList.item(i);
68         NamedNodeMap atributos = pool.getAttributes();
69         String JavaDoc poolName = atributos.item(0).getNodeValue();
70         if (log4j.isDebugEnabled()) log4j.debug("poolName: " + poolName);
71         this.defaultPoolName = poolName;
72
73         child = pool.getFirstChild();
74         child = child.getNextSibling();
75         String JavaDoc dbDriver = ((Text)child.getFirstChild()).getData();
76         if (log4j.isDebugEnabled()) log4j.debug("dbDriver: " + dbDriver);
77         Class.forName(dbDriver);
78         child = child.getNextSibling();
79         child = child.getNextSibling();
80         String JavaDoc dbServer = ((Text)child.getFirstChild()).getData();
81         if (log4j.isDebugEnabled()) log4j.debug("dbServer: " + dbServer);
82         this.bbdd = dbServer;
83         child = child.getNextSibling();
84         child = child.getNextSibling();
85         Text textDbLogin = (Text)child.getFirstChild();
86         String JavaDoc dbLogin;
87         if (textDbLogin != null) {
88           dbLogin = textDbLogin.getData();
89           if (log4j.isDebugEnabled()) log4j.debug("dbLogin: " + dbLogin);
90         } else dbLogin = null;
91         child = child.getNextSibling();
92         child = child.getNextSibling();
93         Text textDbPassword = (Text)child.getFirstChild();
94         String JavaDoc dbPassword;
95         if (textDbPassword != null) {
96           dbPassword = textDbPassword.getData();
97           if (log4j.isDebugEnabled()) log4j.debug("dbPassword: " + dbPassword);
98         } else dbPassword = null;
99         this.myPool=DriverManager.getConnection(dbServer, dbLogin, dbPassword);
100         this.myPool.setAutoCommit(true);
101         
102         child = child.getNextSibling();
103         child = child.getNextSibling();
104         int minConns = Integer.valueOf(((Text)child.getFirstChild()).getData()).intValue();
105         if (log4j.isDebugEnabled()) log4j.debug("minConns: " + minConns);
106         child = child.getNextSibling();
107         child = child.getNextSibling();
108         int maxConns = Integer.valueOf(((Text)child.getFirstChild()).getData()).intValue();
109         if (log4j.isDebugEnabled()) log4j.debug("maxConns: " + maxConns);
110         child = child.getNextSibling();
111         child = child.getNextSibling();
112         double maxConnTime = Double.valueOf(((Text)child.getFirstChild()).getData()).doubleValue();
113         if (log4j.isDebugEnabled()) log4j.debug("maxConnTime: " + Double.toString(maxConnTime));
114         child = child.getNextSibling();
115         child = child.getNextSibling();
116         String JavaDoc dbSessionConfig = ((Text)child.getFirstChild()).getData();
117         if (log4j.isDebugEnabled()) log4j.debug("dbSessionConfig: " + dbSessionConfig);
118         child = child.getNextSibling();
119         child = child.getNextSibling();
120         String JavaDoc myrdbms = ((Text)child.getFirstChild()).getData();
121         if (log4j.isDebugEnabled()) log4j.debug("rdbms: " + myrdbms);
122
123
124         this.rdbms = myrdbms;
125       }
126     }
127     catch (Exception JavaDoc e) {
128       log4j.error(e);
129       throw new SQLException("Failed when creating database connections pool");
130     }
131   }
132
133   public void destroy() {
134     try {
135       if (myPool!=null) myPool.close();
136       myPool = null;
137     } catch(SQLException e){
138       log4j.error("SQL error in closeConnection: " + e);
139     }
140   }
141
142   public Connection getConnection() throws NoConnectionAvailableException {
143     return this.myPool;
144   }
145
146   public String JavaDoc getRDBMS() {
147     return rdbms;
148   }
149
150   public boolean releaseConnection(Connection conn) {
151     if (conn==null) return false;
152     try {
153       conn.setAutoCommit(true);
154       //conn.close();
155
} catch (Exception JavaDoc ex) {
156       ex.printStackTrace();
157       return false;
158     }
159     return true;
160   }
161
162   public Connection getTransactionConnection() throws NoConnectionAvailableException, SQLException {
163     Connection conn = getConnection();
164     if (conn==null) throw new NoConnectionAvailableException("CouldnĀ“t get an available connection");
165     conn.setAutoCommit(false);
166     return conn;
167   }
168
169   public void releaseCommitConnection(Connection conn) throws SQLException {
170     if (conn==null) return;
171     conn.commit();
172     releaseConnection(conn);
173   }
174
175   public void releaseRollbackConnection(Connection conn) throws SQLException {
176     if (conn==null) return;
177     conn.rollback();
178     releaseConnection(conn);
179   }
180
181   public PreparedStatement getPreparedStatement(String JavaDoc poolName, String JavaDoc SQLPreparedStatement) throws Exception JavaDoc {
182     return getPreparedStatement(SQLPreparedStatement);
183   }
184
185   public PreparedStatement getPreparedStatement(String JavaDoc SQLPreparedStatement) throws Exception JavaDoc {
186     if (log4j.isDebugEnabled()) log4j.debug("connection requested");
187     Connection conn = getConnection();
188     if (log4j.isDebugEnabled()) log4j.debug("connection established");
189     return getPreparedStatement(conn, SQLPreparedStatement);
190   }
191
192   public PreparedStatement getPreparedStatement(Connection conn, String JavaDoc SQLPreparedStatement) throws SQLException {
193     if (conn == null || SQLPreparedStatement==null || SQLPreparedStatement.equals("")) return null;
194     PreparedStatement ps = null;
195     try {
196       if (log4j.isDebugEnabled()) log4j.debug("preparedStatement requested");
197       ps = conn.prepareStatement(SQLPreparedStatement, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
198       if (log4j.isDebugEnabled()) log4j.debug("preparedStatement received");
199     } catch(SQLException e) {
200       log4j.error("getPreparedStatement: " + SQLPreparedStatement + "\n" + e);
201       releaseConnection(conn);
202       throw e;
203     }
204     return(ps);
205   }
206
207   public CallableStatement getCallableStatement(String JavaDoc poolName, String JavaDoc SQLCallableStatement) throws Exception JavaDoc {
208     return getCallableStatement(SQLCallableStatement);
209   }
210
211   public CallableStatement getCallableStatement(String JavaDoc SQLCallableStatement) throws Exception JavaDoc {
212     Connection conn = getConnection();
213     return getCallableStatement(conn, SQLCallableStatement);
214   }
215
216   public CallableStatement getCallableStatement(Connection conn, String JavaDoc SQLCallableStatement) throws SQLException {
217     if (conn==null || SQLCallableStatement==null || SQLCallableStatement.equals("")) return null;
218     CallableStatement cs = null;
219     try {
220       cs = conn.prepareCall(SQLCallableStatement);
221     } catch(SQLException e) {
222       log4j.error("getCallableStatement: " + SQLCallableStatement + "\n" + e);
223       releaseConnection(conn);
224       throw e;
225     }
226     return(cs);
227   }
228
229   public Statement getStatement(String JavaDoc name) throws Exception JavaDoc {
230     return getStatement();
231   }
232
233   public Statement getStatement() throws Exception JavaDoc {
234     Connection conn = getConnection();
235     return getStatement(conn);
236   }
237
238   public Statement getStatement(Connection conn) throws SQLException {
239     if (conn == null) return null;
240     try {
241       return(conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY));
242     } catch(SQLException e) {
243       log4j.error("getStatement: " + e);
244       releaseConnection(conn);
245       throw e;
246     }
247   }
248
249   public void releasePreparedStatement(PreparedStatement preparedStatement) throws SQLException {
250     if (preparedStatement == null) return;
251     Connection conn = null;
252     try {
253       conn = preparedStatement.getConnection();
254       preparedStatement.close();
255       releaseConnection(conn);
256     } catch (SQLException e) {
257       log4j.error("releasePreparedStatement: " + e);
258       releaseConnection(conn);
259       throw e;
260     }
261   }
262
263   public void releaseCallableStatement(CallableStatement callableStatement) throws SQLException {
264     if (callableStatement == null) return;
265     Connection conn = null;
266     try {
267       conn = callableStatement.getConnection();
268       callableStatement.close();
269       releaseConnection(conn);
270     } catch (SQLException e) {
271       log4j.error("releaseCallableStatement: " + e);
272       releaseConnection(conn);
273       throw e;
274     }
275   }
276
277   public void releaseStatement(Statement statement) throws SQLException {
278     if (statement == null) return;
279     Connection conn = null;
280     try {
281       conn = statement.getConnection();
282       statement.close();
283       releaseConnection(conn);
284     } catch (SQLException e) {
285       log4j.error("releaseStatement: " + e);
286       releaseConnection(conn);
287       throw e;
288     }
289   }
290
291   public void releaseTransactionalStatement(Statement statement) throws SQLException {
292     if (statement == null) return;
293     statement.close();
294   }
295
296   public void releaseTransactionalPreparedStatement(PreparedStatement preparedStatement) throws SQLException {
297     if (preparedStatement==null) return;
298     preparedStatement.close();
299   }
300
301   /**
302   * Returns the actual status of the dynamic pool.
303   */

304   public String JavaDoc getStatus() {
305     StringBuffer JavaDoc strResultado = new StringBuffer JavaDoc();
306     strResultado.append("Not implemented yet");
307     return strResultado.toString();
308   }//End getStatus()
309
}
310
Popular Tags