001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.mashup.db.common;
042:
043: import java.sql.Connection;
044: import java.sql.Driver;
045: import java.sql.DriverManager;
046: import java.util.Properties;
047:
048: import com.sun.sql.framework.exception.BaseException;
049:
050: /**
051: * @author Jonathan Giron
052: * @version $Revision$
053: */
054: public class FlatfileDBConnectionFactory {
055:
056: public static final String DRIVER_NAME = "org.axiondb.jdbc.AxionDriver";
057: public static final String DEFAULT_FLATFILE_JDBC_URL_PREFIX = "jdbc:axiondb:";
058:
059: private static FlatfileDBConnectionFactory instance;
060:
061: public static final String KEY_IGNORE_LOCK_FILE = "org.axiondb.engine.DiskDatabase.IGNORE_LOCK_FILE";
062:
063: public static FlatfileDBConnectionFactory getInstance() {
064: if (instance == null) {
065: instance = new FlatfileDBConnectionFactory();
066: }
067: return instance;
068: }
069:
070: private FlatfileDBConnectionFactory() {
071: }
072:
073: public String getDriverClassName() {
074: return DRIVER_NAME;
075: }
076:
077: public Connection getConnection(String jdbcUrl)
078: throws BaseException {
079: return getConnection(jdbcUrl, null, null);
080: }
081:
082: public Connection getConnection(String jdbcUrl, Properties props,
083: ClassLoader cl) throws BaseException {
084: if (jdbcUrl == null) {
085: throw new BaseException("JDBC URL not set.");
086: }
087:
088: Connection conn = null;
089: ClassLoader oldContextLoader = Thread.currentThread()
090: .getContextClassLoader();
091:
092: try {
093: /* Not doing as stated in the commented section below. Using system Class Loader.
094: Facing problem when connection used by other modules like ETL Engine. */
095:
096: // WT #67399: if class loader is not specified, use the class loader
097: // associated with flatfile db module to avoid instantiating Axion classes
098: // which are associated with eBAM.
099: // if (cl == null) {
100: // cl = FlatfileDBConnectionFactory.class.getClassLoader();
101: // }
102: //
103: // Thread.currentThread().setContextClassLoader(cl);
104: registerDriver();
105:
106: if (props != null) {
107: conn = DriverManager.getConnection(jdbcUrl, props);
108: } else {
109: conn = DriverManager.getConnection(jdbcUrl);
110: }
111: } catch (Exception ex) {
112: throw new BaseException(ex);
113: } finally {
114: Thread.currentThread().setContextClassLoader(
115: oldContextLoader);
116: }
117: return conn;
118: }
119:
120: public Connection getConnection(String jdbcUrl, String uid,
121: String passwd, ClassLoader cl) throws BaseException {
122: if (jdbcUrl == null) {
123: throw new BaseException("JDBC URL not set.");
124: }
125:
126: Connection conn = null;
127: ClassLoader oldContextLoader = Thread.currentThread()
128: .getContextClassLoader();
129:
130: try {
131: /* Not doing as stated in the commented section below. Using system Class Loader.
132: Facing problem when connection used by other modules like ETL Engine. */
133:
134: // WT #67399: if class loader is not specified, use the class loader
135: // associated with flatfile db module to avoid instantiating Axion classes
136: // which are associated with eBAM.
137: // if (cl == null) {
138: // cl = FlatfileDBConnectionFactory.class.getClassLoader();
139: // }
140: //Thread.currentThread().setContextClassLoader(cl);
141: Driver drv = registerDriver();
142: Properties prop = new Properties();
143: prop.setProperty("user", uid);
144: prop.setProperty("password", passwd);
145: conn = drv.connect(jdbcUrl, prop);
146: } catch (Exception ex) {
147: throw new BaseException(ex);
148: } finally {
149: Thread.currentThread().setContextClassLoader(
150: oldContextLoader);
151: }
152:
153: return conn;
154: }
155:
156: public Object getIgnoreLockProperty() {
157: return System.getProperties().get(KEY_IGNORE_LOCK_FILE);
158: }
159:
160: public void setIgnoreLockProperty(Object newValue) {
161: if (null == newValue) {
162: System.getProperties().remove(
163: FlatfileDBConnectionFactory.KEY_IGNORE_LOCK_FILE);
164: } else {
165: System.getProperties().put(
166: FlatfileDBConnectionFactory.KEY_IGNORE_LOCK_FILE,
167: newValue);
168: }
169: }
170:
171: /**
172: * Registers the JDBC driver specific to this factory.
173: *
174: * @return Driver Class representing JDBC driver implementation class
175: */
176: private Driver registerDriver() throws Exception {
177: /* Using system class loader to load axion driver so that
178: the connection can be used by other modules like ETL Engine also.*/
179:
180: Driver drv = (Driver) Thread.currentThread()
181: .getContextClassLoader()
182: .loadClass(getDriverClassName()).newInstance();
183: return drv;
184: }
185: }
|