-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathConnectx.java
More file actions
178 lines (152 loc) · 5.74 KB
/
Connectx.java
File metadata and controls
178 lines (152 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Jython Database Specification API 2.0
*
*
* Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
*
*/
package com.ziclix.python.sql.connect;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
import org.python.core.Py;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.Untraversable;
import com.ziclix.python.sql.PyConnection;
import com.ziclix.python.sql.zxJDBC;
import com.ziclix.python.sql.util.PyArgParser;
/**
* Connect using through a javax.sql.DataSource or javax.sql.ConnectionPooledDataSource.
*
* @author brian zimmer
*/
@Untraversable
public class Connectx extends PyObject {
private static final String SET = "set";
private static final PyString _doc =
new PyString("establish a connection through a javax.sql.DataSource or "
+ "javax.sql.ConnectionPooledDataSource");
@Override
public PyObject __findattr_ex__(String name) {
if ("__doc__".equals(name)) {
return _doc;
}
return super.__findattr_ex__(name);
}
/**
* Construct a javax.sql.DataSource or javax.sql.ConnectionPooledDataSource
*/
@Override
public PyObject __call__(PyObject[] args, String[] keywords) {
Connection c = null;
PyConnection pc = null;
Object datasource = null;
PyArgParser parser = new PyArgParser(args, keywords);
try {
String klass = (String) parser.arg(0).__tojava__(String.class);
datasource = Class.forName(klass).getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to instantiate datasource");
}
String[] kws = parser.kws();
for (int i = 0; i < kws.length; i++) {
String methodName = kws[i];
if (methodName == null) {
continue;
}
Object value = parser.kw(kws[i]).__tojava__(Object.class);
if (methodName.length() > SET.length()) {
if (!SET.equals(methodName.substring(0, SET.length()))) {
// prepend "set"
invoke(datasource, SET + methodName.substring(0, 1).toUpperCase()
+ methodName.substring(1), value);
} else {
// starts with "set" so just pass it on
invoke(datasource, methodName, value);
}
} else {
// shorter than "set" so it can't be a full method name
invoke(datasource,
SET + methodName.substring(0, 1).toUpperCase() + methodName.substring(1),
value);
}
}
try {
if (datasource instanceof ConnectionPoolDataSource) {
c = ((ConnectionPoolDataSource) datasource).getPooledConnection().getConnection();
} else if (datasource instanceof DataSource) {
c = ((DataSource) datasource).getConnection();
}
} catch (SQLException e) {
throw zxJDBC.makeException(zxJDBC.DatabaseError, e);
}
try {
if (c == null || c.isClosed()) {
throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to establish connection");
}
pc = new PyConnection(c);
} catch (SQLException e) {
throw zxJDBC.makeException(zxJDBC.DatabaseError, e);
}
return pc;
}
@Override
public String toString() {
return String.format("<connectx object at %s>", Py.id(this));
}
/**
* Method invoke
*
* @param src
* @param methodName
* @param value
*/
protected void invoke(Object src, String methodName, Object value) {
Method method = null;
StringBuffer exceptionMsg = new StringBuffer("method [");
exceptionMsg.append(methodName).append("] using arg type [");
exceptionMsg.append(value.getClass()).append("], value [");
exceptionMsg.append(value.toString()).append("]");
try {
method = getMethod(src.getClass(), methodName, value.getClass());
if (method == null) {
throw zxJDBC.makeException("no such " + exceptionMsg);
}
method.invoke(src, new Object[]{value});
} catch (IllegalAccessException e) {
throw zxJDBC.makeException("illegal access for " + exceptionMsg);
} catch (InvocationTargetException e) {
throw zxJDBC.makeException("invocation target exception for " + exceptionMsg);
}
return;
}
/**
* Try to find the method by the given name. If failing that, see if the valueClass
* is perhaps a primitive and attempt to recurse using the primitive type. Failing
* that return null.
*/
protected Method getMethod(Class<?> srcClass, String methodName, Class<?> valueClass) {
Method method = null;
try {
method = srcClass.getMethod(methodName, new Class[]{valueClass});
} catch (NoSuchMethodException e) {
Class<?> primitive = null;
try {
Field f = valueClass.getField("TYPE");
primitive = (Class<?>) f.get(valueClass);
} catch (NoSuchFieldException ex) {
} catch (IllegalAccessException ex) {
} catch (ClassCastException ex) {
}
if (primitive != null && primitive.isPrimitive()) {
return getMethod(srcClass, methodName, primitive);
}
}
return method;
}
}