KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > database > JNDIDataSourceProvider


1 /**
2  * $RCSfile: JNDIDataSourceProvider.java,v $
3  * $Revision: 1.3 $
4  * $Date: 2005/04/11 21:02:05 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.database;
13
14 import org.jivesoftware.util.JiveGlobals;
15 import org.jivesoftware.util.Log;
16
17 import javax.naming.Context JavaDoc;
18 import javax.naming.InitialContext JavaDoc;
19 import javax.sql.DataSource JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.*;
23
24 /**
25  * An implementation of ConnectionProvider that utilizes a JDBC 2.0 DataSource
26  * made available via JNDI. This is useful for application servers where a pooled
27  * data connection is already provided so Jive can share the pool with the
28  * other applications.<p>
29  * <p/>
30  * The JNDI location of the DataSource stored as the Jive property
31  * <code>database.JNDIProvider.name</code>. This can be overridden by setting
32  * the provider's <code>name</code> property if required.
33  *
34  * @author <a HREF="mailto:joe@truemesh.com">Joe Walnes</a>
35  * @see ConnectionProvider
36  */

37 public class JNDIDataSourceProvider implements ConnectionProvider {
38
39     private String JavaDoc dataSourceName;
40     private DataSource JavaDoc dataSource;
41
42     /**
43      * Keys of JNDI properties to query PropertyManager for.
44      */

45     private static final String JavaDoc[] jndiPropertyKeys = {
46         Context.APPLET,
47         Context.AUTHORITATIVE,
48         Context.BATCHSIZE,
49         Context.DNS_URL,
50         Context.INITIAL_CONTEXT_FACTORY,
51         Context.LANGUAGE,
52         Context.OBJECT_FACTORIES,
53         Context.PROVIDER_URL,
54         Context.REFERRAL,
55         Context.SECURITY_AUTHENTICATION,
56         Context.SECURITY_CREDENTIALS,
57         Context.SECURITY_PRINCIPAL,
58         Context.SECURITY_PROTOCOL,
59         Context.STATE_FACTORIES,
60         Context.URL_PKG_PREFIXES
61     };
62
63     /**
64      * Initialize.
65      */

66     public JNDIDataSourceProvider() {
67         dataSourceName = JiveGlobals.getXMLProperty("database.JNDIProvider.name");
68     }
69
70     public String JavaDoc getName() {
71         return "JNDI DataSource Connection Provider";
72     }
73
74     public String JavaDoc getDescription() {
75         return "Connection Provider for Jive to lookup pooled "
76                 + "DataSource from JNDI location. Requires 'name' "
77                 + "property with JNDI location. This can be set in "
78                 + "the properties file as 'JNDIDataSource.name'";
79     }
80
81     public String JavaDoc getAuthor() {
82         return "Joe Walnes - joe@truemesh.com";
83     }
84
85     public int getMajorVersion() {
86         return 2;
87     }
88
89     public int getMinorVersion() {
90         return 1;
91     }
92
93     public boolean isPooled() {
94         return true;
95     }
96
97     public void start() {
98         if (dataSourceName == null || dataSourceName.equals("")) {
99             error("No name specified for DataSource. JNDI lookup will fail", null);
100             return;
101         }
102         try {
103             Properties contextProperties = new Properties();
104             for (int i = 0; i < jndiPropertyKeys.length; i++) {
105                 String JavaDoc k = jndiPropertyKeys[i];
106                 String JavaDoc v = JiveGlobals.getXMLProperty(k);
107                 if (v != null) {
108                     contextProperties.setProperty(k, v);
109                 }
110             }
111             Context JavaDoc context = null;
112             if (contextProperties.size() > 0) {
113                 context = new InitialContext JavaDoc(contextProperties);
114             }
115             else {
116                 context = new InitialContext JavaDoc();
117             }
118             dataSource = (DataSource JavaDoc)context.lookup(dataSourceName);
119         }
120         catch (Exception JavaDoc e) {
121             error("Could not lookup DataSource at '" + dataSourceName + "'", e);
122         }
123     }
124
125     public void restart() {
126         destroy();
127         start();
128     }
129
130     public void destroy() {
131
132     }
133
134     public Connection JavaDoc getConnection() {
135         if (dataSource == null) {
136             error("DataSource has not been initialized.", null);
137             return null;
138         }
139         try {
140             return dataSource.getConnection();
141         }
142         catch (SQLException JavaDoc e) {
143             error("Could not retrieve Connection from DataSource", e);
144             return null;
145         }
146     }
147
148     public String JavaDoc getProperty(String JavaDoc name) {
149         if ("name".equals(name)) {
150             return dataSourceName;
151         }
152         else {
153             return null;
154         }
155     }
156
157     public void setProperty(String JavaDoc name, String JavaDoc value) {
158         if ("name".equals(name)) {
159             this.dataSourceName = value;
160             JiveGlobals.setXMLProperty("database.JNDIProvider.name", value);
161         }
162     }
163
164     public Iterator propertyNames() {
165         List list = new ArrayList();
166         list.add("name");
167         return Collections.unmodifiableList(list).iterator();
168     }
169
170     public String JavaDoc getPropertyDescription(String JavaDoc name) {
171         if ("name".equals(name)) {
172             return "JNDI name to lookup. eg: java:comp/env/jdbc/MyDataSource";
173         }
174         else {
175             return null;
176         }
177     }
178
179     /**
180      * Log an error.
181      *
182      * @param msg Description of error
183      * @param e Exception to printStackTrace (may be null)
184      */

185     private final void error(String JavaDoc msg, Exception JavaDoc e) {
186         Log.error(msg, e);
187     }
188 }
Popular Tags