KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ziclix > python > sql > connect > Connectx


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: Connectx.java,v 1.4 2005/02/23 04:26:19 bzimmer Exp $
5  *
6  * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
7  *
8  */

9 package com.ziclix.python.sql.connect;
10
11 import java.sql.*;
12 import java.util.*;
13 import javax.sql.*;
14 import java.lang.reflect.*;
15
16 import org.python.core.*;
17 import com.ziclix.python.sql.*;
18 import com.ziclix.python.sql.util.*;
19
20 /**
21  * Connect using through a javax.sql.DataSource or javax.sql.ConnectionPooledDataSource.
22  *
23  * @author brian zimmer
24  * @author last revised by $Author: bzimmer $
25  * @version $Revision: 1.4 $
26  */

27 public class Connectx extends PyObject {
28
29     private final String JavaDoc SET = "set";
30     private final PyString doc = new PyString("establish a connection through a javax.sql.DataSource or javax.sql.ConnectionPooledDataSource");
31
32     /**
33      * Constructor Connectx
34      */

35     public Connectx() {
36     }
37
38     /**
39      * Method __findattr__
40      *
41      * @param String name
42      * @return PyObject
43      */

44     public PyObject __findattr__(String JavaDoc name) {
45
46         if ("__doc__".equals(name)) {
47             return doc;
48         }
49
50         return super.__findattr__(name);
51     }
52
53     /**
54      * Construct a javax.sql.DataSource or javax.sql.ConnectionPooledDataSource
55      */

56     public PyObject __call__(PyObject[] args, String JavaDoc[] keywords) {
57
58         Connection c = null;
59         PyConnection pc = null;
60         Object JavaDoc datasource = null;
61         PyArgParser parser = new PyArgParser(args, keywords);
62
63         try {
64             String JavaDoc _class = (String JavaDoc) parser.arg(0).__tojava__(String JavaDoc.class);
65
66             datasource = Class.forName(_class).newInstance();
67         } catch (Exception JavaDoc e) {
68             throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to instantiate datasource");
69         }
70
71         String JavaDoc[] kws = parser.kws();
72         Class JavaDoc clazz = datasource.getClass();
73
74         for (int i = 0; i < kws.length; i++) {
75             String JavaDoc methodName = kws[i];
76
77             if (methodName == null) {
78                 continue;
79             }
80
81             Object JavaDoc value = parser.kw(kws[i]).__tojava__(Object JavaDoc.class);
82
83             if (methodName.length() > SET.length()) {
84                 if (!SET.equals(methodName.substring(0, SET.length()))) {
85
86                     // prepend "set"
87
invoke(datasource, SET + methodName.substring(0, 1).toUpperCase() + methodName.substring(1), value);
88                 } else {
89
90                     // starts with "set" so just pass it on
91
invoke(datasource, methodName, value);
92                 }
93             } else {
94
95                 // shorter than "set" so it can't be a full method name
96
invoke(datasource, SET + methodName.substring(0, 1).toUpperCase() + methodName.substring(1), value);
97             }
98         }
99
100         try {
101             if (datasource instanceof ConnectionPoolDataSource) {
102                 c = ((ConnectionPoolDataSource) datasource).getPooledConnection().getConnection();
103             } else if (datasource instanceof DataSource) {
104                 c = ((DataSource) datasource).getConnection();
105             }
106         } catch (SQLException e) {
107             throw zxJDBC.makeException(zxJDBC.DatabaseError, e);
108         }
109
110         try {
111             if ((c == null) || c.isClosed()) {
112                 throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to establish connection");
113             }
114
115             pc = new PyConnection(c);
116         } catch (SQLException e) {
117             throw zxJDBC.makeException(zxJDBC.DatabaseError, e);
118         }
119
120         return pc;
121     }
122
123     /**
124      * Method toString
125      *
126      * @return String
127      */

128     public String JavaDoc toString() {
129         return "<connectx object instance at " + Py.id(this) + ">";
130     }
131
132     /**
133      * Method invoke
134      *
135      * @param Object src
136      * @param String methodName
137      * @param Object value
138      */

139     protected void invoke(Object JavaDoc src, String JavaDoc methodName, Object JavaDoc value) {
140
141         Method method = null;
142         StringBuffer JavaDoc exceptionMsg = new StringBuffer JavaDoc("method [").append(methodName).append("] using arg type [");
143
144         exceptionMsg.append(value.getClass()).append("], value [").append(value.toString()).append("]");
145
146         try {
147             method = getMethod(src.getClass(), methodName, value.getClass());
148
149             if (method == null) {
150                 throw zxJDBC.makeException("no such " + exceptionMsg);
151             }
152
153             method.invoke(src, new Object JavaDoc[]{value});
154         } catch (IllegalAccessException JavaDoc e) {
155             throw zxJDBC.makeException("illegal access for " + exceptionMsg);
156         } catch (InvocationTargetException e) {
157             throw zxJDBC.makeException("invocation target exception for " + exceptionMsg);
158         }
159
160         return;
161     }
162
163     /**
164      * Try to find the method by the given name. If failing that, see if the valueClass
165      * is perhaps a primitive and attempt to recurse using the primitive type. Failing
166      * that return null.
167      */

168     protected Method getMethod(Class JavaDoc srcClass, String JavaDoc methodName, Class JavaDoc valueClass) {
169
170         Method method = null;
171
172         try {
173             method = srcClass.getMethod(methodName, new Class JavaDoc[]{valueClass});
174         } catch (NoSuchMethodException JavaDoc e) {
175             Class JavaDoc primitive = null;
176
177             try {
178                 Field f = valueClass.getField("TYPE");
179
180                 primitive = (Class JavaDoc) f.get(valueClass);
181             } catch (NoSuchFieldException JavaDoc ex) {
182             } catch (IllegalAccessException JavaDoc ex) {
183             } catch (ClassCastException JavaDoc ex) {
184             }
185
186             if ((primitive != null) && primitive.isPrimitive()) {
187                 return getMethod(srcClass, methodName, primitive);
188             }
189         }
190
191         return method;
192     }
193
194     // __class__ boilerplate -- see PyObject for details
195

196     /**
197      * Field __class__
198      */

199     public static PyClass __class__;
200
201     /**
202      * Method getPyClass
203      *
204      * @return PyClass
205      */

206     protected PyClass getPyClass() {
207         return __class__;
208     }
209 }
210
Popular Tags