001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.jdbc.schema;
020:
021: import java.io.PrintWriter;
022: import java.security.AccessController;
023: import java.security.PrivilegedActionException;
024: import java.sql.Connection;
025: import java.sql.Driver;
026: import java.sql.DriverManager;
027: import java.sql.SQLException;
028: import java.util.List;
029: import java.util.Properties;
030:
031: import org.apache.openjpa.jdbc.sql.DBDictionary;
032: import org.apache.openjpa.lib.util.J2DoPrivHelper;
033: import org.apache.openjpa.util.StoreException;
034:
035: /**
036: * Non-pooling driver data source.
037: */
038: public class SimpleDriverDataSource implements DriverDataSource {
039:
040: private String _connectionDriverName;
041: private String _connectionURL;
042: private String _connectionUserName;
043: private String _connectionPassword;
044: private Properties _connectionProperties;
045: private Properties _connectionFactoryProperties;
046: private Driver _driver;
047: private ClassLoader _classLoader;
048:
049: public Connection getConnection() throws SQLException {
050: return getConnection(null);
051: }
052:
053: public Connection getConnection(String username, String password)
054: throws SQLException {
055: Properties props = new Properties();
056: if (username == null)
057: username = _connectionUserName;
058: if (username != null)
059: props.put("user", username);
060:
061: if (password == null)
062: password = _connectionPassword;
063: if (password != null)
064: props.put("password", password);
065:
066: return getConnection(props);
067: }
068:
069: public Connection getConnection(Properties props)
070: throws SQLException {
071: return getDriver().connect(_connectionURL, props);
072: }
073:
074: public int getLoginTimeout() {
075: return 0;
076: }
077:
078: public void setLoginTimeout(int seconds) {
079: }
080:
081: public PrintWriter getLogWriter() {
082: return DriverManager.getLogWriter();
083: }
084:
085: public void setLogWriter(PrintWriter out) {
086: }
087:
088: public void initDBDictionary(DBDictionary dict) {
089: }
090:
091: public void setConnectionURL(String connectionURL) {
092: _connectionURL = connectionURL;
093: }
094:
095: public String getConnectionURL() {
096: return _connectionURL;
097: }
098:
099: public void setConnectionUserName(String connectionUserName) {
100: _connectionUserName = connectionUserName;
101: }
102:
103: public String getConnectionUserName() {
104: return _connectionUserName;
105: }
106:
107: public void setConnectionPassword(String connectionPassword) {
108: _connectionPassword = connectionPassword;
109: }
110:
111: public void setConnectionProperties(Properties props) {
112: _connectionProperties = props;
113: }
114:
115: public Properties getConnectionProperties() {
116: return _connectionProperties;
117: }
118:
119: public void setConnectionFactoryProperties(Properties props) {
120: _connectionFactoryProperties = props;
121: }
122:
123: public Properties getConnectionFactoryProperties() {
124: return _connectionFactoryProperties;
125: }
126:
127: public List createConnectionDecorators() {
128: return null;
129: }
130:
131: public void setClassLoader(ClassLoader classLoader) {
132: _classLoader = classLoader;
133: }
134:
135: public ClassLoader getClassLoader() {
136: return _classLoader;
137: }
138:
139: public void setConnectionDriverName(String connectionDriverName) {
140: _connectionDriverName = connectionDriverName;
141: }
142:
143: public String getConnectionDriverName() {
144: return _connectionDriverName;
145: }
146:
147: private Driver getDriver() {
148: if (_driver != null)
149: return _driver;
150:
151: try {
152: _driver = DriverManager.getDriver(_connectionURL);
153: if (_driver != null)
154: return _driver;
155: } catch (Exception e) {
156: }
157:
158: try {
159: Class.forName(_connectionDriverName, true, _classLoader);
160: } catch (Exception e) {
161: }
162: try {
163: _driver = DriverManager.getDriver(_connectionURL);
164: if (_driver != null)
165: return _driver;
166: } catch (Exception e) {
167: }
168:
169: try {
170: Class c = Class.forName(_connectionDriverName, true,
171: _classLoader);
172: _driver = (Driver) AccessController
173: .doPrivileged(J2DoPrivHelper.newInstanceAction(c));
174: return _driver;
175: } catch (Exception e) {
176: if (e instanceof RuntimeException)
177: throw (RuntimeException) e;
178: if (e instanceof PrivilegedActionException)
179: e = ((PrivilegedActionException) e).getException();
180: throw new StoreException(e);
181: }
182: }
183: }
|