001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: /*
066: * DataSourceInterface.java
067: *
068: * copyright 2002 AMYCASE .
069: *
070: */
071: package com.jcorporate.expresso.core.db.datasource;
072:
073: import com.jcorporate.expresso.core.db.config.JNDIConfig;
074:
075: import javax.naming.Context;
076: import javax.naming.InitialContext;
077: import javax.naming.NamingException;
078: import javax.sql.DataSource;
079: import java.sql.Connection;
080: import java.sql.SQLException;
081: import java.util.Hashtable;
082:
083: /**
084: * <p>JndiDataSource are the pure core of the Expresso framework.</p>
085: * <p/>
086: * Interface for wrapping J2EE datasource connection retrieve
087: *
088: * @author Yves Henri AMAIZO
089: * @see com.jcorporate.expresso.core.db.DBException
090: * @see com.jcorporate.expresso.core.db.DBConnection
091: */
092: public class JndiDataSource implements DataSourceInterface {
093: private static final String this Class = JndiDataSource.class
094: .getName()
095: + ".";
096:
097: /**
098: * The local DataSource object for setting the connection to database via JNDI.
099: */
100: protected DataSource ds = null;
101:
102: /**
103: * The JDBC Configuration retrieve from config-expresso.xml for the context.
104: */
105: protected JNDIConfig myJndi = null;
106:
107: /**
108: * The local Database URL object for setting the connection to database via JNDI.
109: */
110: protected String dbURL = null;
111:
112: /**
113: * Constructor
114: * Create a new datasource object
115: */
116: public JndiDataSource() {
117: }
118:
119: /**
120: * Constructor
121: * Create a new datasource object
122: *
123: * @param ConfigJndi
124: */
125: public JndiDataSource(JNDIConfig newJndi, String newURL) {
126: myJndi = newJndi;
127: dbURL = newURL;
128: }
129:
130: /**
131: * Configure and set up the JNDI search for database Pool Factory connection.
132: * Initial JNDI context Factory for searchin in java environment
133: * Lookup for database info from this environment.
134: *
135: * @param ConfigJdbc
136: * @param ConfigJndi
137: */
138:
139: public void setupContext() throws DSException {
140: String myName = (this Class + "setupContext()");
141: Context envContext = null;
142: Hashtable envTable = null;
143:
144: try {
145: envTable = new Hashtable();
146: if (!"".equals(myJndi.getInitialContextFactory())) {
147: envTable.put(Context.INITIAL_CONTEXT_FACTORY, myJndi
148: .getInitialContextFactory());
149: }
150: if (!"".equals(myJndi.getProviderURL())) {
151: envTable.put(Context.PROVIDER_URL, myJndi
152: .getProviderURL());
153: }
154: if (!"".equals(myJndi.getSecurityPrincipal())) {
155: envTable.put(Context.SECURITY_PRINCIPAL, myJndi
156: .getSecurityPrincipal());
157: }
158: if (!"".equals(myJndi.getSecurityCredentials())) {
159: envTable.put(Context.SECURITY_CREDENTIALS, myJndi
160: .getSecurityCredentials());
161: }
162: if (!"".equals(myJndi.getSecurityAuthentication())) {
163: envTable.put(Context.SECURITY_AUTHENTICATION, myJndi
164: .getSecurityAuthentication());
165: }
166: if (!"".equals(myJndi.getSecurityProtocol())) {
167: envTable.put(Context.SECURITY_PROTOCOL, myJndi
168: .getSecurityProtocol());
169: }
170: if (!"".equals(myJndi.getDnsURL())) {
171: envTable.put(Context.DNS_URL, myJndi.getDnsURL());
172: }
173: envContext = new InitialContext(envTable);
174: if (envContext != null) {
175: ds = (DataSource) envContext.lookup(getDbURL());
176: envContext.close();
177: } else {
178: throw new DSException(
179: myName
180: + ":Cannot initialize Initial Context Factory for Datasource Object via JNDI. "
181: + " DATASOURCE : '("
182: + myJndi.getInitialContextFactory()
183: + ")'");
184: }
185: } catch (NamingException ne) {
186: throw new DSException(myName
187: + ":Cannot get Naming Object via JNDI "
188: + " and INITIAL_CONTEXT_FACTORY '"
189: + myJndi.getInitialContextFactory() + "'"
190: + " and DATASOURCE '" + getDbURL() + "'"
191: + ":JDBC returned a null connection. "
192: + " Error : " + ne.getMessage());
193: }
194:
195: }
196:
197: /**
198: * Get connection from JNDI Factory retrieve from the context previously set up.
199: */
200: public Connection getConnection() throws DSException {
201: String myName = (this Class + "." + "getConnection()");
202:
203: try {
204: if (ds == null) {
205: throw new DSException(
206: myName
207: + ":Cannot get a connection to database via JNDI DataSource Handle is null'");
208: }
209: return ds.getConnection();
210: } catch (SQLException se) {
211: throw new DSException(
212: myName
213: + ":Cannot get a connection to database via JNDI DataSource '"
214: + getDbURL() + "' (" + ")", se.getMessage());
215: }
216:
217: }
218:
219: /**
220: * Get connection from JNDI Factory retrieve from the context previously set up.
221: */
222: public Connection getConnection(String Username, String Password)
223: throws DSException {
224: String myName = (this Class + "." + "getConnection(String, String)");
225:
226: try {
227: if (ds == null) {
228: throw new DSException(
229: myName
230: + ":Cannot get a connection to database via JNDI DataSource Handle is null'");
231: }
232: return ds.getConnection(Username, Password);
233: } catch (SQLException se) {
234: throw new DSException(
235: myName
236: + ":Cannot get a connection to database via JNDI DataSource '"
237: + getDbURL() + "' (" + Username + ", "
238: + Password + ")", se.getMessage());
239: }
240:
241: }
242:
243: /**
244: * Gets the myJndi
245: *
246: * @return Returns a ConfigJndi
247: */
248: public JNDIConfig getMyJndi() {
249: return myJndi;
250: }
251:
252: /**
253: * Sets the myJndi
254: *
255: * @param myJndi The myJndi to set
256: */
257: public void setMyJndi(JNDIConfig myJndi) {
258: this .myJndi = myJndi;
259: }
260:
261: /**
262: * Gets the dbURL
263: *
264: * @return Returns a String
265: */
266: public String getDbURL() {
267: return dbURL;
268: }
269:
270: /**
271: * Sets the dbURL
272: *
273: * @param dbURL The dbURL to set
274: */
275: public void setDbURL(String dbURL) {
276: this.dbURL = dbURL;
277: }
278:
279: }
|