1 /*
2 Loader - tool for transfering data from one JDBC source to another and
3 doing transformations during copy.
4 Copyright (C) 2002-2003 Together
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 You should have received a copy of the GNU Lesser General Public
14 License along with this library; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 Loader.java
17 Date: 03.03.2003.
18 @version 2.1 alpha
19 @authors:
20 Milosevic Sinisa sinisa@prozone.yu
21 Radoslav Dutina rale@prozone.co.yu
22 */
23
24 package org.webdocwf.util.loader;
25
26 import java.io.*;
27 import java.util.*;
28 import java.sql.*;
29
30 /***
31 * DBConnectionManager class creates all connection to database.
32 * @author Radoslav Dutina
33 * @version 1.0
34 */
35 public class DBConnectionManager {
36 private Vector drivers=new Vector();
37 private Hashtable pools=new Hashtable();
38 private static DBConnectionManager instance; // The single instance
39 private Hashtable openConnections=new Hashtable();
40 private Vector allConnectons=new Vector();
41
42 public String loaderJobPath="";
43 public String connectinPrefix="";
44 public boolean fileSystemDatabase=false;
45
46
47 /***
48 * This method set all connection into vector
49 * @param allConnections represents all connection property
50 */
51 public DBConnectionManager(Vector allConnections) {
52 this.allConnectons=allConnections;
53 init();
54 }
55
56
57 /***
58 * This method set path to LoaderJob.olj file
59 * @param loaderJob represents string path to LoaderJob.olj file
60 */
61 public void setLoaderJobPath(String loaderJob){
62 File file=new File(loaderJob);
63 this.loaderJobPath=file.getAbsoluteFile().getParent()+System.getProperty("file.separator");
64 }
65
66 /***
67 * This method set connection prefix
68 * @param prefix is value of connection prefix
69 */
70 public void setConnectionPrefix(String prefix){
71 this.connectinPrefix=prefix;
72 }
73
74 /***
75 * This method set the value of fileSystemDatabase parameter
76 * @param doParse is value of parameter
77 */
78 public void setParsePermission(boolean doParse){
79 this.fileSystemDatabase=doParse;
80 }
81
82 /***
83 * This method initialized DBConnectionManager object
84 */
85 private void init(){
86
87 loadDrivers(allConnectons);
88 createPools(allConnectons);
89 }
90
91 private void loadDrivers(Vector allConnectons){
92 if(allConnectons.size()!=0){
93 for(int i=0;i<allConnectons.size();i=i+4){
94 if(i>3){
95 if(allConnectons.get(i).toString().equalsIgnoreCase(allConnectons.get(i-4).toString())){
96 //do nothing
97 }else{
98 try{
99 Driver driver=(Driver)Class.forName(allConnectons.get(i).toString()).newInstance();
100 DriverManager.registerDriver(driver);
101 drivers.add(driver);
102 }catch(Exception e){
103 e.getMessage();
104 }
105 }
106 }else{
107 try{
108 Driver driver=(Driver)Class.forName(allConnectons.get(i).toString()).newInstance();
109 DriverManager.registerDriver(driver);
110 drivers.add(driver);
111 }catch(Exception e){
112 e.getMessage();
113 }
114 }
115 }
116 }
117 }
118
119 private void createPools(Vector allConnectons){
120 for(int i=0;i<allConnectons.size();i=i+4){
121 String url=allConnectons.get(i+1).toString();
122 if(url.indexOf("jdbc:microsoft:sqlserver")!=-1) {
123 if(url.indexOf("SelectMethod")==-1) {
124 url = url+";SelectMethod=cursor";
125 }
126 }
127 String poolName=url;
128 String user=allConnectons.get(i+2).toString();
129 String password=allConnectons.get(i+3).toString();
130 boolean check=false;
131 if(pools.size()>0){
132 check=pools.containsKey(poolName);
133 }
134 if(!check){
135 DBConnectionPool pool=new DBConnectionPool(poolName,url,user,password);
136 pools.put(poolName,pool);
137 }
138 }
139 }
140
141 /***
142 * This method get connection from connection pool
143 * @param name is the name of the pool
144 * @return connection
145 */
146 public Connection getConnection(String name){
147 DBConnectionPool pool=(DBConnectionPool)pools.get(name);
148 if(pool!=null){
149 return pool.getConnection();
150 }
151 return null;
152 }
153
154 /***
155 * This method release (close) all connection and deregister all drivers
156 * @param exception defines if application calls release from exception method or not
157 */
158 public void release(String exception){
159 Enumeration allPools=pools.elements();
160 while(allPools.hasMoreElements()){
161 DBConnectionPool pool=(DBConnectionPool)allPools.nextElement();
162 pool.release(exception);
163 }
164 Enumeration allDrivers=drivers.elements();
165 while(allDrivers.hasMoreElements()){
166 Driver driver=(Driver)allDrivers.nextElement();
167 try{
168 DriverManager.deregisterDriver(driver);
169 }catch(Exception e){
170 e.getMessage();
171 }
172 }
173 }
174
175 private class DBConnectionPool{
176 private String name=null;
177 private String url=null;
178 private String user=null;
179 private String password=null;
180
181
182 /***
183 * Construct the object of DBConnectionPool class with associated parameters
184 * @param name is the name of the connection
185 * @param url is url to database which we wont to connect
186 * @param user is the user name
187 * @param password is user password
188 */
189 public DBConnectionPool(String name,String url,String user,String password){
190 this.name=name;
191 this.url=url;
192 this.user=user;
193 this.password=password;
194
195 }
196
197 /***
198 * This method return connection if the connection exists, or create new connection
199 * if don't
200 * @return connection
201 */
202 public Connection getConnection(){
203 Connection conn=null;
204 if(openConnections.size()>0){
205 conn=(Connection)openConnections.get(name);
206 if(conn!=null){
207 return conn;
208 }
209 conn=newConnections();
210 }else{
211 conn=newConnections();
212 }
213 return conn;
214 }
215
216 private Connection newConnections(){
217 Connection conn=null;
218 url=Utils.getAbsolutePathFromDatabaseURL(connectinPrefix,loaderJobPath,
219 url,fileSystemDatabase);
220 try{
221 if(user==null){
222 conn=DriverManager.getConnection(url);
223 openConnections.put(name,conn);
224 }else{
225 conn=DriverManager.getConnection(url,user,password);
226 openConnections.put(name,conn);
227 }
228
229 }catch(Exception e){
230 e.printStackTrace();
231 }
232 return conn;
233 }
234
235 /***
236 * This method close and commit connection
237 * @param exception defines if application calls release from exception method or not
238 */
239 public void release(String exception){
240 Connection conn=(Connection)openConnections.get(name);
241 try{
242 if(!conn.isClosed()){
243 if(exception.equalsIgnoreCase("false"))
244 conn.commit();
245 conn.close();
246 }
247 openConnections.remove(name);
248 }catch(Exception e){
249 e.getMessage();
250 }
251 }
252
253 }
254 }
This page was automatically generated by Maven