001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.module.database;
011:
012: import java.sql.Connection;
013: import java.sql.SQLException;
014: import javax.sql.DataSource;
015: import javax.sql.PooledConnection;
016: import javax.sql.ConnectionPoolDataSource;
017: import javax.naming.InitialContext;
018: import javax.naming.Context;
019:
020: import org.mmbase.module.*;
021: import org.mmbase.util.logging.*;
022:
023: /**
024: * This class is used to retrieve a connection, which is provided by naming resources.
025: * With the usage of naming resource, it is possible to configure the database resource
026: * inside the application server and let the application server do the pooling. Since
027: * this is a J2EE concept, this class provides support for usage of this.
028: *
029: * @author Eduard Witteveen
030: * @version $Id: Naming.java,v 1.6 2007/06/19 13:59:30 michiel Exp $
031: * @deprecated Datasource can be configured in mmbaseroot.xml
032: */
033: public class Naming extends ProcessorModule implements JDBCInterface {
034: private static Logger log = Logging.getLoggerInstance(Naming.class);
035: private static String PROPERTY_CONTEXT_NAME = "context";
036: private static String PROPERTY_DATASOURCE_NAME = "datasource";
037: private Object datasource = null;
038:
039: /** our own multi-connection implementation.. arrgg....*/
040: private class NamingMultiConnection extends
041: MultiConnectionImplementation {
042: /** constructor to set the connection which has to be retrieved */
043: NamingMultiConnection(Connection con) {
044: super (null, con);
045: // this should take care of everything (we hope)
046: state = CON_BUSY;
047: }
048:
049: /** override the close, since the MultiConnection want to tell the pool that it is closed */
050: public void close() throws SQLException {
051: con.close();
052: }
053:
054: /** claim what? */
055: public void claim() {
056: }
057:
058: /** release what? */
059: public void release() {
060: }
061:
062: /** the usage of what? */
063: public int getUsage() {
064: return 0;
065: }
066:
067: /** the start time of what? */
068: public int getStartTime() {
069: return 0;
070: }
071:
072: /** the start time of what? */
073: public long getStartTimeMillis() {
074: return 0;
075: }
076: }
077:
078: public Naming(String name) {
079: super (name);
080: }
081:
082: /**
083: * Init this module. Will check if properties are available and try to get a datasource, to
084: * test if we can use it.
085: */
086: public void init() {
087: String context = getInitParameter(PROPERTY_CONTEXT_NAME);
088: if (context == null)
089: throw new RuntimeException("the property '"
090: + PROPERTY_CONTEXT_NAME + "' was not set");
091: String source = getInitParameter(PROPERTY_DATASOURCE_NAME);
092: if (source == null)
093: throw new RuntimeException("the property '"
094: + PROPERTY_CONTEXT_NAME + "' was not set");
095:
096: // do the naming stuff..
097: try {
098: Context initCtx = new InitialContext();
099: Context envCtx = (Context) initCtx.lookup(context);
100: datasource = envCtx.lookup(source);
101: if (datasource == null) {
102: String msg = "datasource was null for context:"
103: + context + " with source:" + source;
104: log.error(msg);
105: throw new RuntimeException(msg);
106: }
107: if (datasource instanceof ConnectionPoolDataSource) {
108: ConnectionPoolDataSource ds = (ConnectionPoolDataSource) datasource;
109: log.info("Using the interface:"
110: + ConnectionPoolDataSource.class.getName()
111: + "(implemented by:" + ds.getClass().getName()
112: + " to get new database connections(time out: "
113: + ds.getLoginTimeout() + " seconds).");
114: } else if (datasource instanceof DataSource) {
115: log.info("Using the interface:"
116: + DataSource.class.getName()
117: + "(implemented by:"
118: + datasource.getClass().getName()
119: + " to get new database connections.");
120: } else {
121: String msg = "Dont know how to retrieve a connection from datasource:"
122: + datasource.getClass().getName();
123: log.error(msg);
124: throw new RuntimeException(msg);
125: }
126:
127: // try to get an connection, so we can see if it all works..
128: Connection con = getConnection();
129: if (con == null) {
130: String msg = "Test run of retrieving a test-run failed.";
131: log.error(msg);
132: throw new RuntimeException(msg);
133: }
134: // closing a connection is very important!
135: con.close();
136:
137: } catch (javax.naming.NamingException ne) {
138: String msg = "The following error occured while trying to initalise the datasource for context:'"
139: + context
140: + "' datasource:'"
141: + source
142: + "' :\n"
143: + Logging.stackTrace(ne);
144: log.error(msg);
145: throw new RuntimeException(msg);
146: } catch (java.sql.SQLException se) {
147: String msg = "The following error occured while trying to retrieve a connection from the datasource for context:'"
148: + context
149: + "' datasource:'"
150: + source
151: + "' :\n"
152: + Logging.stackTrace(se);
153: log.error(msg);
154: throw new RuntimeException(msg);
155: }
156: }
157:
158: /**
159: * is a reload the same as an init?
160: */
161: public void reload() {
162: init();
163: }
164:
165: /**
166: * retrieves an connection to the database, depending on the class which is used as datasource
167: * @return Connection A connection to the database
168: */
169: private Connection getConnection() throws java.sql.SQLException {
170: if (datasource == null) {
171: log
172: .error("Getting connection before init of jdbc module. Trying to reinitalize the database layer.");
173: init();
174: }
175:
176: if (datasource instanceof ConnectionPoolDataSource) {
177: ConnectionPoolDataSource ds = (ConnectionPoolDataSource) datasource;
178: PooledConnection pc = ds.getPooledConnection();
179: return pc.getConnection();
180: } else if (datasource instanceof DataSource) {
181: DataSource ds = (DataSource) datasource;
182: return ds.getConnection();
183: } else {
184: String msg = "Dont know how to retrieve a connection from datasource:"
185: + (datasource != null ? datasource : datasource
186: .getClass().getName());
187: log.error(msg);
188: throw new RuntimeException(msg);
189: }
190: }
191:
192: public MultiConnection getConnection(String url, String name,
193: String password) throws SQLException {
194: return new NamingMultiConnection(getConnection());
195: }
196:
197: public MultiConnection getConnection(String url)
198: throws SQLException {
199: return new NamingMultiConnection(getConnection());
200: }
201:
202: public Connection getDirectConnection(String url)
203: throws SQLException {
204: return getConnection();
205: }
206:
207: public Connection getDirectConnection(String url, String name,
208: String password) throws SQLException {
209: return getConnection();
210: }
211:
212: // below all the things we dont use..
213: public void unload() {
214: }
215:
216: public void shutdown() {
217: }
218:
219: public String makeUrl() {
220: return null;
221: }
222:
223: public String makeUrl(String dbm) {
224: return null;
225: }
226:
227: public String makeUrl(String host, String dbm) {
228: return null;
229: }
230:
231: public String makeUrl(String host, int port, String dbm) {
232: return null;
233: }
234:
235: public String getUser() {
236: return null;
237: }
238:
239: public String getPassword() {
240: return null;
241: }
242:
243: public String getDatabaseName() {
244: return null;
245: }
246:
247: public void checkTime() {
248: }
249: }
|