View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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    // private reference to the jUDDI logger
38    private static Log log = LogFactory.getLog(ConnectionManager.class);
39  
40    // Shared jUDDI DataSource
41    private static DataSource dataSource = null;
42  
43    /***
44     *
45     */
46    public static Connection acquireConnection()
47      throws SQLException
48    {
49  	  
50      // Make sure we've got a DataSource first
51      if (dataSource == null)
52        dataSource = lookupDataSource();
53  
54      Connection conn = null;
55      if (dataSource != null) {
56        conn = dataSource.getConnection();
57      } else {
58          //check if we wanted to use a dataSource
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      // make sure we still need to lookup the DataSource
77      if (dataSource != null)
78        return dataSource;
79  
80      //check if we wanted to use a dataSource
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      // look it up.
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 }