KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minerva > pool > jdbc > JDBCPoolDataSource


1 /*
2  * Licensed under the X license (see http://www.x.org/terms.htm)
3  */

4 package org.ofbiz.minerva.pool.jdbc;
5
6 import java.io.PrintWriter JavaDoc;
7 import java.sql.Connection JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.HashSet JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Properties JavaDoc;
13
14 import javax.naming.Context JavaDoc;
15 import javax.naming.InitialContext JavaDoc;
16 import javax.naming.Name JavaDoc;
17 import javax.naming.NamingException JavaDoc;
18 import javax.naming.RefAddr JavaDoc;
19 import javax.naming.Reference JavaDoc;
20 import javax.naming.Referenceable JavaDoc;
21 import javax.naming.StringRefAddr JavaDoc;
22 import javax.naming.spi.ObjectFactory JavaDoc;
23 import javax.sql.DataSource JavaDoc;
24
25 import org.apache.log4j.Logger;
26 import org.ofbiz.base.util.Log4jLoggerWriter;
27 import org.ofbiz.minerva.pool.ObjectPool;
28
29 /**
30  * DataSource for non-transactional JDBC pools. This handles configuration
31  * parameters for both the pool and the JDBC driver. It is important that you
32  * set all the configuration parameters before you initialize the DataSource,
33  * and you must initialize it before you use it. All the configuration
34  * parameters are not documented here; you are instead referred to ObjectPool
35  * and JDBCConnectionFactory.
36  * @see org.ofbiz.minerva.pool.ObjectPool
37  * @see org.ofbiz.minerva.pool.jdbc.JDBCConnectionFactory
38  *
39  * @author Aaron Mulder (ammulder@alumni.princeton.edu)
40  */

41 public class JDBCPoolDataSource implements DataSource JavaDoc, Referenceable JavaDoc, ObjectFactory JavaDoc {
42
43     private static Logger log = Logger.getLogger(JDBCPoolDataSource.class);
44
45     private static HashMap JavaDoc sources = new HashMap JavaDoc();
46
47     /**
48      * Gets all the current JDBC pool data sources.
49      */

50     public static Collection JavaDoc getDataSources() {
51         return new HashSet JavaDoc(sources.values());
52     }
53
54     /**
55      * Gets a specific JDBC pool data source by pool name.
56      */

57     public static JDBCPoolDataSource getDataSource(String JavaDoc poolName) {
58         return (JDBCPoolDataSource) sources.get(poolName);
59     }
60
61     private ObjectPool pool;
62     private JDBCConnectionFactory factory;
63     private PrintWriter JavaDoc logWriter;
64     private int timeout;
65     private boolean initialized = false;
66     private String JavaDoc jndiName;
67
68     /**
69      * Creates a new JDBC pool data source. Be sure to configure it and then
70      * call initialize before you try to use it.
71      */

72     public JDBCPoolDataSource() {
73         pool = new ObjectPool();
74         factory = new JDBCConnectionFactory();
75         PoolDriver.instance();
76     }
77
78     // Unique properties
79
/**
80      * If you use this to set a JNDI name, this pool will be bound to that name
81      * using the default InitialContext. You can also do this manually if you
82      * have additional requirements.
83      */

84     public void setJNDIName(String JavaDoc name) throws NamingException JavaDoc {
85         InitialContext JavaDoc ctx = new InitialContext JavaDoc();
86         if (jndiName != null && !jndiName.equals(name))
87             ctx.unbind(jndiName);
88         if (name != null)
89             ctx.bind(name, this);
90         jndiName = name;
91     }
92
93     /**
94      * Gets the JNDI name this pool is bound to. Only valid if you used
95      * setJNDIName to bind it.
96      * @see #setJNDIName
97      */

98     public String JavaDoc getJNDIName() {
99         return jndiName;
100     }
101
102     // JDBC properties
103
public void setJDBCURL(String JavaDoc url) {
104         factory.setConnectURL(url);
105     }
106
107     public String JavaDoc getJDBCURL() {
108         return factory.getConnectURL();
109     }
110
111     public void setJDBCProperties(Properties JavaDoc props) {
112         factory.setConnectProperties(props);
113     }
114
115     public void setProperties(String JavaDoc props) {
116         setJDBCProperties(parseProperties(props));
117     }
118
119     public Properties JavaDoc getJDBCProperties() {
120         return factory.getConnectProperties();
121     }
122
123     public void setJDBCUser(String JavaDoc user) {
124         factory.setUser(user);
125     }
126
127     public String JavaDoc getJDBCUser() {
128         return factory.getUser();
129     }
130
131     public void setJDBCPassword(String JavaDoc password) {
132         factory.setPassword(password);
133     }
134
135     public String JavaDoc getJDBCPassword() {
136         return factory.getPassword();
137     }
138 // Pool properties
139
public void setPoolName(String JavaDoc name) {
140         pool.setName(name);
141         sources.put(pool.getName(), this);
142     }
143
144     public String JavaDoc getPoolName() {
145         return pool.getName();
146     }
147
148     public void setMinSize(int size) {
149         pool.setMinSize(size);
150     }
151
152     public int getMinSize() {
153         return pool.getMinSize();
154     }
155
156     public void setMaxSize(int size) {
157         pool.setMaxSize(size);
158     }
159
160     public int getMaxSize() {
161         return pool.getMaxSize();
162     }
163
164     public void setBlocking(boolean blocking) {
165         pool.setBlocking(blocking);
166     }
167
168     public boolean isBlocking() {
169         return pool.isBlocking();
170     }
171
172     public void setIdleTimeoutEnabled(boolean allowShrinking) {
173         pool.setIdleTimeoutEnabled(allowShrinking);
174     }
175
176     public boolean isIdleTimeoutEnabled() {
177         return pool.isIdleTimeoutEnabled();
178     }
179
180     public void setGCEnabled(boolean allowGC) {
181         pool.setGCEnabled(allowGC);
182     }
183
184     public boolean isGCEnabled() {
185         return pool.isGCEnabled();
186     }
187
188     public void setMaxIdleTimeoutPercent(float percent) {
189         pool.setMaxIdleTimeoutPercent(percent);
190     }
191
192     public float getMaxIdleTimeoutPercent() {
193         return pool.getMaxIdleTimeoutPercent();
194     }
195
196     public void setIdleTimeout(long millis) {
197         pool.setIdleTimeout(millis);
198     }
199
200     public long getIdleTimeout() {
201         return pool.getIdleTimeout();
202     }
203
204     public void setGCMinIdleTime(long millis) {
205         pool.setGCMinIdleTime(millis);
206     }
207
208     public long getGCMinIdleTime() {
209         return pool.getGCMinIdleTime();
210     }
211
212     public void setGCInterval(long millis) {
213         pool.setGCInterval(millis);
214     }
215
216     public long getGCInterval() {
217         return pool.getGCInterval();
218     }
219
220     public void setInvalidateOnError(boolean invalidate) {
221         pool.setInvalidateOnError(invalidate);
222     }
223
224     public boolean isInvalidateOnError() {
225         return pool.isInvalidateOnError();
226     }
227
228     public void setTimestampUsed(boolean timestamp) {
229         pool.setTimestampUsed(timestamp);
230     }
231
232     public boolean isTimestampUsed() {
233         return pool.isTimestampUsed();
234     }
235
236 // Other methods
237

238     /**
239      * Initializes the pool. You need to have configured all the pool and
240      * JDBC properties first.
241      */

242     public void initialize() {
243         initialized = true;
244         pool.setObjectFactory(factory);
245         pool.initialize();
246     }
247
248     /**
249      * Returns a string describing the pool status (number of connections
250      * created, used, and maximum).
251      */

252     public String JavaDoc getPoolStatus() {
253         return pool.toString();
254     }
255
256     /**
257      * Shuts down this data source and the underlying pool. If you used
258      * setJNDI name to bind it in JNDI, it is unbound.
259      */

260     public void close() {
261         try {
262             setJNDIName(null);
263         } catch (NamingException JavaDoc e) {
264             log.warn("Can't unbind from JNDI", e);
265         }
266         sources.remove(pool.getName());
267         pool.shutDown();
268         pool = null;
269         factory = null;
270     }
271
272     /**
273      * Gets a connection from the pool.
274      */

275     public Connection JavaDoc getConnection() throws java.sql.SQLException JavaDoc {
276         if (!initialized)
277             initialize();
278
279         return (Connection JavaDoc) pool.getObject();
280     }
281
282     /**
283      * Gets a connection from the pool. If a new connection must be
284      * created, it will use the specified user name and password. If there is
285      * a connection available in the pool, it will be used, regardless of the
286      * user name and password use to created it initially.
287      */

288     public Connection JavaDoc getConnection(String JavaDoc user, String JavaDoc password) throws java.sql.SQLException JavaDoc {
289         if (!initialized)
290             initialize();
291
292         factory.setUser(user);
293         factory.setPassword(password);
294         return (Connection JavaDoc) pool.getObject();
295     }
296
297     /**
298      * Gets a log writer used to record pool events.
299      */

300     public PrintWriter JavaDoc getLogWriter() throws java.sql.SQLException JavaDoc {
301         return logWriter;
302     }
303
304     /**
305      * Sets a log writer used to record pool events.
306      */

307     public void setLogWriter(PrintWriter JavaDoc writer) throws java.sql.SQLException JavaDoc {
308         if (writer == null) {
309             logWriter = null;
310         } else {
311             if (logWriter == null) {
312                 logWriter = new Log4jLoggerWriter(log);
313             }
314         }
315     }
316
317     /**
318      * This property is not used by this implementation.
319      */

320     public int getLoginTimeout() throws java.sql.SQLException JavaDoc {
321         return timeout;
322     }
323
324     /**
325      * This property is not used by this implementation.
326      */

327     public void setLoginTimeout(int timeout) throws java.sql.SQLException JavaDoc {
328         this.timeout = timeout;
329     }
330
331     /**
332      * This method is used for parsing JDBCProperties
333      */

334     private static Properties JavaDoc parseProperties(String JavaDoc string) {
335         Properties JavaDoc props = new Properties JavaDoc();
336         if (string == null || string.length() == 0)
337             return props;
338
339         int lastPos = -1;
340         int pos = string.indexOf(";");
341         while (pos > -1) {
342             addProperty(props, string.substring(lastPos + 1, pos));
343             lastPos = pos;
344             pos = string.indexOf(";", lastPos + 1);
345         }
346         addProperty(props, string.substring(lastPos + 1));
347         return props;
348     }
349
350     /**
351      * This method is used for parsing JDBCProperties
352      */

353     private static void addProperty(Properties JavaDoc props, String JavaDoc property) {
354         int pos = property.indexOf("=");
355         if (pos < 0) {
356             System.err.println("Unable to parse property '" + property + "' - please use 'name=value'");
357             return;
358         }
359         props.setProperty(property.substring(0, pos), property.substring(pos + 1));
360     }
361
362     // Referenceable implementation ----------------------------------
363
/**
364      * Gets a reference to this data source.
365      */

366     public Reference JavaDoc getReference() {
367         return new Reference JavaDoc(getClass().getName(), new StringRefAddr JavaDoc("JDBCPool", pool.getName()), getClass().getName(), null);
368     }
369
370     // ObjectFactory implementation ----------------------------------
371
/**
372      * Decodes a reference to a specific pool data source.
373      */

374     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx, Hashtable JavaDoc environment) {
375         if (obj instanceof Reference JavaDoc) {
376             Reference JavaDoc ref = (Reference JavaDoc) obj;
377             if (ref.getClassName().equals(getClass().getName())) {
378                 RefAddr JavaDoc addr = ref.get("JDBCPool");
379                 return sources.get(addr.getContent());
380             }
381         }
382         return null;
383     }
384 }
385
386 /*
387 vim:tabstop=3:et:shiftwidth=3
388 */

389
Popular Tags