View Javadoc

1   /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
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   * This class is used to create the JBI service.
53   */
54  public class ConsumerServiceCreator {
55  
56      /**
57       * The logger for this class and its instances.
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       * Default constructor.
67       */
68      public ConsumerServiceCreator() {
69          // NOP
70      }
71  
72  //  /**
73  //   * This method is used to create the JBI service.
74  //   *
75  //   * @param   consumerServiceDescriptor   The consumer service descriptor
76  //   * @throws  ServiceCreationException    The service creation exception
77  //   */
78  //  public void createJbiService_old(
79  //    ConsumerServiceDescriptor consumerServiceDescriptor)
80  //    throws ServiceCreationException {
81  //
82  //		ClassLoader oldClassLoader = Thread.currentThread()
83  //				.getContextClassLoader();
84  //		try {
85  //
86  //			Thread.currentThread().setContextClassLoader(
87  //					consumerServiceDescriptor.getServerCorbaClassesHolder()
88  //							.getOriginalClassLoader());
89  //
90  //			// get the generated classes from the descriptor
91  //			ServerCorbaClassesHolder serverCorbaClassesHolder = consumerServiceDescriptor
92  //					.getServerCorbaClassesHolder();
93  //
94  //			// create and set up the invocation handler
95  //			ConsumerInvocationHandler consumerInvocationHandler = new ConsumerInvocationHandler(
96  //					consumerServiceDescriptor);
97  //			consumerServiceDescriptor
98  //					.setConsumerInvocationHandler(consumerInvocationHandler);
99  //
100 //			// create corbaImpl
101 //			Class corbaImplClass = serverCorbaClassesHolder.getCorbaImplClass();
102 //			Constructor corbaImplConstructor = getConstructorWithConsumerInvocationHandler(corbaImplClass);
103 //			Object corbaImpl = newObject(corbaImplConstructor,
104 //					new Object[] { consumerInvocationHandler });
105 //			LOG.debug("new corbaImpl=" + corbaImpl);
106 //
107 //			// creating orb
108 //			ORB orb = ORB.init((String[]) null, consumerServiceDescriptor
109 //					.getOrbProperties());
110 //			LOG.debug("ORB.init ... ok");
111 //			consumerServiceDescriptor.setOrb(orb);
112 //
113 //			// creating poatie
114 //			Constructor constructor = null;
115 //			Servant poaTie = null;
116 //			
117 //			if (serverCorbaClassesHolder.isGenerateClassesFromIDL()) {
118 //				
119 //				// registering value factory for value types
120 //				Map<String, java.lang.Object> map = consumerServiceDescriptor
121 //						.getServerCorbaClassesHolder()
122 //						.getValueTypeIdAndInstance();
123 //
124 //				if (map == null || map.size() == 0) {
125 //					LOG
126 //							.info("CRB000130_No_value_type_factory_to_register_using_the_ORB");
127 //				} else {
128 //					LOG.debug("ValueTypeFactoryMap.size =" + map.size());
129 //
130 //					// registering all valuetypes factories.
131 //					for (String id : map.keySet()) {
132 //						ValueFactory vf = (ValueFactory) map.get(id);
133 //
134 //						LOG.debug("Registering a ValueType Factory. id=" + id
135 //								+ "; instance=" + vf);
136 //
137 //						((org.omg.CORBA_2_3.ORB) orb).register_value_factory(
138 //								id, vf);
139 //					}
140 //				}
141 //
142 //				constructor = getConstructorFromPoaTie(serverCorbaClassesHolder
143 //						.getCorbaPOATie(), new Class[] {
144 //						serverCorbaClassesHolder.getCorbaOperations(),
145 //						POA.class });
146 //
147 //				LOG.debug("getConstructorFromPoaTie ... ok");
148 //
149 //				POA rootPoa;
150 //				try {
151 //					rootPoa = POAHelper.narrow(orb
152 //							.resolve_initial_references("RootPOA"));
153 //				} catch (InvalidName e) {
154 //					Object[] args = new Object[] { orb, e.getMessage() };
155 //
156 //					LOG.error(
157 //							"CRB000722_Unable_to_instantiate_resolve_RootPOA",
158 //							args, e);
159 //					throw new ServiceCreationException(
160 //							"CRB000722_Unable_to_instantiate_resolve_RootPOA",
161 //							args, e);
162 //				}
163 //
164 //				poaTie = newPoaTie(constructor, new Object[] { corbaImpl,
165 //						rootPoa });
166 //				LOG.debug("newPoaTie ... ok. Servant=" + poaTie);
167 //			} else {
168 //				constructor = getConstructorFromPoaTie(serverCorbaClassesHolder
169 //						.getCorbaPOATie(), new Class[] {});
170 //				LOG.debug("getDefaultConstructorFromPoaTie ... ok");
171 //
172 //				Object[] arguments = new Object[] {};
173 //				poaTie = newPoaTie(constructor, arguments);
174 //				LOG.debug("newPoaTie ... ok. Servant=" + poaTie);
175 //
176 //				// setOrb
177 //				setOrbOnPoaTie(poaTie, serverCorbaClassesHolder
178 //						.getCorbaPOATie(), orb);
179 //				LOG.debug("setup ORB on the Servant ... done");
180 //
181 //				// setTarget
182 //				setTargetOnPoaTie(poaTie, serverCorbaClassesHolder
183 //						.getCorbaPOATie(), corbaImpl); // Impl
184 //			}
185 //
186 //			LOG
187 //					.debug("Registering the Corba Servant Implementation on the Servant(TIE)"
188 //							+ "... done");
189 //
190 //			consumerServiceDescriptor.setPoaTie(poaTie);
191 //	} finally {
192 //		Thread.currentThread().setContextClassLoader(oldClassLoader);
193 //	}
194 //  }
195     /**
196      * This method is used to create the JBI service.
197      *
198      * @param consumerServiceDescriptor
199      *            The consumer service descriptor
200      * @throws ServiceCreationException
201      *             The service creation exception
202      */
203     public void createJbiService(
204             ConsumerServiceDescriptor consumerServiceDescriptor)
205             throws ServiceCreationException {
206 
207         Constructor constructor = null;
208         Servant poaTie = null;
209 
210         // get the generated classes from the descriptor
211         ServerCorbaClassesHolder serverCorbaClassesHolder = consumerServiceDescriptor.getServerCorbaClassesHolder();
212 
213         // different behaviour if the classes were generated from IDL
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                 //aggiungo jacorb.implname per gestire la persistenza.
224                 //La aggiungo sempre tanto non fa conlitto quando non si usa jacorb
225 
226                 // creating orb
227                 Properties props = consumerServiceDescriptor.getOrbProperties();
228                 if (consumerServiceDescriptor.isPersistent()) {
229                     //so far this works only with jacorb
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                 // create and set up the invocation handler
249                 ConsumerInvocationHandler consumerInvocationHandler = new ConsumerInvocationHandler(
250                         consumerServiceDescriptor);
251                 consumerServiceDescriptor.setConsumerInvocationHandler(consumerInvocationHandler);
252 
253                 // create corbaImpl
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                 // registering value factory for value types
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                     // registering all valuetypes factories.
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                         /* create policies */
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                         /* create POA */
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                         //must manually activate the object for the POA with an ID
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             // creating orb
357             ORB orb = ORB.init((String[]) null, consumerServiceDescriptor.getOrbProperties());
358             LOG.debug("ORB.init ... ok");
359             consumerServiceDescriptor.setOrb(orb);
360 
361             // create and set up the invocation handler
362             ConsumerInvocationHandler consumerInvocationHandler = new ConsumerInvocationHandler(
363                     consumerServiceDescriptor);
364             consumerServiceDescriptor.setConsumerInvocationHandler(consumerInvocationHandler);
365 
366             // create corbaImpl
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             // setOrb
380             setOrbOnPoaTie(poaTie, serverCorbaClassesHolder.getCorbaPOATie(),
381                     orb);
382             LOG.debug("setup ORB on the Servant ... done");
383 
384             // setTarget
385             setTargetOnPoaTie(poaTie,
386                     serverCorbaClassesHolder.getCorbaPOATie(), corbaImpl); // Impl
387         }
388 
389         LOG.debug("Registering the Corba Servant Implementation on the Servant(TIE)" + "... done");
390 
391         consumerServiceDescriptor.setPoaTie(poaTie);
392     }
393 
394     /**
395      * This method is used to set up the target class that the skeleton delegates
396      * all the corba operations.
397      *
398      * @param   poaTie        The instance of the skeleton.
399      * @param   poaTieClass   The class of the skeleton.
400      * @param   poaDelegate   The delegate class.
401      *
402      * @throws  ServiceCreationException
403      *              When the 'getMethod' or the 'invoke' via reflection cause an
404      *              error.
405      */
406     protected void setTargetOnPoaTie(
407             Servant poaTie,
408             Class poaTieClass,
409             Object poaDelegate) throws ServiceCreationException {
410 
411         // create
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         // invoke
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      * This method is used to set up the ORB in the skeleton.
476      *
477      * @param   poaTie        The instance of the skeleton.
478      * @param   poaTieClass   The class of the skeleton.
479      * @param   orb           The ORB.
480      *
481      * @throws  ServiceCreationException
482      *              When the 'getMethod' or the 'invoke' via reflection cause an
483      *              error.
484      */
485     protected void setOrbOnPoaTie(
486             Servant poaTie,
487             Class poaTieClass,
488             ORB orb) throws ServiceCreationException {
489 
490         // create
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         // invoke
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      * This method is used to create the default constructor of the Poa Tie Class.
554      *
555      * @param   poaTieClass   The skeleton.
556      *
557      * @return  The default constructor of the skeleton.
558      *
559      * @throws  ServiceCreationException
560      *              When the 'getConstructor' via reflection causes an error.
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      * This method is used to create the constructor where the input parameter
592      * is a ConsumerInvocationHandler.
593      *
594      * @param   clazz   The class that contains the constructor.
595      *
596      * @return  The constructor.
597      *
598      * @throws  ServiceCreationException
599      *              When the 'getConstructor' via reflection causes an error.
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      * This method creates the Servant using the constructor passed in input.
632      *
633      * @param   constructor     The default constructor of the skeleton.
634      *
635      * @return  The Servant created.
636      *
637      * @throws  ServiceCreationException
638      *              When the 'new' via reflection causes an error.
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      * This method creates an Object.
684      *
685      * @param   constructor     A constructor of a class.
686      * @param   parameter       The parameter for the constructor.
687      *
688      * @return  The Object created.
689      *
690      * @throws  ServiceCreationException
691      *              When the 'new' via reflection causes an error.
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 //------------------------------------------------------------------------------ PRE
736     /**
737      * @param consumerServiceDescriptor  The consumer service descriptor
738      * @throws ServiceCreationException  The service creation exception
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         // creating poa delegate
753         Object poaDelegate = Proxy.newProxyInstance(
754                 serverCorbaClassesHolder.getCorbaPOATie().getClassLoader(),
755                 new Class[]{serverCorbaClassesHolder.getCorbaOperations()},
756                 consumerInvocationHandler);
757 
758         // creating orb
759         ORB orb = ORB.init(
760                 (String[]) null, consumerServiceDescriptor.getOrbProperties());
761         consumerServiceDescriptor.setOrb(orb);
762         // creating poa tie
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         // registering value factory for value types
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             // registering all valuetypes factories.
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 //------------------------------------------------------------------------------ PRE
856 
857     /**
858      * @param consumerServiceDescriptor    The consumer service descriptor
859      * @throws ServiceActivationException  The service activation exception
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         // Check localization type
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             // TODO Get real error code
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             // print stringified object reference to file
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         // get the root naming context
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         // Use NamingContextExt which is part of the Interoperable
960         // Naming Service specification.
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         // bind the Object Reference in Naming
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 }