1 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.registry.RegistryEngine; 28 import org.apache.juddi.util.Config; 29 30 31 34 public class ConnectionManager 35 { 36 private static Log log = LogFactory.getLog(ConnectionManager.class); 38 39 private static DataSource dataSource = null; 41 42 45 public static Connection aquireConnection() 46 throws SQLException 47 { 48 if (dataSource == null) 50 dataSource = lookupDataSource(); 51 52 Connection conn = null; 53 if (dataSource != null) 54 conn = dataSource.getConnection(); 55 56 return conn; 57 } 58 59 62 private static synchronized DataSource lookupDataSource() 63 throws SQLException 64 { 65 if (dataSource != null) 67 return dataSource; 68 69 try 71 { 72 String dataSourceName = 73 Config.getStringProperty(RegistryEngine.PROPNAME_DATASOURCE_NAME, 74 RegistryEngine.DEFAULT_DATASOURCE_NAME); 75 76 log.info("Using JNDI to aquire a JDBC DataSource with " + 77 "name: "+dataSourceName); 78 79 InitialContext initCtx = new InitialContext (); 80 dataSource = (DataSource )initCtx.lookup(dataSourceName); 81 } 82 catch (NamingException nex) { 83 log.error("Exception occurred while attempting to acquire " + 84 "a JDBC DataSource from JNDI: "+nex.getMessage()); 85 } 86 87 return dataSource; 88 } 89 } 90 | Popular Tags |