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.Jbi4CorbaRuntimeException;
13  import it.imolinfo.jbi4corba.jbi.Messages;
14  import it.imolinfo.jbi4corba.jbi.component.Jbi4CorbaSUManager;
15  import it.imolinfo.jbi4corba.jbi.endpoint.Jbi4CorbaEndpoint;
16  import it.imolinfo.jbi4corba.jbi.endpoint.ProviderEndpoint;
17  import it.imolinfo.jbi4corba.utils.HelperEPRUtils;
18  import it.imolinfo.jbi4corba.webservice.descriptor.ProviderServiceDescriptor;
19  import it.imolinfo.jbi4corba.webservice.generator.AnyType;
20  import it.imolinfo.jbi4corba.webservice.generator.AnyTypeWrapper;
21  import it.imolinfo.jbi4corba.webservice.generator.InterfaceType;
22  import it.imolinfo.jbi4corba.webservice.generator.MethodSignature;
23  import it.imolinfo.jbi4corba.webservice.generator.Param;
24  import it.imolinfo.jbi4corba.webservice.generator.SearchedType;
25  import it.imolinfo.jbi4corba.webservice.generator.TypeUtils;
26  import it.imolinfo.jbi4corba.webservice.generator.UnionType;
27  import it.imolinfo.jbi4corba.webservice.generator.UnionTypeUtils;
28  import it.imolinfo.jbi4corba.webservice.generator.typedef.TypeDef;
29  import it.imolinfo.jbi4corba.webservice.generator.typedef.TypeDefUtil;
30  
31  import java.lang.reflect.Array;
32  import java.lang.reflect.Field;
33  import java.lang.reflect.InvocationTargetException;
34  import java.lang.reflect.Method;
35  import java.lang.reflect.Modifier;
36  import java.math.BigDecimal;
37  import java.math.BigInteger;
38  import java.net.URLClassLoader;
39  import java.util.ArrayList;
40  import java.util.Arrays;
41  import java.util.HashSet;
42  import java.util.List;
43  import java.util.Set;
44  
45  import javax.xml.namespace.QName;
46  import javax.xml.ws.Holder;
47  import javax.xml.ws.wsaddressing.W3CEndpointReference;
48  import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;
49  
50  import org.apache.commons.lang.ArrayUtils;
51  import org.apache.commons.lang.builder.ToStringBuilder;
52  import org.omg.CORBA.Any;
53  import org.omg.CORBA.AnySeqHelper;
54  import org.omg.CORBA.BooleanSeqHelper;
55  import org.omg.CORBA.CharSeqHelper;
56  import org.omg.CORBA.DoubleSeqHelper;
57  import org.omg.CORBA.FloatSeqHelper;
58  import org.omg.CORBA.LongLongSeqHelper;
59  import org.omg.CORBA.LongSeqHelper;
60  import org.omg.CORBA.ORB;
61  import org.omg.CORBA.OctetSeqHelper;
62  import org.omg.CORBA.ShortSeqHelper;
63  import org.omg.CORBA.StringSeqHelper;
64  import org.omg.CORBA.TCKind;
65  import org.omg.CORBA.TypeCode;
66  import org.omg.CORBA.ULongLongSeqHelper;
67  import org.omg.CORBA.ULongSeqHelper;
68  import org.omg.CORBA.UShortSeqHelper;
69  import org.omg.CORBA.WCharSeqHelper;
70  import org.omg.CORBA.WStringSeqHelper;
71  import org.omg.CORBA.TypeCodePackage.BadKind;
72  import org.omg.CORBA.portable.InputStream;
73  
74  /**
75   * This class provides utility functions to transform objects from jax-ws
76   * objects to Corba objects and vice-a-versa
77   */
78  public class CorbaTransformationUtils {
79  
80      /**
81       * Logger.
82       */
83      private static final Logger LOG = LoggerFactory.getLogger(CorbaTransformationUtils.class);
84      /**
85       * The responsible to translate localized messages.
86       */
87      private static final Messages MESSAGES = Messages.getMessages(CorbaTransformationUtils.class);
88  
89      /**
90       * changeFromCorbaToServiceObjects - Transform Corba objects to jax-ws
91       * objects in operation return type and exceptions
92       *
93       *
94       * @param corbaObject
95       * @param runtimeInfo
96       * @param objectType
97       * @return
98       * @throws SecurityException
99       * @throws IllegalArgumentException
100      * @throws ClassNotFoundException
101      * @throws NoSuchFieldException
102      * @throws IllegalAccessException
103      * @throws InvocationTargetException
104      * @throws InstantiationException
105      * @throws NoSuchMethodException
106      */
107     public static Object changeFromCorbaToServiceObject(Object corbaObject,
108             RuntimeInformation runtimeInfo, Class objectType)
109             throws SecurityException, IllegalArgumentException,
110             ClassNotFoundException, NoSuchFieldException,
111             IllegalAccessException, InvocationTargetException,
112             InstantiationException, NoSuchMethodException {
113 
114         if (corbaObject == null) {
115             return null;
116         }
117 
118         Object crbObject = corbaObject;
119 
120         Object serviceObject = null;
121         int dimensions[] = null;
122         LOG.debug("ObjectType ==>" + objectType);
123 
124         if (TypeUtils.isPrimitive(objectType.getName()) || TypeUtils.isJavaType(objectType.getName()) || TypeUtils.isPrimitiveParamSign(objectType.getName())) {
125             return corbaObject;
126         }
127 
128         if (TypeUtils.isEnumType(objectType, runtimeInfo.getAllEnumTypes().keySet())) {
129             return transformEnumType(corbaObject, runtimeInfo.getCorbaClassLoader(),
130                     runtimeInfo.getServiceClassLoader());
131         }
132 
133         boolean returnTypeIsArray = corbaObject.getClass().isArray();
134         if (returnTypeIsArray) {
135             dimensions = TypeUtils.getObjectDimensions(corbaObject);
136             LOG.debug("ISARRAY" + corbaObject.getClass().getName());
137 
138         }
139 
140         LOG.debug(runtimeInfo.getAllCorbaTypes().toString());
141 
142         SearchedType sType = TypeUtils.isSearchedTypeAll(TypeUtils.getTypeNameWithoutBrackets(objectType), false, runtimeInfo.getAllCorbaTypes());
143         if (sType == null) {
144             if (AnyType.isAnyType(TypeUtils.getTypeNameWithoutBrackets(objectType))) {
145                 sType = new AnyType();
146             }
147         }
148         if (sType != null) {
149             // transform
150 
151             if (sType instanceof UnionType) {
152                 String utWrapper = sType.getTypeName() + "Wrapper";
153                 Class utWrapperClass = runtimeInfo.getServiceClassLoader().loadClass(utWrapper);
154                 if (returnTypeIsArray) {
155                     serviceObject = Array.newInstance(utWrapperClass,
156                             dimensions);
157                 } else {
158                     serviceObject = utWrapperClass.newInstance();
159                 }
160                 transformFromCorbaType(serviceObject, sType, crbObject,
161                         runtimeInfo, true, null, null);
162             }
163             if (sType instanceof InterfaceType) {
164 
165                 serviceObject = objectToEPR(crbObject, runtimeInfo.getJbi4CorbaEndpoint(), objectType);
166             }
167             if (sType instanceof AnyType) {
168                 AnyTypeWrapper anyWr = new AnyTypeWrapper();
169                 transformFromCorbaType(anyWr, sType, crbObject, runtimeInfo,
170                         true, null, null);
171                 serviceObject = anyWr.getValue();
172             }
173         } else {
174             // recursive comparish
175             if (returnTypeIsArray) {
176                 serviceObject = Array.newInstance(
177                         runtimeInfo.getServiceClassLoader().loadClass(
178                         TypeUtils.getTypeNameWithoutBrackets(objectType)),
179                         dimensions);
180             } else {
181                 serviceObject = runtimeInfo.getServiceClassLoader().loadClass(objectType.getName()).newInstance();
182             }
183             LOG.debug("Modified Object===>" + serviceObject.getClass());
184             LOG.debug("Orig Object===>" + crbObject.getClass());
185             LOG.debug("Modified Object===>" + serviceObject.getClass().getClassLoader());
186             LOG.debug("Orig Object===>" + crbObject.getClass().getClassLoader());
187             buildServiceObjectfromCorbaObject(serviceObject, crbObject, runtimeInfo);          
188         }
189         return serviceObject;
190     }
191 
192     /**
193      * recursiveInvokationAdapter - Transform jax-ws objects to Corba objects in
194      * operation parameters
195      *
196      * @param serviceParams
197      * @param runtimeInfo
198      * @param methodSignature
199      * @return
200      * @throws ClassNotFoundException
201      * @throws InstantiationException
202      * @throws IllegalAccessException
203      * @throws SecurityException
204      * @throws NoSuchFieldException
205      * @throws IllegalArgumentException
206      * @throws InvocationTargetException
207      * @throws NoSuchMethodException
208      */
209     public static Object[] changeFromServiceToCorbaObjects(Object[] serviceParams,
210             RuntimeInformation runtimeInfo, MethodSignature methodSignature)
211             throws ClassNotFoundException, InstantiationException,
212             IllegalAccessException, SecurityException, NoSuchFieldException,
213             IllegalArgumentException, InvocationTargetException,
214             NoSuchMethodException {
215         int idx = 0;
216         Object[] corbaParams = new Object[serviceParams.length];
217         for (Param paramSig : methodSignature.getParameters()) {
218 
219             LOG.debug("RecursiveInvokationAdapter Params ==>" + serviceParams.length);
220             LOG.debug(" Paramsig ==>" + paramSig.toString());
221             LOG.debug(" Paramsig ==>" + paramSig.getName());
222             LOG.debug(" Paramsig ==>" + paramSig.getTypeName());
223             LOG.debug(" OriginalClassLoader==>" + runtimeInfo.getCorbaClassLoader());
224             LOG.debug("Params ClassLoader" + serviceParams.getClass().getClassLoader());            
225             if (paramSig.isHolder()) {
226                 // holders are already treated
227                 corbaParams[idx] = serviceParams[idx];
228 
229             } else {
230                 corbaParams[idx] = changeFromServiceToCorbaObject(serviceParams[idx], runtimeInfo, paramSig.getType());
231             }
232             idx++;
233         }
234 
235         return corbaParams;
236     }
237 
238     /**
239      * recursiveInvokationAdapter - Transform jax-ws objects to Corba objects in
240      * operation parameters
241      * @TODO: ADD A CORRECT AND MORE MEANINGFUL COMMENT!!!!!!!!!!
242      *
243      * @param corbaParams
244      * @param runtimeInfo
245      * @param methodSignature
246      * @return
247      * @throws ClassNotFoundException
248      * @throws InstantiationException
249      * @throws IllegalAccessException
250      * @throws SecurityException
251      * @throws NoSuchFieldException
252      * @throws IllegalArgumentException
253      * @throws InvocationTargetException
254      * @throws NoSuchMethodException
255      */
256     public static Object[] changeFromCorbaToServiceObjects(Object[] corbaParams,
257             RuntimeInformation runtimeInfo, MethodSignature methodSignature)
258             throws ClassNotFoundException, InstantiationException,
259             IllegalAccessException, SecurityException, NoSuchFieldException,
260             IllegalArgumentException, InvocationTargetException,
261             NoSuchMethodException {
262         int idx = 0;
263         Object[] serviceParams = new Object[corbaParams.length];
264         for (Param paramSig : methodSignature.getParameters()) {
265 
266             Object serviceObject = null;
267             int dimensions[] = null;
268 
269             LOG.debug("ObjectType ==>" + paramSig.getType());
270 
271             if (paramSig.isHolder()) {
272                 // holders are already treated
273                 serviceParams[idx] = corbaParams[idx];
274             } else {
275                 serviceParams[idx] = changeFromCorbaToServiceObject(corbaParams[idx], runtimeInfo, paramSig.getType());
276             }
277             idx++;
278         }
279 
280         return serviceParams;
281     }
282 
283     /**
284      * changeFromCorbaToServiceObjects - Transform Corba objects to jax-ws
285      * objects in operation return type and exceptions
286      *
287      *
288      * @param res
289      * @param runtimeInfo
290      * @param objectType
291      * @return
292      * @throws SecurityException
293      * @throws IllegalArgumentException
294      * @throws ClassNotFoundException
295      * @throws NoSuchFieldException
296      * @throws IllegalAccessException
297      * @throws InvocationTargetException
298      * @throws InstantiationException
299      * @throws NoSuchMethodException
300      */
301     public static Object changeFromServiceToCorbaObject(Object serviceObject,
302             RuntimeInformation runtimeInfo, Class objectType)
303             throws SecurityException, IllegalArgumentException,
304             ClassNotFoundException, NoSuchFieldException,
305             IllegalAccessException, InvocationTargetException,
306             InstantiationException, NoSuchMethodException {
307 
308         if (serviceObject == null) {
309             return null;
310         }
311 
312         if (TypeUtils.isPrimitive(objectType.getName()) || TypeUtils.isJavaType(objectType.getName()) || TypeUtils.isPrimitiveParamSign(objectType.getName())) {
313             return serviceObject;
314         } else if (TypeUtils.isEnumType(objectType, runtimeInfo.getAllEnumTypes().keySet())) {
315             return transformEnumType(serviceObject, runtimeInfo.getServiceClassLoader(), runtimeInfo.getCorbaClassLoader());
316         } else {
317 
318 
319             Class corbaClass = null;
320             if (AnyType.isAnyType(TypeUtils.getTypeNameWithoutBrackets(objectType))) {
321                 corbaClass = Any.class;
322             } else {
323                 corbaClass = runtimeInfo.getCorbaClassLoader().loadClass(
324                         TypeUtils.getTypeNameWithoutBrackets(objectType));
325             }
326             if (LOG.isDebugEnabled()) {
327                 if (serviceObject == null) {
328                     LOG.debug(" =========> SERVICE OBJECT NULL <========");
329                 }
330             }
331             
332             // MARCO: eì qua il problema...invece di essere un array di Any
333             // Deve essere un ANY con dentro un array!
334             // ut alla fine di questo metodo è corretto (è un'ANY), mentre crbObject no.
335             LOG.debug(" ====>" + serviceObject.getClass().getName());
336             LOG.debug(" ====>" + serviceObject.getClass().getClassLoader());
337 
338             LOG.debug("OriginalClass ==>" + corbaClass.toString());
339             LOG.debug("ModifiedClass ==>" + serviceObject.toString());
340 
341             Object crbObject = null;
342             
343             if (serviceObject.getClass().isArray()) {
344                 if (AnyType.isAnyType(TypeUtils.getTypeNameWithoutBrackets(objectType))) {
345                 	LOG.debug("IS ANY, we have to put an array inside an ANY");
346                     // crbObject = new Any[(Array.getLength(serviceObject)).length];
347                 	crbObject = runtimeInfo.getOrb().create_any();
348                 } else {
349                     int dimentions[] = TypeUtils.getObjectDimensions(serviceObject);
350                     crbObject = Array.newInstance(corbaClass, dimentions);
351                 }
352                 
353             } else {
354                 if (AnyType.isAnyType(TypeUtils.getTypeNameWithoutBrackets(objectType))) {
355                     crbObject = runtimeInfo.getOrb().create_any();
356                 } else {
357                     try {
358                         crbObject = corbaClass.newInstance();
359                     } catch (InstantiationException e) {
360                         LOG.debug("errore istanziando corba object", e);
361                     }
362                 }
363             }
364             
365             LOG.debug("OrigiObject ==>" + crbObject);
366 
367             boolean isArray = objectType.isArray();
368             LOG.debug("objectType.isArray() ==>" + isArray);
369             LOG.debug("objectType.getName() ==>" + objectType.getName());
370             
371             SearchedType ut = TypeUtils.isSearchedTypeAll(objectType.getName(), isArray,
372                     runtimeInfo.getAllCorbaTypes());
373             
374             LOG.debug("ut: ==>" + ut);
375             if (ut == null) {
376                 if (AnyType.isAnyType(TypeUtils.getTypeNameWithoutBrackets(objectType))) {
377                     ut = new AnyType();
378                 }
379             }
380             if (ut != null) {
381                 // transform
382                 LOG.debug("Transform=====>" + crbObject);
383                 LOG.debug("CLASSLOADER=====>" + serviceObject.getClass().getClassLoader());
384 
385                 if (ut instanceof InterfaceType) {
386                     String ior = null;
387                     if (serviceObject != null && serviceObject instanceof W3CEndpointReference) {
388                         W3CEndpointReference epr = (W3CEndpointReference) serviceObject;
389 
390                         LOG.debug("Document Fragment EPR ===> " + epr.toString());
391                         ior = HelperEPRUtils.readAddressFromEPR(epr.toString());
392                         LOG.debug("IOR ===> " + ior);
393 
394                     }
395 
396                     LOG.debug("IOR ===> " + ior);
397                     // Load Helper and Do the narrow
398                     // Class helperClass =
399                     // originalClassLoader.loadClass(origFields[i].getType().getName()
400                     // + "Helper");
401                     if (!ior.equals("")) {
402                         Class helperClass = loadHelperbyName(
403                                 corbaClass.getName(),
404                                 runtimeInfo.getCorbaClassLoader());
405                         crbObject = narrowObjbyIOR(helperClass,
406                                 ior, runtimeInfo.getOrb());
407                     }
408                 } else {
409                     LOG.debug("ut before transformToCorbaType ==>" + ut);
410                     transformToCorbaType(crbObject, ut, serviceObject,
411                             runtimeInfo, true);
412                 }
413             } else {
414                 // recursive comparish
415                 LOG.debug("Fill=====>" + crbObject);
416                 LOG.debug("CLASSLOADER=====>" + serviceObject.getClass().getClassLoader());
417 
418                 if (TypeUtils.isPrimitive(crbObject.getClass().getName()) || TypeUtils.isJavaType(crbObject.getClass().getName()) || TypeUtils.isPrimitiveParamSign(crbObject.getClass().getName())) {
419                     crbObject = serviceObject;
420                 } else if (TypeUtils.isEnumType(crbObject.getClass(),
421                         runtimeInfo.getAllEnumTypes().keySet())) {
422                     crbObject = transformEnumType(serviceObject, runtimeInfo.getServiceClassLoader(), runtimeInfo.getCorbaClassLoader());
423                 } else {
424                     buildCorbaObjectfromServiceObject(crbObject, serviceObject,
425                             runtimeInfo);
426                 }
427             }
428             return crbObject;
429         }
430 
431     }
432 
433     /**
434      * Change the Holder params for the correct servant invocation.
435      *
436      * @param methodSignature
437      * @param params
438      * @param runtimeInfo
439      * @return
440      * @throws InstantiationException
441      * @throws IllegalAccessException
442      * @throws SecurityException
443      * @throws NoSuchFieldException
444      * @throws ClassNotFoundException
445      * @throws IllegalArgumentException
446      * @throws InvocationTargetException
447      * @throws NoSuchMethodException
448      */
449     public static Object[] changeHoldersFromServiceToCorbaObjects(
450             MethodSignature methodSignature, List<Object> params,
451             RuntimeInformation runtimeInfo) throws InstantiationException,
452             IllegalAccessException, SecurityException, NoSuchFieldException,
453             ClassNotFoundException, IllegalArgumentException,
454             InvocationTargetException, NoSuchMethodException {
455 
456         // This method :
457         // - for each param, get the holder value (casting to the method real
458         // type)
459         // - Instanciate the CORBA Holder param
460         // - Set the value into the CORBA Holder param
461         // -Return the Object array for the CORBA invocation
462 
463         ArrayList<Object> corbaParams = new ArrayList<Object>();
464 
465         for (int i = 0; i < methodSignature.getParameters().size(); i++) {
466             Param paramSig = (Param) methodSignature.getParameters().get(i);
467 
468             // If holer, the valuemust be copied.
469             Object value = null;
470             if (paramSig.isHolder()) {
471 
472                 // Reads ths jaxws holder value
473                 Holder jaxwsHolder = (Holder) params.get(i);
474                 if (jaxwsHolder.value == null) {
475                     LOG.debug("Holder is Null!!!!");
476                     //LOG.debug("--> NULL AND ARRAY "+paramSig.getHolderValueType().isArray());
477                     if (paramSig.getHolderValueType().isArray()) {
478                         // LOG.debug("--> NULL AND ARRAY "+paramSig.getType().isArray());
479                         value = Array.newInstance(paramSig.getType(), 0);
480                     }
481                 } else {
482                     value = jaxwsHolder.value;
483                 }
484                 Object corbaHolder = null;
485                 Object actualHolderValueObject = null;
486 
487                 Class corbaHolderType = runtimeInfo.getCorbaClassLoader().loadClass(paramSig.getHolderType().getName());
488 
489                 corbaHolder = corbaHolderType.newInstance();
490 
491                 // Gets the class value field and sets it as accessible
492                 Field corbaHolderValueField = corbaHolderType.getField("value");
493                 corbaHolderValueField.setAccessible(true);
494 
495                 if (TypeUtils.isPrimitive(paramSig.getHolderValueType().getName()) || TypeUtils.isJavaType(paramSig.getHolderValueType().getName()) || TypeUtils.isPrimitiveParamSign(paramSig.getHolderValueType().getName())) {
496                     LOG.debug("Holder value type is PRIMITIVE or JAVA class");
497                     actualHolderValueObject = value;
498                 } else if (TypeUtils.isEnumType(paramSig.getHolderValueType(),
499                         runtimeInfo.getAllEnumTypes().keySet())) {
500                     actualHolderValueObject = transformEnumType(value,
501                             runtimeInfo.getServiceClassLoader(), runtimeInfo.getCorbaClassLoader());
502                 } else {
503                     // check if holder has searchedType as value
504                     String holderValueTypeStr = TypeUtils.getTypeNameWithoutBrackets(paramSig.getHolderValueType());
505                     SearchedType sType = TypeUtils.isSearchedTypeAll(
506                             holderValueTypeStr, false, runtimeInfo.getAllCorbaTypes());
507                     if (AnyType.isAnyType(holderValueTypeStr)) {
508                         sType = new AnyType();
509                     }
510 
511 //					LOG.debug("Read value: " + value + " of class: "
512 //							+ value.getClass().getName() + "ClassLoader:"
513 //							+ value.getClass().getClassLoader());
514                     // Gets the corba holder class, loading it with the actual
515                     // classLoader.
516                     if (sType instanceof InterfaceType) {
517                         String ior = null;
518                         if (value instanceof W3CEndpointReference && value != null) {
519                             W3CEndpointReference epr = (W3CEndpointReference) value;
520 
521                             LOG.debug("Document Fragment EPR ===> " + epr.toString());
522                             ior = HelperEPRUtils.readAddressFromEPR(epr.toString());
523                             LOG.debug("IOR ===> " + ior);
524 
525                         }
526 
527                         LOG.debug("IOR ===> " + ior);
528                         // Load Helper and Do the narrow
529                         // Class helperClass =
530                         // originalClassLoader.loadClass(origFields[i].getType().getName()
531                         // + "Helper");
532                         if (!ior.equals("")) {
533                             Class helperClass = loadHelperbyName(
534                                     corbaHolderValueField.getType().getName(),
535                                     runtimeInfo.getCorbaClassLoader());
536                             actualHolderValueObject = narrowObjbyIOR(helperClass,
537                                     ior, runtimeInfo.getOrb());
538                         }
539 
540                     } else {
541 
542                         if (sType instanceof AnyType) {
543                             actualHolderValueObject = runtimeInfo.getOrb().create_any();
544                         } else {
545                             Class origHolderValueClass = runtimeInfo.getCorbaClassLoader().loadClass(
546                                     TypeUtils.getTypeNameWithoutBrackets(corbaHolderValueField.getType()));
547 
548                             if (value.getClass().isArray()) {
549 
550                                 //Added to manage ampty array
551                                 if (value == null) {
552                                     actualHolderValueObject = Array.newInstance(
553                                             origHolderValueClass, 0);
554                                     LOG.debug("!!!!!>>> Is Empty array: ");
555                                 } /*********************************************/
556                                 else {
557                                     int dimentions[] = TypeUtils.getObjectDimensions(value);
558                                     actualHolderValueObject = Array.newInstance(
559                                             origHolderValueClass, dimentions);
560                                     if (LOG.isDebugEnabled()) {
561                                     	LOG.debug("!!!!!>>> Is array: " + Arrays.toString(dimentions));
562                                     }
563                                 }
564 
565                             } else {
566                                 actualHolderValueObject = origHolderValueClass.newInstance();
567                             }
568 
569                         }
570                         // Gets ths actual holder, set it as the value and sets
571                         // to the "value" corbaholder field.
572                         LOG.debug("!!!!!!!!! >>>>>orig holder: " + actualHolderValueObject);
573                         LOG.debug("!!!!!!!!! >>>>>mod holder: " + value);
574                         if (sType != null) {
575                             transformToCorbaType(actualHolderValueObject,
576                                     sType, value, runtimeInfo, true);
577                         } else {
578                             if (TypeUtils.isPrimitive(actualHolderValueObject.getClass().getName()) || TypeUtils.isJavaType(actualHolderValueObject.getClass().getName()) || TypeUtils.isPrimitiveParamSign(actualHolderValueObject.getClass().getName())) {
579                                 actualHolderValueObject = value;
580                             } else if (TypeUtils.isEnumType(
581                                     actualHolderValueObject.getClass(),
582                                     runtimeInfo.getAllEnumTypes().keySet())) {
583                                 actualHolderValueObject = transformEnumType(
584                                         value, runtimeInfo.getServiceClassLoader(),
585                                         runtimeInfo.getCorbaClassLoader());
586                             } else {
587                                 buildCorbaObjectfromServiceObject(
588                                         actualHolderValueObject, value,
589                                         runtimeInfo);
590                             }
591                         }
592                     }
593                 }
594                 corbaHolderValueField.set(corbaHolder, actualHolderValueObject);
595                                                
596                 corbaParams.add(corbaHolder);
597 
598             } else {                
599                 corbaParams.add(params.get(i));
600             }
601 
602         }
603         return corbaParams.toArray();
604     }
605 
606     /**
607      * Change the Holder params after the servant invocation. It's the opposite
608      * of method <code>changeHolderParamsBeforeServantInvocation</code>: get
609      * the Corba holder values and set it into the
610      * <code>javax.xml.ws.Holder</code> params elements.
611      *
612      *
613      * @param methodSignature
614      * @param params
615      * @param paramArrayAfterInvocation
616      * @param runtimeInfo
617      * @throws InstantiationException
618      * @throws IllegalAccessException
619      * @throws SecurityException
620      * @throws NoSuchFieldException
621      * @throws IllegalArgumentException
622      * @throws ClassNotFoundException
623      * @throws InvocationTargetException
624      * @throws NoSuchMethodException
625      */
626     public static void changeHoldersFromCorbaToServiceObjects(
627             MethodSignature methodSignature, List<Object> params,
628             Object[] paramArrayAfterInvocation, RuntimeInformation runtimeInfo)
629             throws InstantiationException, IllegalAccessException,
630             SecurityException, NoSuchFieldException, IllegalArgumentException,
631             ClassNotFoundException, InvocationTargetException,
632             NoSuchMethodException {
633 
634         int idx = 0;
635         for (int i = 0; i < methodSignature.getParameters().size(); i++) {
636             Param paramSig = (Param) methodSignature.getParameters().get(i);
637             // If holder, the value must be copied. If no holder, no action must
638             // be processed.
639             if (paramSig.isHolder()) {
640 
641                 // check if holder has searchedType as value
642                 String holderValueTypeStr = TypeUtils.getTypeNameWithoutBrackets(paramSig.getHolderValueType());
643                 SearchedType sType = TypeUtils.isSearchedTypeAll(
644                         holderValueTypeStr, false, runtimeInfo.getAllCorbaTypes());
645                 if (AnyType.isAnyType(holderValueTypeStr)) {
646                     sType = new AnyType();
647                 }
648 
649                 // Gets the actual value from the CORBA holder, for example the
650                 // String contained into the StringHolder.
651                 Field valueCorbaField = (paramArrayAfterInvocation[i]).getClass().getField("value");
652                 Object value = valueCorbaField.get(paramArrayAfterInvocation[i]);
653                 LOG.debug("corba holder value type: " + (value != null ? value.getClass() : "null value"));
654                 // Sets the value into the javax.xml.ws.Holder
655                 Holder holder = (Holder) params.get(i);
656                 // holder.value = value;
657 
658                 //null value handling
659                 if (value == null) {
660                     holder.value = null;
661                     continue;
662                 }
663 
664                 if (sType != null) {
665                     if (sType instanceof AnyType) {
666                         AnyTypeWrapper anyWr = new AnyTypeWrapper();
667                         transformFromCorbaType(anyWr, sType, value,
668                                 runtimeInfo, true, null, null);
669                         holder.value = anyWr.getValue();
670                     } //Added by Luca for bug fixing with EPR
671                     //when interface are used with holder rhe invocation return the request parameter
672                     //Now it seems fixed
673                     else if (sType instanceof InterfaceType) {
674 
675                         holder.value = objectToEPR(value, runtimeInfo.getJbi4CorbaEndpoint(), holder.getClass());
676                     } else {
677                         if (sType instanceof UnionType) {
678                             Class srvClass = runtimeInfo.getServiceClassLoader().loadClass(
679                                     holderValueTypeStr + "Wrapper");
680                             holder.value = srvClass.newInstance();
681                         }
682                         transformFromCorbaType(holder.value, sType, value,
683                                 runtimeInfo, true, null, null);
684                     }
685                 } else {
686                     if (TypeUtils.isPrimitive(value.getClass().getName()) || TypeUtils.isJavaType(value.getClass().getName()) || TypeUtils.isPrimitiveParamSign(value.getClass().getName())) {
687                         holder.value = value;
688                     } else if (TypeUtils.isEnumType(value.getClass(),
689                             runtimeInfo.getAllEnumTypes().keySet())) {
690                         holder.value = transformEnumType(value, runtimeInfo.getCorbaClassLoader(), runtimeInfo.getServiceClassLoader());
691                     } else {
692 
693                         if (value.getClass().isArray()) {
694                             holder.value = Array.newInstance(
695                                     runtimeInfo.getServiceClassLoader().loadClass(TypeUtils.getTypeNameWithoutBrackets(value.getClass())), TypeUtils.getObjectDimensions(value));
696                         } else {
697                             holder.value = runtimeInfo.getServiceClassLoader().loadClass(TypeUtils.getTypeNameWithoutBrackets(value.getClass())).newInstance();
698                         }
699                         buildServiceObjectfromCorbaObject(holder.value, value,
700                                 runtimeInfo);
701                     }
702                 // actuaHolder = value;
703                 }
704                 LOG.debug("Holder After---->" + holder.value.getClass());
705                 LOG.debug("Holder After---->" + holder.value.toString());
706                 paramArrayAfterInvocation[idx] = holder;
707             }
708             idx++;
709         }
710     }
711 
712     /**
713      * buildServiceObjectfromCorbaObject - Transform Corba objects to jax-ws objects in
714      * parameter
715      *
716      * @param serviceObject
717      * @param corbaObject
718      * @param runtimeInfo
719      * @throws ClassNotFoundException
720      * @throws SecurityException
721      * @throws NoSuchFieldException
722      * @throws IllegalArgumentException
723      * @throws IllegalAccessException
724      * @throws InvocationTargetException
725      * @throws InstantiationException
726      * @throws NoSuchMethodException
727      */
728     private static void buildServiceObjectfromCorbaObject(Object serviceObject,
729             Object corbaObject, RuntimeInformation runtimeInfo)
730             throws ClassNotFoundException, SecurityException,
731             NoSuchFieldException, IllegalArgumentException,
732             IllegalAccessException, InvocationTargetException,
733             InstantiationException, NoSuchMethodException {
734 
735         LOG.debug("ORIG OBJECT ==>" + corbaObject);
736         LOG.debug("MODIFIED OBJECT ==>" + serviceObject);
737 
738         if (serviceObject.getClass().isArray()) {
739             LOG.debug("!!!!>>>>>> IsARRAY");
740             Object[] corbaObjectArray = (Object[]) corbaObject;
741             Object[] serviceObjectArray = (Object[]) serviceObject;
742             LOG.debug("!!!!>>>>>> arrayLent: " + corbaObjectArray.length);
743             LOG.debug("!!!!>>>>>> component type: " + corbaObject.getClass().getComponentType());
744             for (int i = 0; i < corbaObjectArray.length; i++) {
745                 // Invertire Original con modified
746                 if (corbaObjectArray[i].getClass().isArray()) {
747                     serviceObjectArray[i] = Array.newInstance(
748                             runtimeInfo.getServiceClassLoader().loadClass(
749                             TypeUtils.getTypeNameWithoutBrackets(serviceObject.getClass())),
750                             TypeUtils.getObjectDimensions(corbaObjectArray[i]));
751                 } else if (TypeUtils.isEnumType(corbaObjectArray[i].getClass(),
752                         runtimeInfo.getAllEnumTypes().keySet())) {
753 
754                     serviceObjectArray[i] = transformEnumType(corbaObjectArray[i], runtimeInfo.getCorbaClassLoader(),
755                             runtimeInfo.getServiceClassLoader());
756 
757                 } else {
758                     serviceObjectArray[i] = serviceObject.getClass().getComponentType().newInstance();
759                 }
760                 // origObjectArray[i] =
761                 // origObject.getClass().getComponentType().newInstance();
762                 if (TypeUtils.isPrimitive(corbaObjectArray[i].getClass().getName()) || TypeUtils.isJavaType(corbaObjectArray[i].getClass().getName()) || TypeUtils.isPrimitiveParamSign(corbaObjectArray[i].getClass().getName())) {
763                     serviceObjectArray[i] = corbaObjectArray[i];
764                 } else if (TypeUtils.isEnumType(corbaObjectArray[i].getClass(),
765                         runtimeInfo.getAllEnumTypes().keySet())) {
766                     serviceObjectArray[i] = transformEnumType(
767                             corbaObjectArray[i], runtimeInfo.getCorbaClassLoader(), runtimeInfo.getServiceClassLoader());
768                 } else {
769                     buildServiceObjectfromCorbaObject(serviceObjectArray[i],
770                             corbaObjectArray[i], runtimeInfo);
771                 }
772                 LOG.debug("!!!!>>>>>> service component type object: " + serviceObjectArray[i]);
773             }
774 
775         } else {
776 
777             Class corbaClass = runtimeInfo.getCorbaClassLoader().loadClass(
778                     TypeUtils.getTypeNameWithoutBrackets(corbaObject.getClass()));
779             Class serviceClass = runtimeInfo.getServiceClassLoader().loadClass(
780                     TypeUtils.getTypeNameWithoutBrackets(serviceObject.getClass()));
781 
782             List<Field> allCrbFields = new ArrayList<Field>();
783             getAllFields(corbaClass, allCrbFields);
784             Field[] crbFields = allCrbFields.toArray(new Field[allCrbFields.size()]);
785             LOG.debug("ORIGFIELDS LENGHT" + crbFields.length);
786             LOG.debug("MODIFIED OBJECT" + serviceObject);
787 
788             LOG.debug("ORIG OBJECT" + corbaObject);
789             LOG.debug("MODIFIED CLASS" + serviceClass);
790             for (int i = 0; i < crbFields.length; i++) {
791                 crbFields[i].setAccessible(true);
792                 Field srvField = getField(serviceClass, crbFields[i].getName());// modClass.getDeclaredField(origFields[i].getName());
793                 srvField.setAccessible(true);
794                 LOG.debug("ORIG FIELD" + crbFields[i].getName());
795                 LOG.debug("MOD FIELD" + srvField.getName());
796                 LOG.debug("ORIG FIELD VALUE" + crbFields[i].get(corbaObject));
797 
798                 if (srvField.getName().equals(crbFields[i].getName())) {
799                     // field is primitive or java type
800                     LOG.debug(">>>>>>>>>>>" + crbFields[i].getType().getName());
801                     if (TypeUtils.isPrimitive(crbFields[i].getType().getName()) || TypeUtils.isJavaType(crbFields[i].getType().getName()) || TypeUtils.isPrimitiveParamSign(crbFields[i].getType().getName())) {
802 
803                         LOG.debug("PRIMITIVE");
804                         srvField.set(serviceObject, crbFields[i].get(corbaObject));
805 
806                     } else if (TypeUtils.isEnumType(crbFields[i].getType(),
807                             runtimeInfo.getAllEnumTypes().keySet())) {
808                         srvField.set(serviceObject, transformEnumType(
809                                 crbFields[i].get(corbaObject), runtimeInfo.getCorbaClassLoader(), runtimeInfo.getServiceClassLoader()));
810                     } // complex type
811                     else {
812 
813                         Class crbFieldClass = crbFields[i].getType();
814                         LOG.debug("FieldType====>" + crbFieldClass.getName());
815                         SearchedType sType = TypeUtils.isSearchedTypeAll(
816                                 TypeUtils.getTypeNameWithoutBrackets(crbFieldClass),
817                                 false, runtimeInfo.getAllCorbaTypes());
818                         if (AnyType.isAnyType(TypeUtils.getTypeNameWithoutBrackets(crbFieldClass))) {
819                             sType = new AnyType();
820                         }
821                         if (sType != null) {
822                             LOG.debug("CORBA ");
823                             if (sType instanceof InterfaceType) {
824                                  //5-01-2010 OSSSUPPORT 20 Fix
825                                 //After Fixing
826                                 //serviceObject = objectToEPR(crbFields[i].get(corbaObject), runtimeInfo.getJbi4CorbaEndpoint(), corbaClass);
827                                 //Managed intrefaceType on complexType
828                                 srvField.set(serviceObject, objectToEPR(crbFields[i].get(corbaObject), runtimeInfo.getJbi4CorbaEndpoint(), corbaClass));
829                             } else {
830                                 Object srvFieldObj = null;
831                                 if (sType instanceof AnyType) {
832                                     AnyTypeWrapper anyWr = new AnyTypeWrapper();
833 
834                                     transformFromCorbaType(anyWr, sType,
835                                             crbFields[i].get(corbaObject),
836                                             runtimeInfo, false, srvField,
837                                             serviceObject);
838 
839                                     srvFieldObj = anyWr.getValue();
840                                     srvField.set(serviceObject, srvFieldObj);
841                                 } else {
842                                     if (crbFieldClass.isArray()) {
843                                         srvFieldObj = Array.newInstance(
844                                                 runtimeInfo.getServiceClassLoader().loadClass(
845                                                 TypeUtils.getTypeNameWithoutBrackets(srvField.getType())),
846                                                 TypeUtils.getObjectDimensions(crbFields[i].get(corbaObject)));
847                                     } else {
848                                         srvFieldObj = runtimeInfo.getServiceClassLoader().loadClass(
849                                                 srvField.getType().getName()).newInstance();
850 
851                                     }
852                                     LOG.debug("Union field: " + srvField.getName() + " -> " + srvFieldObj);
853                                     transformFromCorbaType(srvFieldObj,
854                                             sType, crbFields[i].get(corbaObject),
855                                             runtimeInfo, false, srvField,
856                                             serviceObject);
857                                 }
858                             // modifField.set(modifiedObject,
859                             // modifFieldObj);
860 
861                             }
862                         } else {
863                             // complex type but not corba type
864                             LOG.debug("COMPLEX ");
865                             LOG.debug(" TYPE==>" + crbFields[i].getType());
866                             // ***
867 
868                             if (srvField.getType().getName().startsWith("[")) {
869                                 Object[] obj = (Object[]) crbFields[i].get(corbaObject);
870                                 LOG.debug("Modifield Fild Array Lenght" + obj.length);
871                                 srvField.set(serviceObject,
872                                         Array.newInstance(srvField.getType().getComponentType(),
873                                         obj.length));
874                             } else {
875                                 srvField.set(serviceObject, srvField.getType().newInstance());
876                             }
877                             //LOG.debug(crbFields[i].toString());
878                             if (crbFields[i].get(corbaObject) == null) {
879                                 continue;
880                             }
881                             if (TypeUtils.isPrimitive(crbFields[i].get(
882                                     corbaObject).getClass().getName()) || TypeUtils.isJavaType(crbFields[i].get(
883                                     corbaObject).getClass().getName()) || TypeUtils.isPrimitiveParamSign(crbFields[i].get(corbaObject).getClass().getName())) {
884                                 srvField.set(serviceObject, crbFields[i].get(corbaObject));
885                             } else if (TypeUtils.isEnumType(crbFields[i].get(
886                                     corbaObject).getClass(), runtimeInfo.getAllEnumTypes().keySet())) {
887                                 srvField.set(
888                                         serviceObject,
889                                         transformEnumType(
890                                         crbFields[i].get(corbaObject),
891                                         runtimeInfo.getCorbaClassLoader(),
892                                         runtimeInfo.getServiceClassLoader()));
893                             } else {
894                                 buildServiceObjectfromCorbaObject(srvField.get(serviceObject), crbFields[i].get(corbaObject), runtimeInfo);
895                             }
896                         }
897                     }
898 
899                 }
900             }
901 
902         }
903     }
904 
905     /**
906      * transformFromCorbaType Transform Corba objects to jax-ws objects (corba
907      * special types: Union, Interface, Any)
908      *
909      * @param serviceObject
910      * @param sType
911      * @param corbaObject
912      * @param runtimeInfo
913      * @param isParam
914      * @param parentField
915      * @param parentObject
916      * @throws SecurityException
917      * @throws NoSuchFieldException
918      * @throws IllegalArgumentException
919      * @throws IllegalAccessException
920      * @throws ClassNotFoundException
921      * @throws InvocationTargetException
922      * @throws InstantiationException
923      * @throws NoSuchMethodException
924      */
925     private static void transformFromCorbaType(Object serviceObject,
926             SearchedType sType, Object corbaObject,
927             RuntimeInformation runtimeInfo, boolean isParam, Field parentField,
928             Object parentObject) throws SecurityException,
929             NoSuchFieldException, IllegalArgumentException,
930             IllegalAccessException, ClassNotFoundException,
931             InvocationTargetException, InstantiationException,
932             NoSuchMethodException {
933         LOG.debug("Transform From Corba Type");
934         if (serviceObject.getClass().isArray()) {
935             LOG.debug("!!!!>>>>>> IsARRAY");
936             Object[] corbaObjectArray = (Object[]) corbaObject;
937             Object[] serviceObjectArray = (Object[]) serviceObject;
938             LOG.debug("!!!!>>>>>> arrayLent: " + corbaObjectArray.length);
939             LOG.debug("!!!!>>>>>> component type: " + corbaObject.getClass().getComponentType());
940             for (int i = 0; i < corbaObjectArray.length; i++) {
941                 // Invertire Original con modified
942                 serviceObjectArray[i] = serviceObject.getClass().getComponentType().newInstance();
943                 // origObjectArray[i] =
944                 // origObject.getClass().getComponentType().newInstance();
945                 transformFromCorbaType(serviceObjectArray[i], sType,
946                         corbaObjectArray[i], runtimeInfo, isParam, parentField,
947                         parentObject);
948                 LOG.debug("!!!!>>>>>> component type object: " + corbaObjectArray[i]);
949             }
950 
951         } else {
952             if (sType instanceof UnionType) {
953                 // transform from unionType
954                 Object fieldValue = serviceObject;
955                 Field utWrapperField = null;
956 
957                 Class srvObject = runtimeInfo.getServiceClassLoader().loadClass(
958                         TypeUtils.getTypeNameWithoutBrackets(serviceObject.getClass()));
959                 utWrapperField = srvObject.getDeclaredField(UnionTypeUtils.UNION_WRAPPER_FIELD);
960                 utWrapperField.setAccessible(true);
961                 // fieldValue = new Object();
962                 // utWrapperField.set(modifiedObject,fieldValue);
963 
964 
965                 for (String fieldName : ((UnionType) sType).getTypeFieldNameList()) {
966                     Class corbaObjectClass = runtimeInfo.getCorbaClassLoader().loadClass(
967                             TypeUtils.getTypeNameWithoutBrackets(corbaObject.getClass()));
968                     Method crbObjectMeth = corbaObjectClass.getMethod(fieldName);
969 
970                     try {
971                         Object unionFieldObject = crbObjectMeth.invoke(corbaObject);
972                         boolean isFound = false;
973                         if (TypeUtils.isPrimitive(unionFieldObject.getClass().getName()) || TypeUtils.isJavaType(unionFieldObject.getClass().getName()) || TypeUtils.isPrimitiveParamSign(unionFieldObject.getClass().getName())) {
974                             utWrapperField.set(serviceObject,
975                                     unionFieldObject);
976                             isFound = true;
977                         } else if (TypeUtils.isEnumType(unionFieldObject.getClass(), runtimeInfo.getAllEnumTypes().keySet())) {
978                             utWrapperField.set(
979                                     serviceObject,
980                                     transformEnumType(
981                                     unionFieldObject,
982                                     runtimeInfo.getCorbaClassLoader(),
983                                     runtimeInfo.getServiceClassLoader()));
984                             isFound = true;
985                         } else {
986                             if (AnyType.isAnyType(TypeUtils.getTypeNameWithoutBrackets(
987                                     unionFieldObject.getClass()))) {
988                                 fieldValue = new AnyTypeWrapper();
989                             } else {
990                                 if (unionFieldObject.getClass().isArray()) {
991                                     fieldValue = Array.newInstance(
992                                             runtimeInfo.getServiceClassLoader().loadClass(
993                                             TypeUtils.getTypeNameWithoutBrackets(unionFieldObject.getClass())),
994                                             TypeUtils.getObjectDimensions(unionFieldObject));
995                                 } else {
996                                     fieldValue = runtimeInfo.getServiceClassLoader().loadClass(
997                                             unionFieldObject.getClass().getName()).newInstance();
998 
999                                 }
1000                                 utWrapperField.set(serviceObject,
1001                                         fieldValue);
1002                             }
1003                         }
1004 
1005                         if (!isParam) {
1006                             parentField.set(parentObject, serviceObject);
1007                         }
1008                         if (isFound) {
1009                             break;
1010                         }
1011                         LOG.debug("!!!!>>>>>> union field inited: " + fieldName + " value: " + unionFieldObject);
1012                         SearchedType searchedType = TypeUtils.isSearchedTypeAll(
1013                                 TypeUtils.getTypeNameWithoutBrackets(unionFieldObject.getClass()), false,
1014                                 runtimeInfo.getAllCorbaTypes());
1015                         if (AnyType.isAnyType(TypeUtils.getTypeNameWithoutBrackets(
1016                                 unionFieldObject.getClass()))) {
1017                             searchedType = new AnyType();
1018                         }
1019                         if (searchedType != null) {
1020 
1021                             transformFromCorbaType(fieldValue, searchedType,
1022                                     unionFieldObject, runtimeInfo, false,
1023                                     parentField, parentObject);
1024 
1025                             if (AnyType.isAnyType(TypeUtils.getTypeNameWithoutBrackets(
1026                                     unionFieldObject.getClass()))) {
1027 
1028                                 utWrapperField.set(serviceObject,
1029                                         ((AnyTypeWrapper) fieldValue).getValue());
1030                                 if (!isParam) {
1031                                     parentField.set(parentObject,
1032                                             ((AnyTypeWrapper) fieldValue).getValue());
1033                                 }
1034                             }
1035                         } else {
1036                             buildServiceObjectfromCorbaObject(fieldValue,
1037                                     unionFieldObject, runtimeInfo);
1038                         }
1039                         break;
1040 
1041                     } catch (InvocationTargetException ex) {
1042                         // ignore exception - union field is not initialized
1043                         // this will be thrown until finding the initialized
1044                         // field
1045                         LOG.debug("EXCEPTION: " + ex.getClass() + " message: " + ex.getMessage());
1046                     }
1047                 }
1048             }
1049             //Fixed
1050 //                        if (sType instanceof InterfaceType) {
1051 //				// transform from interface
1052 //                                // 
1053 //                                // 
1054 //                        }
1055 
1056             if (sType instanceof AnyType) {
1057                 LOG.debug(">>>> Is Any:");
1058                 if (corbaObject.getClass().isArray()) {
1059                     Object result = Array.newInstance(Object.class, TypeUtils.getObjectDimensions(corbaObject));
1060                     Object[] resultArray = (Object[]) result;
1061                     Object[] corbaObjectArray = (Object[]) corbaObject;
1062                     for (int i = 0; i < resultArray.length; i++) {
1063                         AnyTypeWrapper anyWr = new AnyTypeWrapper();
1064                         transformFromCorbaType(anyWr, sType, corbaObjectArray[i], runtimeInfo, isParam, parentField, parentObject);
1065                         resultArray[i] = anyWr.getValue();
1066                     }
1067                     ((AnyTypeWrapper) serviceObject).setValue(resultArray);
1068                 } else {
1069                     Object result = transformFromAny((Any) corbaObject, null, null, runtimeInfo);
1070                     ((AnyTypeWrapper) serviceObject).setValue(result);
1071                 }
1072 
1073             }
1074         }
1075     }
1076 
1077     /**
1078      * transformFromAny - Transform Corba objects to jax-ws objects (Any type)
1079      *
1080      * @param paramAny
1081      * @param runtimeInfo
1082      * @return
1083      * @throws SecurityException
1084      * @throws NoSuchFieldException
1085      * @throws IllegalArgumentException
1086      * @throws IllegalAccessException
1087      * @throws ClassNotFoundException
1088      * @throws InvocationTargetException
1089      * @throws InstantiationException
1090      * @throws NoSuchMethodException
1091      */
1092     private static Object transformFromAny(Any paramAny, TypeCode typeCode, InputStream anyInputStream,
1093             RuntimeInformation runtimeInfo) throws SecurityException,
1094             NoSuchFieldException, IllegalArgumentException,
1095             IllegalAccessException, ClassNotFoundException,
1096             InvocationTargetException, InstantiationException,
1097             NoSuchMethodException {
1098         Object localObject = null;
1099         
1100         
1101         // TYPEDEF in an ANY *******************
1102         // The typeCode is always null when extracting a typeDef from an Any:
1103         if (typeCode == null) {
1104         	try {
1105         		String typeId = paramAny.type().id();
1106         		TypeDef typeDef = TypeDefUtil.getTypeDefFromID(runtimeInfo.getTypeDefs(), typeId);
1107         			if (LOG.isDebugEnabled()) {
1108         				LOG.debug("ID found: "  + typeId);
1109         		}
1110         		LOG.debug("TypeDef found: "  + typeDef);
1111         		// Here also with the typedef!
1112         		if (typeDef != null) {
1113         			LOG.debug(">>>> typedef, value: " + paramAny);
1114         			TypeCode localTypeCode1 = null;
1115         			try {
1116         				localTypeCode1 = paramAny.type().content_type();
1117         			} catch (BadKind b) {
1118         				LOG.debug("Not a type with an ID...see CORBA specs. Cannot be a typedef...");
1119         			}
1120         			// Recursive call...NOW should NOT control typedefs...
1121         			localObject = transformFromAny(paramAny, localTypeCode1, anyInputStream, runtimeInfo);
1122         			// But we cannot return it, since it's a Typedef.
1123         			if (LOG.isDebugEnabled()) {
1124         				LOG.debug("Found TypeDef of class: " + localObject.getClass().getName() + "with ID:" + typeId);
1125         			}
1126 
1127         			Object obj = typeDef.getTypeDefObject(runtimeInfo.getServiceClassLoader(), localObject);
1128         			return obj;        		                                                                
1129         		}
1130         	} catch (BadKind b) {        		
1131         		LOG.debug("Not a type with an ID...see CORBA specs. Cannot be a typedef...");
1132         	}
1133         }
1134         // TYPEDEF end *********
1135                 
1136         TCKind localTCKind = (typeCode == null) ? paramAny.type().kind() : typeCode.kind();
1137         if (localTCKind.equals(TCKind.tk_string)) {
1138             if (anyInputStream == null) {
1139             	LOG.debug("REading string from any");
1140                 localObject = paramAny.extract_string();
1141             } else {
1142             	LOG.debug("REading string from inputstream");
1143                 localObject = anyInputStream.read_string();
1144             }
1145             LOG.debug(">>>> type: string, value: " + localObject);
1146         } else if (localTCKind.equals(TCKind.tk_long)) {
1147             if (anyInputStream == null) {
1148                 localObject = new Integer(paramAny.extract_long());
1149             } else {
1150                 localObject = new Integer(anyInputStream.read_long());
1151             }
1152 
1153             LOG.debug(">>>> type: long, value: " + localObject);
1154         } else if (localTCKind.equals(TCKind.tk_double)) {
1155             if (anyInputStream == null) {
1156                 localObject = new Double(paramAny.extract_double());
1157             } else {
1158                 localObject = new Double(anyInputStream.read_long());
1159             }
1160 
1161             LOG.debug(">>>> type: double, value: " + localObject);
1162         } else if (localTCKind.equals(TCKind.tk_boolean)) {
1163             if (anyInputStream == null) {
1164                 localObject = new Boolean(paramAny.extract_boolean());
1165             } else {
1166                 localObject = new Boolean(anyInputStream.read_boolean());
1167             }
1168 
1169             LOG.debug(">>>> type: boolean, value: " + localObject);
1170         } else if (localTCKind.equals(TCKind.tk_float)) {
1171             if (anyInputStream == null) {
1172                 localObject = new Float(paramAny.extract_float());
1173             } else {
1174                 localObject = new Float(anyInputStream.read_float());
1175             }
1176 
1177             LOG.debug(">>>> type: float, value: " + localObject);
1178         } else if (localTCKind.equals(TCKind.tk_char)) {
1179             if (anyInputStream == null) {
1180                 localObject = new Character(paramAny.extract_char());
1181             } else {
1182                 localObject = new Character(anyInputStream.read_char());
1183             }
1184 
1185             LOG.debug(">>>> type: char, value: " + localObject);
1186         } else if (localTCKind.equals(TCKind.tk_fixed)) {
1187             if (anyInputStream == null) {
1188                 localObject = paramAny.extract_fixed();
1189             } else {
1190                 localObject = anyInputStream.read_fixed();
1191             }
1192 
1193             LOG.debug(">>>> type: fixed, value: " + localObject);
1194         } else if (localTCKind.equals(TCKind.tk_longdouble)) // ??
1195         {
1196             if (anyInputStream == null) {
1197                 localObject = new Double(paramAny.extract_double());
1198             } else {
1199                 localObject = new Double(anyInputStream.read_double());
1200             }
1201 
1202             LOG.debug(">>>> type: longDouble, value: " + localObject);
1203         } else if (localTCKind.equals(TCKind.tk_longlong)) {
1204             if (anyInputStream == null) {
1205                 localObject = new Long(paramAny.extract_longlong());
1206             } else {
1207                 localObject = new Long(anyInputStream.read_longlong());
1208             }
1209 
1210             LOG.debug(">>>> type: longlong, value: " + localObject);
1211         } else if (localTCKind.equals(TCKind.tk_null)) {
1212             localObject = null;
1213             LOG.debug(">>>> type: null, value: " + localObject);
1214         } else if (localTCKind.equals(TCKind.tk_octet)) {
1215             if (anyInputStream == null) {
1216                 localObject = new Byte(paramAny.extract_octet());
1217             } else {
1218                 localObject = new Byte(anyInputStream.read_octet());
1219             }
1220 
1221             LOG.debug(">>>> type: octet, value: " + localObject);
1222         } else if (localTCKind.equals(TCKind.tk_short)) {
1223             if (anyInputStream == null) {
1224                 localObject = new Short(paramAny.extract_short());
1225             } else {
1226                 localObject = new Short(anyInputStream.read_short());
1227             }
1228 
1229             LOG.debug(">>>> type: ushort, value: " + localObject);
1230         } else if (localTCKind.equals(TCKind.tk_ushort)) {
1231             if (anyInputStream == null) {
1232                 localObject = new Short(paramAny.extract_ushort());
1233             } else {
1234                 localObject = new Short(anyInputStream.read_ushort());
1235             }
1236 
1237             LOG.debug(">>>> type: ushort, value: " + localObject);
1238         } else if (localTCKind.equals(TCKind.tk_ulong)) {
1239             if (anyInputStream == null) {
1240                 localObject = new Long(paramAny.extract_ulong());
1241             } else {
1242                 localObject = new Long(anyInputStream.read_ulong());
1243             }
1244 
1245             LOG.debug(">>>> type: ushort, value: " + localObject);
1246         } else if (localTCKind.equals(TCKind.tk_ulonglong)) {
1247             if (anyInputStream == null) {
1248                 localObject = new Long(paramAny.extract_ulonglong());
1249             } else {
1250                 localObject = new Long(anyInputStream.read_ulonglong());
1251             }
1252 
1253             LOG.debug(">>>> type: ushort, value: " + localObject);
1254         } else if (localTCKind.equals(TCKind.tk_wchar)) {
1255             if (anyInputStream == null) {
1256                 localObject = new Character(paramAny.extract_wchar());
1257             } else {
1258                 localObject = new Character(anyInputStream.read_wchar());
1259             }
1260 
1261             LOG.debug(">>>> type: wchar, value: " + localObject);
1262         } else if (localTCKind.equals(TCKind.tk_wstring)) {
1263             if (anyInputStream == null) {
1264                 localObject = new String(paramAny.extract_wstring());
1265             } else {
1266                 localObject = new String(anyInputStream.read_wstring());
1267             }
1268 
1269             LOG.debug(">>>> type: wstring, value: " + localObject);
1270         } else if (localTCKind.equals(TCKind.tk_any)) {
1271             if (anyInputStream == null) {
1272                 localObject = transformFromAny(paramAny.extract_any(), null, null, runtimeInfo);
1273             } else {
1274                 localObject = transformFromAny(anyInputStream.read_any(), null, null, runtimeInfo);
1275             }
1276 
1277             LOG.debug(">>>> type: any, value: " + localObject);
1278         } else if ((localTCKind.equals(TCKind.tk_array))) {
1279             // localObject = transformFromAny(paramAny, orb, serviceDescriptor);
1280             LOG.debug(">>>> type: array, value: " + paramAny);
1281             TypeCode localTypeCode1 = null;
1282             try {
1283 
1284 
1285                 if (typeCode == null) {
1286                     localTypeCode1 = paramAny.type();
1287                 } else {
1288                     localTypeCode1 = typeCode;
1289                 }
1290                 List<Integer> idxs = new ArrayList<Integer>();
1291                 
1292                 while (localTypeCode1.kind().equals(TCKind.tk_array)) {
1293                     idxs.add(localTypeCode1.length());
1294                     LOG.debug(">>>> type: array length: " + localTypeCode1);
1295                     LOG.debug(">>>> type: array length: " + localTypeCode1.length());
1296                     localTypeCode1 = localTypeCode1.content_type();
1297                 }
1298                 
1299                 LOG.debug(">>>> TYPE array(Content) " + localTypeCode1.kind().value());
1300 
1301                 Integer[] arrayIndexes =  idxs.toArray(new Integer[]{});
1302                 int[] arrayIndexesInt =  new int[arrayIndexes.length];
1303                 for (int i = 0; i < arrayIndexes.length; i++) {
1304                 	arrayIndexesInt[i] = arrayIndexes[i].intValue();
1305                 }
1306                 int totalArrayLentgh = 1;
1307                 for (Integer leng : idxs) {
1308                 	totalArrayLentgh = totalArrayLentgh * leng;
1309                 }
1310                 if (LOG.isDebugEnabled()) {
1311                 	LOG.debug(">>>> type: array TOTAL length: " + totalArrayLentgh);
1312                 	LOG.debug("Array dimensions: " + ArrayUtils.toString(arrayIndexesInt));
1313                 	LOG.debug("Array first dimensions: " + arrayIndexes.length);
1314                 }
1315                 
1316                 
1317                 InputStream inpStream = paramAny.create_input_stream();               
1318 
1319                 if (localTypeCode1.kind().equals(TCKind.tk_union) || localTypeCode1.kind().equals(TCKind.tk_struct) || localTypeCode1.kind().equals(TCKind.tk_value) || localTypeCode1.kind().equals(TCKind.tk_value_box) || localTypeCode1.kind().equals(TCKind.tk_enum) || localTypeCode1.kind().equals(TCKind.tk_except) || localTypeCode1.kind().equals(TCKind.tk_native) || localTypeCode1.kind().equals(TCKind.tk_abstract_interface)) {
1320 
1321                     localObject = transformFromAny(paramAny, localTypeCode1, inpStream, runtimeInfo);
1322 
1323                 } else {
1324                 	Object[] objects = new Object[totalArrayLentgh];
1325                 	for (int i = 0;   i < totalArrayLentgh; i++) {                			
1326                 		LOG.debug("--->TransForm Array -->" + i);
1327                 		// The array Object
1328                 		Object object = transformFromAny(paramAny, localTypeCode1, inpStream, runtimeInfo);
1329                 		LOG.debug("Recovered form array: " + object);
1330                 		objects[i] = object;                		                	
1331                 	}
1332                 	                	                	                       
1333                 	// Creates the multidimensiona array from the linearized oject
1334                 	Object myArray = Array.newInstance(Object.class, arrayIndexesInt);                	
1335                 	delinearizeArray(objects, myArray, 0);
1336       	
1337                 	localObject = myArray;
1338                 }
1339 
1340 
1341             } catch (BadKind e) {
1342                 // TODO Auto-generated catch block
1343                 e.printStackTrace();
1344             }
1345 
1346 //        } else if (localTCKind.equals(TCKind.tk_alias)) {
1347 //        	// Here also with the typedef!
1348 //        	
1349 //            LOG.debug(">>>> type: alias, value: " + paramAny);
1350 //            TypeCode localTypeCode1 = null;
1351 //            try {            	
1352 //                if (typeCode == null) {
1353 //                    localTypeCode1 = paramAny.type().content_type();
1354 //                } else {
1355 //                    localTypeCode1 = typeCode.content_type();
1356 //                }            	
1357 //            	
1358 //        	    // Now, Obj is a "wrapped Object". We have to convert it to the typeDef.        		        		
1359 //        		// Recursive call...
1360 //        		localObject = transformFromAny(paramAny, localTypeCode1, null, runtimeInfo);
1361 //        		// But we cannot return it, since it's a Typedef.
1362 //        		LOG.info("Found TypeDef of class: " + localObject.getClass().getName() + "with ID:" + paramAny.type().id());
1363 //
1364 //        		TypeDef typeDef = TypeDefUtil.getTypeDefFromID(runtimeInfo.getTypeDefs(), paramAny.type().id());        		        		
1365 //        		Object obj = typeDef.getTypeDefObject(runtimeInfo.getServiceClassLoader(), localObject);
1366 //        		return obj;        		                                                                
1367 //
1368 //            } catch (BadKind e) {
1369 //                // TODO Auto-generated catch block
1370 //                e.printStackTrace();
1371 //            }
1372 
1373         } else if (localTCKind.equals(TCKind.tk_sequence)) {
1374             try {
1375 
1376                 LOG.debug(">>>> type: sequence, value: " + paramAny);
1377 
1378                 TCKind localTCKind2 = paramAny.type().content_type().content_type().kind();
1379 
1380                 if (localTCKind2.equals(TCKind.tk_string)) {
1381                     localObject = StringSeqHelper.extract(paramAny);
1382                     LOG.debug(">>>> type: sequence - string, value: " + localObject);
1383                 } else if (localTCKind2.equals(TCKind.tk_long)) {
1384                     localObject = LongSeqHelper.extract(paramAny);
1385                     LOG.debug(">>>> type: sequence - long, value: " + localObject);
1386                 } else if (localTCKind2.equals(TCKind.tk_double)) {
1387                     localObject = DoubleSeqHelper.extract(paramAny);
1388                     LOG.debug(">>>> type: sequence - double, value: " + localObject);
1389                 } else if (localTCKind2.equals(TCKind.tk_boolean)) {
1390                     localObject = BooleanSeqHelper.extract(paramAny);
1391                     LOG.debug(">>>> type: sequence - boolean, value: " + localObject);
1392                 } else if (localTCKind2.equals(TCKind.tk_float)) {
1393                     localObject = FloatSeqHelper.extract(paramAny);
1394                     LOG.debug(">>>> type: sequence - float, value: " + localObject);
1395                 } else if (localTCKind2.equals(TCKind.tk_char)) {
1396                     localObject = CharSeqHelper.extract(paramAny);
1397                     LOG.debug(">>>> type: sequence - char, value: " + localObject);
1398 
1399                 } else if (localTCKind2.equals(TCKind.tk_longdouble)) // ??
1400                 {
1401                     localObject = DoubleSeqHelper.extract(paramAny);
1402                     LOG.debug(">>>> type: sequence - longDouble, value: " + localObject);
1403                 } else if (localTCKind2.equals(TCKind.tk_longlong)) {
1404                     localObject = LongLongSeqHelper.extract(paramAny);
1405                     LOG.debug(">>>> type: sequence - longlong, value: " + localObject);
1406                 } else if (localTCKind2.equals(TCKind.tk_null)) {
1407                     localObject = null;
1408                     LOG.debug(">>>> type: null, value: " + localObject);
1409                 } else if (localTCKind2.equals(TCKind.tk_octet)) {
1410                     localObject = OctetSeqHelper.extract(paramAny);
1411                     LOG.debug(">>>> type: sequence - octet, value: " + localObject);
1412                 } else if (localTCKind2.equals(TCKind.tk_short)) {
1413                     localObject = ShortSeqHelper.extract(paramAny);
1414                     LOG.debug(">>>> type: sequence - ushort, value: " + localObject);
1415                 } else if (localTCKind2.equals(TCKind.tk_ushort)) {
1416                     localObject = UShortSeqHelper.extract(paramAny);
1417                     LOG.debug(">>>> type: sequence - ushort, value: " + localObject);
1418                 } else if (localTCKind2.equals(TCKind.tk_ulong)) {
1419                     localObject = ULongSeqHelper.extract(paramAny);
1420                     LOG.debug(">>>> type: sequence - ushort, value: " + localObject);
1421                 } else if (localTCKind2.equals(TCKind.tk_ulonglong)) {
1422                     localObject = ULongLongSeqHelper.extract(paramAny);
1423                     LOG.debug(">>>> type: sequence - ushort, value: " + localObject);
1424                 } else if (localTCKind2.equals(TCKind.tk_wchar)) {
1425                     localObject = WCharSeqHelper.extract(paramAny);
1426                     LOG.debug(">>>> type: sequence - wchar, value: " + localObject);
1427                 } else if (localTCKind2.equals(TCKind.tk_wstring)) {
1428                     localObject = WStringSeqHelper.extract(paramAny);
1429                     LOG.debug(">>>> type: sequence - wstring, value: " + localObject);
1430                 } else if (localTCKind2.equals(TCKind.tk_any)) {
1431 
1432                     Any[] anys = AnySeqHelper.extract(paramAny);
1433 
1434                     Object[] arrayOfObject = new Object[anys.length];
1435 
1436                     for (int j = 0; j < anys.length; ++j) {
1437                         arrayOfObject[j] = transformFromAny(
1438                                 anys[j], null, null, runtimeInfo);
1439                     }
1440 
1441                     localObject = arrayOfObject;
1442                     LOG.debug(">>>> type: sequence - any, value: " + localObject);
1443                 } else {
1444                     LOG.debug(">>>> type:  sequence - complex, value: " + localObject);
1445                     //3-10-2009 CRB-240 any containing a sequence.
1446                     //If is a redefined sequence get the Correct Helper and extract the class
1447                     String corbaObjClassStr = null;
1448                     //**************************************
1449                     //Extract Helper from Map
1450                     corbaObjClassStr = runtimeInfo.getIdTOClassNameMap().get(paramAny.type().id());
1451                     //Added to fix idljbug
1452 
1453                     if (corbaObjClassStr == null) {
1454                         String lastElem = paramAny.type().id().substring(paramAny.type().id().lastIndexOf("/"), paramAny.type().id().length());
1455                         String vers = lastElem.substring(lastElem.indexOf(":"));
1456                         corbaObjClassStr = runtimeInfo.getIdTOClassNameMap().get(paramAny.type().id().substring(0, paramAny.type().id().lastIndexOf("/")) + vers);
1457                     }
1458                     if (corbaObjClassStr != null) {
1459 
1460                         LOG.debug("---->" + corbaObjClassStr);
1461 
1462                         //Object Class
1463 
1464                         Object arrayOfObject = TypeUtils.extractCorbaTypeFromAny(paramAny, corbaObjClassStr, runtimeInfo.getCorbaClassLoader());
1465                         //Service Class
1466 
1467                         //  Calculate the class name without squares                        
1468                         // String clazzName = arrayOfObject.getClass().getCanonicalName().substring(0, arrayOfObject.getClass().getCanonicalName().length() - 2);
1469                         Class cl = arrayOfObject.getClass();                        
1470                         while (cl.isArray()) {
1471                         	cl = cl.getComponentType();
1472                         }
1473                         String clazzName = cl.getCanonicalName();
1474 
1475                         Class contentClazz = null;
1476                         
1477                         if (!TypeUtils.isPrimitive(clazzName)) {
1478                             contentClazz = runtimeInfo.getServiceClassLoader().loadClass(clazzName);
1479                             // Object result = Array.newInstance(contentClazz, TypeUtils.getObjectDimensions(arrayOfObject)[0]);
1480                             Object result = Array.newInstance(contentClazz, TypeUtils.getObjectDimensions(arrayOfObject));
1481                             //Transform from Corba to Service
1482                             buildServiceObjectfromCorbaObject(result, arrayOfObject, runtimeInfo);
1483                             localObject = result;
1484 
1485                         } else {
1486 
1487                             LOG.debug("Is Primitive");
1488                             contentClazz = TypeUtils.getJavaLangClassPrimitive(clazzName);
1489                             localObject = arrayOfObject;
1490 
1491                         }
1492                         if (LOG.isDebugEnabled()) {
1493                         	LOG.debug("Returning array: " + ArrayUtils.toString(localObject));
1494                         }
1495 
1496                     } else {
1497 
1498                         LOG.debug(">>>> type: sequence - unknown, value: " + paramAny.type().name() + " component: " +
1499                                 paramAny.type().content_type().content_type().kind().value());
1500 
1501                     }
1502 
1503                 }
1504             } catch (BadKind e) {
1505                 // invalid sequence
1506                 LOG.debug("Invalid Type: " + e.getMessage());
1507             }
1508         } else if (localTCKind.equals(TCKind.tk_objref)) {
1509             // TODO how to treat org.omg.CORBA.Object
1510             localObject = paramAny.extract_Object();
1511             LOG.debug(">>>> type: objref, value: " + paramAny);
1512 
1513         } else if (localTCKind.equals(TCKind.tk_union) || localTCKind.equals(TCKind.tk_struct) || localTCKind.equals(TCKind.tk_value) || localTCKind.equals(TCKind.tk_value_box) || localTCKind.equals(TCKind.tk_enum) || localTCKind.equals(TCKind.tk_except) || localTCKind.equals(TCKind.tk_native) || localTCKind.equals(TCKind.tk_abstract_interface)) {
1514             String corbaObjClassStr = null;
1515             String corbaObjClassNoImplStr = null;  
1516             try {
1517                 LOG.debug(">>>> TYPECODE ID: " + paramAny.type().id());
1518 
1519                 corbaObjClassStr = runtimeInfo.getIdTOClassNameMap().get(paramAny.type().id());
1520                 if (corbaObjClassStr == null) {
1521                     String lastElem = paramAny.type().id().substring(paramAny.type().id().lastIndexOf("/"), paramAny.type().id().length());
1522                     String vers = lastElem.substring(lastElem.indexOf(":"));
1523                     corbaObjClassStr = runtimeInfo.getIdTOClassNameMap().get(paramAny.type().id().substring(0, paramAny.type().id().lastIndexOf("/")) + vers);
1524                 }
1525                 LOG.debug(">>>> ASSOCIATED CLASS : " + corbaObjClassStr);
1526             } catch (BadKind e) {
1527                 // invalid sequence
1528                 LOG.debug("Invalid Type: " + e.getMessage());
1529             }
1530             
1531             Object corbaObj = null;
1532             if (anyInputStream == null) {
1533                 corbaObj = TypeUtils.extractCorbaTypeFromAny(paramAny, corbaObjClassStr, runtimeInfo.getCorbaClassLoader());
1534             } else {
1535                 corbaObj = TypeUtils.readCorbaTypeFromAnyInputStream(anyInputStream, corbaObjClassStr, runtimeInfo.getCorbaClassLoader());
1536             }
1537             
1538             String corbaObjClassName = corbaObj.getClass().getName();
1539             if ((localTCKind.equals(TCKind.tk_value) || localTCKind.equals(TCKind.tk_value_box)) && corbaObjClassName.endsWith("Impl")) {
1540                 // remove "Impl" from value types            	
1541             	corbaObjClassNoImplStr = corbaObjClassName.substring(0, corbaObjClassName.length() - 4);
1542             } else {
1543             	corbaObjClassNoImplStr = corbaObjClassName;
1544             }
1545 
1546             if (LOG.isDebugEnabled()) {
1547             	LOG.debug("corbaObjClassNoImplStr: " + corbaObjClassNoImplStr);
1548             	LOG.debug(">>>> type: union or struct, value: " + corbaObj + " \n class: " + corbaObj.getClass());
1549             }
1550             
1551             SearchedType st = TypeUtils.isSearchedTypeAll(TypeUtils.getTypeNameWithoutBrackets(corbaObj.getClass()), false,
1552                     runtimeInfo.getAllCorbaTypes());
1553             
1554             LOG.debug("Searched type: " + st);
1555             if (st != null) {
1556                 AnyTypeWrapper anyWr = new AnyTypeWrapper();
1557                 Field anyWrField = anyWr.getClass().getDeclaredField("value");
1558                 Object srvObj = new Object();
1559                 if (st instanceof UnionType) {
1560                     Class utWrapperClass = runtimeInfo.getServiceClassLoader().loadClass(st.getTypeName() + "Wrapper");
1561                     if (corbaObj.getClass().isArray()) {
1562                         srvObj = Array.newInstance(utWrapperClass,
1563                                 TypeUtils.getObjectDimensions(corbaObj));
1564                     } else {
1565                         srvObj = utWrapperClass.newInstance();
1566                     }
1567 
1568                 }
1569 
1570                 transformFromCorbaType(srvObj, st, corbaObj, runtimeInfo,
1571                         false, anyWrField, anyWr);
1572 
1573                 localObject = anyWr.getValue();
1574             } else {
1575                 for (Class type : runtimeInfo.getAllIDLTypes()) {
1576                 	// Uses the corbaObjClassStr, since if VT, the impl is not used.
1577                 	String typeNameWithoutBrackets = TypeUtils.getTypeNameWithoutBrackets(corbaObjClassNoImplStr);
1578                 	LOG.debug("TypeNameWithoutBrackets : " + typeNameWithoutBrackets + ", comparing with " + type.getName());
1579                     if (typeNameWithoutBrackets.equals(type.getName())) {
1580                     	LOG.debug("Found :" + type.getName());
1581                         Class serviceObjClass = runtimeInfo.getServiceClassLoader().loadClass(
1582                                 TypeUtils.getTypeNameWithoutBrackets(type));
1583                         Object serviceObj = null;
1584                         LOG.debug("corbaObj.getClass(): " + corbaObj.getClass().getName());
1585                         
1586 
1587                         //Added to fix Any of Enum
1588                         if (TypeUtils.isEnumType(corbaObj.getClass(), runtimeInfo.getAllEnumTypes().keySet())) {
1589 
1590                             serviceObj = transformEnumType(corbaObj, runtimeInfo.getCorbaClassLoader(),
1591                                     runtimeInfo.getServiceClassLoader());
1592                             localObject = serviceObj;
1593 
1594                             break;
1595 
1596                         } else if (corbaObj.getClass().isArray()) {
1597                             serviceObj = Array.newInstance(serviceObjClass,
1598                                     TypeUtils.getObjectDimensions(corbaObj));
1599                         } else {
1600                             serviceObj = serviceObjClass.newInstance();
1601                         }
1602                         buildServiceObjectfromCorbaObject(serviceObj, corbaObj,
1603                                 runtimeInfo);
1604                         localObject = serviceObj;
1605                         break;
1606                     }
1607                 }
1608             }
1609         }
1610 
1611         return localObject;
1612     }
1613 
1614     /**
1615      * buildCorbaObjectfromServiceObject - Transform original corba objects for servant
1616      * invocation
1617      *
1618      * @param corbaObject
1619      * @param serviceObject
1620      * @param runtimeInfo
1621      * @throws SecurityException
1622      * @throws NoSuchFieldException
1623      * @throws IllegalArgumentException
1624      * @throws IllegalAccessException
1625      * @throws InvocationTargetException
1626      * @throws ClassNotFoundException
1627      * @throws NoSuchMethodException
1628      * @throws InstantiationException
1629      */
1630     private static void buildCorbaObjectfromServiceObject(Object corbaObject,
1631             Object serviceObject, RuntimeInformation runtimeInfo)
1632             throws SecurityException, NoSuchFieldException,
1633             IllegalArgumentException, IllegalAccessException,
1634             InvocationTargetException, ClassNotFoundException,
1635             NoSuchMethodException, InstantiationException {
1636 
1637         LOG.debug("Oirginal Object Class Loader ===>" + corbaObject.getClass().getClassLoader());
1638         LOG.debug("Modified Object Class Loader ===>" + serviceObject.getClass().getClassLoader());
1639         // field is primitive or java type
1640 
1641         LOG.debug("!!!!>>>>>> origObject: " + corbaObject + " class: " + corbaObject.getClass() + " classloader: " + corbaObject.getClass().getClassLoader());
1642         LOG.debug("!!!!>>>>>> modifiedObject: " + serviceObject + " class: " + serviceObject.getClass() + " classloader: " + serviceObject.getClass().getClassLoader());
1643         if (corbaObject.getClass().isArray()) {
1644             LOG.debug("!!!!>>>>>> IsARRAY");
1645             Object[] corbaObjectArray = (Object[]) corbaObject;
1646             Object[] serviceObjectArray = (Object[]) serviceObject;
1647             LOG.debug("!!!!>>>>>> arrayLent: " + corbaObjectArray.length);
1648             LOG.debug("!!!!>>>>>> component type: " + corbaObject.getClass().getComponentType());
1649             for (int i = 0; i < corbaObjectArray.length; i++) {
1650                 if (serviceObjectArray[i].getClass().isArray()) {
1651                     LOG.debug("!!!!>>>>>> embeded arrayLent: " + ((Object[]) serviceObjectArray[i]).length);
1652                     for (int j = 0; j < ((Object[]) serviceObjectArray[i]).length; j++) {
1653                         LOG.debug("!!!!>>>>>> embeded array: value " + j + " - " + ((Object[]) serviceObjectArray[i])[j]);
1654                     }
1655                     corbaObjectArray[i] = Array.newInstance(
1656                             runtimeInfo.getCorbaClassLoader().loadClass(
1657                             TypeUtils.getTypeNameWithoutBrackets(corbaObject.getClass())),
1658                             TypeUtils.getObjectDimensions(serviceObjectArray[i]));
1659                 } else {
1660                     corbaObjectArray[i] = corbaObject.getClass().getComponentType().newInstance();
1661                 }
1662 
1663                 if (TypeUtils.isPrimitive(corbaObjectArray[i].getClass().getName()) || TypeUtils.isJavaType(corbaObjectArray[i].getClass().getName()) || TypeUtils.isPrimitiveParamSign(corbaObjectArray[i].getClass().getName())) {
1664                     corbaObjectArray[i] = serviceObjectArray[i];
1665                 }/* else if (TypeUtils.isEnumType(corbaObjectArray[i].getClass(),
1666                 runtimeInfo.getAllEnumTypes().keySet())) {
1667                 corbaObjectArray[i] = transformEnumType(
1668                 serviceObjectArray[i], runtimeInfo
1669                 .getServiceClassLoader(), runtimeInfo
1670                 .getCorbaClassLoader());
1671 
1672                 }*/ else {
1673                     buildCorbaObjectfromServiceObject(corbaObjectArray[i],
1674                             serviceObjectArray[i], runtimeInfo);
1675                 }
1676                 LOG.debug("!!!!>>>>>> component type object: " + corbaObjectArray[i]);
1677             }
1678 
1679         } else {
1680 
1681             Class corbaClass = runtimeInfo.getCorbaClassLoader().loadClass(
1682                     TypeUtils.getTypeNameWithoutBrackets(corbaObject.getClass()));
1683             Class serviceClass = runtimeInfo.getServiceClassLoader().loadClass(
1684                     TypeUtils.getTypeNameWithoutBrackets(serviceObject.getClass()));
1685 
1686             List<Field> allCrbFields = new ArrayList<Field>();
1687             getAllFields(corbaClass, allCrbFields);
1688             Field[] crbFields = allCrbFields.toArray(new Field[allCrbFields.size()]);// origClass.getDeclaredFields();
1689             for (int i = 0; i < crbFields.length; i++) {
1690                 crbFields[i].setAccessible(true);
1691                 Field srvField = getField(serviceClass, crbFields[i].getName());// modClass.getDeclaredField(origFields[i].getName());
1692                 srvField.setAccessible(true);
1693                 if (srvField.get(serviceObject) == null) {
1694                     LOG.warn("Warning: Field " + srvField.getName() + " from service object " + serviceObject + " is null");
1695                     continue;
1696                 }
1697                 // field is primitive or java type
1698                 LOG.debug("!!!!>>>>>> origFieldTypeName: " + crbFields[i].getType().getName());
1699                 LOG.debug("!!!!>>>>>> modifField: " + srvField.getType().getName());
1700                 if (crbFields[i].isAccessible()) {
1701 	                if (TypeUtils.isPrimitive(crbFields[i].getType().getName()) || TypeUtils.isJavaType(crbFields[i].getType().getName()) || TypeUtils.isPrimitiveParamSign(crbFields[i].getType().getName())) {
1702 	                    LOG.debug("PRIMITIVE OR JAVATYPE");
1703 	                    crbFields[i].set(corbaObject, srvField.get(serviceObject));	                    
1704 	                } else if (TypeUtils.isEnumType(crbFields[i].getType(),
1705 	                        runtimeInfo.getAllEnumTypes().keySet())) {
1706 	                    crbFields[i].set(corbaObject, transformEnumType(srvField.get(serviceObject), runtimeInfo.getServiceClassLoader(), runtimeInfo.getCorbaClassLoader()));
1707 	
1708 	                } // complex type
1709 	                else {
1710 	
1711 	                    String crbFieldTypeName = crbFields[i].getType().getName();
1712 	                    SearchedType ut = TypeUtils.isSearchedTypeAll(
1713 	                            crbFieldTypeName, crbFieldTypeName.startsWith("["), runtimeInfo.getAllCorbaTypes());
1714 	                    if (AnyType.isAnyType(TypeUtils.getTypeNameWithoutBrackets(
1715 	                            crbFields[i].getType()))) {
1716 	                        ut = new AnyType();
1717 	                    }
1718 	                    // field is corba type
1719 	
1720 	                    if (ut != null) {
1721 	                        LOG.debug("TRANSFORM==>2 *****************************************");
1722 	                        LOG.debug("===>" + crbFields[i].toString());
1723 	                        LOG.debug("===>" + corbaObject.toString());
1724 	                        LOG.debug("===>" + crbFields[i].getGenericType());
1725 	                        LOG.debug("===>" + crbFields[i].getName());
1726 	
1727 	                        LOG.debug("===>" + ut.getClass());
1728 	                        LOG.debug("===>" + serviceObject.toString());
1729 	                        LOG.debug("*******************************************************");
1730 	
1731 	                        if (ut instanceof InterfaceType) {
1732 	                            String ior = null;
1733 	                            if (srvField.get(serviceObject) instanceof W3CEndpointReference) {
1734 	                                W3CEndpointReference epr = (W3CEndpointReference) srvField.get(serviceObject);
1735 	
1736 	                                LOG.debug("Document Fragment EPR ===> " + epr.toString());
1737 	                                ior = HelperEPRUtils.readAddressFromEPR(epr.toString());
1738 	                                LOG.debug("IOR ===> " + ior);
1739 	
1740 	                            }
1741 	                            // ORB orb =
1742 	                            // serviceDescriptor.getEndpoint().getOrb();
1743 	                            LOG.debug("IOR ===> " + ior);
1744 	
1745 	                            // Load Helper and Do the narrow
1746 	                            // Class helperClass =
1747 	                            // originalClassLoader.loadClass(origFields[i].getType().getName()
1748 	                            // + "Helper");
1749 	                            Class helperClass = loadHelperbyName(crbFields[i].getType().getName(), runtimeInfo.getCorbaClassLoader());
1750 	                            crbFields[i].set(corbaObject, narrowObjbyIOR(
1751 	                                    helperClass, ior, runtimeInfo.getOrb()));
1752 	
1753 	                        } else {
1754 	
1755 	                            Object fieldValue = null;
1756 	                            if (AnyType.isAnyType(TypeUtils.getTypeNameWithoutBrackets(
1757 	                                    crbFields[i].getType()))) {
1758 	                                if (crbFields[i].getType().isArray()) {
1759 	                                    fieldValue = new Any[((Object[]) srvField.get(serviceObject)).length];
1760 	//									                     Array
1761 	//											.newInstance(
1762 	//													Any.class,
1763 	//													TypeUtils
1764 	//															.getObjectDimensions(modifiedObject));
1765 	                                } else {
1766 	                                    fieldValue = runtimeInfo.getOrb().create_any();
1767 	
1768 	                                }
1769 	                            } else {
1770 	                                if (crbFields[i].getType().isArray()) {
1771 	                                    fieldValue = Array.newInstance(
1772 	                                            runtimeInfo.getCorbaClassLoader().loadClass(
1773 	                                            TypeUtils.getTypeNameWithoutBrackets(crbFields[i].getType())),
1774 	                                            TypeUtils.getObjectDimensions(serviceObject));
1775 	                                } else {
1776 	                                    fieldValue = runtimeInfo.getCorbaClassLoader().loadClass(
1777 	                                            crbFields[i].getType().getName()).newInstance();
1778 	
1779 	                                }
1780 	                            }
1781 	                            crbFields[i].set(corbaObject, fieldValue);
1782 	                            transformToCorbaType(fieldValue, ut, srvField.get(serviceObject), runtimeInfo, false);
1783 	                        }
1784 	                    } // complex type but not corba type
1785 	                    else {
1786 	                        // Init TODO refactoring
1787 	                        LOG.debug(" TYPE==>" + crbFields[i].getType());
1788 	                        LOG.debug(" TYPE NAME==>" + crbFields[i].getType().getName());
1789 	                        LOG.debug(" MODIFIED OBJECT==>" + srvField.getType().getName());
1790 	
1791 	                        // Create a new Instance for the Field
1792 	                        if (crbFields[i].getType().getName().startsWith("[")) {
1793 	                            Object[] obj = (Object[]) srvField.get(serviceObject);
1794 	                            LOG.debug("Modifield Fild Array Lenght" + obj.length);
1795 	                            LOG.debug(crbFields[i].getType().getComponentType().toString());
1796 	                            crbFields[i].set(corbaObject, Array.newInstance(
1797 	                                    crbFields[i].getType().getComponentType(),
1798 	                                    obj.length));
1799 	                        } else {
1800 	                            crbFields[i].set(corbaObject, crbFields[i].getType().newInstance());
1801 	                        }
1802 	                        if (TypeUtils.isPrimitive(crbFields[i].get(corbaObject).getClass().getName()) || TypeUtils.isJavaType(crbFields[i].get(
1803 	                                corbaObject).getClass().getName()) || TypeUtils.isPrimitiveParamSign(crbFields[i].get(corbaObject).getClass().getName())) {
1804 	                            crbFields[i].set(corbaObject, srvField.get(serviceObject));
1805 	                        } else if (TypeUtils.isEnumType(crbFields[i].get(
1806 	                                corbaObject).getClass(), runtimeInfo.getAllEnumTypes().keySet())) {
1807 	                            crbFields[i].set(corbaObject, transformEnumType(
1808 	                                    srvField.get(serviceObject), runtimeInfo.getServiceClassLoader(),
1809 	                                    runtimeInfo.getCorbaClassLoader()));
1810 	
1811 	                        } else {
1812 	                            buildCorbaObjectfromServiceObject(crbFields[i].get(corbaObject), srvField.get(serviceObject), runtimeInfo);
1813 	                        }
1814 	                    }
1815 	                }
1816                 }
1817             }
1818         }
1819 
1820     }
1821 
1822     /**
1823      * transformToCorbaType treat special corba types case (as parameter/return
1824      * type and as field)
1825      *
1826      * @param corbaObject
1827      * @param ut
1828      * @param serviceObject
1829      * @param runtimeInfo
1830      * @param isParam
1831      * @throws ClassNotFoundException
1832      * @throws SecurityException
1833      * @throws NoSuchFieldException
1834      * @throws IllegalArgumentException
1835      * @throws IllegalAccessException
1836      * @throws InvocationTargetException
1837      * @throws NoSuchMethodException
1838      * @throws InstantiationException
1839      */
1840     private static void transformToCorbaType(Object corbaObject,
1841             SearchedType ut, Object serviceObject,
1842             RuntimeInformation runtimeInfo, boolean isParam)
1843             throws ClassNotFoundException, SecurityException,
1844             NoSuchFieldException, IllegalArgumentException,
1845             IllegalAccessException, InvocationTargetException,
1846             NoSuchMethodException, InstantiationException {
1847 
1848         if (corbaObject.getClass().isArray()) {
1849             LOG.debug("!!!!>>>>>> IsARRAY");
1850             Object[] corbaObjectArray = (Object[]) corbaObject;
1851             Object[] serviceObjectArray = (Object[]) serviceObject;
1852             LOG.debug("!!!!>>>>>> arrayLent: " + corbaObjectArray.length);
1853             LOG.debug("!!!!>>>>>> modif arrayLent: " + serviceObjectArray.length);
1854             LOG.debug("!!!!>>>>>> component type: " + corbaObject.getClass().getComponentType());
1855             LOG.debug("!!!!>>>>>> modif component type: " + serviceObjectArray.getClass().getComponentType());
1856             for (int i = 0; i < corbaObjectArray.length; i++) {
1857                 if (AnyType.isAnyType(corbaObject.getClass().getComponentType().getName())) {
1858                     corbaObjectArray[i] = runtimeInfo.getOrb().create_any();
1859                 } else {
1860                     corbaObjectArray[i] = corbaObject.getClass().getComponentType().newInstance();
1861                 }
1862                 transformToCorbaType(corbaObjectArray[i], ut,
1863                         serviceObjectArray[i], runtimeInfo, isParam);
1864                 LOG.debug("!!!!>>>>>> component type object: " + corbaObjectArray[i]);
1865             }
1866 
1867         } else {
1868 
1869             if (ut instanceof UnionType) {
1870                 // transform to unionType
1871                 LOG.debug("!!!!!>>> Instance of uniontype");
1872                 Object fieldValue = serviceObject;
1873 
1874                 String utWrapper = ut.getTypeName() + "Wrapper";
1875                 Class utWrapperClass = runtimeInfo.getServiceClassLoader().loadClass(utWrapper);
1876 
1877                 Field utWrapperField = utWrapperClass.getDeclaredField(UnionTypeUtils.UNION_WRAPPER_FIELD);
1878                 LOG.debug("utWrapperClassString: " + utWrapper + " utWrapperClass: " + utWrapperClass + " utWrapperField: " + utWrapperField);
1879                 utWrapperField.setAccessible(true);
1880                 fieldValue = utWrapperField.get(serviceObject);
1881                 LOG.debug("!!!!!>>> Field type" + fieldValue.getClass() + " value: " + fieldValue);
1882 
1883                 LOG.debug("!!!!!>>> UnionFields: ");
1884                 boolean fieldFound = false;
1885                 String fieldAny = null;
1886                 for (String fieldName : ((UnionType) ut).getTypeFieldNameList()) {
1887                     Class fieldType = ((UnionType) ut).getFieldType(fieldName);
1888 
1889                     // MARCO: it can be null in an Union with only one choice...
1890                     if (fieldType != null) {
1891 
1892                         Class typeToCompare = fieldType;
1893                         LOG.debug("!!!!!>>> Field type of union: " + fieldType);
1894 
1895                         Class primitiveJavaLangType = TypeUtils.getJavaLangClassPrimitive(fieldType.getName());
1896                         if (primitiveJavaLangType != null) {
1897                             typeToCompare = primitiveJavaLangType;
1898                         }
1899                         LOG.debug("!!!!!>>> Field type to compare: " + typeToCompare);
1900                         if (fieldValue.getClass().equals(typeToCompare)) {
1901                             LOG.debug("!!!!!>>> Field name: " + fieldName);
1902                             Class corbaClass = runtimeInfo.getCorbaClassLoader().loadClass(
1903                                     TypeUtils.getTypeNameWithoutBrackets(corbaObject.getClass()));
1904                             LOG.debug("!!!!!>>> CORBA class: " + corbaClass);
1905                             Method methodForfieldToFill = corbaClass.getMethod(
1906                                     fieldName, fieldType);
1907                             LOG.debug("!!!!!>>> methodForfieldToFill class: " + methodForfieldToFill);
1908                             LOG.debug("!!!!!>>> fieldValue: " + fieldValue);
1909 
1910                             methodForfieldToFill.invoke(corbaObject, fieldValue);
1911                             fieldFound = true;
1912                             break;
1913 
1914                         } else {
1915                             boolean isUnion = false;
1916                             String fieldTypeStr = TypeUtils.getTypeNameWithoutBrackets(fieldType);
1917                             String fieldValueTypeStr = TypeUtils.getTypeNameWithoutBrackets(fieldValue.getClass());
1918                             LOG.debug("!!!!!>>> Field type str: " + fieldTypeStr);
1919                             LOG.debug("!!!!!>>> Field Value type str: " + fieldValueTypeStr);
1920 
1921                             SearchedType st = TypeUtils.isSearchedTypeAll(
1922                                     fieldTypeStr, false, runtimeInfo.getAllCorbaTypes());
1923                             // search for union
1924                             if (st == null && fieldTypeStr.endsWith("Wrapper")) {
1925                                 String tempFieldTypeStr = fieldTypeStr.substring(0, fieldTypeStr.length() - 7);
1926                                 st = TypeUtils.isSearchedTypeAll(
1927                                         tempFieldTypeStr, false, runtimeInfo.getAllCorbaTypes());
1928                                 if (st != null) {
1929                                     fieldTypeStr = tempFieldTypeStr;
1930                                     isUnion = true;
1931                                 }
1932                             }
1933                             if (AnyType.isAnyType(fieldTypeStr)) {
1934                                 st = new AnyType();
1935                                 fieldAny = fieldName;
1936                             }
1937                             if (st != null) {
1938                                 if (fieldValueTypeStr.equals("java.lang.Object") || isUnion) {
1939                                     LOG.debug("!!!!!>>> Is embeded anytype: " + st);
1940                                     Class corbaClass = runtimeInfo.getCorbaClassLoader().loadClass(
1941                                             TypeUtils.getTypeNameWithoutBrackets(corbaObject.getClass()));
1942                                     Class crbFieldClass = null;
1943                                     if (AnyType.isAnyType(fieldTypeStr)) {
1944                                         crbFieldClass = Any.class;
1945                                     } else {
1946                                         crbFieldClass = runtimeInfo.getCorbaClassLoader().loadClass(
1947                                                 fieldTypeStr);
1948                                     }
1949 
1950                                     Object fieldSTValue = null;
1951                                     if (fieldValue.getClass().isArray()) {
1952                                         if (AnyType.isAnyType(fieldTypeStr)) {
1953                                             fieldSTValue = new Any[((Object[]) fieldValue).length];
1954                                         } else {
1955                                             int dimentions[] = TypeUtils.getObjectDimensions(fieldValue);
1956                                             fieldSTValue = Array.newInstance(
1957                                                     crbFieldClass, dimentions);
1958                                             LOG.debug("!!!!!>>> Is array: " + dimentions);
1959                                         }
1960                                     } else {
1961                                         if (AnyType.isAnyType(fieldTypeStr)) {
1962                                             fieldSTValue = runtimeInfo.getOrb().create_any();
1963                                         } else {
1964                                             fieldSTValue = fieldType.newInstance();
1965                                         }
1966                                     }
1967                                     transformToCorbaType(fieldSTValue, st, fieldValue,
1968                                             runtimeInfo, false);
1969                                     LOG.debug("!!!!!>>> Transformed: " + fieldSTValue);
1970                                     Method methodForfieldToFill = corbaClass.getMethod(
1971                                             fieldName, fieldSTValue.getClass());
1972                                     methodForfieldToFill.invoke(corbaObject,
1973                                             fieldSTValue);
1974                                     LOG.debug("!!!!!>>> Transformed full: " + corbaObject);
1975                                     fieldFound = true;
1976                                 }
1977                             }
1978                         }
1979                     }
1980                 }
1981                 // if no field match and union has any field
1982                 if (!fieldFound && fieldAny != null) {
1983                     LOG.debug("!!!!!>>>ANY in Union: " + fieldAny + " value: " + fieldValue);
1984                     Class fieldType = ((UnionType) ut).getFieldType(fieldAny);
1985                     Class corbaClass = runtimeInfo.getCorbaClassLoader().loadClass(
1986                             TypeUtils.getTypeNameWithoutBrackets(corbaObject.getClass()));
1987                     Method methodForfieldToFill = corbaClass.getMethod(
1988                             fieldAny, fieldType);
1989 
1990                     methodForfieldToFill.invoke(corbaObject, transformToAny(fieldValue, null, runtimeInfo));
1991                 }
1992             }
1993 
1994             if (ut instanceof AnyType) {
1995 
1996                 LOG.debug("!!!!!>>> Is ANY type: Value " + serviceObject);
1997 
1998                 transformToAny(serviceObject, ((org.omg.CORBA.Any) corbaObject),
1999                         runtimeInfo);
2000             }
2001         }
2002 
2003     }
2004 
2005     /**
2006      * * transformToAny - Transform jax-ws objects to Corba objects (Any type)
2007      *
2008      *
2009      * @param serviceObject
2010      * @param any
2011      * @param runtimeInfo
2012      * @return
2013      * @throws ClassNotFoundException
2014      * @throws SecurityException
2015      * @throws IllegalArgumentException
2016      * @throws NoSuchFieldException
2017      * @throws IllegalAccessException
2018      * @throws InvocationTargetException
2019      * @throws NoSuchMethodException
2020      * @throws InstantiationException
2021      */
2022     public static Any transformToAny(Object serviceObject, Any any,
2023             RuntimeInformation runtimeInfo) throws ClassNotFoundException,
2024             SecurityException, IllegalArgumentException, NoSuchFieldException,
2025             IllegalAccessException, InvocationTargetException,
2026             NoSuchMethodException, InstantiationException {
2027         Any paramAny = runtimeInfo.getOrb().create_any();
2028         if (any != null) {
2029             paramAny = any;
2030         }
2031         if (serviceObject instanceof String) {
2032             paramAny.insert_string((String) serviceObject);
2033         } else if (serviceObject instanceof Long) {
2034             paramAny.insert_long(((Long) serviceObject).intValue());
2035         } else if (serviceObject instanceof Integer) {
2036             paramAny.insert_long(((Integer) serviceObject).intValue());
2037         } else if (serviceObject instanceof Double) {
2038             paramAny.insert_double(((Double) serviceObject).doubleValue());
2039         } else if (serviceObject instanceof Float) {
2040             paramAny.insert_float(((Float) serviceObject).floatValue());
2041         } else if (serviceObject instanceof Boolean) {
2042             paramAny.insert_boolean(((Boolean) serviceObject).booleanValue());
2043         } else if (serviceObject instanceof Character) {
2044             paramAny.insert_char(((Character) serviceObject).charValue());
2045         } else if (serviceObject instanceof Byte) {
2046             paramAny.insert_octet(((Byte) serviceObject).byteValue());
2047             LOG.debug(">>>> type: octet, value: " + serviceObject);
2048         } else if (serviceObject instanceof Short) {
2049             paramAny.insert_short(((Short) serviceObject).shortValue());
2050             LOG.debug(">>>> type: ushort, value: " + serviceObject);
2051         } else if (serviceObject instanceof BigInteger) {
2052             if (((BigInteger) serviceObject).toString().startsWith("-")) {
2053                 paramAny.insert_string(((BigInteger) serviceObject).toString());
2054             } else {
2055                 paramAny.insert_fixed(new BigDecimal((BigInteger) serviceObject));
2056             }
2057 
2058             LOG.debug(">>>> type: BigInteger -> fixed, value: " + serviceObject);
2059         } else if (serviceObject instanceof BigDecimal) {
2060             if (((BigDecimal) serviceObject).toString().startsWith("-")) {
2061                 paramAny.insert_string(((BigDecimal) serviceObject).toString());
2062             } else {
2063                 paramAny.insert_fixed((BigDecimal) serviceObject);
2064             }
2065 
2066             LOG.debug(">>>> type: BigInteger -> fixed, value: " + serviceObject);
2067 
2068         } else {
2069             LOG.debug(">>>> type: Other type(complex?), value: " + serviceObject);
2070             LOG.debug(">>>> Service Object Class ----> " + serviceObject.getClass());
2071             String paramObjectTypeStr = TypeUtils.getTypeNameWithoutBrackets(serviceObject.getClass());
2072             int dimentions[] = null;
2073             boolean isArray = serviceObject.getClass().isArray();
2074             if (isArray) {
2075                 dimentions = TypeUtils.getObjectDimensions(serviceObject);
2076             }
2077             Class corbaClass = null;
2078             Object serviceObj = null;
2079             boolean classFound = false;
2080 
2081             for (Class type : runtimeInfo.getAllIDLTypes()) {
2082             	if (LOG.isDebugEnabled()) { 
2083             		LOG.debug("****Search Class for Any assignement************************");
2084             		LOG.debug("Check for type paramObjectTypeStr --->" + paramObjectTypeStr);
2085             		LOG.debug("Check for type type.getName--->" + type.getName());
2086             	}
2087                 if (paramObjectTypeStr.equals(type.getName())) {
2088 
2089                     corbaClass = runtimeInfo.getCorbaClassLoader().loadClass(paramObjectTypeStr);
2090 
2091                     if (TypeUtils.isEnumType(corbaClass, runtimeInfo.getAllEnumTypes().keySet()) && !isArray) {
2092                         LOG.debug("is Enum");
2093                         serviceObj = transformEnumType(serviceObject, runtimeInfo.getServiceClassLoader(), runtimeInfo.getCorbaClassLoader());
2094                     } else {
2095                         if (isArray) {
2096                             serviceObj = Array.newInstance(corbaClass, dimentions);
2097 
2098                         } else {
2099                             serviceObj = corbaClass.newInstance();
2100                         }
2101 
2102                         buildCorbaObjectfromServiceObject(serviceObj, serviceObject,
2103                                 runtimeInfo);
2104                     }
2105                     if (LOG.isDebugEnabled()) {
2106                     	LOG.debug(">>>> type: Complex, value: " + serviceObj);
2107                     }
2108                     classFound = true;
2109                     break;
2110                 }
2111             }
2112             if (!classFound) {
2113                 SearchedType st = TypeUtils.isSearchedTypeAll(
2114                         paramObjectTypeStr, false, runtimeInfo.getAllCorbaTypes());
2115                 // check if is wrapper for union ( temporary: TODO change when completing task CRB-206 replace all union types with wrappers)
2116                 if (st == null) {
2117                     if (paramObjectTypeStr.endsWith("Wrapper")) {
2118                         // remove "Wrapper" to look for union
2119                         paramObjectTypeStr = paramObjectTypeStr.substring(0, paramObjectTypeStr.length() - 7);
2120                         st = TypeUtils.isSearchedTypeAll(
2121                                 paramObjectTypeStr, false, runtimeInfo.getAllCorbaTypes());
2122                     }
2123                 }
2124                 if (st != null) {
2125                     corbaClass = runtimeInfo.getCorbaClassLoader().loadClass(paramObjectTypeStr);
2126                     if (isArray) {
2127                         serviceObj = Array.newInstance(corbaClass, dimentions);
2128                     } else {
2129                         serviceObj = corbaClass.newInstance();
2130                     }
2131 
2132                     transformToCorbaType(serviceObj, st, serviceObject,
2133                             runtimeInfo, true);
2134                     LOG.debug(">>>> type: CorbaType(union, interface), value: " + serviceObj);
2135                     classFound = true;
2136                 }
2137             }
2138             if (!classFound) {
2139                 //OOOPS!!
2140                 LOG.debug(">>>>!!!! type: Unknown, value: " + serviceObject);
2141             } else {
2142                 TypeUtils.encloseCorbaTypeInAny(paramAny, serviceObj, corbaClass, runtimeInfo, runtimeInfo.getTypeDefs());
2143                 LOG.debug(">>>> ANY : " + paramAny);
2144                 LOG.debug(">>>> ANY kind: " + paramAny.type().kind());
2145 
2146             }
2147 
2148         }
2149         return paramAny;
2150     }
2151 
2152     /**
2153      * This method receive a Corba Object and trasform it to an
2154      * W3CEndpointReference
2155      *
2156      * @param Object
2157      *            the corba Object interface
2158      * @param jbi4corbaEndPoint
2159      * @return W3CEndpointReference
2160      * Class objectType
2161      */
2162     private static W3CEndpointReference objectToEPR(Object obj,
2163             Jbi4CorbaEndpoint jbi4corbaEndPoint, Class objectType) throws ClassNotFoundException {
2164 
2165         Jbi4CorbaSUManager su = jbi4corbaEndPoint.getSuManager();
2166         ProviderServiceDescriptor serviceDesc = null;
2167         Set<String> keys = new HashSet<String>();
2168         Set<String> classes = new HashSet<String>();
2169         String className = null;
2170         ProviderEndpoint pes = null;
2171         //-------------- Added To Manage Object ---------------------------
2172         //-----------------------------------------------------------------
2173 
2174         if (jbi4corbaEndPoint instanceof ProviderEndpoint) {
2175             pes = (ProviderEndpoint) jbi4corbaEndPoint;
2176             keys = pes.getServiceDescriptor().getIdToClassNameMap().keySet();
2177         }
2178 
2179         //If the interface is Corba Object find correct type with is a 
2180         if (objectType.equals(org.omg.CORBA.Object.class)) {
2181             //per ogni tipo della lista di interfacce conosciute controllo che obj is a se trovo il tipo break
2182             if (obj instanceof org.omg.CORBA.Object) {
2183                 for (String s : keys) {
2184                     LOG.debug("Check class with name" + s);
2185                     if (((org.omg.CORBA.Object) obj)._is_a(s)) {
2186                         className = pes.getServiceDescriptor().getIdToClassNameMap().get(s);
2187                         //Remove Helper from String className
2188                         className = className.substring(0, className.length() - 6);
2189 
2190                         if (LOG.isDebugEnabled()) {
2191                             LOG.debug("----------------------------------------------------------------");
2192                             LOG.debug("Object Is A -->" + className);
2193                             LOG.debug("----------------------------------------------------------------");
2194                         }
2195 
2196                         classes.add(className);
2197                     }
2198                 }
2199                 //If there are more then one class search
2200                 //Not Used.... To verify
2201 //                        if(classes.size()>1){
2202 //
2203 //                            if(LOG.isDebugEnabled()){
2204 //                                    LOG.debug("----------------------------------------------------------------");
2205 //                                    LOG.debug("                        Check SuperClass                        ");
2206 //                                    LOG.debug("----------------------------------------------------------------");
2207 //                            }
2208 //                            //Check the intersection of classes with superclasses
2209 //                            for( String clazz:classes){
2210 //                               Set<String> intersection=new HashSet<String>(classes);
2211 //                               Class loadedClass = pes.getServiceDescriptor().getOriginalClassLoader().loadClass(clazz+"Operations");
2212 //                               Set<String> superClasses= new HashSet<String>();
2213 //                               getHierachy(loadedClass, superClasses);
2214 //                               intersection.retainAll(superClasses);
2215 //                               if(intersection.size()==0){
2216 //                                   className= clazz;
2217 //                                   break;
2218 //                               }
2219 //                            }
2220 //
2221 //                        }
2222 
2223                 className = "interface " + className;
2224             }
2225         } else {
2226             className = obj.getClass().getInterfaces()[0].toString();
2227         }
2228 
2229         if (className == null) {
2230             throw new Jbi4CorbaRuntimeException("CRB--- The Interface don't exist in the SU");
2231         }
2232         //-------------------------------------------------------------
2233 
2234         // foreach deployed endpoint
2235         // TODO ---NullPointer Ecceptions
2236         for (Jbi4CorbaEndpoint endp : su.getDeployedEndpoints()) {
2237 
2238             if (endp instanceof ProviderEndpoint) {
2239 
2240                 ProviderEndpoint pe = (ProviderEndpoint) endp;
2241                 // We have to find the service that rappresent the returned
2242                 // interface
2243                 // if the obj is equals to the CorbaObjectInterface we can crate
2244                 // an EPR from this
2245                 // Service Descriptor
2246                 String op = "Operations";
2247                 String intName = pe.getServiceDescriptor().getServiceInterface().toString().substring(
2248                         0,
2249                         pe.getServiceDescriptor().getServiceInterface().toString().length() - op.length());
2250                 if (LOG.isDebugEnabled()) {
2251                     LOG.debug("----------------------------------------------------------------");
2252                     LOG.debug("Result Interface -->" + className);
2253                     LOG.debug("Current Endpoint Interface -->" + intName);
2254                     LOG.debug("----------------------------------------------------------------");
2255                 }
2256 
2257                 if (className.equals(intName)) {
2258                     serviceDesc = pe.getServiceDescriptor();
2259                     /*The Reference is made by the endpoint that provide the interface*/
2260                     /*The destination endpoint */
2261                     jbi4corbaEndPoint = endp;
2262                     break;
2263                 }
2264             }
2265         }
2266 
2267         ORB orb = serviceDesc.getEndpoint().getOrb();
2268         // Retrieve the IOR starting form Corba Object
2269         String ior = orb.object_to_string((org.omg.CORBA.Object) obj);
2270 
2271         return createEPR(ior, jbi4corbaEndPoint);
2272 
2273     }
2274 
2275     private static void getHierachy(Class clazz, Set<String> classHierarchy) throws ClassNotFoundException {
2276 
2277         if (clazz.getSuperclass() != null) {
2278             classHierarchy.add(clazz.getName());
2279             getHierachy(clazz.getSuperclass(), classHierarchy);
2280         }
2281         return;
2282     }
2283 
2284     /**
2285      * This Method generate a W3CEndpointReference from a IOR
2286      *
2287      * @param String
2288      *            ior
2289      * @param psd -
2290      *            ProviderServiceDescriptor
2291      * @return W3CEndpointRefernece
2292      */
2293     private static W3CEndpointReference createEPR(String ior,
2294             Jbi4CorbaEndpoint psd) {
2295 
2296         W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder();
2297         // We have to generate a new Temporary endpoint deploy start it and
2298         // generate an EPR
2299         // For dynamic partnrelink associations
2300         QName serviceName = psd.getServiceName();
2301         QName endPointName = new QName(psd.getEndpointName());
2302 
2303         builder.address("jbi4corba:" + ior);
2304         builder.serviceName(serviceName);
2305         builder.endpointName(endPointName);
2306         LOG.debug("GENERATED EPR WITH >>>> address --> jbi4corba:" + ior + "\n serviceName -->" + serviceName.toString() + "\n endpointName -->" + endPointName.toString());
2307         W3CEndpointReference epr = builder.build();
2308         
2309         return epr;
2310 
2311     }
2312 
2313     /**
2314      * This method execute the narrow from the Helper and ior
2315      *
2316      * @param helperClass
2317      *            the helper
2318      * @param ior
2319      *            the reference to the object
2320      * @param orb
2321      *            the Orb reference
2322      *
2323      * @return Object narrowed Object
2324      */
2325     private static Object narrowObjbyIOR(Class helperClass, String ior, ORB orb)
2326             throws NoSuchMethodException, IllegalAccessException,
2327             IllegalArgumentException, InvocationTargetException {
2328 
2329         Object narrowedObj = helperClass.getDeclaredMethod("narrow",
2330                 org.omg.CORBA.Object.class).invoke(helperClass,
2331                 orb.string_to_object(ior));
2332         return narrowedObj;
2333     }
2334 
2335     /**
2336      * This Method Load the correct Helper for the narrowing of Corba Object
2337      * starting form the name of the interface
2338      *
2339      * @param String
2340      *            interfaceName
2341      * @param ChildFirstClassLoader
2342      *            originalClassLoader
2343      *
2344      * @return Class helper
2345      *
2346      */
2347     private static Class loadHelperbyName(String interfaceName,
2348             URLClassLoader originalClassLoader)
2349             throws ClassNotFoundException {
2350 
2351         return originalClassLoader.loadClass(interfaceName + "Helper");
2352 
2353     }
2354 
2355     /**
2356      *
2357      * @param enumObj
2358      * @param fromClassLoader
2359      * @param toClassLoader
2360      * @return
2361      * @throws SecurityException
2362      * @throws NoSuchMethodException
2363      * @throws ClassNotFoundException
2364      * @throws IllegalArgumentException
2365      * @throws IllegalAccessException
2366      * @throws InvocationTargetException
2367      */
2368     private static Object transformEnumType(Object enumObj,
2369             URLClassLoader fromClassLoader, URLClassLoader toClassLoader)
2370             throws SecurityException, NoSuchMethodException,
2371             ClassNotFoundException, IllegalArgumentException,
2372             IllegalAccessException, InvocationTargetException {
2373 
2374         Class fromEnumClass = fromClassLoader.loadClass(TypeUtils.getTypeNameWithoutBrackets(enumObj.getClass()));
2375         Class toEnumClass = toClassLoader.loadClass(TypeUtils.getTypeNameWithoutBrackets(enumObj.getClass()));
2376 
2377         if (enumObj.getClass().isArray()) {
2378             int dimentions[] = TypeUtils.getObjectDimensions(enumObj);
2379             Object enumArray = Array.newInstance(toEnumClass, dimentions);
2380             Object[] fromEnumArray = (Object[]) enumObj;
2381             Object[] toEnumArray = (Object[]) enumArray;
2382             for (int i = 0; i < fromEnumArray.length; i++) {
2383                 toEnumArray[i] = transformEnumType(fromEnumArray[i],
2384                         fromClassLoader, toClassLoader);
2385             }
2386 
2387             return toEnumArray;
2388         } else {
2389 
2390             Method valueMethod = fromEnumClass.getMethod("value");
2391             Object value = valueMethod.invoke(enumObj);
2392 
2393             Method from_intMethod = toEnumClass.getMethod("from_int", int.class);
2394 
2395             return from_intMethod.invoke(null, ((Integer) value).intValue());
2396         }
2397     }
2398 
2399     /**
2400      * getAllFields gets fields of class or inherited even if are not public
2401      *
2402      * @param cls
2403      * @param fields
2404      */
2405     private static void getAllFields(Class cls, List<Field> fields) {
2406         if (cls == null || cls.getName().startsWith("java.lang.")) {
2407             return;
2408         }
2409 
2410         Field[] clsFields = cls.getDeclaredFields();
2411         for (Field field : clsFields) {
2412             if (!ignoreField(field)) {
2413                 fields.add(field);
2414             }
2415         }
2416 
2417         getAllFields(cls.getSuperclass(), fields);
2418 
2419     }
2420 
2421     /**
2422      * Ignore certain fields
2423      *
2424      * @param field
2425      * @return
2426      */
2427     private static boolean ignoreField(Field field) {
2428         int fieldMofidiers = field.getModifiers();
2429 
2430         //treat value type case(ignore field private static String[] _truncatable_ids)
2431         if (field.getName().equals("_truncatable_ids") && Modifier.isPrivate(fieldMofidiers) && Modifier.isStatic(fieldMofidiers)) {
2432             return true;
2433         }
2434 
2435         return false;
2436     }
2437 
2438     /**
2439      * getField gets field of class or inherited even if is not public
2440      *
2441      * @param cls
2442      * @param fieldName
2443      * @return
2444      */
2445     private static Field getField(Class cls, String fieldName) {
2446         Field[] clsFields = cls.getDeclaredFields();
2447         for (Field field : clsFields) {
2448             if (field.getName().equals(fieldName)) {
2449                 return field;
2450             }
2451         }
2452 
2453         return getField(cls.getSuperclass(), fieldName);
2454     }
2455 
2456     /**
2457      * Gets the <code>MethodSignature</code> object from the <code>Method</code>
2458      * class. The correspondace between the MethodSignature and the method can
2459      * be saved for further optimization.
2460      *
2461      * @param method
2462      * @return
2463      */
2464     public static MethodSignature getMetodSignatureFromMethod(Method method, List<MethodSignature> methodSignatures) {
2465 
2466         MethodSignature methodSignature = null;
2467         for (int i = 0; i < methodSignatures.size(); i++) {
2468             methodSignature = (MethodSignature) methodSignatures.get(i);
2469             LOG.debug("METHOD SIGNATURE ===>" + methodSignature);
2470             if (compareMethodsWithNameAndParameters(method, methodSignature.getMethod())) {
2471                 return methodSignature;
2472             }
2473         }
2474         String msg = MESSAGES.getString(
2475                 "CRB000765_No_method_signature_found_for_method",
2476                 new Object[]{method.toGenericString()});
2477         LOG.warn(msg);
2478         return null;
2479     }
2480 
2481     /**
2482      * Compares two method looking for the name and the parameters (not the
2483      * original class).
2484      *
2485      * @param methodA
2486      * @param methodB
2487      * @return
2488      */
2489     private static boolean compareMethodsWithNameAndParameters(Method methodA,
2490             Method methodB) {
2491         boolean ret = false;
2492 
2493         boolean paramEquals = compareParameterTypes(
2494                 methodA.getParameterTypes(), methodB.getParameterTypes());
2495 
2496         if ((paramEquals) && (methodA.getName().equals(methodB.getName()))) {
2497             ret = true;
2498         }
2499         return ret;
2500     }
2501 
2502     /**
2503      * True if two <code>Class</code> array are of <code>Class</code> of the
2504      * same type.
2505      *
2506      * @param typesA
2507      * @param typesB
2508      * @return
2509      */
2510     private static boolean compareParameterTypes(Class[] typesA, Class[] typesB) {
2511         boolean ret = true;
2512 
2513         if (typesA.length != typesB.length) {
2514             ret = false;
2515         } else {
2516             for (int i = 0; i < typesA.length; i++) {
2517                 if (!(typesA[i].getName().equals(typesB[i].getName()))) {
2518 
2519                     ret = false;
2520                     break;
2521                 }
2522             }
2523         }
2524         return ret;
2525     }
2526             
2527     /**
2528      * Returns a multidimentional array from a linearized Object, see CRB-214.
2529      * @param objects
2530      * @param dimentions
2531      * @return
2532      */
2533     public static int  delinearizeArray(Object[] objects, Object myArray, int actualElement) {    
2534     	
2535     	LOG.debug("Delinearizing array called with array: " 
2536     			+ ArrayUtils.toString(myArray) + " and actualElement: " + actualElement);
2537     	
2538     	Object arrEl = myArray;  
2539     	int arrLen = Array.getLength(arrEl);
2540     	LOG.debug("myArray.getClass().getComponentType():" + myArray.getClass().getComponentType());
2541     	LOG.debug("arrEl:" + ArrayUtils.toString(arrEl));
2542     	LOG.debug("myArray.getClass().getComponentType().isArray:" + myArray.getClass().getComponentType().isArray());
2543     	if (myArray.getClass().getComponentType().isArray()) {
2544     		for (int k = 0; k < arrLen; k++) {
2545     			actualElement = delinearizeArray(objects, Array.get(arrEl, k) , actualElement);
2546     		}
2547     	} else {
2548     		for (int i = 0; i < arrLen; i++) {
2549     			LOG.debug("totIndex:" + actualElement + " Object: " +objects[actualElement]);                			
2550     			Array.set(myArray,i, objects[actualElement]);
2551     			actualElement ++;        			
2552     		}    		
2553     	}
2554     	return actualElement;
2555     }
2556    
2557         	
2558 }