KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > database > Naming


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module.database;
11
12 import java.sql.Connection JavaDoc;
13 import java.sql.SQLException JavaDoc;
14 import javax.sql.DataSource JavaDoc;
15 import javax.sql.PooledConnection JavaDoc;
16 import javax.sql.ConnectionPoolDataSource JavaDoc;
17 import javax.naming.InitialContext JavaDoc;
18 import javax.naming.Context JavaDoc;
19
20 import org.mmbase.module.*;
21 import org.mmbase.util.logging.*;
22
23 /**
24  * This class is used to retrieve a connection, which is provided by naming resources.
25  * With the usage of naming resource, it is possible to configure the database resource
26  * inside the application server and let the application server do the pooling. Since
27  * this is a J2EE concept, this class provides support for usage of this.
28  *
29  * @author Eduard Witteveen
30  * @version $Id: Naming.java,v 1.4 2005/12/24 11:35:45 michiel Exp $
31  */

32 public class Naming extends ProcessorModule implements JDBCInterface {
33     private static Logger log = Logging.getLoggerInstance(Naming.class.getName());
34     private static String JavaDoc PROPERTY_CONTEXT_NAME = "context";
35     private static String JavaDoc PROPERTY_DATASOURCE_NAME = "datasource";
36     private Object JavaDoc datasource = null;
37
38     /** our own multi-connection implementation.. arrgg....*/
39     private class NamingMultiConnection extends MultiConnection {
40     /** constructor to set the connection which has to be retrieved */
41     NamingMultiConnection(Connection JavaDoc con) {
42         super(null, con);
43         // this should take care of everything (we hope)
44
state = CON_BUSY;
45     }
46     /** override the close, since the MultiConnection want to tell the pool that it is closed */
47     public void close() throws SQLException JavaDoc {
48         con.close();
49     }
50     /** claim what? */
51     public void claim() {
52     }
53     /** release what? */
54     public void release() {
55     }
56     /** the usage of what? */
57     public int getUsage() {
58         return 0;
59     }
60     /** the start time of what? */
61     public int getStartTime() {
62         return 0;
63     }
64     /** the start time of what? */
65     public long getStartTimeMillis() {
66         return 0;
67     }
68     }
69
70     /**
71      * Init this module. Will check if properties are available and try to get a datasource, to
72      * test if we can use it.
73      */

74     public void init() {
75     String JavaDoc context = getInitParameter(PROPERTY_CONTEXT_NAME);
76     if(context == null) throw new RuntimeException JavaDoc("the property '" + PROPERTY_CONTEXT_NAME + "' was not set");
77     String JavaDoc source = getInitParameter(PROPERTY_DATASOURCE_NAME);
78     if(source == null) throw new RuntimeException JavaDoc("the property '" + PROPERTY_CONTEXT_NAME + "' was not set");
79
80     // do the naming stuff..
81
try {
82         Context JavaDoc initCtx = new InitialContext JavaDoc();
83         Context JavaDoc envCtx = (Context JavaDoc) initCtx.lookup(context);
84         datasource = envCtx.lookup(source);
85         if(datasource == null) {
86         String JavaDoc msg = "datasource was null for context:" + context + " with source:" + source;
87         log.error(msg);
88         throw new RuntimeException JavaDoc(msg);
89         }
90         if(datasource instanceof ConnectionPoolDataSource JavaDoc) {
91         ConnectionPoolDataSource JavaDoc ds = (ConnectionPoolDataSource JavaDoc)datasource;
92         log.info("Using the interface:" + ConnectionPoolDataSource JavaDoc.class.getName() + "(implemented by:" + ds.getClass().getName() + " to get new database connections(time out: " + ds.getLoginTimeout() + " seconds).");
93         }
94         else if(datasource instanceof DataSource JavaDoc) {
95         log.info("Using the interface:" + DataSource JavaDoc.class.getName() + "(implemented by:" + datasource.getClass().getName() + " to get new database connections.");
96         }
97         else {
98         String JavaDoc msg = "Dont know how to retrieve a connection from datasource:" + datasource.getClass().getName();
99         log.error(msg);
100         throw new RuntimeException JavaDoc(msg);
101         }
102
103         // try to get an connection, so we can see if it all works..
104
Connection JavaDoc con = getConnection();
105         if(con == null) {
106         String JavaDoc msg = "Test run of retrieving a test-run failed.";
107         log.error(msg);
108         throw new RuntimeException JavaDoc(msg);
109         }
110         // closing a connection is very important!
111
con.close();
112
113     } catch(javax.naming.NamingException JavaDoc ne) {
114         String JavaDoc msg = "The following error occured while trying to initalise the datasource for context:'" + context + "' datasource:'" + source + "' :\n" + Logging.stackTrace(ne);
115         log.error(msg);
116         throw new RuntimeException JavaDoc(msg);
117     }
118     catch(java.sql.SQLException JavaDoc se) {
119         String JavaDoc msg = "The following error occured while trying to retrieve a connection from the datasource for context:'" + context + "' datasource:'" + source + "' :\n" + Logging.stackTrace(se);
120         log.error(msg);
121         throw new RuntimeException JavaDoc(msg);
122     }
123     }
124
125     /**
126      * is a reload the same as an init?
127      */

128     public void reload() {
129     init();
130     }
131
132     /**
133      * retrieves an connection to the database, depending on the class which is used as datasource
134      * @return Connection A connection to the database
135      */

136     private Connection JavaDoc getConnection() throws java.sql.SQLException JavaDoc {
137         if (datasource == null) {
138             log.error("Getting connection before init of jdbc module. Trying to reinitalize the database layer.");
139             init();
140         }
141
142     if(datasource instanceof ConnectionPoolDataSource JavaDoc) {
143         ConnectionPoolDataSource JavaDoc ds = (ConnectionPoolDataSource JavaDoc) datasource;
144         PooledConnection JavaDoc pc = ds.getPooledConnection();
145         return pc.getConnection();
146     }
147     else if(datasource instanceof DataSource JavaDoc) {
148         DataSource JavaDoc ds = (DataSource JavaDoc) datasource;
149         return ds.getConnection();
150     }
151     else {
152         String JavaDoc msg = "Dont know how to retrieve a connection from datasource:" + (datasource != null ? datasource : datasource.getClass().getName());
153         log.error(msg);
154         throw new RuntimeException JavaDoc(msg);
155     }
156     }
157     public MultiConnection getConnection(String JavaDoc url, String JavaDoc name, String JavaDoc password) throws SQLException JavaDoc {
158     return new NamingMultiConnection(getConnection());
159     }
160     public MultiConnection getConnection(String JavaDoc url) throws SQLException JavaDoc {
161     return new NamingMultiConnection(getConnection());
162     }
163
164     public Connection JavaDoc getDirectConnection(String JavaDoc url) throws SQLException JavaDoc {
165     return getConnection();
166     }
167     public Connection JavaDoc getDirectConnection(String JavaDoc url,String JavaDoc name,String JavaDoc password) throws SQLException JavaDoc {
168     return getConnection();
169     }
170
171     // below all the things we dont use..
172
public void unload() {}
173     public void shutdown() {}
174     public String JavaDoc makeUrl(){return null;}
175     public String JavaDoc makeUrl(String JavaDoc dbm) {return null;}
176     public String JavaDoc makeUrl(String JavaDoc host,String JavaDoc dbm) {return null;}
177     public String JavaDoc makeUrl(String JavaDoc host,int port,String JavaDoc dbm) {return null;}
178     public String JavaDoc getUser() {return null;}
179     public String JavaDoc getPassword() {return null;}
180     public String JavaDoc getDatabaseName(){return null;}
181     public void checkTime(){}
182 }
183
Popular Tags