KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > util > jdbc > DataSourceConnectionProvider


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25
26
27 package org.nemesis.forum.util.jdbc;
28
29 import java.sql.Connection JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import javax.naming.Context JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.sql.DataSource JavaDoc;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.nemesis.forum.config.ConfigLoader;
40
41
42
43 /**
44  * An implementation of DbConnectionProvider that utilizes a JDBC 2.0 DataSource
45  * made available via JNDI. This is useful for application servers where a pooled
46  * data connection is already provided so can share the pool with the
47  * other applications.<p>
48  *
49  * The JNDI location of the DataSource is retrieved from the
50  * {@link com.Yasna.forum.PropertyManager} as the
51  * <code>JNDIDataSource.name</code> property. This can be overiden by setting
52  * the provider's <code>name</code> property if required.
53  *
54  * @author <a HREF="mailto:joe@truemesh.com">Joe Walnes</a>
55  *
56  * @see com.Yasna.forum.database.DbConnectionProvider
57  */

58 public class DataSourceConnectionProvider extends DbConnectionProvider {
59     
60     static protected Log log =LogFactory.getLog(DataSourceConnectionProvider.class);
61
62     
63     private static final boolean POOLED = true;
64
65     private DataSource JavaDoc dataSource;
66
67
68     /**
69      * Keys of JNDI properties to query PropertyManager for.
70      */

71     /*private static final String[] jndiPropertyKeys = {
72         Context.APPLET ,
73         Context.AUTHORITATIVE ,
74         Context.BATCHSIZE ,
75         Context.DNS_URL ,
76         Context.INITIAL_CONTEXT_FACTORY ,
77         Context.LANGUAGE ,
78         Context.OBJECT_FACTORIES ,
79         Context.PROVIDER_URL ,
80         Context.REFERRAL ,
81         Context.SECURITY_AUTHENTICATION ,
82         Context.SECURITY_CREDENTIALS ,
83         Context.SECURITY_PRINCIPAL ,
84         Context.SECURITY_PROTOCOL ,
85         Context.STATE_FACTORIES ,
86         Context.URL_PKG_PREFIXES
87     };
88 */

89     /**
90      * Initialize.
91      */

92     public DataSourceConnectionProvider() {
93         
94         //properties = new Properties();
95
//properties.setProperty("name",Config.DB_JNDI_DATASOURCE);
96

97     }
98
99     /**
100      * Lookup DataSource from JNDI context.
101     */

102     protected void start() {
103         //java:comp/env/
104
String JavaDoc name = ConfigLoader.getInstance().getConfig().getJDBCProviderProperties().getProperty("JNDIDataSource");
105         if (name==null || name.length()==0) {
106             log.error("No name specified for DataSource JNDI lookup - 'name' " +
107             "Property should be set.");
108             return;
109         }
110         try {
111             Properties JavaDoc contextProperties = new Properties JavaDoc();
112             contextProperties.setProperty("name",name);
113             
114             /*for (int i=0; i<jndiPropertyKeys.length; i++) {
115                 String k = jndiPropertyKeys[i];
116                 String v = PropertyManager.getProperty(k);
117                 if (v != null) {
118                     contextProperties.setProperty(k,v);
119                 }
120             }*/

121             Context JavaDoc context = new InitialContext JavaDoc(contextProperties);
122             dataSource = (DataSource JavaDoc) context.lookup( name );
123             
124             //Context initCtx = new InitialContext();
125
//Context envCtx = (Context) initCtx.lookup("java:comp/env");
126
//dataSource = (DataSource) envCtx.lookup("jdbc/MyDataSource");
127

128         }
129         catch (Exception JavaDoc e) {
130             log.error("Could not lookup DataSource at '" + name + "'"/*,e*/);
131         }
132     }
133
134     /**
135      * Destroy then start.
136      */

137     protected void restart() {
138         
139         destroy();
140         start();
141     }
142
143     /**
144      * Save properties.
145      */

146     protected void destroy() {
147         
148         return;
149     }
150
151     /**
152      * Get new Connection from DataSource.
153      */

154     public Connection JavaDoc getConnection() {
155         
156         if (dataSource==null) {
157             log.error("DataSource has not yet been looked up",null);
158             return null;
159         }
160         try {
161             return dataSource.getConnection();
162         }
163         catch (SQLException JavaDoc e) {
164             log.error("Could not retrieve Connection from DataSource",e);
165             return null;
166         }
167     }
168
169    
170
171
172    
173
174 }
175
Popular Tags