1 package org.opensync.tools;
2
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.sql.Connection;
6 import java.sql.Driver;
7 import java.sql.DriverManager;
8 import java.sql.SQLException;
9 import java.util.Enumeration;
10 import java.util.Hashtable;
11 import java.util.Properties;
12 import java.util.StringTokenizer;
13 import java.util.Vector;
14
15 import javax.xml.parsers.ParserConfigurationException;
16
17 import org.xml.sax.SAXException;
18
19
20 public class PoolManager {
21 static private PoolManager instance=null;
22 private int clients;
23 private Vector drivers = new Vector();
24 private Hashtable pools = new Hashtable();
25 static private String logfile;
26 static private String connectionPropertiesFile;
27 private boolean initialized = false;
28
29 protected PoolManager() {
30
31 }
32
33 static public PoolManager getInstance() {
34 if (instance==null) {
35 instance = new PoolManager();
36 }
37 instance.clients++;
38 return instance;
39 }
40
41 static public void reloadConfig() {
42 instance = null;
43 }
44
45 public ConnectionPool getConnectionPool(String name){
46 return (ConnectionPool)pools.get(name);
47 }
48 public boolean isPoolExists(String name){
49 return pools.get(name) != null;
50 }
51 synchronized public void init() throws SQLException,IOException,FileNotFoundException,SAXException,ParserConfigurationException {
52 if(initialized) return;
53
54
55 ConnectionProperties conns = new ConnectionProperties(connectionPropertiesFile);
56 Vector connections = conns.getConnections();
57 for (int i=0;i<connections.size();i++) {
58 Properties p = (Properties)connections.elementAt(i);
59 loadDrivers(p);
60 createPools(p);
61 }
62 initialized = true;
63 }
64
65 private void loadDrivers(Properties props) throws SQLException{
66 String driverClasses = props.getProperty("drivers");
67 StringTokenizer st = new StringTokenizer(driverClasses);
68
69 while (st.hasMoreElements()) {
70 String driverClassName = st.nextToken().trim();
71 try {
72 Driver driver = (Driver)Class.forName(driverClassName).newInstance();
73 DriverManager.registerDriver(driver);
74 drivers.addElement(driver);
75 if (Utils.debug) System.out.println("Pilot registered...");
76 }
77 catch (Exception e) {
78 Utils.LogToFile(logfile,"Pilot impossible to register...");
79 throw new SQLException("Pilot " + driverClassName + " impossible to register...");
80 }
81 }
82 }
83
84 private void createPools(Properties props)throws SQLException {
85 Enumeration propNames = props.propertyNames();
86 String poolName = props.getProperty("name");
87
88 while (propNames.hasMoreElements()) {
89 String name = (String)propNames.nextElement();
90 if (name.equals("url")) {
91 String url = props.getProperty("url");
92 if (url == null) {
93 Utils.LogToFile(logfile,"no url associated to "+poolName);
94 continue;
95 }
96 String user = props.getProperty("user");
97 String password = props.getProperty("password");
98 String maxConns = props.getProperty("maxconns", "0");
99 String operator_concat = props.getProperty("operator_concat");
100 String conversion_function = props.getProperty("conversion_function");
101
102 int max;
103 try {
104 max = Integer.valueOf(maxConns).intValue();
105 }
106 catch (NumberFormatException e) {
107 Utils.LogToFile(logfile,"number max exception... ");
108 max = 0;
109 }
110 String initConns = props.getProperty("initconns", "0");
111 int init;
112 try {
113 init = Integer.valueOf(initConns).intValue();
114 }
115 catch (NumberFormatException e) {
116 Utils.LogToFile(logfile,"number min exception... ");
117 init = 0;
118 }
119 String loginTimeOut = props.getProperty("logintimeout", "5");
120 int timeOut;
121 try {
122 timeOut = Integer.valueOf(loginTimeOut).intValue();
123 }
124 catch (NumberFormatException e) {
125 Utils.LogToFile(logfile,"number timeOut exception... ");
126 timeOut = 5;
127 }
128 if (Utils.debug) System.out.println("PoolManager::createPools");
129 ConnectionPool pool = new ConnectionPool(
130 url,user,password,max,init,timeOut,logfile,operator_concat,conversion_function
131 );
132 pools.put(poolName,pool);
133 }
134 }
135 }
136 public ConnectionPool createPool(String poolName,String driver,String url,String user,
137 String password,int max,int init,int timeOut,String logfile,String operator_concat,String conversion_function)throws Exception {
138 Driver drv = (Driver)Class.forName(driver).newInstance();
139 DriverManager.registerDriver(drv);
140 drivers.addElement(drv);
141 if (Utils.debug) System.out.println("PoolManager::createPool");
142 ConnectionPool pool = new ConnectionPool(
143 url,user,password,max,init,timeOut,logfile,operator_concat,conversion_function
144 );
145 pools.put(poolName,pool);
146 return pool;
147 }
148
149 public Connection getConnection(String name)throws SQLException {
150 Connection conn = null;
151 ConnectionPool pool = (ConnectionPool)pools.get(name);
152 if (pool != null) {
153 conn = pool.getConnection();
154 }
155 return conn;
156 }
157
158 static public void setConnectionPropertiesFile(String f) {
159 connectionPropertiesFile = f;
160 }
161 static public void setLogfile(String l) {
162 logfile = l;
163 }
164
165
166 public void freeConnection(String name,Connection con) {
167 if (con != null){
168 ConnectionPool pool = (ConnectionPool)pools.get(name);
169 if (pool != null) pool.freeConnection(con);
170 }
171 }
172
173 public synchronized void release()throws SQLException {
174
175 if (--clients != 0) return;
176
177 Enumeration allPools = pools.elements();
178 while (allPools.hasMoreElements()) {
179 ConnectionPool pool = (ConnectionPool)allPools.nextElement();
180 pool.release();
181 }
182
183 Enumeration allDrivers = drivers.elements();
184 while (allDrivers.hasMoreElements()) {
185 Driver driver = (Driver)allDrivers.nextElement();
186 try {
187 DriverManager.deregisterDriver(driver);
188 }
189 catch (SQLException e) {
190 Utils.LogToFile(logfile,"Driver impossible to cancel...");
191 Utils.SqlLogToFile("SQLException: " + e.getMessage());
192 throw e;
193 }
194 }
195 }
196 }