001: package net.sourceforge.squirrel_sql.jdbcproxy;
002:
003: /*
004: * Copyright (C) 2006 Rob Manning
005: * manningr@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: import java.lang.reflect.Method;
023: import java.sql.Connection;
024: import java.sql.Driver;
025: import java.sql.DriverPropertyInfo;
026: import java.sql.SQLException;
027: import java.util.ArrayList;
028: import java.util.Collections;
029: import java.util.List;
030: import java.util.Properties;
031:
032: public class ProxyDriver implements Driver {
033:
034: Driver _driver = null;
035:
036: public static final String DRIVER_URL_PROP_KEY = "driverUrl";
037: public static final String DRIVER_CLASS_PROP_KEY = "driverClass";
038: public static final String DRIVER_USER_PROP_KEY = "driverUser";
039: public static final String DRIVER_PASS_PROP_KEY = "driverPass";
040: public static final String TRACK_METHOD_CALLS_KEY = "trackMethodCalls";
041:
042: public ProxyDriver() {
043: }
044:
045: public int getMajorVersion() {
046: return _driver.getMajorVersion();
047: }
048:
049: public int getMinorVersion() {
050: return _driver.getMinorVersion();
051: }
052:
053: public boolean jdbcCompliant() {
054: return _driver.jdbcCompliant();
055: }
056:
057: public boolean acceptsURL(String url) throws SQLException {
058: System.out.println("acceptsUrl: url=" + url);
059: if (url.startsWith("jdbc:proxydriver")) {
060: System.out.println("true");
061: return true;
062: } else {
063: System.out.println("false");
064: return false;
065: }
066: }
067:
068: public Connection connect(String proxyUrl, Properties props)
069: throws SQLException {
070: if (null == props) {
071: throw new IllegalArgumentException(
072: "Properties arg was null");
073: }
074: ProxyMethodManager.setDriverProperties(props);
075: System.out.println("Connecting with proxyUrl=" + proxyUrl);
076: String propsUrl = props.getProperty(DRIVER_URL_PROP_KEY);
077: String propsClass = props.getProperty(DRIVER_CLASS_PROP_KEY);
078: String propsUser = props.getProperty(DRIVER_USER_PROP_KEY);
079: String propsPass = props.getProperty(DRIVER_PASS_PROP_KEY);
080: try {
081: _driver = (Driver) Class.forName(propsClass).newInstance();
082:
083: Properties driverProps = new Properties();
084: driverProps.put("user", propsUser);
085: driverProps.put("password", propsPass);
086:
087: Connection tmp = _driver.connect(propsUrl, driverProps);
088: if (tmp == null) {
089: throw new RuntimeException("Connect failed");
090: }
091: return new ProxyConnection(tmp);
092: } catch (SQLException e) {
093: throw e;
094: } catch (ClassNotFoundException e) {
095: System.err.println("Unable to locate class " + propsClass);
096: return null;
097: } catch (Exception e) {
098: e.printStackTrace();
099: return null;
100: }
101: }
102:
103: public DriverPropertyInfo[] getPropertyInfo(String url,
104: Properties info) throws SQLException {
105: List<DriverPropertyInfo> list = new ArrayList<DriverPropertyInfo>();
106:
107: DriverPropertyInfo classInfo = new SortableDriverProperty(
108: DRIVER_CLASS_PROP_KEY, null);
109: classInfo.required = true;
110: list.add(classInfo);
111: DriverPropertyInfo urlInfo = new SortableDriverProperty(
112: DRIVER_URL_PROP_KEY, null);
113: urlInfo.required = true;
114: list.add(urlInfo);
115: DriverPropertyInfo userInfo = new SortableDriverProperty(
116: DRIVER_USER_PROP_KEY, null);
117: userInfo.required = true;
118: list.add(userInfo);
119: DriverPropertyInfo passInfo = new SortableDriverProperty(
120: DRIVER_PASS_PROP_KEY, null);
121: passInfo.required = true;
122: list.add(passInfo);
123: DriverPropertyInfo trackCalls = new SortableDriverProperty(
124: TRACK_METHOD_CALLS_KEY, null);
125: trackCalls.required = false;
126: list.add(trackCalls);
127: List<SortableDriverProperty> connMethods = getMethods(
128: "java.sql.Connection", "ProxyConnection");
129: Collections.sort(connMethods);
130: list.addAll(connMethods);
131: List<SortableDriverProperty> mdMethods = getMethods(
132: "java.sql.DatabaseMetaData", "ProxyDatabaseMetaData");
133: Collections.sort(mdMethods);
134: list.addAll(mdMethods);
135: List<SortableDriverProperty> psMethods = getMethods(
136: "java.sql.PreparedStatement", "ProxyPreparedStatement");
137: Collections.sort(psMethods);
138: list.addAll(psMethods);
139: List<SortableDriverProperty> rsMethods = getMethods(
140: "java.sql.ResultSet", "ProxyResultSet");
141: Collections.sort(rsMethods);
142: list.addAll(rsMethods);
143: List<SortableDriverProperty> rsmdMethods = getMethods(
144: "java.sql.ResultSetMetaData", "ProxyResultSetMetaData");
145: Collections.sort(rsmdMethods);
146: list.addAll(rsmdMethods);
147: List<SortableDriverProperty> stmtMethods = getMethods(
148: "java.sql.Statement", "ProxyStatement");
149: Collections.sort(stmtMethods);
150: list.addAll(stmtMethods);
151: DriverPropertyInfo[] ar = new DriverPropertyInfo[list.size()];
152: return list.toArray(ar);
153: }
154:
155: /**
156: * @param args
157: */
158: public static void main(String[] args) throws SQLException {
159: ProxyDriver driver = new ProxyDriver();
160: Properties props = new Properties();
161: props.put(DRIVER_URL_PROP_KEY,
162: "jdbc:db2://localhost:50000/sample");
163: props.put(DRIVER_CLASS_PROP_KEY, "com.ibm.db2.jcc.DB2Driver");
164: props.put(DRIVER_USER_PROP_KEY, "dbcopy");
165: props.put(DRIVER_PASS_PROP_KEY, "password");
166: Connection con = driver.connect("jdbc:proxydriver", props);
167: if (con == null) {
168: System.out.println("Connection failed");
169: } else {
170: System.out.println("Connection succeeded");
171: }
172:
173: }
174:
175: private List<SortableDriverProperty> getMethods(String className,
176: String proxyClassName) {
177: ArrayList<SortableDriverProperty> result = new ArrayList<SortableDriverProperty>();
178: try {
179: Class<?> c = Class.forName(className);
180: Method[] methods = c.getDeclaredMethods();
181:
182: if (methods.length > 0) {
183: for (int i = 0; i < methods.length; i++) {
184: Method method = methods[i];
185: SortableDriverProperty info = new SortableDriverProperty(
186: proxyClassName + "." + method.getName(),
187: null);
188: info.required = false;
189: result.add(info);
190: }
191: }
192: } catch (Exception e) {
193: e.printStackTrace();
194: }
195: return result;
196: }
197:
198: private class SortableDriverProperty extends DriverPropertyInfo
199: implements Comparable<SortableDriverProperty> {
200: public SortableDriverProperty(String name, String value) {
201: super (name, value);
202: }
203:
204: /* (non-Javadoc)
205: * @see java.lang.Comparable#compareTo(java.lang.Object)
206: */
207: public int compareTo(SortableDriverProperty o) {
208: return name.compareTo(o.name);
209: }
210: }
211: }
|