KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > utils > JNDIConnectionProvider


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.utils;
22
23 import java.sql.Connection JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.sql.DataSource JavaDoc;
30 import javax.sql.XADataSource JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 /**
36  * <p>
37  * A <code>ConnectionProvider</code> that provides connections from a <code>DataSource</code>
38  * that is managed by an application server, and made available via JNDI.
39  * </p>
40  *
41  * @see DBConnectionManager
42  * @see ConnectionProvider
43  * @see PoolingConnectionProvider
44  *
45  * @author James House
46  * @author Sharada Jambula
47  * @author Mohammad Rezaei
48  * @author Patrick Lightbody
49  * @author Srinivas Venkatarangaiah
50  */

51 public class JNDIConnectionProvider implements ConnectionProvider {
52
53     /*
54      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55      *
56      * Data members.
57      *
58      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59      */

60
61     private String JavaDoc url;
62
63     private Properties JavaDoc props;
64
65     private Object JavaDoc datasource;
66
67     private boolean alwaysLookup = false;
68
69     private final Log log = LogFactory.getLog(getClass());
70
71     /*
72      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73      *
74      * Constructors.
75      *
76      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
77      */

78
79     /**
80      * Constructor
81      *
82      * @param jndiUrl
83      * The url for the datasource
84      */

85     public JNDIConnectionProvider(String JavaDoc jndiUrl, boolean alwaysLookup) {
86         this.url = jndiUrl;
87         this.alwaysLookup = alwaysLookup;
88         init();
89     }
90
91     /**
92      * Constructor
93      *
94      * @param jndiUrl
95      * The URL for the DataSource
96      * @param jndiProps
97      * The JNDI properties to use when establishing the InitialContext
98      * for the lookup of the given URL.
99      */

100     public JNDIConnectionProvider(String JavaDoc jndiUrl, Properties JavaDoc jndiProps,
101             boolean alwaysLookup) {
102         this.url = jndiUrl;
103         this.props = jndiProps;
104         this.alwaysLookup = alwaysLookup;
105         init();
106     }
107
108     /*
109      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110      *
111      * Interface.
112      *
113      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114      */

115
116     protected Log getLog() {
117         return log;
118     }
119
120     private void init() {
121
122         if (!isAlwaysLookup()) {
123             Context JavaDoc ctx = null;
124             try {
125                 ctx = (props != null) ? new InitialContext JavaDoc(props) : new InitialContext JavaDoc();
126
127                 datasource = (DataSource JavaDoc) ctx.lookup(url);
128             } catch (Exception JavaDoc e) {
129                 getLog().error(
130                         "Error looking up datasource: " + e.getMessage(), e);
131             } finally {
132                 if (ctx != null) {
133                     try { ctx.close(); } catch(Exception JavaDoc ignore) {}
134                 }
135             }
136         }
137     }
138
139     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
140         Context JavaDoc ctx = null;
141         try {
142             Object JavaDoc ds = this.datasource;
143
144             if (ds == null || isAlwaysLookup()) {
145                 ctx = (props != null) ? new InitialContext JavaDoc(props): new InitialContext JavaDoc();
146
147                 ds = ctx.lookup(url);
148                 if (!isAlwaysLookup()) {
149                     this.datasource = ds;
150                 }
151             }
152
153             if (ds == null) {
154                 throw new SQLException JavaDoc( "There is no object at the JNDI URL '" + url + "'");
155             }
156
157             if (ds instanceof XADataSource JavaDoc) {
158                 return (((XADataSource JavaDoc) ds).getXAConnection().getConnection());
159             } else if (ds instanceof DataSource JavaDoc) {
160                 return ((DataSource JavaDoc) ds).getConnection();
161             } else {
162                 throw new SQLException JavaDoc("Object at JNDI URL '" + url + "' is not a DataSource.");
163             }
164         } catch (Exception JavaDoc e) {
165             this.datasource = null;
166             throw new SQLException JavaDoc(
167                     "Could not retrieve datasource via JNDI url '" + url + "' "
168                             + e.getClass().getName() + ": " + e.getMessage());
169         } finally {
170             if (ctx != null) {
171                 try { ctx.close(); } catch(Exception JavaDoc ignore) {}
172             }
173         }
174     }
175
176     public boolean isAlwaysLookup() {
177         return alwaysLookup;
178     }
179
180     public void setAlwaysLookup(boolean b) {
181         alwaysLookup = b;
182     }
183
184     /*
185      * @see org.quartz.utils.ConnectionProvider#shutdown()
186      */

187     public void shutdown() throws SQLException JavaDoc {
188         // do nothing
189
}
190
191 }
192
Popular Tags