01: package com.jat.integration.db;
02:
03: import java.sql.Connection;
04: import java.sql.SQLException;
05: import java.util.Hashtable;
06: import javax.naming.InitialContext;
07:
08: import com.jat.core.config.Config;
09: import com.jat.core.log.LogManager;
10: import com.jat.integration.DataSource;
11: import com.jat.integration.IntegrationException;
12:
13: /**
14: * <p>Title: JAT</p>
15: * <p>Description: This class implements a database access through JNDI
16: * (for example a data source of an application server).
17: * </p>
18: * <p><b>Configuration:</b><br/>
19: * In the data source name section put the following parameters:
20: * <ul>
21: * <li><b>data_source_name</b> the JNDI name of the data source</li>
22: * <li>query definition (see query configuration in {@link com.jat.integration.db.GenericDatabaseDataSource} for details)</li>
23: * </ul>
24: * <i>Example:</i>
25: * <blockquote>
26: * [<i>myDataSourceName</i>]<br/>
27: * data_source_name = <i>JNDIdataSourceName</i><br/>
28: * query1.name = <i>myQueryName</i><br/>
29: * query1.value = <i>select * from dual</i>
30: * </blockquote>
31: * </p>
32: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
33: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
34: * @author stf
35: * @version 1.2
36: * @since 1.2
37: * @see com.jat.integration.DataSource
38: * @see com.jat.integration.db.GenericDatabaseDataSource
39: */
40:
41: public class JndiDataSource extends GenericDatabaseDataSource implements
42: DataSource {
43:
44: public final static String CONFIG_DATA_SOURCE_NAME = "data_source_name";
45:
46: public JndiDataSource() {
47: }
48:
49: protected void initDataSource() throws IntegrationException {
50: LogManager.sendDebug(this .getClass().getName()
51: + "::initDataSource: start");
52: try {
53: dataSourceName = Config.getCurrent().getValue(
54: this .getName(), CONFIG_DATA_SOURCE_NAME);
55: //inizializza il contesto JNDI
56: InitialContext ctx_ = null;
57: ctx_ = new javax.naming.InitialContext(
58: new java.util.Hashtable());
59: //lookup del DataSource sul JNDI di weblogic
60: dataSource = (javax.sql.DataSource) ctx_
61: .lookup(dataSourceName);
62: } catch (Exception ex) {
63: LogManager.sendError(this .getClass().getName()
64: + "initDataSource: data source '" + this .getName()
65: + "': exception: " + ex);
66: throw new IntegrationException(this .getClass().getName()
67: + "initDataSource: data source '" + this .getName()
68: + "': exception: " + ex);
69: } finally {
70: }
71: LogManager.sendDebug(this .getClass().getName()
72: + "::initDataSource: end");
73: }
74:
75: protected Hashtable putInitProperties(Hashtable hash)
76: throws Exception {
77: hash.put(CONFIG_DATA_SOURCE_NAME, this .dataSourceName);
78: return hash;
79: }
80:
81: protected Connection getConnection() throws SQLException {
82: return this .dataSource.getConnection();
83: }
84:
85: protected javax.sql.DataSource dataSource;
86: protected String dataSourceName;
87:
88: }
|