1
2
3
4
5
6
7
8 package it.imolinfo.jbi4ejb.webservice.generator;
9
10 import it.imolinfo.jbi4ejb.Logger;
11 import it.imolinfo.jbi4ejb.LoggerFactory;
12 import it.imolinfo.jbi4ejb.configuration.InterfaceExtractorUtil;
13 import it.imolinfo.jbi4ejb.exception.ClassGenerationException;
14 import it.imolinfo.jbi4ejb.exception.EJBWSDLGenerationException;
15 import it.imolinfo.jbi4ejb.jbi.wsdl.Jbi4EjbAddress;
16 import it.imolinfo.jbi4ejb.jbi.wsdl.Jbi4EjbBinding;
17 import it.imolinfo.jbi4ejb.jbi.wsdl.Jbi4EjbExtension;
18 import it.imolinfo.jbi4ejb.jbi.wsdl.Jbi4EjbTypes;
19
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.io.ObjectStreamClass;
25 import java.net.MalformedURLException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Properties;
32 import java.util.Set;
33
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.Fault;
41 import javax.wsdl.Input;
42 import javax.wsdl.Operation;
43 import javax.wsdl.Output;
44 import javax.wsdl.Port;
45 import javax.wsdl.PortType;
46 import javax.wsdl.WSDLException;
47 import javax.wsdl.extensions.ExtensionRegistry;
48 import javax.wsdl.factory.WSDLFactory;
49 import javax.wsdl.xml.WSDLReader;
50 import javax.wsdl.xml.WSDLWriter;
51 import javax.xml.namespace.QName;
52
53 import org.codehaus.xfire.aegis.AegisBindingProvider;
54 import org.codehaus.xfire.aegis.type.Configuration;
55 import org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry;
56 import org.codehaus.xfire.service.Service;
57 import org.codehaus.xfire.service.binding.ObjectServiceFactory;
58 import org.codehaus.xfire.soap.SoapConstants;
59 import org.codehaus.xfire.util.ServiceUtils;
60 import org.codehaus.xfire.wsdl.AbstractWSDL;
61
62 import com.ibm.wsdl.Constants;
63 import com.ibm.wsdl.factory.WSDLFactoryImpl;
64
65
66
67
68
69
70
71 public final class WSDLGenerator {
72
73
74 private static final Logger LOG = LoggerFactory.getLogger(EJBUtils.class);
75
76
77
78
79 private WSDLGenerator() {}
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 @SuppressWarnings( { "deprecation", "unchecked" })
102 public static File createWsdlFromJar(String className, String ejbJarPath,
103 String wsdlFileName, WSDLDescriptor descriptor, File tempDir)
104 throws EJBWSDLGenerationException {
105
106
107 try {
108 JarUtil.unjar(new File(ejbJarPath), tempDir);
109 } catch (IOException e) {
110
111 LOG.error(e.getMessage());
112 throw new EJBWSDLGenerationException(e);
113 }
114
115
116 return createWsdlFromClassesDirectory(className, tempDir, wsdlFileName, descriptor);
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 @SuppressWarnings( { "deprecation", "unchecked" })
137 public static File createWsdlFromEar(String className, String earJarPath,
138 String wsdlFileName, WSDLDescriptor descriptor)
139 throws EJBWSDLGenerationException {
140
141
142 String tempDirPath = InterfaceExtractorUtil.extractEarClassesInTempDirectory(earJarPath);
143
144
145 return createWsdlFromClassesDirectory(className, new File(tempDirPath), wsdlFileName, descriptor);
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 @SuppressWarnings( { "deprecation", "unchecked" })
170 public static File createWsdlFromClassesDirectory(String className, File classesDirectory,
171 String wsdlFileName, WSDLDescriptor descriptor, File tempDir)
172 throws EJBWSDLGenerationException {
173
174
175 try {
176
177 EJBUtils.copyDirectory(classesDirectory, tempDir);
178
179
180 return createWsdlFromClassesDirectory(className, tempDir, wsdlFileName, descriptor);
181
182 } catch (IOException ioex) {
183
184 LOG.error(ioex.getMessage());
185 throw new EJBWSDLGenerationException(ioex);
186 }
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 @SuppressWarnings( { "deprecation", "unchecked" })
209 private static File createWsdlFromClassesDirectory(String className, File classesDirectory,
210 String wsdlFileName, WSDLDescriptor descriptor)
211 throws EJBWSDLGenerationException {
212
213
214 try {
215
216
217 Properties classesIDs = getClassesID(className, classesDirectory);
218
219
220 EJBUtils.removeEJBRemoteInterface(className, classesDirectory.getAbsolutePath());
221
222
223 ClassLoader classLoader = null;
224 try {
225 classLoader = Util.getURLClassLoader(classesDirectory.getAbsolutePath(), null);
226 } catch (MalformedURLException ex) {
227
228 String msg = ex.getMessage();
229 LOG.error(msg);
230 throw new ClassGenerationException(msg);
231 }
232
233 LOG.debug("Class name: " + className);
234
235
236 Class remoteInterfaceClass = classLoader.loadClass(className);
237
238 return createWsdlFromClass(remoteInterfaceClass, wsdlFileName, descriptor, classesIDs);
239
240 } catch (ClassNotFoundException cex) {
241
242 LOG.error(cex.getMessage());
243 throw new EJBWSDLGenerationException(cex);
244 } catch (ClassGenerationException cex) {
245
246 LOG.error(cex.getMessage());
247 throw new EJBWSDLGenerationException(cex);
248 }
249 }
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268 @SuppressWarnings( { "deprecation", "unchecked" })
269 private static File createWsdlFromClass(Class remoteInterfaceClass,
270 String wsdlFileName, WSDLDescriptor descriptor, Properties classesId)
271 throws EJBWSDLGenerationException {
272
273
274 try {
275
276 ObjectServiceFactory factory = new ObjectServiceFactory();
277
278
279 Map<String, Object> props = new HashMap<String, Object>();
280
281
282 QName interfaceName = ServiceUtils.makeQualifiedNameFromClass(remoteInterfaceClass);
283 props.put(ObjectServiceFactory.PORT_TYPE, interfaceName);
284
285 props.put(AbstractWSDL.GENERATE_IMPORTS, "true");
286 props.put(ObjectServiceFactory.STYLE, SoapConstants.STYLE_WRAPPED);
287 props.put(ObjectServiceFactory.USE, SoapConstants.USE_LITERAL);
288
289 factory.getSoap11Transports().clear();
290 factory.getSoap12Transports().clear();
291
292
293 AegisBindingProvider binder = (AegisBindingProvider)factory.getBindingProvider();
294 DefaultTypeMappingRegistry tmr = (DefaultTypeMappingRegistry)binder.getTypeMappingRegistry();
295
296
297 Configuration configuration = tmr.getConfiguration();
298 configuration.setDefaultMinOccurs(1);
299 configuration.setDefaultNillable(true);
300
301 Service service = factory.create(remoteInterfaceClass, interfaceName.getLocalPart(), interfaceName.getNamespaceURI(), props);
302
303 File wsdlFile = new File(wsdlFileName);
304 wsdlFile.createNewFile();
305
306 FileOutputStream f = new FileOutputStream(wsdlFile);
307 service.getWSDLWriter().write(f);
308
309 addJbi4EjbExtensionsToWSDL(wsdlFile, descriptor, classesId, service);
310
311
312
313
314 return wsdlFile;
315 } catch (MalformedURLException mex) {
316
317 LOG.error(mex.getMessage());
318 throw new EJBWSDLGenerationException(mex);
319 } catch (IOException ioex) {
320
321 LOG.error(ioex.getMessage());
322 throw new EJBWSDLGenerationException(ioex);
323 } catch (WSDLException ex) {
324
325 LOG.error(ex.getMessage());
326 throw new EJBWSDLGenerationException(ex);
327 }
328 }
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349 @SuppressWarnings( { "deprecation", "unchecked" })
350 private static void addJbi4EjbExtensionsToWSDL(File wsdlFile, WSDLDescriptor wsdlDescriptor, Properties classesId, Service xfireService)
351 throws EJBWSDLGenerationException, WSDLException {
352
353
354 Jbi4EjbAddress ejbAddress = new Jbi4EjbAddress();
355 ejbAddress.setName(wsdlDescriptor.getName());
356 ejbAddress.setLocalizationType(wsdlDescriptor.getLocalizationType());
357 ejbAddress.setElementType(Jbi4EjbExtension.Q_ELEM_JBI4EJB_ADDRESS);
358
359
360 Jbi4EjbBinding ejbBinding = new Jbi4EjbBinding();
361 if (wsdlDescriptor.isCorbaName()) {
362 ejbBinding.setOrbProperties(wsdlDescriptor.getOrbProperties());
363 } else {
364
365 ejbBinding.setJndiProperties(wsdlDescriptor.getJndiProperties());
366 }
367 ejbBinding.setElementType(Jbi4EjbExtension.Q_ELEM_JBI4EJB_BINDING);
368
369
370 Jbi4EjbTypes ejbTypes = new Jbi4EjbTypes();
371 ejbTypes.setTypesSerialVersionUIDs(classesId);
372 ejbTypes.setElementType(Jbi4EjbExtension.Q_ELEM_JBI4EJB_TYPES);
373
374 Definition wsdl = readWsdl(wsdlFile);
375
376
377 wsdl.addNamespace(Jbi4EjbExtension.DEFAULT_PREFIX,
378 Jbi4EjbExtension.NS_URI_JBI4EJB);
379
380 javax.wsdl.Service service = wsdl.getService(xfireService.getName());
381
382
383 String portName = service.getQName().getLocalPart() + "Port";
384 Port port = wsdl.createPort();
385 port.setName(portName);
386 service.addPort(port);
387
388
389 String bindingName = service.getQName().getLocalPart() + "Binding";
390 Binding binding = wsdl.createBinding();
391 wsdl.addBinding(binding);
392
393 QName bindingQName = new QName(service.getQName().getNamespaceURI(), bindingName);
394 binding.setQName(bindingQName);
395 port.setBinding(binding);
396
397
398 QName portTypeQName = xfireService.getServiceInfo().getPortType();
399 PortType portType = wsdl.getPortType(portTypeQName);
400 if (portType == null) {
401
402 String msg = "No PortType found wth name: " + portTypeQName;
403 LOG.error(msg);
404 throw new EJBWSDLGenerationException(msg);
405 }
406
407 binding.setPortType(portType);
408
409 List<Operation> operations = portType.getOperations();
410 for (Operation operation: operations) {
411 BindingOperation bop = wsdl.createBindingOperation();
412 bop.setName(operation.getName());
413 Input input = operation.getInput();
414 if (input != null) {
415 BindingInput bindingInput = wsdl.createBindingInput();
416 bindingInput.setName(input.getName());
417 bop.setBindingInput(bindingInput);
418 }
419 Output output = operation.getOutput();
420 if (output != null) {
421 BindingOutput bindingOutput = wsdl.createBindingOutput();
422 bindingOutput.setName(output.getName());
423 bop.setBindingOutput(bindingOutput);
424 }
425
426 Map faults = operation.getFaults();
427 Iterator faultIt = faults.entrySet().iterator();
428 while (faultIt.hasNext()) {
429 Fault fault = (Fault)((Map.Entry)faultIt.next()).getValue();
430 BindingFault bindingFault = wsdl.createBindingFault();
431 bindingFault.setName(fault.getName());
432 bop.addBindingFault(bindingFault);
433 }
434 binding.addBindingOperation(bop);
435 }
436
437 binding.setUndefined(false);
438
439
440 port.addExtensibilityElement(ejbAddress);
441 binding.addExtensibilityElement(ejbBinding);
442 wsdl.addExtensibilityElement(ejbTypes);
443
444
445 wsdlFile.delete();
446
447 try {
448 writeWsdl(wsdl, wsdlFile);
449 } catch (IOException e) {
450
451 String msg = "Error in writing definition on file system";
452 LOG.error(msg);
453 throw new EJBWSDLGenerationException(msg);
454 }
455
456 }
457
458
459
460
461
462
463
464 private static Definition readWsdl(final File f) throws WSDLException {
465 final WSDLFactory wsdlFactory = WSDLFactory.newInstance();
466 ExtensionRegistry registry = wsdlFactory
467 .newPopulatedExtensionRegistry();
468 final WSDLReader reader = ((WSDLFactoryImpl) wsdlFactory)
469 .newWSDLReader();
470 reader.setFeature(Constants.FEATURE_VERBOSE, false);
471 reader.setFeature(Constants.FEATURE_IMPORT_DOCUMENTS, true);
472 Jbi4EjbExtension.register(registry);
473 LOG.debug("Extension QName: " + Jbi4EjbExtension.NS_URI_JBI4EJB);
474 reader.setExtensionRegistry(registry);
475 final Definition def = reader.readWSDL(f.getAbsolutePath());
476 return def;
477 }
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494 private static void writeWsdl(Definition wsdl, final File wsdlFile) throws WSDLException, IOException {
495 final WSDLFactory wsdlFactory = WSDLFactory.newInstance();
496
497 ExtensionRegistry registry = wsdlFactory
498 .newPopulatedExtensionRegistry();
499 Jbi4EjbExtension.register(registry);
500 wsdl.setExtensionRegistry(registry);
501 final WSDLWriter writer = wsdlFactory.newWSDLWriter();
502 writer.writeWSDL(wsdl, new FileWriter(wsdlFile));
503 }
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518 @SuppressWarnings("unchecked")
519 public static Properties getClassesID(String remoteInterface, File classesDir) throws EJBWSDLGenerationException {
520
521 LOG.debug("looking for " + remoteInterface + " in directory: " + classesDir.getAbsolutePath());
522
523 Properties classesID = new Properties();
524
525 String remoteInterfaceFileName = remoteInterface.replace('.',File.separatorChar);
526
527 LOG.debug("remoteInterfaceFileName: " + remoteInterfaceFileName);
528 List<File> list = new ArrayList<File>();
529 list.add(new File(classesDir.getAbsolutePath() + File.separatorChar + remoteInterfaceFileName));
530
531
532 Set<Class> classesToSerialize;
533 try {
534 classesToSerialize = Util.findClassUsed(classesDir.getAbsolutePath(), list);
535 } catch (ClassGenerationException e) {
536
537 LOG.error(e.getMessage());
538 throw new EJBWSDLGenerationException(e);
539 }
540
541 Iterator iter = classesToSerialize.iterator();
542 while (iter.hasNext()) {
543 Class classToSerialize = (Class)iter.next();
544 LOG.debug("Looking for class: " + classToSerialize.getName());
545
546 ObjectStreamClass objectStreamClass = ObjectStreamClass.lookup(classToSerialize);
547
548 if (objectStreamClass == null) {
549
550 LOG.warn("The objectStreamClass is null for class: " + classToSerialize.getName()+
551 " the class is Serializable?");
552
553 }
554 if (objectStreamClass != null) {
555
556 long uid = objectStreamClass.getSerialVersionUID();
557
558
559
560
561
562
563
564
565
566
567 LOG.debug(classToSerialize.getName() + " uid: " + uid);
568 classesID.put(classToSerialize.getName() , Long.valueOf(uid));
569 }
570 }
571
572 return classesID;
573 }
574
575 }