1
2
3
4
5
6
7
8
9
10 package it.imolinfo.jbi4cics.webservices.runtime;
11
12 import it.imolinfo.jbi4cics.Logger;
13 import it.imolinfo.jbi4cics.LoggerFactory;
14 import it.imolinfo.jbi4cics.connection.jca.cics.CICSInteractionDescription;
15 import it.imolinfo.jbi4cics.exception.Jbi4cicsException;
16 import it.imolinfo.jbi4cics.exception.LocationException;
17 import it.imolinfo.jbi4cics.jbi.BCELClassLoader;
18 import it.imolinfo.jbi4cics.jbi.wsdl.Jbi4CicsAddress;
19 import it.imolinfo.jbi4cics.jbi.wsdl.Jbi4CicsBinding;
20 import it.imolinfo.jbi4cics.jbi.wsdl.Jbi4CicsExtension;
21 import it.imolinfo.jbi4cics.jbi.xfire.JbiTransport;
22 import it.imolinfo.jbi4cics.locator.SimpleLocation;
23 import it.imolinfo.jbi4cics.security.Account;
24 import it.imolinfo.jbi4cics.webservices.descriptor.ServiceDescriptor;
25 import it.imolinfo.jbi4cics.webservices.utils.generators.ServiceBeanGenerator;
26 import it.imolinfo.jbi4cics.webservices.utils.generators.ServiceInterfaceGenerator;
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.HashMap;
32 import java.util.Map;
33 import javax.jbi.component.ComponentContext;
34 import javax.wsdl.Binding;
35 import javax.wsdl.BindingFault;
36 import javax.wsdl.BindingInput;
37 import javax.wsdl.BindingOperation;
38 import javax.wsdl.BindingOutput;
39 import javax.wsdl.Definition;
40 import javax.wsdl.Operation;
41 import javax.wsdl.Port;
42 import javax.wsdl.WSDLException;
43 import javax.wsdl.extensions.ExtensionRegistry;
44 import javax.wsdl.factory.WSDLFactory;
45 import javax.wsdl.xml.WSDLReader;
46 import javax.xml.namespace.QName;
47 import org.codehaus.xfire.DefaultXFire;
48 import org.codehaus.xfire.XFire;
49 import org.codehaus.xfire.aegis.AegisBindingProvider;
50 import org.codehaus.xfire.aegis.type.TypeMapping;
51 import org.codehaus.xfire.service.Service;
52 import org.codehaus.xfire.service.binding.ObjectServiceFactory;
53 import org.codehaus.xfire.soap.SoapConstants;
54 import org.codehaus.xfire.transport.Transport;
55 import org.codehaus.xfire.transport.TransportManager;
56 import org.codehaus.xfire.wsdl.AbstractWSDL;
57 import org.xml.sax.InputSource;
58
59
60
61
62 public final class ServiceCreator {
63
64
65
66
67 private static final int BUFFER_SIZE = 4096;
68
69
70
71
72 private static final Logger LOG
73 = LoggerFactory.getLogger(ServiceCreator.class);
74
75
76
77
78 public ServiceCreator() {
79 }
80
81
82
83
84
85
86
87
88 public Service createService(final ServiceDescriptor desc,
89 final XFire xfire) {
90 QName interfaceName = new QName(desc.getServiceNameSpace(),
91 desc.getServiceInterfaceName());
92
93 return doCreateService(desc, xfire, interfaceName, false);
94 }
95
96
97
98
99
100
101
102
103
104 public Service createJbiService(final ServiceDescriptor desc,
105 final XFire xfire, final QName interfaceName) {
106 return doCreateService(desc, xfire, interfaceName, true);
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120 private Service doCreateService(final ServiceDescriptor desc,
121 final XFire xfire, final QName interfaceName,
122 final boolean isJbiService) {
123 ObjectServiceFactory factory
124 = new ObjectServiceFactory(xfire.getTransportManager());
125 AegisBindingProvider bindingProvider
126 = (AegisBindingProvider) factory.getBindingProvider();
127 Map<String, Object> props = null;
128 Service service;
129 TypeMapping typeMapping;
130
131 if (isJbiService) {
132 props = new HashMap<String, Object>();
133 props.put(ObjectServiceFactory.PORT_TYPE, interfaceName);
134 props.put(ObjectServiceFactory.STYLE, SoapConstants.STYLE_WRAPPED);
135 props.put(ObjectServiceFactory.USE, SoapConstants.USE_LITERAL);
136
137 factory.getSoap12Transports().clear();
138 factory.getSoap11Transports().clear();
139 factory.getSoap11Transports().add(JbiTransport.JBI_BINDING);
140 }
141
142 service = factory.create(desc.getServiceInterface(),
143 desc.getServiceName(), desc.getServiceNameSpace(), props);
144 service.setInvoker(new ServiceInvoker(desc));
145
146
147 service.setProperty(AbstractWSDL.GENERATE_IMPORTS, "true");
148
149 typeMapping = bindingProvider.getTypeMapping(service);
150 typeMapping.register(new BigIntegerType());
151 typeMapping.register(new BigDecimalType());
152
153 return service;
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 public Definition createWsdlFromCopyCobol(final String copyCobol,
174 final String outputCopyCobol, final ServiceDescriptor desc)
175 throws Jbi4cicsException {
176 BCELClassLoader loader
177 = new BCELClassLoader(getClass().getClassLoader());
178 Service service;
179 ByteArrayOutputStream buffer = new ByteArrayOutputStream(BUFFER_SIZE);
180
181
182 new ServiceBeanGenerator(desc, true).generateBeanClass(loader);
183
184
185 new ServiceBeanGenerator(desc, false).generateBeanClass(loader);
186
187
188 new ServiceInterfaceGenerator(desc).generateServiceInterface(loader);
189
190
191 service = createJbiService(desc, createXFire(null), new QName(
192 desc.getServiceNameSpace(), desc.getServiceInterfaceName()));
193
194
195 try {
196 service.getWSDLWriter().write(buffer);
197 return bindToCics(new ByteArrayInputStream(buffer.toByteArray()),
198 copyCobol, outputCopyCobol, desc);
199 } catch (WSDLException e) {
200 LOG.error(e.getLocalizedMessage(), e);
201 throw new Jbi4cicsException(e.getMessage(), e);
202 } catch (IOException e) {
203 LOG.error(e.getLocalizedMessage(), e);
204 throw new Jbi4cicsException(e.getMessage(), e);
205 }
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219 public static XFire createXFire(final ComponentContext componentContext) {
220 XFire xfire = new DefaultXFire();
221 TransportManager manager = xfire.getTransportManager();
222
223
224
225 for (Object o : manager.getTransports().toArray()) {
226 manager.unregister((Transport) o);
227 }
228 manager.register(new JbiTransport(componentContext));
229 return xfire;
230 }
231
232 private Definition bindToCics(final InputStream wsdl,
233 final String copyCobol, final String outputCopyCobol,
234 final ServiceDescriptor desc)
235 throws WSDLException, IOException, LocationException {
236 WSDLFactory factory = WSDLFactory.newInstance();
237 WSDLReader reader = factory.newWSDLReader();
238 ExtensionRegistry registry = factory.newPopulatedExtensionRegistry();
239 Definition def;
240 javax.wsdl.Service service;
241 Port port;
242 Binding binding;
243
244 Jbi4CicsExtension.register(registry);
245 reader.setExtensionRegistry(registry);
246 def = reader.readWSDL(null, new InputSource(wsdl));
247 def.setExtensionRegistry(registry);
248 def.addNamespace(Jbi4CicsExtension.DEFAULT_PREFIX,
249 Jbi4CicsExtension.NS_URI_JBI4CICS);
250
251 service = def.getService(new QName(desc.getServiceNameSpace(),
252 desc.getServiceName()));
253
254
255 service.getPorts().clear();
256
257
258 port = def.createPort();
259 port.setName(desc.getServiceName() + "CicsPort");
260 service.addPort(port);
261
262
263 port.addExtensibilityElement(createJbi4CicsAddressElement(desc));
264
265
266 binding = def.createBinding();
267 binding.setUndefined(false);
268 binding.setQName(new QName(desc.getServiceNameSpace(),
269 desc.getServiceName() + "CicsBinding"));
270 binding.setPortType(def.getPortType(new QName(
271 desc.getServiceNameSpace(), desc.getServiceInterfaceName())));
272 port.setBinding(binding);
273 def.addBinding(binding);
274 removeWsdlSoapElements(def, binding, desc);
275
276
277 binding.addExtensibilityElement(
278 createJbi4CicsBindingElement(copyCobol, outputCopyCobol, desc));
279
280 return def;
281 }
282
283
284
285
286
287
288
289
290
291
292
293 private static Jbi4CicsAddress createJbi4CicsAddressElement(
294 final ServiceDescriptor desc) throws LocationException {
295 Jbi4CicsAddress addr = new Jbi4CicsAddress();
296 Account account = desc.getAccount();
297
298
299 SimpleLocation location = (SimpleLocation) desc.getServiceLocation();
300
301
302 CICSInteractionDescription interactionDesc
303 = (CICSInteractionDescription) desc.getInteractionDescription();
304
305 addr.setElementType(Jbi4CicsExtension.Q_ELEM_JBI4CICS_ADDRESS);
306 addr.setUsername(account.getUsername());
307 addr.setPassword(account.getPassword());
308 addr.setConnectionType(location.getConnectionTypeName());
309 addr.setJNDIConnectionName(location.getLocationName());
310 addr.setProgramName(interactionDesc.getProgramName());
311 addr.setTransactionName(interactionDesc.getTransactionName());
312 addr.setTpn(Boolean.valueOf(interactionDesc.isTpn()));
313 return addr;
314 }
315
316
317
318
319
320
321
322
323
324 private static void removeWsdlSoapElements(final Definition def,
325 final Binding binding, final ServiceDescriptor desc) {
326 QName oldBindingName = new QName(desc.getServiceNameSpace(),
327 desc.getServiceName() + "JBIBinding");
328
329 for (Object o : def.getBinding(oldBindingName).getBindingOperations()) {
330 BindingOperation bindingOp = (BindingOperation) o;
331 Operation operation = bindingOp.getOperation();
332 BindingInput input = bindingOp.getBindingInput();
333 BindingOutput output = bindingOp.getBindingOutput();
334
335
336
337 BindingOperation newBindingOp = def.createBindingOperation();
338 Operation newOperation = def.createOperation();
339 BindingInput newInput = def.createBindingInput();
340 BindingOutput newOutput = def.createBindingOutput();
341
342 newBindingOp.setName(bindingOp.getName());
343 newOperation.setName(operation.getName());
344 newInput.setName(input.getName());
345 newOutput.setName(output.getName());
346 newBindingOp.setOperation(newOperation);
347 newBindingOp.setBindingInput(newInput);
348 newBindingOp.setBindingOutput(newOutput);
349 for (Object obj : bindingOp.getBindingFaults().values()) {
350 BindingFault fault = (BindingFault) obj;
351 BindingFault newFault = def.createBindingFault();
352
353 newFault.setName(fault.getName());
354 newBindingOp.addBindingFault(newFault);
355 }
356
357 binding.addBindingOperation(newBindingOp);
358 }
359 def.removeBinding(oldBindingName);
360 }
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379 private static Jbi4CicsBinding createJbi4CicsBindingElement(
380 final String copyCobol, final String outputCopyCobol,
381 final ServiceDescriptor desc) {
382 Jbi4CicsBinding binding = new Jbi4CicsBinding();
383
384 binding.setElementType(Jbi4CicsExtension.Q_ELEM_JBI4CICS_BINDING);
385 binding.setServicePackageName(desc.getServiceInterfacePackageName());
386 binding.setCodePage(desc.getCodePage());
387 binding.setCopyCobol(copyCobol);
388 if ((outputCopyCobol != null) && !isBlank(outputCopyCobol)) {
389 binding.setSameCopyCobol(Boolean.FALSE);
390 binding.setOutputCopyCobol(outputCopyCobol);
391 }
392
393 return binding;
394 }
395
396
397
398
399
400
401
402
403
404
405
406 private static boolean isBlank(String str) {
407 for (int i = str.length() - 1; i >= 0; --i) {
408 if (str.charAt(i) > ' ') {
409 return false;
410 }
411 }
412 return true;
413 }
414 }