1
2
3
4
5
6
7
8 package it.imolinfo.jbi4corba.webservice.generator;
9
10
11
12
13
14
15
16
17 import it.imolinfo.jbi4corba.Logger;
18 import it.imolinfo.jbi4corba.LoggerFactory;
19 import it.imolinfo.jbi4corba.exception.ClassGenerationException;
20 import it.imolinfo.jbi4corba.exception.Jbi4CorbaRuntimeException;
21 import it.imolinfo.jbi4corba.webservice.generator.typedef.TypeDef;
22 import it.imolinfo.jbi4corba.webservice.runtime.CorbaTransformationUtils;
23 import it.imolinfo.jbi4corba.webservice.runtime.RuntimeInformation;
24
25 import java.lang.reflect.Array;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.net.URLClassLoader;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36
37 import org.omg.CORBA.Any;
38 import org.omg.CORBA.TCKind;
39 import org.omg.CORBA.TypeCode;
40 import org.omg.CORBA.TypeCodePackage.BadKind;
41 import org.omg.CORBA.portable.InputStream;
42
43 @SuppressWarnings("unchecked")
44 public abstract class TypeUtils {
45
46 public static final TCKind INTERFACE = TCKind.tk_objref;
47 public static final TCKind UNION = TCKind.tk_union;
48 public static final TCKind ANY = TCKind.tk_any;
49
50 private static final Map PRIMITIVE_NAME_TYPE_MAP = new HashMap();
51 private static final Set<String> PRIMITIVE_JAVA_PARAMETER_SIGN = new HashSet<String>();
52 private static final Map<String, Class> PRIMITIVE_JAVA_LANG_TYPE_MAP = new HashMap<String, Class>();
53
54 private static final Logger LOG
55 = LoggerFactory.getLogger(TypeUtils.class);
56
57 public static final String JAXB_XML_ELEMENTS = "Ljavax/xml/bind/annotation/XmlElements;";
58
59 public static final String JAXB_XML_ELEMENT = "Ljavax/xml/bind/annotation/XmlElement;";
60
61 public static final String JAXB_XML_TYPE = "Ljavax/xml/bind/annotation/XmlType;";
62
63
64 static {
65 PRIMITIVE_NAME_TYPE_MAP.put("boolean", Boolean.TYPE);
66 PRIMITIVE_NAME_TYPE_MAP.put("byte", Byte.TYPE);
67 PRIMITIVE_NAME_TYPE_MAP.put("char", Character.TYPE);
68 PRIMITIVE_NAME_TYPE_MAP.put("short", Short.TYPE);
69 PRIMITIVE_NAME_TYPE_MAP.put("int", Integer.TYPE);
70 PRIMITIVE_NAME_TYPE_MAP.put("long", Long.TYPE);
71 PRIMITIVE_NAME_TYPE_MAP.put("float", Float.TYPE);
72 PRIMITIVE_NAME_TYPE_MAP.put("double", Double.TYPE);
73 PRIMITIVE_JAVA_LANG_TYPE_MAP.put("boolean", Boolean.class);
74 PRIMITIVE_JAVA_LANG_TYPE_MAP.put("byte", Byte.class);
75 PRIMITIVE_JAVA_LANG_TYPE_MAP.put("char", Character.class);
76 PRIMITIVE_JAVA_LANG_TYPE_MAP.put("short", Short.class);
77 PRIMITIVE_JAVA_LANG_TYPE_MAP.put("int", Integer.class);
78 PRIMITIVE_JAVA_LANG_TYPE_MAP.put("long", Long.class);
79 PRIMITIVE_JAVA_LANG_TYPE_MAP.put("float", Float.class);
80 PRIMITIVE_JAVA_LANG_TYPE_MAP.put("double", Double.class);
81
82 PRIMITIVE_JAVA_PARAMETER_SIGN.add("B");
83 PRIMITIVE_JAVA_PARAMETER_SIGN.add("C");
84 PRIMITIVE_JAVA_PARAMETER_SIGN.add("D");
85 PRIMITIVE_JAVA_PARAMETER_SIGN.add("F");
86 PRIMITIVE_JAVA_PARAMETER_SIGN.add("I");
87 PRIMITIVE_JAVA_PARAMETER_SIGN.add("J");
88 PRIMITIVE_JAVA_PARAMETER_SIGN.add("S");
89 PRIMITIVE_JAVA_PARAMETER_SIGN.add("Z");
90 }
91
92
93
94
95
96
97
98
99
100 public void processTypes(Set<Class> allIDLTypes, String classesDir,
101 Map operationTypes, TCKind searchedType)
102 throws ClassGenerationException {
103
104 for (Class typeClass : allIDLTypes) {
105
106 findType(typeClass, classesDir, operationTypes, searchedType);
107
108 }
109
110 }
111
112
113
114
115
116
117
118
119
120
121
122 protected void findType(Class typeName, String classesDir,
123
124 Map<String, SearchedType> operationTypes, TCKind searchedType)
125 throws ClassGenerationException {
126
127 if (isPrimitive(typeName.getName())) {
128 return;
129 }
130
131
132
133
134
135
136
137
138
139 if (isSearchedType(typeName, classesDir, searchedType)) {
140
141 if (typeName == null)
142 return;
143
144
145
146 SearchedType st = processType(typeName);
147
148
149 operationTypes.put(st.getTypeName(), st);
150 }
151 }
152
153
154
155
156
157
158
159
160
161
162 private static boolean isSearchedType(Class typeClass, String classesDir,
163 TCKind searchedType) throws ClassGenerationException {
164
165
166 TypeCode typeC = getCorbaTypeTypeCode(typeClass, classesDir);
167 if (typeC == null)
168 return false;
169
170 else
171 if (typeC.kind().equals(searchedType))
172 return true;
173
174
175 return false;
176 }
177
178
179
180
181
182
183
184
185
186
187
188 public static TypeCode getCorbaTypeTypeCode(Class typeClass,
189 String classesDir)
190 throws ClassGenerationException {
191 Class typeHelperClass = Util.classLoad(classesDir, typeClass.getName()
192 + "Helper");
193
194 if (typeHelperClass != null) {
195
196 return (TypeCode) invokeMethod(typeHelperClass, "type");
197 } else
198 return null;
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213 public static void encloseCorbaTypeInAny(Any paramAny,
214 Object origObj, Class typeClass, RuntimeInformation runtimeInfo, Map<String, TypeDef>typeDefs)
215 {
216 URLClassLoader classLoader = runtimeInfo.getCorbaClassLoader();
217 Class typeHelperClass = null;
218 try {
219 String typeClassStr = typeClass.getName();
220 try
221 {
222 typeHelperClass = classLoader.loadClass( typeClassStr
223 + "Helper");
224 } catch (ClassNotFoundException e) {
225
226
227 if (typeClassStr.endsWith("Impl"))
228 {
229 typeClassStr = typeClassStr.substring(0, typeClassStr.length() - 4);
230 typeHelperClass = classLoader.loadClass( typeClassStr
231 + "Helper");
232 }
233
234 }
235
236
237 if ((typeDefs.containsKey(typeClassStr)) && (typeHelperClass != null)) {
238 TypeDef typeDef = typeDefs.get(typeClassStr);
239 String aliasedClassName = typeDef.getAliasedClassName();
240 if (LOG.isDebugEnabled()) {
241 LOG.debug("typeDef:" + typeDef);
242 LOG.debug("aliasedClassName: " + aliasedClassName);
243 LOG.debug("typeHelperClass: " + typeHelperClass.getName());
244 LOG.debug("typeHelperClass:" + typeHelperClass + " with classloader: " + typeHelperClass.getClassLoader());
245 }
246 Class aliasedClass = null;
247 if (TypeUtils.isPrimitive(aliasedClassName)) {
248 aliasedClass = typeDef.getAliasedClass();
249 } else {
250
251
252 aliasedClass = Class.forName(aliasedClassName, false, classLoader);
253
254 }
255 if (LOG.isDebugEnabled()) {
256 LOG.debug("aliasedClass: " + aliasedClass + " with classloader: " + aliasedClass.getClassLoader());
257 LOG.debug("origObj: " + origObj.getClass().getName() + " with classloader: " + origObj.getClass().getClassLoader());
258 }
259
260 Object objectValue = null;
261 try {
262 objectValue = typeDef.getAliasedObject(origObj);
263 if (aliasedClass.equals(java.lang.Object.class)) {
264
265 if (LOG.isDebugEnabled()) {
266 LOG.debug("TypeDef of Any case");
267 }
268 aliasedClass = org.omg.CORBA.Any.class;
269 Any myAny = runtimeInfo.getOrb().create_any();
270
271 CorbaTransformationUtils.transformToAny(objectValue, myAny, runtimeInfo);
272 objectValue = myAny;
273 }
274 } catch (NoSuchFieldException e) {
275 e.printStackTrace();
276 } catch (IllegalArgumentException e) {
277 e.printStackTrace();
278 } catch (IllegalAccessException e) {
279 e.printStackTrace();
280 } catch (InvocationTargetException e) {
281 e.printStackTrace();
282 } catch (InstantiationException e) {
283 e.printStackTrace();
284 }
285 Method method = typeHelperClass.getMethod("insert", Any.class, aliasedClass);
286 try {
287
288 method.invoke(null, paramAny, objectValue);
289 } catch (IllegalArgumentException e) {
290 e.printStackTrace();
291 } catch (IllegalAccessException e) {
292 e.printStackTrace();
293 } catch (InvocationTargetException e) {
294 e.printStackTrace();
295 }
296 } else if (typeHelperClass != null) {
297 Method method = typeHelperClass.getMethod("insert", Any.class, classLoader.loadClass(origObj.getClass().getName()));
298 try {
299 method.invoke(null, paramAny, origObj);
300 } catch (IllegalArgumentException e) {
301 e.printStackTrace();
302 } catch (IllegalAccessException e) {
303 e.printStackTrace();
304 } catch (InvocationTargetException e) {
305 e.printStackTrace();
306 }
307
308 }
309 } catch (SecurityException e) {
310 e.printStackTrace();
311 } catch (NoSuchMethodException e) {
312 e.printStackTrace();
313 }catch (ClassNotFoundException e1) {
314 e1.printStackTrace();
315 }
316 }
317
318
319
320
321
322
323
324
325
326
327
328 public static Object extractCorbaTypeFromAny(Any paramAny,
329 String typeClassStr, URLClassLoader classLoader) {
330
331 ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
332 try {
333 Thread.currentThread().setContextClassLoader(classLoader);
334
335 try {
336 if (LOG.isDebugEnabled()) {
337 LOG.debug("To extract ANY of id: " + paramAny.type().id()
338 + " for class: " + typeClassStr);
339 LOG.debug("paramAny: " + paramAny.type().id()
340 + " for class: " + typeClassStr);
341 }
342 } catch (BadKind e1) {
343
344 e1.printStackTrace();
345 }
346
347 Class typeHelperClass = null;
348 try {
349 if (!typeClassStr.endsWith("Helper")) {
350 typeHelperClass = classLoader.loadClass(typeClassStr
351 + "Helper");
352 } else {
353 typeHelperClass = classLoader.loadClass(typeClassStr);
354 }
355 if (typeHelperClass != null) {
356 if (LOG.isDebugEnabled()) {
357 LOG.debug("typeHelperClass class:"
358 + typeHelperClass.getName());
359 LOG.debug("typeHelperClass classloader:"
360 + typeHelperClass.getClassLoader());
361 LOG.debug("Thread classloader:"
362 + Thread.currentThread()
363 .getContextClassLoader());
364 }
365 Method method = typeHelperClass.getMethod("extract",
366 Any.class);
367 try {
368
369 Object obj = method.invoke(null, paramAny);
370
371 return obj;
372 } catch (IllegalArgumentException e) {
373 e.printStackTrace();
374 } catch (IllegalAccessException e) {
375 e.printStackTrace();
376 } catch (InvocationTargetException e) {
377 e.printStackTrace();
378 }
379
380 }
381 } catch (ClassNotFoundException e) {
382 e.printStackTrace();
383 } catch (SecurityException e) {
384 e.printStackTrace();
385 } catch (NoSuchMethodException e) {
386 e.printStackTrace();
387 }
388 } finally {
389 Thread.currentThread().setContextClassLoader(oldCL);
390 }
391 return null;
392 }
393
394
395
396
397
398
399
400
401
402
403 public static Object readCorbaTypeFromAnyInputStream(InputStream paramAny, String typeClass, URLClassLoader classLoader)
404 {
405
406 Class typeHelperClass = null;
407 try {
408 if(!typeClass.endsWith("Helper")){
409 typeHelperClass = classLoader.loadClass( typeClass
410 + "Helper");
411 }else{
412 typeHelperClass = classLoader.loadClass( typeClass);
413 }
414
415 if (typeHelperClass != null) {
416 if (LOG.isDebugEnabled()) {
417 LOG.debug("Using helper for class: " + typeHelperClass);
418 }
419 Method method = typeHelperClass.getMethod("read", InputStream.class);
420 try {
421 Object obj = method.invoke(null, paramAny);
422
423 return obj;
424 } catch (IllegalArgumentException e) {
425
426 e.printStackTrace();
427 } catch (IllegalAccessException e) {
428
429 e.printStackTrace();
430 } catch (InvocationTargetException e) {
431
432 e.printStackTrace();
433 }
434
435 }
436 } catch (ClassNotFoundException e) {
437
438 e.printStackTrace();
439 } catch (SecurityException e) {
440
441 e.printStackTrace();
442 } catch (NoSuchMethodException e) {
443
444 e.printStackTrace();
445 }
446 return null;
447 }
448
449
450
451
452
453 public static SearchedType isSearchedType(String desc, boolean isArray,
454 Map allSearchedTypes, TCKind searchType) {
455
456 SearchedType st = null;
457
458 String type = getTypeFromTypeDescription(desc);
459
460 st = (SearchedType) allSearchedTypes.get(type);
461
462 return st;
463 }
464
465
466
467
468
469
470 public static String getTypeFromTypeDescription(String desc) {
471 String type = desc;
472 if (desc.startsWith("L") && desc.endsWith(";")) {
473 type = desc.substring(1, desc.length() - 1);
474 } else if (desc.startsWith("[")) {
475 type = desc.substring(getArrayDimmentionAsPrefix(desc).length(),
476 desc.length());
477 if (type.startsWith("L") && type.endsWith(";")) {
478 type = type.substring(1, type.length() - 1);
479 }
480 }
481 type = Util.replaceSeparatorWithDot(type);
482
483 return type;
484 }
485
486
487
488
489
490
491
492
493
494 public static SearchedType isSearchedTypeAll(String desc, boolean isArray,
495 Map allSearchedTypes) {
496 SearchedType searchedT = null;
497 if ((desc.length() <= 1 && !isArray) || (desc.length() <= 2 && isArray))
498 return null;
499
500 searchedT = isSearchedType(desc, isArray, allSearchedTypes, INTERFACE);
501 if (searchedT == null)
502 searchedT = isSearchedType(desc, isArray, allSearchedTypes, UNION);
503
504 if (searchedT == null)
505 searchedT = isSearchedType(desc, isArray, allSearchedTypes, ANY);
506
507 return searchedT;
508 }
509
510
511
512
513
514
515
516
517 protected Class getFieldType(String field, Method[] methods) {
518
519
520
521 for (Method method : methods) {
522 if (method.getName().equals(field)
523 && method.getParameterTypes().length == 1) {
524 return method.getParameterTypes()[0];
525 }
526 }
527 return null;
528 }
529
530
531
532
533
534
535
536
537 private static Object invokeMethod(Class clss, String methodName, Class...methodParameters){
538 try {
539
540 Method method = clss.getMethod(methodName, methodParameters);
541 return method.invoke(clss);
542
543 } catch (NoSuchMethodException ex) {
544 throw new Jbi4CorbaRuntimeException(
545 "UnionTypeUtils - invokeMethod: NoSuchMethodException - "
546 + methodName, ex);
547 } catch (SecurityException ex) {
548 throw new Jbi4CorbaRuntimeException(
549 "UnionTypeUtils - invokeMethod: SecurityException - "
550 + methodName, ex);
551 } catch (IllegalAccessException ex) {
552 throw new Jbi4CorbaRuntimeException(
553 "UnionTypeUtils - invokeMethod: IllegalAccessException - "
554 + methodName, ex);
555 } catch (IllegalArgumentException ex) {
556 throw new Jbi4CorbaRuntimeException(
557 "UnionTypeUtils - invokeMethod: IllegalArgumentException - "
558 + methodName, ex);
559 } catch (InvocationTargetException ex) {
560 throw new Jbi4CorbaRuntimeException(
561 "UnionTypeUtils - invokeMethod: InvocationTargetException - "
562 + methodName, ex);
563 }
564 }
565
566
567
568
569
570
571
572
573
574
575 protected static Class getTypeClass(String className, String classesDir,
576 Class interfaceClassType) throws ClassGenerationException {
577 Class typeClass = null;
578
579
580 if (isPrimitive(className)) {
581 typeClass = (Class) PRIMITIVE_NAME_TYPE_MAP.get(className);
582 } else {
583
584 typeClass = Util.classLoadQuiet(classesDir, className);
585 }
586
587
588 if (typeClass == null) {
589
590 String classWithInterfacePackage = interfaceClassType.getPackage()
591 .getName()
592 + "." + className;
593 typeClass = Util.classLoadQuiet(classesDir,
594 classWithInterfacePackage);
595 }
596 if (typeClass != null
597 && (typeClass.getName().startsWith("java.lang") || typeClass
598 .getName().startsWith("java.io"))) {
599
600
601
602 typeClass = null;
603 }
604 return typeClass;
605 }
606
607
608
609
610
611 protected abstract SearchedType processType(Class clsType);
612
613
614
615
616
617
618
619 protected abstract Map<String, Class> getFields(Class clsUnion);
620
621
622
623
624
625 protected abstract void setMethodTypes(MethodSignature methodSignature,
626 Set<String> typeList);
627
628
629
630
631
632
633
634 public static boolean isPrimitive(final String type) {
635 return PRIMITIVE_NAME_TYPE_MAP.containsKey(type);
636 }
637
638
639
640
641
642
643
644 public static boolean isPrimitiveParamSign(final String type) {
645 String typeNoArray = type;
646 int idx = -1;
647 idx = type.lastIndexOf('[');
648 if (idx >= 0) {
649 typeNoArray = typeNoArray.substring(idx + 1);
650 }
651 return PRIMITIVE_JAVA_PARAMETER_SIGN.contains(typeNoArray);
652 }
653
654
655
656
657
658
659
660 public static Class getJavaLangClassPrimitive(final String type) {
661 return PRIMITIVE_JAVA_LANG_TYPE_MAP.get(type);
662 }
663
664
665
666
667
668
669
670 public static boolean isJavaType(final String type) {
671 if (type.startsWith("java.lang") || type.startsWith("java.io"))
672 return true;
673
674 return false;
675 }
676
677
678
679
680
681
682
683 public static String getTypeName(Class type) {
684 if (type.isArray()) {
685
686 Class cl = type;
687 int dimensions = 0;
688 while (cl.isArray()) {
689 dimensions++;
690 cl = cl.getComponentType();
691 }
692 StringBuffer sb = new StringBuffer();
693 sb.append(cl.getName());
694 for (int i = 0; i < dimensions; i++) {
695 sb.append("[]");
696 }
697 return sb.toString();
698
699 }
700 return type.getName();
701 }
702
703
704
705
706
707
708
709 public static String getTypeNameWithoutBrackets(Class type) {
710 String typeStr = getTypeName(type);
711 int idx = typeStr.indexOf('[');
712 if (idx > 0) {
713 return typeStr.substring(0, idx);
714 }
715 return getTypeNameWithoutBrackets(typeStr);
716 }
717
718
719
720
721
722
723
724 public static String getTypeNameWithoutBrackets(String typeStr) {
725 int idx = typeStr.indexOf('[');
726 if (idx > 0) {
727 return typeStr.substring(0, idx);
728 }
729 return typeStr;
730 }
731
732
733
734
735
736
737
738 public static boolean isArray(String type) {
739 if (type.startsWith("[") && type.endsWith(";"))
740 return true;
741 return false;
742 }
743
744
745
746
747
748
749
750 public static int[] getObjectDimensions(Object obj) {
751 int dimension = 0;
752 int dimensions[];
753
754 Class objClass = obj.getClass();
755
756 while (objClass.isArray()) {
757 dimension++;
758 objClass = objClass.getComponentType();
759 }
760
761 dimensions = new int[dimension];
762 for (int i = 0; i < dimension; i++) {
763 dimensions[i] = 0;
764 }
765 int idxArr = 0;
766 Object tempObj = obj;
767 while (idxArr < dimension) {
768 dimensions[idxArr] = Array.getLength(tempObj);
769 LOG.debug("--> Arrays "+Arrays.toString((Object[])tempObj));
770 LOG.debug("--> Arrays "+Arrays.toString(dimensions));
771 if (dimensions[idxArr] == 0)
772 break;
773 tempObj = Array.get(tempObj, 0);
774 idxArr++;
775 }
776
777 return dimensions;
778 }
779
780
781
782
783
784
785
786 protected static List<String> extractParameters(String desc) {
787
788 List<String> listOfParams = new ArrayList<String>();
789 String params = desc.substring(1, desc.lastIndexOf(")"));
790
791 int index = 0;
792 StringBuffer strBuff = new StringBuffer();
793 while (index < params.length()) {
794 int ch = params.charAt(index);
795
796 switch (ch) {
797 case 'B':
798 case 'C':
799 case 'D':
800 case 'F':
801 case 'I':
802 case 'J':
803 case 'S':
804 case 'Z': {
805 strBuff.append(params.charAt(index));
806 listOfParams.add(strBuff.toString());
807 strBuff = new StringBuffer();
808 ++index;
809 break;
810 }
811 case 'L': {
812 int semicol = params.indexOf(';', index + 1);
813
814 if ((params.length() > semicol + 2)
815 && (params.charAt(semicol + 1) == '>')) {
816 semicol += 2;
817 }
818 strBuff.append(params.substring(index, semicol + 1));
819 listOfParams.add(strBuff.toString());
820 strBuff = new StringBuffer();
821 index = semicol + 1;
822 break;
823 }
824 case '[': {
825 strBuff.append('[');
826 ++index;
827 break;
828 }
829 }
830
831 }
832
833 return listOfParams;
834
835 }
836
837
838
839
840
841
842
843 protected static String extractReturnType(String desc) {
844
845 String returnType = desc.substring(desc.indexOf(")") + 1);
846
847 return returnType;
848 }
849
850
851
852
853
854
855
856 public static String getArrayDimmentionAsPrefix(Class cls) {
857 StringBuffer arrStr = new StringBuffer();
858 while (cls.isArray()) {
859 arrStr.append("[");
860 cls = cls.getComponentType();
861 }
862
863 return arrStr.toString();
864 }
865
866
867
868
869
870
871
872 public static String getArrayDimmentionAsPrefix(String desc) {
873 if (desc.startsWith("[")) {
874 return desc.substring(0, desc.lastIndexOf("[") + 1);
875 } else
876 return "";
877 }
878
879
880
881
882
883
884
885
886 public static boolean isEnumType(Class type, Set<String> enumSet) {
887
888 String simpleType = getTypeNameWithoutBrackets(type);
889
890 if (enumSet.contains(simpleType))
891 return true;
892
893 return false;
894 }
895
896
897
898
899
900
901
902 public static String extractTypeFromTypeCode(TypeCode tc)
903 {
904
905
906
907
908
909 try {
910 String tempType = tc.id();
911 String className = tc.name();
912 int idxStart = tempType.indexOf(':');
913 int idxEnd = tempType.lastIndexOf('/');
914 if (idxEnd < 0)
915 {
916 idxEnd = tempType.lastIndexOf('\\');
917 }
918 tempType = tempType.substring(idxStart + 1, idxEnd);
919
920 if (!tempType.contains(className)) {
921 tempType = tempType + '/' + className;
922 }
923
924 return Util.replaceSeparatorWithDot(tempType);
925 } catch (BadKind e) {
926 return null;
927 }
928 }
929
930
931
932
933 }