1
2
3
4
5
6
7
8
9 package it.imolinfo.jbi4cics.connection.jdbc.util;
10
11 import it.imolinfo.jbi4cics.jbi.Messages;
12 import java.lang.reflect.Method;
13 import java.sql.CallableStatement;
14 import java.sql.SQLException;
15 import java.sql.Statement;
16 import java.util.Arrays;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Vector;
22 import it.imolinfo.jbi4cics.Logger;
23 import it.imolinfo.jbi4cics.LoggerFactory;
24
25
26
27
28
29
30 public class CallableStatementInvocationHandler extends PreparedStatementInvocationHandler {
31
32
33
34
35 private static final Logger LOG
36 = LoggerFactory.getLogger(CallableStatementInvocationHandler.class);
37
38
39
40
41 private static final Messages MESSAGES
42 = Messages.getMessages(CallableStatementInvocationHandler.class);
43
44 private List<Object> paramterNameList=new Vector<Object>();
45 private List<Object> parameterIndexList=new Vector<Object>();
46 private Map<String, Object> nameValueParamterMap=new HashMap<String, Object>();
47 private Map<Integer, Object> indexValueParamterMap=new HashMap<Integer, Object>();
48
49
50
51
52 public CallableStatementInvocationHandler(String sql) {
53 super(sql);
54
55 }
56
57
58
59
60
61
62 public CallableStatementInvocationHandler(String sql, int resultSetType, int resultSetConcurrency) {
63 super(sql, resultSetType, resultSetConcurrency);
64
65 }
66
67
68
69
70
71
72
73 public CallableStatementInvocationHandler(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) {
74 super(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
75
76 }
77
78
79
80
81
82 protected Statement createStatement() throws SQLException {
83 return getConnection().prepareCall(sql,resultSetType,resultSetConcurrency,resultSetHoldability);
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
98 if (method.getName().startsWith("execute")){
99 Object object=super.invoke(proxy,method,args);
100 handlePostExecute();
101 return object;
102 }
103
104 Class clazz=method.getDeclaringClass();
105 if (!(clazz.equals(CallableStatement.class))){
106
107 return super.invoke(proxy,method,args);
108 }
109 if (method.getName().startsWith("set")){
110
111 return super.invoke(proxy,method,args);
112 }
113
114
115
116
117
118
119
120
121
122 if (method.getName().startsWith("register")){
123 return handleRegister(proxy, method, args);
124 }
125
126 if (method.getName().startsWith("get")){
127 return handleGetParameter(method, args);
128 }
129
130 return null;
131 }
132
133
134
135
136
137
138
139
140
141 private Object handleRegister(Object proxy, Method method, Object[] args) throws Throwable, SQLException {
142
143 LOG.debug("method.getParameterTypes(): "+Arrays.toString(method.getParameterTypes()));
144 if (method.getParameterTypes()[0].equals(String.class)){
145
146 paramterNameList.add(args[0]);
147
148 return super.invoke(proxy,method,args);
149 }
150 if (method.getParameterTypes()[0].equals(int.class)){
151
152 parameterIndexList.add(args[0]);
153
154 return super.invoke(proxy,method,args);
155 }
156 throw new SQLException(MESSAGES.getString("CIC000700_Error_registering_parameter", new Object[] {Arrays.toString(args)}));
157 }
158
159 private Object handleGetParameter(Method method, Object[] args) throws SQLException {
160 if (isConnected()) {
161 return invokeMethod(method,args);
162 }
163 else {
164 if (args.length>1) {
165 throw new SQLException (MESSAGES.getString("CIC000701_Getting_multiple_parameters_unsupported"));
166 }
167 if (method.getParameterTypes()[0].equals(String.class)){
168 return nameValueParamterMap.get(args[0]);
169 }
170 if (method.getParameterTypes()[0].equals(int.class)){
171 return indexValueParamterMap.get(args[0]);
172 }
173 throw new SQLException(MESSAGES.getString("CIC000702_Unknown_arguments", new Object[] {Arrays.toString(args)}));
174 }
175 }
176
177 private void handlePostExecute() throws SQLException {
178
179 for (Iterator<Object> i=paramterNameList.iterator();i.hasNext();){
180 String parameterName=(String)i.next();
181 Object value=((CallableStatement)getUnderlyingStatement()).getObject(parameterName);
182 nameValueParamterMap.put(parameterName,value);
183 }
184 for (Iterator<Object> i=parameterIndexList.iterator();i.hasNext();){
185 int index=((Integer)i.next()).intValue();
186 Object value=((CallableStatement)getUnderlyingStatement()).getObject(index);
187 indexValueParamterMap.put(new Integer(index),value);
188 }
189 LOG.debug("name : value keys: "+Arrays.toString(nameValueParamterMap.keySet().toArray()));
190 LOG.debug("name : value values: "+Arrays.toString(nameValueParamterMap.values().toArray()));
191 LOG.debug("index : value keys: "+Arrays.toString(indexValueParamterMap.keySet().toArray()));
192 LOG.debug("index : value values: "+Arrays.toString(indexValueParamterMap.values().toArray()));
193 }
194 }
195
196