1
2
3
4
5
6
7
8 package it.imolinfo.jbi4corba.webservice.runtime;
9
10 import it.imolinfo.jbi4corba.Logger;
11 import it.imolinfo.jbi4corba.LoggerFactory;
12 import it.imolinfo.jbi4corba.exception.ServiceActivationException;
13 import it.imolinfo.jbi4corba.exception.ServiceCreationException;
14 import it.imolinfo.jbi4corba.webservice.descriptor.ConsumerServiceDescriptor;
15 import it.imolinfo.jbi4corba.webservice.generator.ServerCorbaClassesHolder;
16
17 import java.io.File;
18 import java.io.FileWriter;
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.lang.reflect.Proxy;
25 import java.util.Arrays;
26 import java.util.Map;
27 import java.util.Properties;
28 import java.util.logging.Level;
29 import javax.xml.namespace.QName;
30 import org.omg.CORBA.ORB;
31 import org.omg.CORBA.ORBPackage.InvalidName;
32 import org.omg.CORBA.Policy;
33 import org.omg.CORBA.portable.ValueFactory;
34 import org.omg.CosNaming.NameComponent;
35 import org.omg.CosNaming.NamingContextExt;
36 import org.omg.CosNaming.NamingContextExtHelper;
37 import org.omg.CosNaming.NamingContextPackage.CannotProceed;
38 import org.omg.CosNaming.NamingContextPackage.NotFound;
39 import org.omg.PortableServer.IdAssignmentPolicyValue;
40 import org.omg.PortableServer.LifespanPolicyValue;
41 import org.omg.PortableServer.POA;
42 import org.omg.PortableServer.POAHelper;
43 import org.omg.PortableServer.POAPackage.ServantAlreadyActive;
44 import org.omg.PortableServer.POAPackage.WrongPolicy;
45 import org.omg.PortableServer.Servant;
46 import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
47 import org.omg.PortableServer.POAPackage.AdapterAlreadyExists;
48 import org.omg.PortableServer.POAPackage.InvalidPolicy;
49 import org.omg.PortableServer.POAPackage.ObjectAlreadyActive;
50
51
52
53
54 public class ConsumerServiceCreator {
55
56
57
58
59 private static final Logger LOG = LoggerFactory.getLogger(ConsumerServiceCreator.class);
60 private static final String LOCALIZATION_NAMESERVICE = "NameService";
61 private static final String LOCALIZATION_IOR = "IOR";
62 public static final String JBI4CORBA_PERSISENT_POA_NAME = "jbi4corba.peristent_poa_name";
63 public static final String JBI4CORBA_OBJECT_ID = "jbi4corba.object_id";
64
65
66
67
68 public ConsumerServiceCreator() {
69
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 public void createJbiService(
204 ConsumerServiceDescriptor consumerServiceDescriptor)
205 throws ServiceCreationException {
206
207 Constructor constructor = null;
208 Servant poaTie = null;
209
210
211 ServerCorbaClassesHolder serverCorbaClassesHolder = consumerServiceDescriptor.getServerCorbaClassesHolder();
212
213
214 if (serverCorbaClassesHolder.isGenerateClassesFromIDL()) {
215
216 ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
217
218 try {
219
220 Thread.currentThread().setContextClassLoader(
221 consumerServiceDescriptor.getServerCorbaClassesHolder().getOriginalClassLoader());
222
223
224
225
226
227 Properties props = consumerServiceDescriptor.getOrbProperties();
228 if (consumerServiceDescriptor.isPersistent()) {
229
230 if ("org.jacorb.orb.ORB".equals(props.getProperty("org.omg.CORBA.ORBClass"))) {
231 if (props.get("jacorb.implname") == null) {
232 QName serviceInterfce = consumerServiceDescriptor.getTargetInterfaceName();
233 String implname = "{" + serviceInterfce.getNamespaceURI() + "}" + serviceInterfce.getLocalPart();
234 props.put("jacorb.implname", implname);
235 }
236 }
237 }
238 ORB orb = ORB.init((String[]) null, props);
239 LOG.debug("ORB.init ... ok");
240 consumerServiceDescriptor.setOrb(orb);
241
242 RuntimeInformation runtimeInfo = new RuntimeInformation(
243 serverCorbaClassesHolder.getUrlClassLoader(),
244 serverCorbaClassesHolder.getOriginalClassLoader(), serverCorbaClassesHolder.getAllCorbaTypes(), consumerServiceDescriptor.getEndpoint(), consumerServiceDescriptor.getOrb(), serverCorbaClassesHolder.getAllIDLTypes(), serverCorbaClassesHolder.getCorbaEnumMap(), serverCorbaClassesHolder.getIdToClassNameMap(), serverCorbaClassesHolder.getTypeDefs());
245
246 consumerServiceDescriptor.setRuntimeInformation(runtimeInfo);
247
248
249 ConsumerInvocationHandler consumerInvocationHandler = new ConsumerInvocationHandler(
250 consumerServiceDescriptor);
251 consumerServiceDescriptor.setConsumerInvocationHandler(consumerInvocationHandler);
252
253
254 Class corbaImplClass = serverCorbaClassesHolder.getCorbaImplClass();
255 Constructor corbaImplConstructor = getConstructorWithConsumerInvocationHandler(corbaImplClass);
256 Object corbaImpl = newObject(corbaImplConstructor,
257 new Object[]{consumerInvocationHandler});
258 LOG.debug("new corbaImpl=" + corbaImpl);
259
260
261 Map<String, java.lang.Object> map = consumerServiceDescriptor.getServerCorbaClassesHolder().getValueTypeIdAndInstance();
262
263 if (map == null || map.size() == 0) {
264 LOG.debug("CRB000130_No_value_type_factory_to_register_using_the_ORB");
265 } else {
266 LOG.debug("ValueTypeFactoryMap.size =" + map.size());
267
268
269 for (String id : map.keySet()) {
270 ValueFactory vf = (ValueFactory) map.get(id);
271
272 LOG.debug("Consumer Registering a ValueType Factory. id=" + id + "; instance=" + vf);
273
274 ((org.omg.CORBA_2_3.ORB) orb).register_value_factory(
275 id, vf);
276 }
277 }
278
279 constructor = getConstructorFromPoaTie(serverCorbaClassesHolder.getCorbaPOATie(), new Class[]{
280 serverCorbaClassesHolder.getCorbaOperations(),
281 POA.class});
282
283 LOG.debug("getConstructorFromPoaTie ... ok");
284
285 POA rootPoa;
286 POA myPoa;
287 String poaName = null;
288 Policy[] policies = null;
289 try {
290 rootPoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
291 if (consumerServiceDescriptor.isPersistent()) {
292 LOG.debug("consumer is persistent");
293
294 policies = new org.omg.CORBA.Policy[2];
295 policies[0] = rootPoa.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
296 policies[1] = rootPoa.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
297
298 if (props.getProperty(JBI4CORBA_PERSISENT_POA_NAME) == null) {
299 poaName = consumerServiceDescriptor.getEndpoint().getEndpointName();
300 } else {
301 poaName = props.getProperty(JBI4CORBA_PERSISENT_POA_NAME);
302 }
303 myPoa = rootPoa.create_POA(poaName, rootPoa.the_POAManager(), policies);
304 LOG.debug("created child poa: " + myPoa);
305 } else {
306 myPoa = rootPoa;
307 }
308 } catch (InvalidName e) {
309 Object[] args = new Object[]{orb, e.getMessage()};
310 LOG.error("CRB000722_Unable_to_instantiate_resolve_RootPOA", args, e);
311 throw new ServiceCreationException("CRB000722_Unable_to_instantiate_resolve_RootPOA", args, e);
312 } catch (AdapterAlreadyExists e) {
313 Object[] args = new Object[]{poaName, policies, e.getMessage()};
314 LOG.error("CRB000770_Peristent_poa_initialization_failed", args, e);
315 throw new ServiceCreationException("CRB000770_Peristent_poa_initialization_failed", args, e);
316 } catch (InvalidPolicy e) {
317 Object[] args = new Object[]{poaName, policies, e.getMessage()};
318 LOG.error("CRB000770_Peristent_poa_initialization_failed", args, e);
319 throw new ServiceCreationException("CRB000770_Peristent_poa_initialization_failed", args, e);
320 }
321
322 poaTie = newPoaTie(constructor, new Object[]{corbaImpl, myPoa});
323 LOG.debug("newPoaTie ... ok. Servant=" + poaTie);
324 if (consumerServiceDescriptor.isPersistent()) {
325
326 String servantName = null;
327 try {
328
329 if (props.getProperty(JBI4CORBA_OBJECT_ID) == null) {
330 QName service = consumerServiceDescriptor.getEndpoint().getServiceName();
331 servantName = "{" + service.getNamespaceURI() + "}" + service.getLocalPart();
332 } else {
333 servantName = props.getProperty(JBI4CORBA_OBJECT_ID);
334 }
335 myPoa.activate_object_with_id(servantName.getBytes(), poaTie);
336 LOG.debug("Servant acivated on Poa");
337 } catch (ServantAlreadyActive e) {
338 Object[] args = new Object[]{poaTie, myPoa};
339 LOG.error("CRB000771_Unable_to_activate_servant_with_peristent_poa", args, e);
340 throw new ServiceCreationException("CRB000771_Unable_to_activate_servant_with_peristent_poa", args, e);
341 } catch (WrongPolicy e) {
342 Object[] args = new Object[]{poaTie, myPoa};
343 LOG.error("CRB000771_Unable_to_activate_servant_with_peristent_poa", args, e);
344 throw new ServiceCreationException("CRB000771_Unable_to_activate_servant_with_peristent_poa", args, e);
345 } catch (ObjectAlreadyActive e) {
346 Object[] args = new Object[]{poaTie, myPoa};
347 LOG.error("CRB000771_Unable_to_activate_servant_with_peristent_poa", args, e);
348 throw new ServiceCreationException("CRB000771_Unable_to_activate_servant_with_peristent_poa", args, e);
349 }
350 }
351 } finally {
352 Thread.currentThread().setContextClassLoader(oldClassLoader);
353 }
354 } else {
355
356
357 ORB orb = ORB.init((String[]) null, consumerServiceDescriptor.getOrbProperties());
358 LOG.debug("ORB.init ... ok");
359 consumerServiceDescriptor.setOrb(orb);
360
361
362 ConsumerInvocationHandler consumerInvocationHandler = new ConsumerInvocationHandler(
363 consumerServiceDescriptor);
364 consumerServiceDescriptor.setConsumerInvocationHandler(consumerInvocationHandler);
365
366
367 Class corbaImplClass = serverCorbaClassesHolder.getCorbaImplClass();
368 Constructor corbaImplConstructor = getConstructorWithConsumerInvocationHandler(corbaImplClass);
369 Object corbaImpl = newObject(corbaImplConstructor,
370 new Object[]{consumerInvocationHandler});
371 LOG.debug("new corbaImpl=" + corbaImpl);
372 constructor = getConstructorFromPoaTie(serverCorbaClassesHolder.getCorbaPOATie(), new Class[]{});
373 LOG.debug("getDefaultConstructorFromPoaTie ... ok");
374
375 Object[] arguments = new Object[]{};
376 poaTie = newPoaTie(constructor, arguments);
377 LOG.debug("newPoaTie ... ok. Servant=" + poaTie);
378
379
380 setOrbOnPoaTie(poaTie, serverCorbaClassesHolder.getCorbaPOATie(),
381 orb);
382 LOG.debug("setup ORB on the Servant ... done");
383
384
385 setTargetOnPoaTie(poaTie,
386 serverCorbaClassesHolder.getCorbaPOATie(), corbaImpl);
387 }
388
389 LOG.debug("Registering the Corba Servant Implementation on the Servant(TIE)" + "... done");
390
391 consumerServiceDescriptor.setPoaTie(poaTie);
392 }
393
394
395
396
397
398
399
400
401
402
403
404
405
406 protected void setTargetOnPoaTie(
407 Servant poaTie,
408 Class poaTieClass,
409 Object poaDelegate) throws ServiceCreationException {
410
411
412
413 Method setTargetMethod = null;
414 try {
415
416 setTargetMethod = poaTieClass.getMethod(
417 "setTarget", new Class[]{java.rmi.Remote.class});
418
419 } catch (SecurityException e) {
420 Object[] args = new Object[]{"setTarget(java.rmi.Remote)",
421 poaTieClass,
422 e.getMessage()};
423
424 LOG.error("CRB000741_CreatingMethodError", args, e);
425 throw new ServiceCreationException(
426 "CRB000741_CreatingMethodError", args, e);
427
428 } catch (NoSuchMethodException e) {
429 Object[] args = new Object[]{"setTarget(java.rmi.Remote)",
430 poaTieClass,
431 e.getMessage()};
432
433 LOG.error("CRB000741_CreatingMethodError", args, e);
434 throw new ServiceCreationException(
435 "CRB000741_CreatingMethodError", args, e);
436 }
437
438
439
440 try {
441
442 setTargetMethod.invoke(poaTie, new Object[]{poaDelegate});
443
444 } catch (IllegalArgumentException e) {
445
446 Object[] args = new Object[]{"setTarget(java.rmi.Remote)",
447 poaTie,
448 e.getMessage()};
449
450 LOG.error("CRB000742_InvokingMethodError", args, e);
451 throw new ServiceCreationException(
452 "CRB000742_InvokingMethodError", args, e);
453
454 } catch (IllegalAccessException e) {
455 Object[] args = new Object[]{"setTarget(java.rmi.Remote)",
456 poaTie,
457 e.getMessage()};
458
459 LOG.error("CRB000742_InvokingMethodError", args, e);
460 throw new ServiceCreationException(
461 "CRB000742_InvokingMethodError", args, e);
462 } catch (InvocationTargetException e) {
463 Object[] args = new Object[]{"setTarget(java.rmi.Remote)",
464 poaTie,
465 e.getMessage()};
466
467 LOG.error("CRB000742_InvokingMethodError", args, e);
468 throw new ServiceCreationException(
469 "CRB000742_InvokingMethodError", args, e);
470 }
471
472 }
473
474
475
476
477
478
479
480
481
482
483
484
485 protected void setOrbOnPoaTie(
486 Servant poaTie,
487 Class poaTieClass,
488 ORB orb) throws ServiceCreationException {
489
490
491
492 Method method = null;
493 try {
494
495 method = poaTieClass.getMethod("orb", new Class[]{org.omg.CORBA.ORB.class});
496
497 } catch (SecurityException e) {
498 Object[] args = new Object[]{"orb(org.omg.CORBA.ORB)",
499 poaTieClass,
500 e.getMessage()};
501
502 LOG.error("CRB000741_CreatingMethodError", args, e);
503 throw new ServiceCreationException(
504 "CRB000741_CreatingMethodError", args, e);
505
506 } catch (NoSuchMethodException e) {
507 Object[] args = new Object[]{"orb(org.omg.CORBA.ORB)",
508 poaTieClass,
509 e.getMessage()};
510
511 LOG.error("CRB000741_CreatingMethodError", args, e);
512 throw new ServiceCreationException(
513 "CRB000741_CreatingMethodError", args, e);
514 }
515
516
517
518 try {
519
520 method.invoke(poaTie, new Object[]{orb});
521
522 } catch (IllegalArgumentException e) {
523
524 Object[] args = new Object[]{"orb(org.omg.CORBA.ORB)",
525 poaTie,
526 e.getMessage()};
527
528 LOG.error("CRB000742_InvokingMethodError", args, e);
529 throw new ServiceCreationException(
530 "CRB000742_InvokingMethodError", args, e);
531
532 } catch (IllegalAccessException e) {
533 Object[] args = new Object[]{"orb(org.omg.CORBA.ORB)",
534 poaTie,
535 e.getMessage()};
536
537 LOG.error("CRB000742_InvokingMethodError", args, e);
538 throw new ServiceCreationException(
539 "CRB000742_InvokingMethodError", args, e);
540 } catch (InvocationTargetException e) {
541 Object[] args = new Object[]{"orb(org.omg.CORBA.ORB)",
542 poaTie,
543 e.getMessage()};
544
545 LOG.error("CRB000742_InvokingMethodError", args, e);
546 throw new ServiceCreationException(
547 "CRB000742_InvokingMethodError", args, e);
548 }
549
550 }
551
552
553
554
555
556
557
558
559
560
561
562 protected Constructor getConstructorFromPoaTie(Class poaTieClass, Class[] classes)
563 throws ServiceCreationException {
564
565 Constructor constructor = null;
566
567 try {
568
569 constructor = poaTieClass.getConstructor(classes);
570
571 } catch (SecurityException e) {
572 Object[] args = new Object[]{
573 poaTieClass, Arrays.toString(classes), e.getMessage()};
574
575 LOG.error("CRB000723_Unable_to_create_constructor", args, e);
576 throw new ServiceCreationException(
577 "CRB000723_Unable_to_create_constructor", args, e);
578 } catch (NoSuchMethodException e) {
579 Object[] args = new Object[]{
580 poaTieClass, Arrays.toString(classes), e.getMessage()};
581
582 LOG.error("CRB000723_Unable_to_create_constructor", args, e);
583 throw new ServiceCreationException(
584 "CRB000723_Unable_to_create_constructor", args, e);
585 }
586
587 return constructor;
588 }
589
590
591
592
593
594
595
596
597
598
599
600
601 protected Constructor getConstructorWithConsumerInvocationHandler(
602 Class clazz) throws ServiceCreationException {
603
604 Constructor constructor = null;
605
606 Class[] classes = new Class[]{ConsumerInvocationHandler.class};
607 try {
608
609 constructor = clazz.getConstructor(classes);
610
611 } catch (SecurityException e) {
612 Object[] args = new Object[]{
613 clazz, Arrays.toString(classes), e.getMessage()};
614
615 LOG.error("CRB000723_Unable_to_create_constructor", args, e);
616 throw new ServiceCreationException(
617 "CRB000723_Unable_to_create_constructor", args, e);
618 } catch (NoSuchMethodException e) {
619 Object[] args = new Object[]{
620 clazz, Arrays.toString(classes), e.getMessage()};
621
622 LOG.error("CRB000723_Unable_to_create_constructor", args, e);
623 throw new ServiceCreationException(
624 "CRB000723_Unable_to_create_constructor", args, e);
625 }
626
627 return constructor;
628 }
629
630
631
632
633
634
635
636
637
638
639
640 protected Servant newPoaTie(Constructor constructor, Object[] arguments)
641 throws ServiceCreationException {
642
643 Servant poaTie = null;
644 try {
645
646 poaTie = (Servant) constructor.newInstance(arguments);
647
648 } catch (IllegalArgumentException e) {
649 Object[] args = new Object[]{
650 constructor, "default constructor", e.getMessage()};
651
652 LOG.error("CRB000724_Unable_to_instatiate_object", args, e);
653 throw new ServiceCreationException(
654 "CRB000724_Unable_to_instatiate_object", args, e);
655 } catch (InstantiationException e) {
656 Object[] args = new Object[]{
657 constructor, "default constructor", e.getMessage()};
658
659 LOG.error("CRB000724_Unable_to_instatiate_object", args, e);
660 throw new ServiceCreationException(
661 "CRB000724_Unable_to_instatiate_object", args, e);
662 } catch (IllegalAccessException e) {
663 Object[] args = new Object[]{
664 constructor, "default constructor", e.getMessage()};
665
666 LOG.error("CRB000724_Unable_to_instatiate_object", args, e);
667 throw new ServiceCreationException(
668 "CRB000724_Unable_to_instatiate_object", args, e);
669 } catch (InvocationTargetException e) {
670 Throwable cause = e.getCause();
671 Object[] args = new Object[]{constructor,
672 "default constructor", cause.getMessage()};
673
674 LOG.error("CRB000725_Exception_calling_constructor", args, cause);
675 throw new ServiceCreationException(
676 "CRB000725_Exception_calling_constructor", args, cause);
677 }
678
679 return poaTie;
680 }
681
682
683
684
685
686
687
688
689
690
691
692
693 protected Object newObject(Constructor constructor, Object[] parameter)
694 throws ServiceCreationException {
695
696 Object newObject = null;
697 try {
698
699 newObject = constructor.newInstance(parameter);
700
701 } catch (IllegalArgumentException e) {
702 Object[] args = new Object[]{
703 constructor, "constructor", e.getMessage()};
704
705 LOG.error("CRB000724_Unable_to_instatiate_object", args, e);
706 throw new ServiceCreationException(
707 "CRB000724_Unable_to_instatiate_object", args, e);
708 } catch (InstantiationException e) {
709 Object[] args = new Object[]{
710 constructor, "constructor", e.getMessage()};
711
712 LOG.error("CRB000724_Unable_to_instatiate_object", args, e);
713 throw new ServiceCreationException(
714 "CRB000724_Unable_to_instatiate_object", args, e);
715 } catch (IllegalAccessException e) {
716 Object[] args = new Object[]{
717 constructor, "constructor", e.getMessage()};
718
719 LOG.error("CRB000724_Unable_to_instatiate_object", args, e);
720 throw new ServiceCreationException(
721 "CRB000724_Unable_to_instatiate_object", args, e);
722 } catch (InvocationTargetException e) {
723 Throwable cause = e.getCause();
724 Object[] args = new Object[]{constructor,
725 "default constructor", cause.getMessage()};
726
727 LOG.error("CRB000725_Exception_calling_constructor", args, cause);
728 throw new ServiceCreationException(
729 "CRB000725_Exception_calling_constructor", args, cause);
730 }
731
732 return newObject;
733 }
734
735
736
737
738
739
740 public void createJbiServicePRE(
741 ConsumerServiceDescriptor consumerServiceDescriptor)
742 throws ServiceCreationException {
743
744 ServerCorbaClassesHolder serverCorbaClassesHolder = consumerServiceDescriptor.getServerCorbaClassesHolder();
745
746 ConsumerInvocationHandler consumerInvocationHandler = new ConsumerInvocationHandler(consumerServiceDescriptor);
747
748 consumerServiceDescriptor.setConsumerInvocationHandler(
749 consumerInvocationHandler);
750
751 LOG.debug("Trying to create the poaDelegate");
752
753 Object poaDelegate = Proxy.newProxyInstance(
754 serverCorbaClassesHolder.getCorbaPOATie().getClassLoader(),
755 new Class[]{serverCorbaClassesHolder.getCorbaOperations()},
756 consumerInvocationHandler);
757
758
759 ORB orb = ORB.init(
760 (String[]) null, consumerServiceDescriptor.getOrbProperties());
761 consumerServiceDescriptor.setOrb(orb);
762
763 POA rootPoa;
764 try {
765 rootPoa = POAHelper.narrow(
766 orb.resolve_initial_references("RootPOA"));
767 } catch (InvalidName e) {
768 Object[] args = new Object[]{orb, e.getMessage()};
769
770 LOG.error("CRB000722_Unable_to_instantiate_resolve_RootPOA", args,
771 e);
772 throw new ServiceCreationException(
773 "CRB000722_Unable_to_instantiate_resolve_RootPOA", args, e);
774 }
775
776
777
778 Map<String, java.lang.Object> map = consumerServiceDescriptor.getServerCorbaClassesHolder().getValueTypeIdAndInstance();
779
780 if (map == null || map.size() == 0) {
781 LOG.debug("CRB000130_No_value_type_factory_to_register_using_the_ORB");
782 } else {
783 LOG.debug("ValueTypeFactoryMap.size=" + map.size());
784
785
786 for (String id : map.keySet()) {
787 ValueFactory vf = (ValueFactory) map.get(id);
788
789 LOG.debug("Registering a ValueType Factory. id=" + id + "; instance=" + vf);
790
791 ((org.omg.CORBA_2_3.ORB) orb).register_value_factory(id, vf);
792 }
793 }
794
795 Constructor constructor;
796 Class[] classes = new Class[]{
797 serverCorbaClassesHolder.getCorbaOperations(), POA.class};
798 try {
799 Class clazz = serverCorbaClassesHolder.getCorbaPOATie();
800
801 constructor = clazz.getConstructor(classes);
802 } catch (SecurityException e) {
803 Object[] args = new Object[]{
804 serverCorbaClassesHolder.getCorbaPOATie(),
805 Arrays.toString(classes), e.getMessage()};
806
807 LOG.error("CRB000723_Unable_to_create_constructor", args, e);
808 throw new ServiceCreationException(
809 "CRB000723_Unable_to_create_constructor", args, e);
810 } catch (NoSuchMethodException e) {
811 Object[] args = new Object[]{
812 serverCorbaClassesHolder.getCorbaPOATie(),
813 Arrays.toString(classes), e.getMessage()};
814
815 LOG.error("CRB000723_Unable_to_create_constructor", args, e);
816 throw new ServiceCreationException(
817 "CRB000723_Unable_to_create_constructor", args, e);
818 }
819 Servant poaTie;
820 Object[] arguments = new Object[]{poaDelegate, rootPoa};
821 try {
822 poaTie = (Servant) constructor.newInstance(arguments);
823 } catch (IllegalArgumentException e) {
824 Object[] args = new Object[]{
825 constructor, Arrays.toString(arguments), e.getMessage()};
826
827 LOG.error("CRB000724_Unable_to_instatiate_object", args, e);
828 throw new ServiceCreationException(
829 "CRB000724_Unable_to_instatiate_object", args, e);
830 } catch (InstantiationException e) {
831 Object[] args = new Object[]{
832 constructor, Arrays.toString(arguments), e.getMessage()};
833
834 LOG.error("CRB000724_Unable_to_instatiate_object", args, e);
835 throw new ServiceCreationException(
836 "CRB000724_Unable_to_instatiate_object", args, e);
837 } catch (IllegalAccessException e) {
838 Object[] args = new Object[]{
839 constructor, Arrays.toString(arguments), e.getMessage()};
840
841 LOG.error("CRB000724_Unable_to_instatiate_object", args, e);
842 throw new ServiceCreationException(
843 "CRB000724_Unable_to_instatiate_object", args, e);
844 } catch (InvocationTargetException e) {
845 Throwable cause = e.getCause();
846 Object[] args = new Object[]{constructor,
847 Arrays.toString(arguments), cause.getMessage()};
848
849 LOG.error("CRB000725_Exception_calling_constructor", args, cause);
850 throw new ServiceCreationException(
851 "CRB000725_Exception_calling_constructor", args, cause);
852 }
853 consumerServiceDescriptor.setPoaTie(poaTie);
854 }
855
856
857
858
859
860
861 public void registerAndActivateService(
862 ConsumerServiceDescriptor consumerServiceDescriptor)
863 throws ServiceActivationException {
864 Servant poaTie = consumerServiceDescriptor.getPoaTie();
865 POA poa = poaTie._default_POA();
866 ORB orb = consumerServiceDescriptor.getOrb();
867
868 LOG.debug("Trying to activate POA");
869 try {
870 poa.the_POAManager().activate();
871 } catch (AdapterInactive e) {
872 Object[] args = new Object[]{poa, e.getMessage()};
873
874 LOG.error("CRB000726_Unable_to_activate_RootPOA", args, e);
875 throw new ServiceActivationException(
876 "CRB000726_Unable_to_activate_RootPOA", args, e);
877 }
878 org.omg.CORBA.Object servantRef = poaTie._this_object(orb);
879 LOG.debug("POA TYPE CLASS -------->" + poaTie.getClass());
880 LOG.debug("Servant Ref -------->" + servantRef.toString());
881
882
883 String localizationType = consumerServiceDescriptor.getLocalizationType();
884
885 if (null == localizationType || "".equals(localizationType)) {
886 LOG.debug("No loalization type specified in WSDL, using NameService");
887 localizationType = LOCALIZATION_NAMESERVICE;
888 }
889
890 if (localizationType.equals(LOCALIZATION_NAMESERVICE)) {
891
892 LOG.debug("Localizing with NameService");
893 localizeWithNameService(orb, servantRef, consumerServiceDescriptor);
894
895 } else if (localizationType.equals(LOCALIZATION_IOR)) {
896 LOG.debug("Localizing with IOR file");
897
898 localizeWithIOR(orb, servantRef, consumerServiceDescriptor);
899 } else {
900
901 throw new ServiceActivationException(
902 "Unsupported localization type:" + localizationType);
903 }
904 }
905
906 private void localizeWithIOR(ORB orb, org.omg.CORBA.Object servantRef,
907 ConsumerServiceDescriptor consumerServiceDescriptor) throws ServiceActivationException {
908
909 File iorFile = new File(consumerServiceDescriptor.getCorbaServiceName());
910 if (iorFile.isDirectory()) {
911 try {
912 String serviceName = consumerServiceDescriptor.getServiceName();
913 LOG.debug("IOR localization path denoted a directory, constructing new file in it with serviceName:" + serviceName);
914 iorFile = File.createTempFile(serviceName, ".ior", iorFile);
915 } catch (IOException e) {
916 throw new ServiceActivationException("CRB000768_File_localization_failed", e);
917
918 }
919 }
920
921 LOG.debug("Writing IOR file to:" + iorFile.getAbsolutePath());
922 String ior = orb.object_to_string(servantRef);
923 PrintWriter pw = null;
924 try {
925 pw = new PrintWriter(new FileWriter(iorFile));
926
927 pw.println(ior);
928
929 pw.flush();
930 pw.close();
931 } catch (IOException e) {
932 Object[] args = new Object[]{
933 servantRef, e.getMessage()};
934 throw new ServiceActivationException("CRB000768_File_localization_failed", args, e);
935 } finally {
936 if (null != pw) {
937 pw.close();
938 }
939 }
940
941
942
943 }
944
945 private void localizeWithNameService(ORB orb, org.omg.CORBA.Object servantRef,
946 ConsumerServiceDescriptor consumerServiceDescriptor) throws ServiceActivationException {
947
948 org.omg.CORBA.Object objRef;
949 try {
950 LOG.debug("Trying to resolve the NameService");
951 objRef = orb.resolve_initial_references("NameService");
952 } catch (InvalidName e) {
953 Object[] args = new Object[]{orb, e.getMessage()};
954
955 LOG.error("CRB000727_Unable_to_resolve_inital_reference", args, e);
956 throw new ServiceActivationException(
957 "CRB000727_Unable_to_resolve_inital_reference", args, e);
958 }
959
960
961 NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
962 String name = consumerServiceDescriptor.getCorbaServiceName();
963 LOG.debug("Trying to registering the service with name: " + name);
964 if (name == null || "".equals(name)) {
965 LOG.error("CRB000728_Trying_to_register_service", name);
966 throw new ServiceActivationException(
967 "CRB000728_Trying_to_register_service",
968 new Object[]{name}, null);
969 }
970
971 NameComponent[] path;
972 try {
973 path = ncRef.to_name(name);
974 } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
975 Object[] args = new Object[]{name, ncRef, e.getMessage()};
976
977 LOG.error("CRB000729_Invalid_name", args, e);
978 throw new ServiceActivationException("CRB000729_Invalid_name",
979 args, e);
980 }
981 try {
982 LOG.debug("Trying to rebind the servant");
983 ncRef.rebind(path, servantRef);
984 } catch (NotFound e) {
985 Object[] args = new Object[]{servantRef, path, ncRef,
986 e.getMessage()};
987
988 LOG.error("CRB000730_Not_found_registering_servant", args, e);
989 throw new ServiceActivationException(
990 "CRB000730_Not_found_registering_servant", args, e);
991 } catch (CannotProceed e) {
992 Object[] args = new Object[]{servantRef, path, ncRef,
993 e.getMessage()};
994
995 LOG.error("CRB000731_Cannot_proceed_registering_servant", args, e);
996 throw new ServiceActivationException(
997 "CRB000731_Cannot_proceed_registering_servant", args, e);
998 } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
999 Object[] args = new Object[]{servantRef, path, ncRef,
1000 e.getMessage()};
1001
1002 LOG.error("CRB000732_Invalid_name_registering_servant", args, e);
1003 throw new ServiceActivationException(
1004 "CRB000732_Invalid_name_registering_servant", args, e);
1005 }
1006
1007 }
1008 }