1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.innergrid.api;
18
19 import java.io.File;
20 import java.net.ConnectException;
21 import java.net.SocketException;
22 import java.net.URL;
23 import java.net.UnknownHostException;
24 import java.rmi.RemoteException;
25 import java.util.Hashtable;
26 import java.util.StringTokenizer;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 import javax.activation.DataHandler;
31 import javax.activation.DataSource;
32 import javax.activation.FileDataSource;
33
34 import org.apache.axis.AxisFault;
35 import org.apache.axis.MessageContext;
36 import org.apache.axis.EngineConfiguration;
37 import org.apache.axis.configuration.FileProvider;
38 import org.apache.axis.client.AxisClient;
39 import org.apache.axis.client.Stub;
40 import org.apache.axis.transport.http.HTTPConstants;
41
42 import com.gridsystems.innergrid.kernel.KernelException;
43
44
45
46
47
48
49
50 public abstract class AbstractImplBase {
51
52
53
54
55 public static final EngineConfiguration CONFIG;
56
57
58
59
60 public static final AxisClient ENGINE;
61
62 static {
63
64 CONFIG = new FileProvider("apifactory-client-config.wsdd");
65 ENGINE = new AxisClient(CONFIG);
66 }
67
68
69
70
71 protected Connection conn = null;
72
73
74
75
76 protected String apiName = null;
77
78
79
80
81 protected Stub stub = null;
82
83
84
85
86 protected URL currentUrl;
87
88
89
90
91
92
93
94
95
96
97 public AbstractImplBase(Connection c, String apiName) throws KernelException {
98 if (c == null) {
99 throw new CKernelException("CLT011");
100 }
101 if (apiName == null || apiName.equals("")) {
102 throw new CKernelException("CLT014");
103 }
104
105 this.conn = c;
106 this.apiName = apiName;
107 }
108
109
110
111
112
113
114
115
116
117 protected abstract Stub axisCreateStub(URL url) throws AxisFault;
118
119
120
121
122
123
124
125 protected Stub axisGetStub() throws KernelException {
126 URL newUrl = this.conn.getUrl(apiName);
127 if (this.stub == null || !newUrl.equals(currentUrl)) {
128 try {
129 stub = axisCreateStub(newUrl);
130 } catch (AxisFault af) {
131 throw processException(af);
132 }
133
134
135 currentUrl = newUrl;
136
137
138 axisInitStub(stub);
139 }
140
141
142 axisConfigStub(stub);
143
144 return stub;
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 @SuppressWarnings("unchecked")
162 protected void axisInitStub(Stub stub) {
163 Hashtable headers = (Hashtable)stub._getProperty(HTTPConstants.REQUEST_HEADERS);
164 if (headers == null) {
165 headers = new Hashtable();
166 stub._setProperty(HTTPConstants.REQUEST_HEADERS, headers);
167 }
168
169
170 stub._setProperty(MessageContext.HTTP_TRANSPORT_VERSION,
171 HTTPConstants.HEADER_PROTOCOL_V11);
172
173 }
174
175
176
177
178
179
180 @SuppressWarnings("unchecked")
181 protected void axisConfigStub(Stub stub) {
182 Hashtable headers = (Hashtable)stub._getProperty(HTTPConstants.REQUEST_HEADERS);
183 if (conn.isChunkedTransferEnabled()) {
184 headers.put(HTTPConstants.HEADER_TRANSFER_ENCODING,
185 HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED);
186 } else {
187 headers.remove(HTTPConstants.HEADER_TRANSFER_ENCODING);
188 }
189
190 String conType = (conn.isKeepAliveEnabled())
191 ? HTTPConstants.HEADER_CONNECTION_KEEPALIVE
192 : HTTPConstants.HEADER_CONNECTION_CLOSE;
193
194 stub._setProperty(HTTPConstants.HEADER_CONNECTION, conType);
195 stub.setTimeout(conn.getTimeout());
196
197 Credentials credentials = this.conn.getCredentials();
198 if (credentials != null) {
199 credentials.setup(stub);
200 }
201 }
202
203
204
205
206
207
208
209
210 protected void axisCheckAttachments(Object[] params) throws KernelException {
211 int count = (params == null) ? 0 : params.length;
212 for (int i = 0; i < count; i++) {
213 File f = null;
214 if (params[i] instanceof FileDataSource) {
215 f = ((FileDataSource)params[i]).getFile();
216 } else if (params[i] instanceof DataHandler) {
217 DataSource ds = ((DataHandler)params[i]).getDataSource();
218 if (ds instanceof FileDataSource) {
219 f = ((FileDataSource)ds).getFile();
220 }
221 } else if (params[i] instanceof File) {
222 f = (File)params[i];
223 }
224
225 if (f != null && !f.exists()) {
226 throw new CKernelException("CLT021", f.getPath());
227 }
228 }
229 }
230
231
232
233
234
235
236
237 protected KernelException processException(Exception e) {
238
239 if (e instanceof KernelException) {
240 return (KernelException)e;
241 } else if (e instanceof AxisFault) {
242 AxisFault af = (AxisFault) e;
243
244 Throwable cause = af.getCause();
245 if (cause != null) {
246 String host = this.conn.getHost();
247 String port = String.valueOf(this.conn.getPort());
248
249
250 if (cause instanceof ConnectException) {
251
252 return new CKernelException(e, "CLT001", port, host);
253 } else if (cause instanceof UnknownHostException) {
254
255 return new CKernelException(e, "CLT000", host);
256 } else if (cause instanceof SocketException) {
257
258 return new CKernelException(e, "CLT002", host);
259 }
260 }
261 if (af.getFaultCode().getLocalPart().equals("Server.NoService")) {
262
263 String reason = af.getFaultReason();
264 String apiname = "?";
265 if (reason != null) {
266 int pos = reason.lastIndexOf(' ');
267 if (pos != -1) {
268 apiname = reason.substring(pos + 1, reason.length());
269 }
270 }
271 return new CKernelException(e, "CLT003", apiname);
272 } else if (af.getFaultCode().getLocalPart().equals("CLT004")) {
273 return new CKernelException(e, "CLT004", e.getMessage());
274 } else if (af.getFaultString().indexOf(
275 "could not find deserializer for type") != -1) {
276 return getDeserializationException(af);
277 } else if (af.getFaultString().startsWith("(404)/kernel/api")) {
278 return new CKernelException("CLT006");
279 }
280 }
281
282 if (e instanceof RemoteException) {
283 RemoteException re = (RemoteException) e;
284 return KernelException.fromRemoteException(re);
285 }
286
287
288 return new CKernelException(e, "UNK000", e.getMessage());
289 }
290
291
292
293
294
295
296
297
298
299
300
301
302
303 private CKernelException getDeserializationException(AxisFault axisFault) {
304
305
306 final int groupCount = 3;
307
308 Pattern pat = Pattern.compile("'in(\\d+).+http\\:\\/\\/([\\w\\.]*)}(\\w+)$");
309 Matcher mat = pat.matcher(axisFault.getFaultString());
310 String[] params = new String[2];
311 if (mat.find() && (mat.groupCount() == groupCount)) {
312 params[0] = mat.group(1);
313
314
315 String packageName = namespaceToClass(mat.group(2), mat.group(groupCount));
316 params[1] = packageName + mat.group(groupCount);
317 } else {
318 params[0] = "?";
319 params[1] = "?";
320 }
321 return new CKernelException(axisFault, "CLT005", params);
322 }
323
324
325
326
327
328
329
330
331
332 private String namespaceToClass(String namespace, String localName) {
333 StringBuffer sb = new StringBuffer(localName);
334 for (StringTokenizer st = new StringTokenizer(namespace, "."); st.hasMoreTokens();) {
335 sb.insert(0, ".");
336 sb.insert(0, st.nextToken());
337 }
338 return sb.toString();
339 }
340 }