1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.juddi.util.jdbc;
17
18 import java.sql.Connection;
19 import java.sql.SQLException;
20
21 import javax.naming.InitialContext;
22 import javax.naming.NamingException;
23 import javax.sql.DataSource;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.juddi.datastore.jdbc.Database;
28 import org.apache.juddi.registry.RegistryEngine;
29 import org.apache.juddi.util.Config;
30
31
32 /***
33 * @author Steve Viens (sviens@apache.org)
34 */
35 public class ConnectionManager
36 {
37
38 private static Log log = LogFactory.getLog(ConnectionManager.class);
39
40
41 private static DataSource dataSource = null;
42
43 /***
44 *
45 */
46 public static Connection acquireConnection()
47 throws SQLException
48 {
49
50
51 if (dataSource == null)
52 dataSource = lookupDataSource();
53
54 Connection conn = null;
55 if (dataSource != null) {
56 conn = dataSource.getConnection();
57 } else {
58
59 boolean isUseDatasource =
60 Config.getBooleanProperty(RegistryEngine.PROPNAME_IS_USE_DATASOURCE,
61 RegistryEngine.DEFAULT_IS_USE_DATASOURCE.booleanValue());
62 if (!isUseDatasource) {
63 conn = Database.aquireConnection();
64 }
65 }
66
67 return conn;
68 }
69
70 /***
71 *
72 */
73 private static synchronized DataSource lookupDataSource()
74 throws SQLException
75 {
76
77 if (dataSource != null)
78 return dataSource;
79
80
81 boolean isUseDatasource =
82 Config.getBooleanProperty(RegistryEngine.PROPNAME_IS_USE_DATASOURCE,
83 RegistryEngine.DEFAULT_IS_USE_DATASOURCE.booleanValue());
84 if (!isUseDatasource) {
85 log.info("Using direct JDBC, since "
86 + RegistryEngine.PROPNAME_IS_USE_DATASOURCE + " is set to "
87 + isUseDatasource);
88 return null;
89 }
90
91
92 try
93 {
94 String dataSourceName =
95 Config.getStringProperty(RegistryEngine.PROPNAME_DATASOURCE_NAME,
96 RegistryEngine.DEFAULT_DATASOURCE_NAME);
97
98 log.info("Using JNDI to aquire a JDBC DataSource with " +
99 "name: "+dataSourceName);
100
101 InitialContext initCtx = new InitialContext();
102 dataSource = (DataSource)initCtx.lookup(dataSourceName);
103 }
104 catch (NamingException nex) {
105 log.error("Exception occurred while attempting to acquire " +
106 "a JDBC DataSource from JNDI: "+nex.getMessage());
107 }
108
109 return dataSource;
110 }
111 }