001: /*
002: Loader - tool for transfering data from one JDBC source to another and
003: doing transformations during copy.
004: Copyright (C) 2002-2003 Together
005: This library is free software; you can redistribute it and/or
006: modify it under the terms of the GNU Lesser General Public
007: License as published by the Free Software Foundation; either
008: version 2.1 of the License, or (at your option) any later version.
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013: You should have received a copy of the GNU Lesser General Public
014: License along with this library; if not, write to the Free Software
015: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: Loader.java
017: Date: 03.03.2003.
018: @version 2.1 alpha
019: @authors:
020: Milosevic Sinisa sinisa@prozone.yu
021: Radoslav Dutina rale@prozone.co.yu
022: */
023:
024: package org.webdocwf.util.loader;
025:
026: import java.io.*;
027: import java.util.*;
028: import java.sql.*;
029:
030: /**
031: * DBConnectionManager class creates all connection to database.
032: * @author Radoslav Dutina
033: * @version 1.0
034: */
035: public class DBConnectionManager {
036: private Vector drivers = new Vector();
037: private Hashtable pools = new Hashtable();
038: private static DBConnectionManager instance; // The single instance
039: private Hashtable openConnections = new Hashtable();
040: private Vector allConnectons = new Vector();
041:
042: public String loaderJobPath = "";
043: public String connectinPrefix = "";
044: public boolean fileSystemDatabase = false;
045:
046: /**
047: * This method set all connection into vector
048: * @param allConnections represents all connection property
049: */
050: public DBConnectionManager(Vector allConnections) {
051: this .allConnectons = allConnections;
052: init();
053: }
054:
055: /**
056: * This method set path to LoaderJob.olj file
057: * @param loaderJob represents string path to LoaderJob.olj file
058: */
059: public void setLoaderJobPath(String loaderJob) {
060: File file = new File(loaderJob);
061: this .loaderJobPath = file.getAbsoluteFile().getParent()
062: + System.getProperty("file.separator");
063: }
064:
065: /**
066: * This method set connection prefix
067: * @param prefix is value of connection prefix
068: */
069: public void setConnectionPrefix(String prefix) {
070: this .connectinPrefix = prefix;
071: }
072:
073: /**
074: * This method set the value of fileSystemDatabase parameter
075: * @param doParse is value of parameter
076: */
077: public void setParsePermission(boolean doParse) {
078: this .fileSystemDatabase = doParse;
079: }
080:
081: /**
082: * This method initialized DBConnectionManager object
083: */
084: private void init() {
085:
086: loadDrivers(allConnectons);
087: createPools(allConnectons);
088: }
089:
090: private void loadDrivers(Vector allConnectons) {
091: if (allConnectons.size() != 0) {
092: for (int i = 0; i < allConnectons.size(); i = i + 4) {
093: if (i > 3) {
094: if (allConnectons
095: .get(i)
096: .toString()
097: .equalsIgnoreCase(
098: allConnectons.get(i - 4).toString())) {
099: //do nothing
100: } else {
101: try {
102: Driver driver = (Driver) Class.forName(
103: allConnectons.get(i).toString())
104: .newInstance();
105: DriverManager.registerDriver(driver);
106: drivers.add(driver);
107: } catch (Exception e) {
108: e.getMessage();
109: }
110: }
111: } else {
112: try {
113: Driver driver = (Driver) Class.forName(
114: allConnectons.get(i).toString())
115: .newInstance();
116: DriverManager.registerDriver(driver);
117: drivers.add(driver);
118: } catch (Exception e) {
119: e.getMessage();
120: }
121: }
122: }
123: }
124: }
125:
126: private void createPools(Vector allConnectons) {
127: for (int i = 0; i < allConnectons.size(); i = i + 4) {
128: String url = allConnectons.get(i + 1).toString();
129: if (url.indexOf("jdbc:microsoft:sqlserver") != -1) {
130: if (url.indexOf("SelectMethod") == -1) {
131: url = url + ";SelectMethod=cursor";
132: }
133: }
134: String poolName = url;
135: String user = allConnectons.get(i + 2).toString();
136: String password = allConnectons.get(i + 3).toString();
137: boolean check = false;
138: if (pools.size() > 0) {
139: check = pools.containsKey(poolName);
140: }
141: if (!check) {
142: DBConnectionPool pool = new DBConnectionPool(poolName,
143: url, user, password);
144: pools.put(poolName, pool);
145: }
146: }
147: }
148:
149: /**
150: * This method get connection from connection pool
151: * @param name is the name of the pool
152: * @return connection
153: */
154: public Connection getConnection(String name) {
155: DBConnectionPool pool = (DBConnectionPool) pools.get(name);
156: if (pool != null) {
157: return pool.getConnection();
158: }
159: return null;
160: }
161:
162: /**
163: * This method release (close) all connection and deregister all drivers
164: * @param exception defines if application calls release from exception method or not
165: */
166: public void release(String exception) {
167: Enumeration allPools = pools.elements();
168: while (allPools.hasMoreElements()) {
169: DBConnectionPool pool = (DBConnectionPool) allPools
170: .nextElement();
171: pool.release(exception);
172: }
173: Enumeration allDrivers = drivers.elements();
174: while (allDrivers.hasMoreElements()) {
175: Driver driver = (Driver) allDrivers.nextElement();
176: try {
177: DriverManager.deregisterDriver(driver);
178: } catch (Exception e) {
179: e.getMessage();
180: }
181: }
182: }
183:
184: private class DBConnectionPool {
185: private String name = null;
186: private String url = null;
187: private String user = null;
188: private String password = null;
189:
190: /**
191: * Construct the object of DBConnectionPool class with associated parameters
192: * @param name is the name of the connection
193: * @param url is url to database which we wont to connect
194: * @param user is the user name
195: * @param password is user password
196: */
197: public DBConnectionPool(String name, String url, String user,
198: String password) {
199: this .name = name;
200: this .url = url;
201: this .user = user;
202: this .password = password;
203:
204: }
205:
206: /**
207: * This method return connection if the connection exists, or create new connection
208: * if don't
209: * @return connection
210: */
211: public Connection getConnection() {
212: Connection conn = null;
213: if (openConnections.size() > 0) {
214: conn = (Connection) openConnections.get(name);
215: if (conn != null) {
216: return conn;
217: }
218: conn = newConnections();
219: } else {
220: conn = newConnections();
221: }
222: return conn;
223: }
224:
225: private Connection newConnections() {
226: Connection conn = null;
227: url = Utils.getAbsolutePathFromDatabaseURL(connectinPrefix,
228: loaderJobPath, url, fileSystemDatabase);
229: try {
230: if (user == null) {
231: conn = DriverManager.getConnection(url);
232: openConnections.put(name, conn);
233: } else {
234: conn = DriverManager.getConnection(url, user,
235: password);
236: openConnections.put(name, conn);
237: }
238:
239: } catch (Exception e) {
240: e.printStackTrace();
241: }
242: return conn;
243: }
244:
245: /**
246: * This method close and commit connection
247: * @param exception defines if application calls release from exception method or not
248: */
249: public void release(String exception) {
250: Connection conn = (Connection) openConnections.get(name);
251: try {
252: if (!conn.isClosed()) {
253: if (exception.equalsIgnoreCase("false"))
254: conn.commit();
255: conn.close();
256: }
257: openConnections.remove(name);
258: } catch (Exception e) {
259: e.getMessage();
260: }
261: }
262:
263: }
264: }
|