KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > util > jdbc > ConnectionManager


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 JavaDoc;
19 import java.sql.SQLException JavaDoc;
20
21 import javax.naming.InitialContext JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import javax.sql.DataSource JavaDoc;
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 /**
32  * @author Steve Viens (sviens@apache.org)
33  */

34 public class ConnectionManager
35 {
36   // private reference to the jUDDI logger
37
private static Log log = LogFactory.getLog(ConnectionManager.class);
38
39   // Shared jUDDI DataSource
40
private static DataSource JavaDoc dataSource = null;
41
42   /**
43    *
44    */

45   public static Connection JavaDoc aquireConnection()
46     throws SQLException JavaDoc
47   {
48     // Make sure we've got a DataSource first
49
if (dataSource == null)
50       dataSource = lookupDataSource();
51
52     Connection JavaDoc conn = null;
53     if (dataSource != null)
54       conn = dataSource.getConnection();
55
56     return conn;
57   }
58
59   /**
60    *
61    */

62   private static synchronized DataSource JavaDoc lookupDataSource()
63     throws SQLException JavaDoc
64   {
65     // make sure we still need to lookup the DataSource
66
if (dataSource != null)
67       return dataSource;
68
69     // look it up.
70
try
71     {
72       String JavaDoc 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 JavaDoc initCtx = new InitialContext JavaDoc();
80       dataSource = (DataSource JavaDoc)initCtx.lookup(dataSourceName);
81     }
82     catch (NamingException JavaDoc 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