001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.client;
017:
018: import javax.sql.DataSource;
019: import java.io.PrintWriter;
020: import java.net.URI;
021: import java.net.URISyntaxException;
022: import java.sql.Connection;
023: import java.sql.DriverManager;
024: import java.sql.SQLException;
025:
026: /**
027: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
028: */
029: public class ClientDataSource implements DataSource {
030: private final String jdbcUrl;
031: private final String defaultPassword;
032: private final String defaultUserName;
033:
034: public static void main(String[] args) throws URISyntaxException {
035: URI uri1;
036: uri1 = new URI("datasource", null, "/path", null, null);
037: uri1 = new URI("datasource", null, "/path", null, null);
038: System.out.println("uri = " + uri1);
039: uri1 = new URI("datasource", "host", "/path", null, null);
040: System.out.println("uri = " + uri1);
041: uri1 = new URI("datasource", "host", "/path", "query",
042: "fragment");
043: System.out.println("uri = " + uri1);
044: uri1 = new URI("jdbc:derby://localhost:8080/databaseName");
045: print(uri1);
046: print(new URI(uri1.getSchemeSpecificPart()));
047: }
048:
049: private static void print(URI uri1) {
050: System.out.println("uri = " + uri1);
051: System.out.println(" scheme = " + uri1.getScheme());
052: System.out
053: .println(" part = " + uri1.getSchemeSpecificPart());
054: System.out.println(" host = " + uri1.getHost());
055: System.out.println(" path = " + uri1.getPath());
056: System.out.println(" query = " + uri1.getQuery());
057: }
058:
059: public ClientDataSource(DataSourceMetaData d) {
060: this (d.getJdbcDriver(), d.getJdbcUrl(), d.getDefaultUserName(),
061: d.getDefaultPassword());
062: }
063:
064: public ClientDataSource(String jdbcDriver, String jdbcUrl,
065: String defaultUserName, String defaultPassword) {
066: this .defaultPassword = defaultPassword;
067: this .defaultUserName = defaultUserName;
068: this .jdbcUrl = jdbcUrl;
069: ClassLoader classLoader = Thread.currentThread()
070: .getContextClassLoader();
071: try {
072: Class.forName(jdbcDriver, true, classLoader);
073: } catch (ClassNotFoundException e) {
074: throw new IllegalStateException(
075: "Cannot use DataSource in client VM without the JDBC Driver in classpath: "
076: + jdbcDriver, e);
077: } catch (NoClassDefFoundError e) {
078: throw new IllegalStateException(
079: "Cannot use DataSource in client VM without the JDBC Driver in classpath: "
080: + jdbcDriver, e);
081: }
082: }
083:
084: public Connection getConnection() throws SQLException {
085: return getConnection(defaultUserName, defaultPassword);
086: }
087:
088: public Connection getConnection(String username, String password)
089: throws SQLException {
090: Connection connection = DriverManager.getConnection(jdbcUrl,
091: username, password);
092: return connection;
093: }
094:
095: public int getLoginTimeout() throws SQLException {
096: return 0;
097: }
098:
099: public PrintWriter getLogWriter() throws SQLException {
100: return null;
101: }
102:
103: public void setLoginTimeout(int seconds) throws SQLException {
104: }
105:
106: public void setLogWriter(PrintWriter out) throws SQLException {
107: }
108:
109: public boolean isWrapperFor(java.lang.Class<?> iface) {
110: if (iface == null)
111: throw new NullPointerException("iface is null");
112: return iface.isInstance(this );
113: }
114:
115: @SuppressWarnings({"unchecked"})
116: public <T> T unwrap(Class<T> iface) throws SQLException {
117: if (iface == null)
118: throw new NullPointerException("iface is null");
119: if (iface.isInstance(this )) {
120: return (T) this ;
121: }
122: throw new SQLException(getClass().getName()
123: + " does not implement " + iface.getName());
124: }
125: }
|