1
2
3
4
5
6
7
8
9
10 package it.imolinfo.jbi4ejb.jbi.component;
11
12 import it.imolinfo.jbi4ejb.Logger;
13 import it.imolinfo.jbi4ejb.LoggerFactory;
14 import it.imolinfo.jbi4ejb.descriptor.ProviderServiceDescriptor;
15 import it.imolinfo.jbi4ejb.exception.EJBWSDLGenerationException;
16 import it.imolinfo.jbi4ejb.exception.Jbi4EjbDeployException;
17 import it.imolinfo.jbi4ejb.exception.Jbi4EjbException;
18 import it.imolinfo.jbi4ejb.jbi.Messages;
19 import it.imolinfo.jbi4ejb.jbi.component.runtime.ComponentRuntime;
20 import it.imolinfo.jbi4ejb.jbi.component.runtime.DefaultServiceUnitManager;
21 import it.imolinfo.jbi4ejb.jbi.component.runtime.RuntimeHelper;
22 import it.imolinfo.jbi4ejb.jbi.endpoint.Jbi4EjbEndpoint;
23 import it.imolinfo.jbi4ejb.jbi.endpoint.Jbi4EjbProviderEndpoint;
24 import it.imolinfo.jbi4ejb.jbi.wsdl.Jbi4EjbAddress;
25 import it.imolinfo.jbi4ejb.jbi.wsdl.Jbi4EjbBinding;
26 import it.imolinfo.jbi4ejb.jbi.wsdl.Jbi4EjbExtension;
27 import it.imolinfo.jbi4ejb.jbi.wsdl.Jbi4EjbExtensionUtils;
28 import it.imolinfo.jbi4ejb.jbi.wsdl.Jbi4EjbTypes;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import javax.jbi.JBIException;
36 import javax.jbi.management.DeploymentException;
37 import javax.jbi.servicedesc.ServiceEndpoint;
38 import javax.wsdl.Definition;
39 import javax.wsdl.PortType;
40 import javax.wsdl.WSDLException;
41 import javax.wsdl.extensions.ExtensionRegistry;
42 import javax.wsdl.factory.WSDLFactory;
43 import javax.wsdl.xml.WSDLReader;
44 import javax.xml.namespace.QName;
45 import javax.xml.parsers.DocumentBuilder;
46 import javax.xml.parsers.DocumentBuilderFactory;
47 import javax.xml.parsers.ParserConfigurationException;
48
49 import org.w3c.dom.Document;
50 import org.xml.sax.SAXException;
51
52 import com.ibm.wsdl.Constants;
53 import com.ibm.wsdl.factory.WSDLFactoryImpl;
54 import com.sun.jbi.management.descriptor.ConfigurationException;
55 import com.sun.jbi.management.descriptor.EndpointIdentifier;
56 import com.sun.jbi.management.descriptor.SUDescriptorSupport;
57
58
59
60
61
62
63
64 public class Jbi4EjbSUManager extends DefaultServiceUnitManager {
65
66
67 private static final Logger LOG = LoggerFactory
68 .getLogger(Jbi4EjbSUManager.class);
69 private static final Messages MESSAGES = Messages.getMessages(Jbi4EjbSUManager.class);
70
71
72 private List<Jbi4EjbEndpoint> deployedEndpoints = new ArrayList<Jbi4EjbEndpoint>();
73
74
75 private List<Jbi4EjbEndpoint> activatedEndpoints = new ArrayList<Jbi4EjbEndpoint>();
76
77
78 private Jbi4EjbLifeCycle lifeCycle = null;
79
80
81
82
83
84
85 public Jbi4EjbSUManager(ComponentRuntime ctx) {
86 super(ctx);
87 this.lifeCycle = (Jbi4EjbLifeCycle)ctx.getLifeCycle();
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public String deploy(String suName, String suZipPath)
103 throws DeploymentException {
104
105 final String taskName = "deploy";
106 boolean isSuccess = true;
107
108 LOG.info("EJB000109_Deploying_su_in_SA", new Object[]{suName},
109 new Object[]{suZipPath});
110
111
112 String retMsg = createComponentTaskResultXML(taskName, isSuccess);
113 return retMsg;
114 }
115
116
117
118
119
120
121
122
123
124
125 public void start(String serviceUnitName) throws javax.jbi.management.DeploymentException {
126
127 LOG.info("EJB000110_Starting_serviceUnit", new Object[]{serviceUnitName});
128
129 LOG.info("EJB000111_Deployed_Endpoints_number", new Object[]{deployedEndpoints.size()});
130
131
132 for (Jbi4EjbEndpoint endpoint : deployedEndpoints) {
133 LOG.debug("endpoint.getSuName():" + endpoint.getSuName() );
134 if (endpoint.getSuName().equals(serviceUnitName)) {
135 try {
136
137 LOG.debug("Registering the service: " + endpoint.getServiceName());
138 endpoint.registerService();
139
140 LOG.debug("Activating the service: " + endpoint.getServiceName());
141 activateEndpoint(endpoint);
142
143 activatedEndpoints.add(endpoint);
144 } catch (Jbi4EjbException e) {
145 String msg=MESSAGES.getString("EJB000112_Failure_in_starting_endpoint",
146 new Object[] {endpoint});
147 LOG.error(msg,e);
148 throw new DeploymentException(msg,e);
149 }
150 }
151 }
152
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public String undeploy(String serviceUnitName, String suZipPath)
170 throws DeploymentException {
171 final String taskName = "undeploy";
172 boolean isSuccess = true;
173
174 String retMsg = createComponentTaskResultXML(taskName, isSuccess);
175 return retMsg;
176 }
177
178
179
180
181
182
183
184
185 @SuppressWarnings("unchecked")
186 public void stop(String serviceUnitName)
187 throws javax.jbi.management.DeploymentException {
188
189 List<Jbi4EjbEndpoint> endpointsToRemove = new ArrayList<Jbi4EjbEndpoint>();
190
191
192 for (Jbi4EjbEndpoint ejbEndpoint : activatedEndpoints) {
193 if (ejbEndpoint.getSuName().equals(serviceUnitName)) {
194 try {
195
196 ejbEndpoint.unregisterService();
197
198 deactivateEndpoint(ejbEndpoint);
199
200 endpointsToRemove.add(ejbEndpoint);
201
202 } catch (Jbi4EjbException e) {
203 String msg=MESSAGES.getString("EJB000113_Failure_in_stopping_endpoint",
204 new Object[] {ejbEndpoint});
205 LOG.error(msg,e);
206 throw new DeploymentException(msg,e);
207 }
208 }
209 }
210 activatedEndpoints.removeAll(endpointsToRemove);
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 public void init(String serviceUnitName, String serviceUnitRootPath)
229 throws javax.jbi.management.DeploymentException {
230
231
232 deployedEndpoints.addAll(processDeploy(serviceUnitName, serviceUnitRootPath));
233
234 LOG.debug("Init: do nothing");
235
236 }
237
238
239
240
241
242
243
244 public void shutDown(String serviceUnitName)
245 throws javax.jbi.management.DeploymentException {
246
247
248 List<Jbi4EjbEndpoint> deployedEndpointsToRemove = new ArrayList<Jbi4EjbEndpoint>();
249
250 for (Jbi4EjbEndpoint ejbEndpoint : deployedEndpoints) {
251 if (ejbEndpoint.getSuName().equals(serviceUnitName)) {
252 deployedEndpointsToRemove.add(ejbEndpoint);
253 }
254 }
255
256 deployedEndpoints.removeAll(deployedEndpointsToRemove);
257 }
258
259
260
261
262
263
264 public Jbi4EjbEndpoint getStartedEndpoint(ServiceEndpoint endpoint) {
265
266 for (Jbi4EjbEndpoint ejbEndpoint : activatedEndpoints) {
267
268
269 if ((ejbEndpoint.getServiceName().equals(endpoint.getServiceName())) &&
270 (ejbEndpoint.getEndpointName().equals(endpoint.getEndpointName()))) {
271 return ejbEndpoint;
272 }
273 }
274
275 return null;
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289 private List<Jbi4EjbEndpoint> processDeploy(String suName, String suZipPath)
290 throws DeploymentException {
291
292 final File suDir = new File(suZipPath);
293
294 LOG.debug("The SU dir path is: " + suDir.getAbsolutePath());
295
296 final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
297 .newInstance();
298 DocumentBuilder documentBuilder = null;
299
300 try {
301 documentBuilder = docBuilderFactory.newDocumentBuilder();
302 } catch (ParserConfigurationException ex) {
303 String msg=MESSAGES.getString("EJB000115_Failure_in_creating_document_builder",
304 new Object[] {docBuilderFactory});
305 LOG.error(msg,ex);
306 throw new DeploymentException(msg,ex);
307 }
308
309
310 final FileToDefinitionInfo[] fileToDefs = readAllDefinitions(suDir);
311
312 final ArrayList<Jbi4EjbEndpoint> endpoints = new ArrayList<Jbi4EjbEndpoint>();
313
314 if (SUDescriptorSupport.TEMP_SWITCH_ENABLE_JBI_ROUTING) {
315 LOG.info("EJB000116_Parse_jbi.xml_to_resolve_service_end_point_in_given_path",
316 new Object[]{suDir.getAbsolutePath()});
317
318 EndpointIdentifier[] svcs = null;
319
320 SUDescriptorSupport descSupport = null;
321 try {
322 descSupport = new SUDescriptorSupport(
323 suDir.getAbsolutePath());
324
325 svcs = descSupport.getServices();
326 } catch (ConfigurationException ex) {
327 String msg=MESSAGES.getString("EJB000117_Failure_in_getting_service_description");
328 LOG.error(msg,ex);
329 throw new DeploymentException(msg,ex);
330 }
331
332 final int len = svcs.length;
333
334 LOG.info("EJB000118_Found_endpoint_declared_in_the_jbi.xml",
335 new Object[]{svcs.length});
336
337 for (int i = 0; i < len; i++) {
338 Definition matchedDef = null;
339 File matchedWSDL = null;
340 Jbi4EjbBinding ejbBinding = null;
341 Jbi4EjbAddress ejbAddress = null;
342 Jbi4EjbTypes ejbTypes = null;
343
344
345 EndpointIdentifier epDesc = svcs[i];
346 for (FileToDefinitionInfo element : fileToDefs) {
347 ejbBinding = Jbi4EjbExtensionUtils.getEjbBinding(element
348 .getDefinition(), epDesc.getServiceName()
349 .toString(), epDesc.getEndpointName());
350 if (ejbBinding != null) {
351 matchedDef = element.getDefinition();
352
353
354 matchedWSDL = element.getFile();
355
356 break;
357 }
358 }
359 if (ejbBinding != null) {
360 ejbAddress = Jbi4EjbExtensionUtils.getEjbAddress(
361 matchedDef, epDesc.getServiceName().toString(),
362 epDesc.getEndpointName());
363
364 if (ejbAddress == null) {
365 String msg=MESSAGES.getString("EJB000119_No_address_found");
366 LOG.error(msg);
367 throw new DeploymentException(msg);
368 }
369
370 ejbTypes = Jbi4EjbExtensionUtils.getEjbTypes(matchedDef);
371 if (ejbTypes == null) {
372 LOG.warn("EJB000120_No_types_found");
373 }
374
375 PortType portType = Jbi4EjbExtensionUtils.getPortType(matchedDef, epDesc.getServiceName().toString(),
376 epDesc.getEndpointName());
377
378
379 final String endpoint = epDesc.getEndpointName();
380 final String endpointNameLocalPart = QName
381 .valueOf(endpoint).getLocalPart();
382
383
384 Jbi4EjbProviderEndpoint ejbEndpoint;
385 try {
386 ejbEndpoint = new Jbi4EjbProviderEndpoint(epDesc.getServiceName(), endpointNameLocalPart);
387 } catch (Jbi4EjbException e1) {
388 String msg=MESSAGES.getString("EJB000121_Failure_in_creating_new_provider_endpoint",
389 new Object[] {epDesc.getServiceName()});
390 LOG.error(msg,e1);
391 throw new DeploymentException(msg,e1);
392 }
393
394
395 ejbEndpoint.setDefinition(matchedDef);
396 ejbEndpoint.setState(Jbi4EjbEndpoint.SHUTDOWN);
397 ejbEndpoint.setSuName(suName);
398 ejbEndpoint.setSuManager(this);
399 ejbEndpoint.setEndpointWSDL(matchedWSDL);
400
401
402 ProviderServiceDescriptor providerServiceDescriptor = new ProviderServiceDescriptor();
403 providerServiceDescriptor.setComponentRootPath(suZipPath);
404 providerServiceDescriptor.setName(ejbAddress.getName());
405 providerServiceDescriptor.setLocalizationType(ejbAddress.getLocalizationType());
406 providerServiceDescriptor.setJndiProperties(ejbBinding.getJndiProperties());
407 providerServiceDescriptor.setOrbProperties(ejbBinding.getOrbProperties());
408 if (ejbTypes != null) {
409 providerServiceDescriptor.setSerialVersionUID(ejbTypes.getTypesSerialVersionUIDs());
410 }
411 providerServiceDescriptor.setPortTypeName(portType.getQName());
412 providerServiceDescriptor.setServiceName(epDesc.getServiceName());
413 ejbEndpoint.setServiceDescriptor(providerServiceDescriptor);
414
415 LOG.info("EJB000122_Parses_the_matched_WSDL");
416 Document result;
417 try {
418 result = documentBuilder.parse(matchedWSDL);
419 } catch (SAXException e) {
420 String msg=MESSAGES.getString("EJB000123_Error_in_parsing_the_deploy_WSDL",
421 new Object[] {e.getMessage()});
422 LOG.error(msg,e);
423 throw new DeploymentException(msg,e);
424 } catch (IOException e) {
425 String msg=MESSAGES.getString("EJB000123_Error_in_parsing_the_deploy_WSDL",
426 new Object[] {e.getMessage()});
427 LOG.error(msg,e);
428 throw new DeploymentException(msg,e);
429 }
430 ejbEndpoint.setServiceDescription(result);
431
432 endpoints.add(ejbEndpoint);
433 }
434 }
435 }
436
437 return endpoints;
438 }
439
440
441
442
443
444
445 private void activateEndpoint(final Jbi4EjbEndpoint endpoint) throws Jbi4EjbDeployException {
446
447 LOG.info("EJB000124_Activating_endpoint", new Object[]{endpoint.getUniqueName()});
448
449 if (activatedEndpoints.indexOf(endpoint) != -1) {
450 String msg=MESSAGES.getString("EJB000125_Failed_to_deploy_endpoint_because_already_registered",
451 new Object[] {endpoint.getUniqueName()});
452 LOG.error(msg);
453 throw new Jbi4EjbDeployException(msg);
454 }
455 try {
456 QName serviceName = (QName) endpoint.getServiceName();
457 ServiceEndpoint serviceEndpoint
458 = RuntimeHelper.getComponentContext().activateEndpoint(endpoint.getServiceName(), endpoint.getEndpointName());
459
460 endpoint.setServiceEndpoint(serviceEndpoint);
461 LOG.info("EJB000126_Activated_endpoint", new Object[]{serviceName});
462 endpoint.setState(Jbi4EjbEndpoint.RUNNING);
463
464 } catch (final JBIException me) {
465 String msg=MESSAGES.getString("EJB000127_Cannot_activate_endpoint",
466 new Object[] {endpoint.getServiceName()},
467 new Object[] {me.getMessage()});
468 LOG.error(msg);
469 throw new Jbi4EjbDeployException(msg);
470 }
471 }
472
473
474
475
476
477
478 public void deactivateEndpoint(final Jbi4EjbEndpoint endpoint) throws Jbi4EjbDeployException {
479 LOG.info("EJB000128_Deactivating_endpoint", new Object[]{endpoint.getUniqueName()});
480
481
482 if(!activatedEndpoints.contains(endpoint)) {
483 LOG.error("EJB000129_Endpoint_not_active", new Object[] {endpoint.getUniqueName()});
484 } else {
485 try {
486 RuntimeHelper.getComponentContext().deactivateEndpoint(endpoint.getServiceEndpoint());
487 LOG.info("EJB000130_Endpoint_deactivated", new Object[]{endpoint.getServiceEndpoint()});
488 endpoint.setState(Jbi4EjbEndpoint.STOPPED);
489 } catch (JBIException me) {
490
491
492
493 String msg=MESSAGES.getString("EJB000131_Cannot_deactivate_endpoint",
494 new Object[] {endpoint.getServiceName()},
495 new Object[] {me.getMessage()});
496 LOG.error(msg);
497 throw new Jbi4EjbDeployException(msg);
498
499 }
500 }
501
502 }
503
504
505
506
507
508
509
510 private List<File> listWSDLFiles(final File currentDir) {
511 final List<File> cumulativeResults = new ArrayList<File>();
512 final File[] filesInCurrentDir = currentDir.listFiles();
513
514 for (File element : filesInCurrentDir) {
515
516 if (element.isFile()) {
517
518 if (element.getName().toLowerCase().endsWith(".wsdl")) {
519 cumulativeResults.add(element);
520 }
521 } else if (element.isDirectory()) {
522 final List<File> wsdlsInSubDirectories = listWSDLFiles(element);
523 cumulativeResults.addAll(wsdlsInSubDirectories);
524 }
525 }
526 return cumulativeResults;
527 }
528
529
530
531
532
533
534
535
536
537
538
539 FileToDefinitionInfo[] readAllDefinitions(final File dir)
540 throws DeploymentException {
541
542 final List<File> wsdls = listWSDLFiles(dir);
543 final File[] wsdlFiles = (File[]) wsdls.toArray(new File[0]);
544
545
546 FileToDefinitionInfo[] fileToDefs = null;
547
548 if (wsdlFiles != null) {
549 fileToDefs = new FileToDefinitionInfo[wsdlFiles.length];
550
551 for (int i = 0; i < wsdlFiles.length; i++) {
552 Definition def;
553 try {
554 def = readWsdl(wsdlFiles[i]);
555 } catch (WSDLException e) {
556
557
558
559 String msg=MESSAGES.getString("EJB000132_Error_in_reading_wsdl_file", new Object[]{e.getMessage()});
560 LOG.error(msg,e);
561 throw new DeploymentException(msg,e);
562
563 }
564 fileToDefs[i] = new FileToDefinitionInfo(wsdlFiles[i], def);
565 }
566 }
567
568 return fileToDefs;
569 }
570
571
572
573
574
575
576
577
578
579 public static Definition readWsdl(final File f) throws WSDLException {
580 final WSDLFactory wsdlFactory = WSDLFactory.newInstance();
581 ExtensionRegistry registry = wsdlFactory
582 .newPopulatedExtensionRegistry();
583 final WSDLReader reader = ((WSDLFactoryImpl) wsdlFactory)
584 .newWSDLReader();
585 reader.setFeature(Constants.FEATURE_VERBOSE, false);
586 reader.setFeature(Constants.FEATURE_IMPORT_DOCUMENTS, true);
587 Jbi4EjbExtension.register(registry);
588 LOG.debug("Extension QName: " + Jbi4EjbExtension.NS_URI_JBI4EJB);
589 reader.setExtensionRegistry(registry);
590 final Definition def = reader.readWSDL(f.getAbsolutePath());
591 return def;
592 }
593
594
595
596
597
598 static class FileToDefinitionInfo {
599
600
601 private File mFile;
602
603
604 private Definition mDefinition;
605
606
607
608
609
610
611
612 FileToDefinitionInfo(final File file, final Definition definition) {
613 mFile = file;
614 mDefinition = definition;
615 }
616
617
618
619
620
621
622 public File getFile() {
623 return mFile;
624 }
625
626
627
628
629
630
631 public Definition getDefinition() {
632 return mDefinition;
633 }
634 }
635
636
637
638
639
640
641 public Jbi4EjbLifeCycle getLifeCycle() {
642 return lifeCycle;
643 }
644
645
646
647
648
649
650
651 public void setLifeCycle(Jbi4EjbLifeCycle lifeCycle) {
652 this.lifeCycle = lifeCycle;
653 }
654
655
656
657
658
659
660 public List<Jbi4EjbEndpoint> getDeployedEndpoints() {
661 return deployedEndpoints;
662 }
663
664
665 }