001: //
002: // Copyright 1999 Craig Spannring
003: //
004: // 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 are met:
008: // 1. Redistributions of source code must retain the above copyright
009: // notice, this list of conditions and the following disclaimer.
010: // 2. Redistributions in binary form must reproduce the above copyright
011: // notice, this list of conditions and the following disclaimer in the
012: // documentation and/or other materials provided with the distribution.
013: // 3. All advertising materials mentioning features or use of this software
014: // must display the following acknowledgement:
015: // This product includes software developed by Craig Spannring
016: // 4. The name of Craig Spannring may not be used to endorse or promote
017: // products derived from this software without specific prior
018: // written permission.
019: //
020: // THIS SOFTWARE IS PROVIDED BY CRAIG SPANNRING ``AS IS'' AND
021: // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: // ARE DISCLAIMED. IN NO EVENT SHALL CRAIG SPANNRING BE LIABLE
024: // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
025: // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
026: // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
027: // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
028: // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
029: // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
030: // SUCH DAMAGE.
031: //
032:
033: package com.internetcds.jdbc.tds;
034:
035: public class Constructors {
036: public static final String cvsVersion = "$Id: Constructors.java,v 1.2 2007-10-19 13:21:40 sinisa Exp $";
037:
038: static boolean dejavu = false;
039:
040: static final int JDBC2_0 = 2;
041: static final int JDBC1_0 = 1;
042:
043: private static int jdbcVersion = JDBC1_0;
044: private static String jdbcVersionName = null;
045:
046: private static java.lang.reflect.Constructor resultSetCtor = null;
047: private static java.lang.reflect.Constructor preparedStatementCtor = null;
048: private static java.lang.reflect.Constructor callableStatementCtor = null;
049: private static java.lang.reflect.Constructor connectionCtor = null;
050:
051: static private java.lang.reflect.Constructor getCtor(
052: String classNamePrefix, Class paramTypes[])
053: throws java.lang.ClassNotFoundException,
054: java.lang.NoSuchMethodException {
055: java.lang.reflect.Constructor result = null;
056: String className = null;
057: Class theClass = null;
058:
059: className = (classNamePrefix + "_" + jdbcVersionName);
060: theClass = java.lang.Class.forName(className);
061:
062: result = theClass.getConstructor(paramTypes);
063:
064: return result;
065: } /* getCtor() */
066:
067: static private void init() throws java.sql.SQLException {
068: try {
069: Class resultSetParamTypes[] = {
070: java.lang.Class
071: .forName("com.internetcds.jdbc.tds.Tds"),
072: java.lang.Class
073: .forName("com.internetcds.jdbc.tds.Statement"),
074: java.lang.Class
075: .forName("com.internetcds.jdbc.tds.Columns") };
076: Class preparedStatementParamTypes[] = {
077: java.lang.Class.forName("java.sql.Connection"),
078: java.lang.Class
079: .forName("com.internetcds.jdbc.tds.Tds"),
080: java.lang.Class.forName("java.lang.String") };
081: Class callableStatementParamTypes[] = {
082: java.lang.Class.forName("java.sql.Connection"),
083: java.lang.Class
084: .forName("com.internetcds.jdbc.tds.Tds"),
085: java.lang.Class.forName("java.lang.String") };
086: Class connectionParamTypes[] = { java.lang.Class
087: .forName("java.util.Properties") };
088:
089: try {
090: Class statement = java.lang.Class
091: .forName("java.sql.Statement");
092: java.lang.reflect.Method execBatch = statement
093: .getDeclaredMethod("executeBatch", new Class[0]);
094:
095: jdbcVersion = JDBC2_0;
096: jdbcVersionName = "2_0";
097: } catch (NoSuchMethodException nsme) {
098: jdbcVersion = JDBC1_0;
099: jdbcVersionName = "1_0";
100: }
101:
102: try {
103: resultSetCtor = getCtor(
104: "com.internetcds.jdbc.tds.ResultSet",
105: resultSetParamTypes);
106: preparedStatementCtor = getCtor(
107: "com.internetcds.jdbc.tds.PreparedStatement",
108: preparedStatementParamTypes);
109: callableStatementCtor = getCtor(
110: "com.internetcds.jdbc.tds.CallableStatement",
111: callableStatementParamTypes);
112: connectionCtor = getCtor(
113: "com.internetcds.jdbc.tds.Connection",
114: connectionParamTypes);
115: } catch (java.lang.ClassNotFoundException e) {
116: if (jdbcVersion == JDBC2_0) {
117: //
118: // If we couldn't find the 2.0 classes, let's try to fall back
119: // to JDBC 1.0
120: //
121: jdbcVersion = JDBC1_0;
122: jdbcVersionName = "1_0";
123: resultSetCtor = getCtor(
124: "com.internetcds.jdbc.tds.ResultSet",
125: resultSetParamTypes);
126: preparedStatementCtor = getCtor(
127: "com.internetcds.jdbc.tds.PreparedStatement",
128: preparedStatementParamTypes);
129: callableStatementCtor = getCtor(
130: "com.internetcds.jdbc.tds.CallableStatement",
131: callableStatementParamTypes);
132: connectionCtor = getCtor(
133: "com.internetcds.jdbc.tds.Connection",
134: connectionParamTypes);
135: }
136: }
137: } catch (java.lang.ClassNotFoundException e) {
138: System.err.println("Couldn't find the class"); // XXX remove println
139: throw new java.sql.SQLException(e.getMessage());
140: } catch (java.lang.NoSuchMethodException e) {
141: System.err.println("Couldn't find a constructor");
142: throw new java.sql.SQLException(e.getMessage());
143: }
144:
145: dejavu = true;
146: } /* init() */
147:
148: public static java.sql.ResultSet newResultSet(Tds tds_,
149: java.sql.Statement stmt_, Columns columns_)
150: throws java.sql.SQLException {
151: if (!dejavu) {
152: init();
153: }
154:
155: java.sql.ResultSet result = null;
156: try {
157: Object params[] = { tds_, stmt_, columns_ };
158:
159: result = (java.sql.ResultSet) resultSetCtor
160: .newInstance(params);
161: } catch (java.lang.reflect.InvocationTargetException e) {
162: throw new java.sql.SQLException(e.getTargetException()
163: .getMessage());
164: } catch (Throwable e) {
165: e.printStackTrace();
166: throw new java.sql.SQLException(e.getMessage());
167: }
168: return result;
169: }
170:
171: public static java.sql.CallableStatement newCallableStatement(
172: Object cx_, com.internetcds.jdbc.tds.Tds tds_,
173: java.lang.String sql_) throws java.sql.SQLException {
174: if (!dejavu) {
175: init();
176: }
177:
178: try {
179: Object params[] = { cx_, tds_, sql_ };
180:
181: return (java.sql.CallableStatement) callableStatementCtor
182: .newInstance(params);
183: } catch (java.lang.reflect.InvocationTargetException e) {
184: throw new java.sql.SQLException(e.getTargetException()
185: .getMessage());
186: } catch (Throwable e) {
187: throw new java.sql.SQLException(e.getMessage());
188: }
189: }
190:
191: public static java.sql.PreparedStatement newPreparedStatement(
192: Object cx_, com.internetcds.jdbc.tds.Tds tds_,
193: java.lang.String sql_) throws java.sql.SQLException {
194: if (!dejavu) {
195: init();
196: }
197:
198: try {
199: Object params[] = { cx_, tds_, sql_ };
200:
201: return (java.sql.PreparedStatement) preparedStatementCtor
202: .newInstance(params);
203: } catch (java.lang.reflect.InvocationTargetException e) {
204: throw new java.sql.SQLException(e.getTargetException()
205: .getMessage());
206: } catch (Throwable e) {
207: throw new java.sql.SQLException(e.getMessage());
208: }
209: }
210:
211: public static java.sql.Connection newConnection(
212: java.util.Properties props_) throws java.sql.SQLException,
213: com.internetcds.jdbc.tds.TdsException {
214: if (!dejavu) {
215: init();
216: }
217:
218: try {
219: Object params[] = { props_, };
220:
221: return (java.sql.Connection) connectionCtor
222: .newInstance(params);
223: } catch (java.lang.reflect.InvocationTargetException e) {
224: throw new java.sql.SQLException(e.getTargetException()
225: .getMessage());
226: } catch (Throwable e) {
227: throw new java.sql.SQLException(e.getMessage());
228: }
229: } /* newConnection() */
230:
231: }
|