1    
2   
3   
4   
5   
6   
7   
8   package it.imolinfo.jbi4corba.webservice.generator;
9   
10  import it.imolinfo.jbi4corba.Logger;
11  import it.imolinfo.jbi4corba.LoggerFactory;
12  import it.imolinfo.jbi4corba.exception.ClassGenerationException;
13  import it.imolinfo.jbi4corba.exception.Jbi4CorbaException;
14  import it.imolinfo.jbi4corba.jbi.JbiServiceDescriptor;
15  import it.imolinfo.jbi4corba.jbi.Messages;
16  import it.imolinfo.jbi4corba.utils.HelperIDLJUtil;
17  import it.imolinfo.jbi4corba.webservice.generator.bcm.ConstructorAdapter;
18  import it.imolinfo.jbi4corba.webservice.generator.bcm.IdlToWsdlAdapter;
19  import it.imolinfo.jbi4corba.webservice.generator.bcm.InterfaceTypeClassAdapter;
20  import it.imolinfo.jbi4corba.webservice.generator.bcm.WebServiceAnnotationAdapter;
21  import it.imolinfo.jbi4corba.webservice.generator.typedef.TypeDef;
22  import it.imolinfo.jbi4corba.webservice.generator.typedef.TypeDefUtil;
23  
24  import java.io.BufferedWriter;
25  import java.io.File;
26  import java.io.FileWriter;
27  import java.io.IOException;
28  import java.io.PrintWriter;
29  import java.io.StringWriter;
30  import java.net.MalformedURLException;
31  import java.net.URISyntaxException;
32  import java.net.URL;
33  import java.net.URLClassLoader;
34  import java.util.ArrayList;
35  import java.util.Arrays;
36  import java.util.Collection;
37  import java.util.HashMap;
38  import java.util.HashSet;
39  import java.util.Hashtable;
40  import java.util.Iterator;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Set;
44  import java.util.Map.Entry;
45  
46  import org.apache.cxf.helpers.ServiceUtils;
47  import org.objectweb.asm.ClassReader;
48  import org.objectweb.asm.ClassVisitor;
49  import org.objectweb.asm.ClassWriter;
50  import org.objectweb.asm.util.CheckClassAdapter;
51  import org.objectweb.asm.util.TraceClassVisitor;
52  
53  
54  
55  
56  
57  
58  @SuppressWarnings("unchecked")
59  public class ProviderServiceClassesGenerator {
60  
61    
62  
63  
64    private static final Logger LOG
65      = LoggerFactory.getLogger(ProviderServiceClassesGenerator.class);
66    
67    private static final Messages MESSAGES = 
68    	Messages.getMessages(ProviderServiceClassesGenerator.class);
69  
70    
71  
72  
73    protected static CodeGenerationProperties codeGenerationProperties
74      = new CodeGenerationProperties();
75  
76    
77    private URLClassLoader urlClassLoader;
78    
79    private ChildFirstClassLoader originalClassLoader;
80    
81    
82  
83  
84  
85  
86  
87    
88    private static Hashtable<String, Hashtable<JbiServiceDescriptor,ClientCorbaClassesHolder>> 
89            idlClassTable =new Hashtable<String, Hashtable<JbiServiceDescriptor,ClientCorbaClassesHolder>>();
90    
91     
92  
93  
94     static private ProviderServiceClassesGenerator instance = null; 
95    
96    
97    
98  
99  
100   public ProviderServiceClassesGenerator() {   
101   }
102 
103   
104 
105 
106 
107 
108 
109 
110   public ProviderServiceClassesGenerator(CodeGenerationProperties prop) {
111     codeGenerationProperties = prop;
112   }
113 
114  
115   
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147   public List<ClientCorbaClassesHolder> generateProviderServiceClasses(
148           JbiServiceDescriptor serviceDescriptor,
149           String workdir,
150           String libDirName, String portTypeName) throws ClassGenerationException {
151           
152           List<JbiServiceDescriptor> serviceDescriptorList=new ArrayList<JbiServiceDescriptor>();
153           List<String> portTypeNameList= new ArrayList<String>();
154           
155           serviceDescriptorList.add(serviceDescriptor);
156           portTypeNameList.add(portTypeName);
157           
158          return generateProviderServiceClasses(serviceDescriptorList, workdir, libDirName, portTypeNameList);
159            
160   }
161   
162   
163    public List<ClientCorbaClassesHolder> generateProviderServiceClasses(
164          JbiServiceDescriptor serviceDescriptor,
165           String workdir,
166           List<String> jars, String portTypeName) throws ClassGenerationException {
167    
168    
169           ArrayList<JbiServiceDescriptor> serviceDescriptorList=new ArrayList<JbiServiceDescriptor>();
170           ArrayList<String> portTypeNameList= new ArrayList<String>();
171           
172           serviceDescriptorList.add(serviceDescriptor);
173           portTypeNameList.add(portTypeName);
174           
175           return generateProviderServiceClasses(serviceDescriptorList, workdir, jars, portTypeNameList);
176    
177    }
178   
179   
180   
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212   public List<ClientCorbaClassesHolder> generateProviderServiceClasses(
213           List<JbiServiceDescriptor> serviceDescriptorList,
214           String workdir,
215           String libDirName, List<String> portTypeName) throws ClassGenerationException   {
216       
217     
218      portTypeName=new ArrayList<String>();
219      for(int i=0;i<serviceDescriptorList.size();i++){
220         portTypeName.add(serviceDescriptorList.get(i).getPortTypeName().getLocalPart());
221      }
222       
223     LOG.debug(">>>>> generateProviderServiceClasses - begin");
224 
225     
226     
227     
228     List<String> jars = null;
229     LOG.debug("codeGenerationProperties.isValueTypeImplementationWithToStringAndEquals():" + codeGenerationProperties.isValueTypeImplementationWithToStringAndEquals());
230     
231     if (codeGenerationProperties.isValueTypeImplementationWithToStringAndEquals()) {
232       jars = Util.prepareClassPath(libDirName);
233     }
234     
235     
236     
237     
238     if (jars == null) {
239                 
240         Class xmlAccessTypeClass=javax.xml.bind.annotation.XmlSeeAlso.class;
241         if ((xmlAccessTypeClass != null) 
242                 && (xmlAccessTypeClass.getProtectionDomain() != null)
243                 && (xmlAccessTypeClass.getProtectionDomain().getCodeSource() != null)) { 
244             URL jaxbJarLocation = xmlAccessTypeClass.getProtectionDomain().getCodeSource().getLocation();
245             LOG.info("JAXB Location: " + jaxbJarLocation);
246             try {
247             LOG.info("JAXB Location to URI: " + jaxbJarLocation.toURI());
248             } catch(URISyntaxException e) {}
249             
250             LOG.info("JAXB Location getPath: " + jaxbJarLocation.getPath());
251             if ((jaxbJarLocation != null) && (jaxbJarLocation.getFile() != null)) {
252                 File jaxbLocationFile;
253                 
254                 
255                 
256                 
257                 try {
258                 	jaxbLocationFile = new File(jaxbJarLocation.toURI());
259                 } catch(URISyntaxException e) {
260                 	jaxbLocationFile = new File(jaxbJarLocation.getPath());
261                 } catch(IllegalArgumentException e) {     
262                     
263                     jaxbLocationFile = new File(jaxbJarLocation.getPath());
264                 }
265 
266                 
267                 
268                 jars = new ArrayList<String>();
269                 try {
270 					jars.add(jaxbLocationFile.getCanonicalPath());
271 				} catch (IOException e) {
272 					Object[] args = new Object[] { jaxbLocationFile, e.getMessage() };
273 					LOG.error("CRB000507_Error_getting_canonicalPath_from_file",
274 							args, e);
275 					throw new ClassGenerationException(
276 							"CRB000507_Error_getting_canonicalPath_from_file",
277 							args, e);
278 				}                
279             }
280         }
281     }
282  
283     List<ClientCorbaClassesHolder> list = generateProviderServiceClasses(
284             serviceDescriptorList,
285             workdir,
286             jars, 
287             portTypeName);
288 
289     LOG.debug(">>>>> generateProviderServiceClasses - end");
290     return list;
291   }
292   
293   
294   
295 
296 
297 
298 
299 
300 
301 
302 
303 
304 
305 
306 
307 
308 
309 
310 
311 
312 
313 
314 
315 
316 
317 
318   public List<ClientCorbaClassesHolder> generateProviderServiceClasses(
319           List<JbiServiceDescriptor> serviceDescriptorList,
320           String workdir,
321           List<String> jars, List<String> portTypeName) throws ClassGenerationException {
322           
323     LOG.debug(">>>>> generateProviderServiceClasses - begin");
324     
325     boolean isRoleConsumer = (
326     		serviceDescriptorList.size() > 0 && 
327     		serviceDescriptorList.get(0).getRole() != null && 
328     		serviceDescriptorList.get(0).getRole().equalsIgnoreCase(JbiServiceDescriptor.CONSUMER));
329 
330    
331     String workdirsrc = workdir + "/src";
332     String workdirclasses = workdir + "/classes";
333     String workdirOrigclasses = workdir + "/origclasses"; 
334     String serviceNameSpace = null;
335     
336     
337    
338     String idlFilename = serviceDescriptorList.get(0).getIdlFileNameDirectory()
339                        + File.separator
340                        + serviceDescriptorList.get(0).getIdlFileName();
341                        
342     
343     
344     
345     
346     
347     if (isRoleConsumer) {
348     	HelperIDLJUtil.idljPoaTie(workdirsrc,
349     	        serviceDescriptorList.get(0).getIdlFileNameDirectory(),
350     	        idlFilename);
351     }
352     else
353     {
354         HelperIDLJUtil.idlj(workdirsrc,
355                 serviceDescriptorList.get(0).getIdlFileNameDirectory(),
356                 idlFilename);
357     }
358 
359 
360     
361     
362     
363     Map<String, List<MethodSignature>> mapOperationsMethod
364       = Util.extractMethodSignatureOfTheCorbaOperations(workdirsrc);
365     
366     
367     
368     
369     List<String> vtList = Util.valueTypesImpl(workdirsrc,
370       codeGenerationProperties.isValueTypeImplementationWithToStringAndEquals());
371     LOG.debug("end creating java sources implementation for value types.");   
372     
373     
374     
375     
376     List<String> javaSources = null;
377     try {
378       javaSources = Util.findJavaSources(workdir);
379       LOG.debug("find java sources ... done");
380     } catch (Jbi4CorbaException e) {
381       Object[] args = new Object[] { workdir };
382 
383       LOG.error("CRB000522_Error_finding_java_classes", args, e);
384       throw new ClassGenerationException(
385               "CRB000522_Error_finding_java_classes", args, e);
386     }
387 
388     Util.compileJavaClasses(workdirsrc,
389                             workdirclasses,
390                             javaSources,
391                             jars,
392                             null);
393 
394     LOG.debug("compileJavaClasses 1 of 2 ... done");
395 
396     
397     
398     
399     Map<String, List<String>> corbaEnumMap
400       = Util.replaceCorbaEnumaration(workdirsrc, workdirclasses);
401 
402     
403     
404     
405     javaSources = null;
406     try {
407       javaSources = Util.findJavaSources(workdir);
408       LOG.debug("find java sources ... done");
409     } catch (Jbi4CorbaException e) {
410       Object[] args = new Object[] { workdir };
411 
412       LOG.error("CRB000522_Error_finding_java_classes", args, e);
413       throw new ClassGenerationException(
414               "CRB000522_Error_finding_java_classes", args, e);
415     }
416     
417     
418     
419     
420     List<String> packages = new ArrayList<String>();    
421     List<String> filesCreated = new ArrayList<String>();
422     LOG.debug("javaSources: "+javaSources);
423     for (String source : javaSources) {
424         
425         String tempworkdirsrc = workdirsrc;
426         if (System.getProperty("os.name").indexOf("Win") >= 0) {
427             
428             LOG.debug("System Operation [ Windows ]");
429             
430             try {
431                 File myTempDir = new File(workdirsrc);
432                 tempworkdirsrc = myTempDir.getCanonicalPath();
433                 
434             } catch (IOException ioe) {
435                 String msg = "Error in getting temp dir:" + ioe.getMessage();
436                 throw new ClassGenerationException(msg, ioe);
437             }
438         }
439         
440         int posPath = source.indexOf(tempworkdirsrc);
441         
442         String classSourceName = source.substring(posPath + tempworkdirsrc.length() + 1 , source.length());  
443         
444         String className = classSourceName.replace(File.separator, ".");
445         String classNameMinusJava = className;
446         if ((className.endsWith(".java"))) {
447             classNameMinusJava = className.substring(0,className.length() - 5);            
448         }
449         String namespace = ServiceUtils.makeNamespaceFromClassName(classNameMinusJava, "http");        
450         int lastSlashIndex = classSourceName.lastIndexOf(File.separator);
451         String packageNameWithSlash = lastSlashIndex!= -1?classSourceName.substring(0,lastSlashIndex):classSourceName;
452         if (packageNameWithSlash.startsWith(File.separator)){
453             packageNameWithSlash=packageNameWithSlash.substring(1);
454         }
455         LOG.debug("packageNameWithSlash: "+packageNameWithSlash);
456         String packageName =  packageNameWithSlash.replace(File.separator, ".");         
457         if(classSourceName.equals(className)){
458         	packageName="";
459         }
460          
461         if (!packages.contains(packageName) && !packageName.equals("")) {
462             String returnPath = createsPackageInfo(namespace, packageName, workdirsrc);
463             filesCreated.add(returnPath);
464             packages.add(packageName);
465         }
466     }
467     javaSources.addAll(filesCreated);
468     
469     
470     
471     Util.compileJavaClasses(workdirsrc,
472                             workdirclasses,
473                             javaSources,
474                             jars,
475                             null);
476     
477     
478      Util.compileJavaClasses(workdirsrc,
479                             workdirOrigclasses,
480                             javaSources,
481                             jars,
482                             null);
483      LOG.debug("compileJavaClasses 2 of 2 ... done");     
484      
485      
486      
487      
488      
489      
490      
491      Map<String,TypeDef> typeDefs = null;
492      try {
493       	
494      	typeDefs = TypeDefUtil.getTypeDefs(workdirclasses);
495      	
496      	if (LOG.isDebugEnabled()) {     
497      		LOG.debug("TypeDefs found: " + typeDefs.size());
498      		Iterator itTypeDefs = typeDefs.keySet().iterator();
499      		while (itTypeDefs.hasNext()) {
500      			LOG.debug("Found TypeDef:" + typeDefs.get(itTypeDefs.next()));
501      		}
502      	}
503      } catch (Jbi4CorbaException e) {
504          Object[] args = new Object[] { workdir };
505          LOG.error("CRB000522_Error_creating_typedef_classes", args, e);
506          throw new ClassGenerationException(
507                  "CRB000522_Error_creating_typedef_classes", args, e);
508      }        
509      
510      
511      
512      
513   	Iterator itTypeDefs = typeDefs.keySet().iterator();
514   	while (itTypeDefs.hasNext()) {
515   		String typeDefKey = (String) itTypeDefs.next();
516   		if (LOG.isDebugEnabled()) {
517   			LOG.debug("Creating class for TypeDef:" + typeDefKey);
518   		} 		
519   		typeDefs.get(typeDefKey).createTypeDefClass(workdirclasses);  		
520   		typeDefs.get(typeDefKey).createTypeDefClass(workdirOrigclasses);
521   	}     
522 
523     
524     
525     
526     Map<String, List<String>> onewayMap
527       = Util.findOnewayOperations(workdirclasses);
528 
529     updateOnewayMethodSignature(onewayMap, mapOperationsMethod);
530 
531     
532     
533     
534     Set<String> exceptions = new HashSet<String>();
535     
536     
537     
538     
539     
540     /
541 
542 
543 
544 
545 
546 
547 
548 
549 
550 
551 
552 
553 
554 
555 
556 
557 
558 
559 
560 
561 
562 
563 
564 
565 
566 
567 
568 
569 
570 
571 
572 
573 
574 
575 
576 
577 
578 
579 
580 
581 
582 
583 
584 
585 
586 
587 
588 
589 
590 
591 
592 
593 
594 
595 
596 
597 
598 
599 
600 
601 
602 
603 
604 
605 
606 
607 
608 
609 
610 
611 
612 
613 
614 
615 
616 
617 
618 
619 
620 
621 
622 
623 
624 
625 
626 
627 
628 
629 
630 
631 
632 
633 
634 
635 
636 
637 
638 
639 
640 
641 
642 
643 
644 
645 
646 
647 
648 
649 
650 
651 
652 
653 
654 
655 
656 
657 
658 
659 
660 
661 
662 
663 
664 
665 
666 
667 
668 
669 
670 
671 
672 
673 
674 
675 
676 
677 
678 
679 
680 
681 
682 
683 
684 
685 
686 
687 
688 
689 
690 
691 
692 
693 
694 
695 
696 
697 
698 
699 
700 
701 
702 
703 
704 
705 
706 
707 
708 
709 
710 
711 
712 
713 
714 
715 
716 
717 
718 
719 
720 
721 
722 
723 
724 
725 
726 
727 
728 
729 
730 
731 
732 
733 
734 
735 
736 
737 
738 
739 
740 
741 
742 
743 
744 
745 
746 
747 
748 
749 
750 
751 
752 
753 
754 
755 
756 
757 
758 
759 
760 
761 
762 
763 
764 
765 
766 
767 
768 
769 
770 
771 
772 
773 
774 
775 
776 
777 
778 
779 
780 
781 
782 
783 
784 
785 
786 
787 
788 
789 
790 
791 
792 
793 
794 
795 
796 
797 
798 
799 
800 
801 
802 
803 
804 
805 
806 
807 
808 
809 
810 
811 
812   private void addUnionWrappers(Map<String, UnionType> operationUnionTypes,
813 		String workdirclasses) throws ClassGenerationException {
814 
815 	
816 	for (Entry<String, UnionType> entry : operationUnionTypes.entrySet())
817 	{
818 		UnionType union = entry.getValue();
819 		
820 		UnionTypeUtils.createUnionClassWrapper(union, operationUnionTypes, workdirclasses);
821 	}
822 	
823 }
824 
825 
826 
827 
828 
829 
830 
831 
832 
833    private String tweakConstructor(
834             String className,
835             String classesDirName, 
836             Map<String, InterfaceType> allInterfaceTypes, boolean isException) throws ClassGenerationException {
837 
838            	
839 
840         ClassWriter  cw = new ClassWriter(true); 
841         ClassVisitor cc = new CheckClassAdapter(cw);
842         StringWriter sw = new StringWriter();
843         ClassVisitor tv = new TraceClassVisitor(cc, new PrintWriter(sw));
844 
845         
846         ConstructorAdapter cv = new ConstructorAdapter(tv, cw, className,allInterfaceTypes, isException);
847 
848         ClassReader cr = Util.getAsmCLassReader(classesDirName, className);
849 
850         
851         cr.accept(cv, true);
852 
853         byte [] newBytecode = cw.toByteArray();
854 
855         
856 
857         String relativeFileName = className.replace('/', File.separatorChar);
858 
859         Util.saveAsJavaClass(classesDirName, relativeFileName, newBytecode);
860         
861         return className.replace('/', '.');
862     }
863    
864 
865 
866 
867 
868 
869 
870 
871 
872 
873 
874   private void tweakInterfacesTypes(String workdirclasses,String Operation, Map<String, InterfaceType> opIntTypes) throws ClassGenerationException {
875 
876 	        
877 	        ClassWriter  cw = new ClassWriter(true); 
878 		    ClassVisitor cc = new CheckClassAdapter(cw);
879 		  
880 		    InterfaceTypeClassAdapter clsa = new InterfaceTypeClassAdapter(cc, opIntTypes);
881 		    
882 		    String absPath = workdirclasses + File.separator
883 		      + Operation.replace('.', File.separatorChar) + ".class";
884 
885 		    ClassReader cr = Util.getAsmCLassReader(absPath);
886 
887 		    cr.accept(clsa, true);  
888 
889 		    byte [] newBytecode = cw.toByteArray();
890 		    
891 		    Util.saveAsJavaClass(absPath, newBytecode);
892 		
893 }
894     
895   
896 
897 
898 
899 
900 
901 
902 
903 
904 
905 
906 
907 
908 
909 
910 
911 
912 
913 
914 
915 
916 
917     
918  
919 
920 
921 
922 
923 
924 
925 
926 
927   private void tweakUnionTypes(String workdirclasses, Collection<UnionType> operationUnionTypes) throws ClassGenerationException {
928 
929 	  for ( UnionType union : operationUnionTypes)
930 		{
931 			
932 			ClassWriter  cw = new ClassWriter(true); 
933 		    ClassVisitor cc = new CheckClassAdapter(cw);
934 		    
935 		    UnionTypeClassAdapter clsa = new UnionTypeClassAdapter(cc, union.getTypeFieldNameList());
936 		    
937 		    String absPath = workdirclasses + File.separator
938 		      + union.getTypeName().replace('.', File.separatorChar) + ".class";
939 
940 		    ClassReader cr = Util.getAsmCLassReader(absPath);
941 
942 		    cr.accept(clsa, true);  
943 
944 		    byte [] newBytecode = cw.toByteArray();
945 
946 		    Util.saveAsJavaClass(absPath, newBytecode);
947 		}
948 }
949   
950   
951 
952 
953 
954 
955 
956   private void tweakOperationsWithUnionTypes(String workdirclasses, Class operationClass, Map<String, UnionType> allUnionTypes) throws ClassGenerationException {
957 
958 			ClassWriter  cw = new ClassWriter(true); 
959 		    ClassVisitor cc = new CheckClassAdapter(cw);
960 		    
961 		    OperationWithUnionTypesAdapter clsa = new OperationWithUnionTypesAdapter(cc, allUnionTypes, workdirclasses);
962 		    
963 		    String absPath = workdirclasses + File.separator
964 		      + operationClass.getName().replace('.', File.separatorChar) + ".class";
965 
966 		    ClassReader cr = Util.getAsmCLassReader(absPath);
967 
968 		    cr.accept(clsa, true);  
969 
970 		    byte [] newBytecode = cw.toByteArray();
971 
972 		    Util.saveAsJavaClass(absPath, newBytecode);
973 	
974 }
975 
976   
977 
978 
979 
980 
981   
982 private void tweakOperationsWithAnyTypes(String workdirclasses, Class operationClass) throws ClassGenerationException {
983 
984 	    ClassWriter  cw = new ClassWriter(true); 
985 	    ClassVisitor cc = new CheckClassAdapter(cw);
986 	    
987 	    OperationWithAnyTypesAdapter clsa = new OperationWithAnyTypesAdapter(cc);
988 	    
989 	    String absPath = workdirclasses + File.separator
990 	      + operationClass.getName().replace('.', File.separatorChar) + ".class";
991 
992 	    ClassReader cr = Util.getAsmCLassReader(absPath);
993 
994 	    cr.accept(clsa, true);  
995 
996 	    byte [] newBytecode = cw.toByteArray();
997 
998 	    Util.saveAsJavaClass(absPath, newBytecode);
999 		
1000 	}
1001   
1002 
1003 
1004 
1005 
1006 
1007 
1008   public String createsPackageInfo(String namespace, String packagePath, String dirClasses) throws ClassGenerationException {
1009            
1010       String returnPath = null;
1011       String packageInfoJavaFileName=null;
1012       if(!packagePath.equals("")){
1013     	  packageInfoJavaFileName = dirClasses + File.separator + packagePath.replace(".", File.separator) + File.separator + "package-info.java";      
1014       }else{
1015     	  packageInfoJavaFileName = dirClasses  + File.separator + "package-info.java";
1016       }
1017       LOG.debug("Creating the package-info.java file into the directory: " + packageInfoJavaFileName);      
1018       File packageInfoJavaFile = new File(packageInfoJavaFileName);
1019       StringBuffer str = new StringBuffer();
1020       str.append("@javax.xml.bind.annotation.XmlSchema(namespace=\"")
1021       .append(namespace)
1022       .append("\"")
1023       .append(",attributeFormDefault=javax.xml.bind.annotation.XmlNsForm.QUALIFIED")
1024       .append("," +
1025       		"elementFormDefault=javax.xml.bind.annotation.XmlNsForm.QUALIFIED)")      		
1026       .append("\n")
1027       .append("package ")
1028       .append(packagePath)
1029       .append(";");
1030       try {
1031     	  
1032         packageInfoJavaFile.createNewFile();
1033         FileWriter fout = new FileWriter(packageInfoJavaFile);
1034         fout.write(str.toString());
1035         fout.flush();
1036         fout.close();
1037         returnPath = packageInfoJavaFile.getCanonicalPath();
1038     } catch (IOException e) {
1039     	String msg=MESSAGES.getString("CRB000561_Error_creating_package_info_file", 
1040     			new Object[] {e.getMessage()});
1041         LOG.error(msg,e);
1042         throw new ClassGenerationException(msg,e);
1043 
1044     }
1045     return returnPath;      
1046   }
1047  
1048 
1049   
1050 
1051 
1052 
1053 
1054 
1055 
1056 
1057 
1058 
1059 
1060 
1061   protected void updateOnewayMethodSignature(
1062     Map<String, List<String>> onewayMap,
1063     Map<String, List<MethodSignature>> mapOperationsMethod) {
1064 
1065     Set<String> onewayClasses = onewayMap.keySet();
1066 
1067     
1068     for (String onewayClass : onewayClasses) {
1069       
1070       List<MethodSignature> methodSignatureList
1071          = mapOperationsMethod.get(onewayClass);
1072       LOG.debug("onewayClass=" + onewayClass
1073         + " => methodSignatureList=" + methodSignatureList);
1074 
1075       
1076       List<String> onewayMethodList = onewayMap.get(onewayClass);
1077 
1078       
1079       
1080       for (String onewayMethod : onewayMethodList) {
1081         boolean ok = setOnewayPropertyOnTheMethodSignature(onewayMethod,
1082                                                            methodSignatureList);
1083         if (! ok) {
1084           Object [] args = new Object[] {onewayMethod};
1085           LOG.warn("CRB000547_OnewayMethodNotFound", args);
1086         }
1087       }
1088 
1089     }
1090   }
1091 
1092   
1093 
1094 
1095 
1096 
1097 
1098 
1099 
1100 
1101 
1102 
1103   protected boolean setOnewayPropertyOnTheMethodSignature(
1104     String onewayMethod, List<MethodSignature> methodSignatureList) {
1105 
1106     if (onewayMethod == null || methodSignatureList == null) {
1107       LOG.warn("CRB000548_NullParameter");
1108       return false;
1109     }
1110 
1111     boolean found = false;
1112 
1113     for (MethodSignature methodSignature : methodSignatureList) {
1114 
1115       
1116 
1117       if (methodSignature.getMethodName().equals(onewayMethod)) {
1118         found = true;
1119         methodSignature.setOneway(true);
1120       }
1121     }
1122 
1123     LOG.debug("oneway method found:" + found);
1124     return found;
1125   }
1126   
1127   
1128   
1129 
1130 
1131 
1132 
1133 
1134 
1135 
1136 
1137 
1138 
1139   private Set<String> tweakCorbaOperation(String qualifiedJavaName, String dir,
1140     List<MethodSignature> methodSignatureList, String nameSpace, String portTypeName, Map<String, UnionType> unionTypes, Map<String, InterfaceType> interfaceTypes, Set<Class> allTypes) throws ClassGenerationException {
1141 
1142     ClassWriter  cw = new ClassWriter(true); 
1143     ClassVisitor cc = new CheckClassAdapter(cw);
1144     StringWriter sw = new StringWriter();
1145     ClassVisitor tv = new TraceClassVisitor(cc, new PrintWriter(sw));
1146 
1147     
1148     WebServiceAnnotationAdapter cv
1149       = new WebServiceAnnotationAdapter(tv, cw, methodSignatureList, nameSpace, portTypeName, dir, unionTypes,interfaceTypes, allTypes);  
1150 
1151     String absPath = dir + File.separator
1152       + qualifiedJavaName.replace('.', File.separatorChar) + ".class";
1153 
1154     ClassReader cr = Util.getAsmCLassReader(absPath);
1155 
1156     cr.accept(cv, true);
1157     LOG.debug("ClassReader.accept ... done");
1158 
1159     LOG.debug("output of tracer during creation of class: " + absPath + "\n"
1160       + sw.toString());
1161 
1162     byte [] newBytecode = cw.toByteArray();
1163 
1164     Util.saveAsJavaClass(absPath, newBytecode);
1165     
1166     return cv.getExceptionsThrown(); 
1167   }
1168 
1169     
1170 
1171 
1172 
1173 
1174 
1175 
1176 
1177 
1178 
1179 
1180 
1181 
1182 
1183     private String tweakIdlToWsdlClasses(
1184             String className,
1185             String classesDirName, Map<String, UnionType> allUnionTypes, Map<String,String> fieldsUniontype,Map<String, InterfaceType> allInterfaceTypes, Map<String,String> fieldsInterfacetype, boolean isException,boolean excludeType) throws ClassGenerationException {
1186 
1187            	LOG.debug("CRB000562_tweakIdlToWsdlClasses_begin");
1188 
1189     		LOG.debug("CRB000563_tweakIdlToWsdlClasses_class_in_dir", 
1190         		new Object[]{className, classesDirName});
1191 
1192         ClassWriter  cw = new ClassWriter(true); 
1193         ClassVisitor cc = new CheckClassAdapter(cw);
1194         StringWriter sw = new StringWriter();
1195         ClassVisitor tv = new TraceClassVisitor(cc, new PrintWriter(sw));
1196 
1197         
1198         IdlToWsdlAdapter cv = new IdlToWsdlAdapter(tv, cw, className, allUnionTypes, fieldsUniontype,allInterfaceTypes,fieldsInterfacetype, isException,excludeType);
1199 
1200         ClassReader cr = Util.getAsmCLassReader(classesDirName, className);
1201 
1202         LOG.debug("getAsmCLassReader ... done");
1203 
1204         cr.accept(cv, true);
1205         LOG.debug("ClassReader.accept ... done");
1206 
1207         LOG.debug("output of tracer during creation of class: "
1208                 + className + "\n" + sw.toString());
1209 
1210         byte [] newBytecode = cw.toByteArray();
1211 
1212         
1213 
1214         String relativeFileName = className.replace('/', File.separatorChar);
1215 
1216         Util.saveAsJavaClass(classesDirName, relativeFileName, newBytecode);
1217 
1218         
1219         
1220         LOG.debug("<<<<<<<<<< tweakIdlToWsdlClasses - end");
1221         return className.replace('/', '.');
1222     }
1223 
1224     
1225     
1226     
1227 
1228     
1229 
1230 
1231 
1232 
1233 
1234 
1235     private String replaceDotWithSlash(String s) {
1236         LOG.debug("replaceDotWithSlash. the input is " + s);
1237         if (s == null) {
1238             LOG.debug("replaceDotWithSlash. "
1239                     + "the input is null. returning empty String");
1240             return "";
1241         }
1242         
1243         if ("".equals(s)) {
1244             LOG.debug("replaceDotWithSlash. "
1245                     + "the input is an empty String. returning empty String");
1246             return "";
1247         }
1248         
1249         String res = s.replaceAll("\\.", "/");
1250         LOG.debug("replaceDotWithSlash. "
1251                 + "the input is " + s + " returning " + res);
1252         return res;
1253     }
1254 
1255     
1256 
1257 
1258 
1259 
1260 
1261 
1262 
1263 
1264 
1265 
1266 
1267 
1268     private List<ClientCorbaClassesHolder> findGeneratedServices(
1269             String classesDir) throws ClassGenerationException {
1270 
1271         LOG.debug(">>>>>>>>>> findGeneratedServices - begin");
1272        
1273         urlClassLoader = null;
1274         String protocol = null;
1275         try {
1276 
1277             if (System.getProperty("os.name").indexOf("Win") >=0) {
1278                 protocol = "file:///";
1279             } else {
1280                 protocol = "file://";
1281             }
1282 
1283             File fcd = new File(classesDir);
1284             LOG.debug("ClassesDir.getAbsolutePath=" + fcd.getAbsolutePath());
1285 
1286             urlClassLoader = new URLClassLoader(
1287                 new URL[] { new URL(protocol + fcd.getAbsolutePath()  + "/") },
1288                 this.getClass().getClassLoader());
1289             
1290           
1291 
1292             LOG.debug("url classloader: "
1293                     + Arrays.asList(urlClassLoader.getURLs()));
1294         } catch (MalformedURLException e) {
1295             Object[] args = new Object[] {
1296                     protocol + new File(classesDir).getAbsolutePath() + "/" };
1297 
1298             LOG.error("CRB000526_Could_not_instantiate_the_url_class_loader",
1299                       args, e);
1300             throw new ClassGenerationException(
1301                     "CRB000526_Could_not_instantiate_the_url_class_loader",
1302                     args, e);
1303         }
1304 
1305         LOG.debug("classes dir: " + classesDir);
1306 
1307         List<Class> generatedClasses
1308             = Util.findGeneratedClasses(classesDir, urlClassLoader);
1309 
1310         LOG.debug("generated classes: " + generatedClasses);
1311         for (int i = 0; i < generatedClasses.size(); i++) {
1312           LOG.debug("CRB000564_Generated_class", 
1313         			new Object[] {generatedClasses.get(i)});
1314         }
1315 
1316         LOG.debug("<<<<<<<<<< findGeneratedServices - end");
1317         return filterInterfaceClasses(generatedClasses, urlClassLoader);
1318         
1319     }
1320     
1321     private void setOrginalClassLoader(String origclassesDir) throws ClassGenerationException{
1322     	 originalClassLoader=null;
1323     	 String protocol = null;
1324     	 try {
1325 
1326              if (System.getProperty("os.name").indexOf("Win") >=0) {
1327                  protocol = "file:///";
1328              } else {
1329                  protocol = "file://";
1330              }
1331 
1332              File fcd = new File(origclassesDir);
1333              LOG.debug("ClassesDir.getAbsolutePath=" + fcd.getAbsolutePath());
1334              
1335              originalClassLoader=new ChildFirstClassLoader(new URL[] { new URL(protocol + fcd.getAbsolutePath()  + "/") },
1336 					   this.getClass().getClassLoader());
1337              
1338                           
1339              LOG.debug("url classloader: "
1340                      + Arrays.asList(originalClassLoader.getURLs()));
1341          } catch (MalformedURLException e) {
1342              Object[] args = new Object[] {
1343                      protocol + new File(origclassesDir).getAbsolutePath() + "/" };
1344 
1345              LOG.error("CRB000526_Could_not_instantiate_the_class_loader",
1346                        args, e);
1347              throw new ClassGenerationException(
1348                      "CRB000526_Could_not_instantiate_the_class_loader",
1349                      args, e);
1350          }
1351     	 
1352     	
1353     }
1354 
1355     
1356 
1357 
1358 
1359 
1360 
1361 
1362 
1363 
1364 
1365 
1366 
1367 
1368     private List<ClientCorbaClassesHolder> filterInterfaceClasses(
1369             List<Class> classes,
1370             ClassLoader classLoader) {
1371 
1372         LOG.debug(">>>>>>>>>> filterInterfaceClasses - begin");
1373 
1374         List<ClientCorbaClassesHolder> result
1375                 = new ArrayList<ClientCorbaClassesHolder>();
1376 
1377         for (Class clazz : classes) {
1378 
1379             
1380             
1381             if (clazz.isInterface()
1382                     && org.omg.CORBA.Object.class.isAssignableFrom(clazz)
1383                     && (! clazz.getName().startsWith("org.omg"))) {
1384 
1385                 
1386 
1387 
1388 
1389 
1390 
1391                 try {
1392                     LOG.debug("trying if [" + clazz.getName()
1393                             + "Helper] exists");
1394                     Class helperClass
1395                             = classLoader.loadClass(clazz.getName() + "Helper");
1396 
1397                     LOG.debug("trying if [" + clazz.getName()
1398                             + "Operations] exists");
1399                     Class operationsClass = classLoader.loadClass(
1400                             clazz.getName() + "Operations");
1401 
1402                     LOG.debug("adding " + clazz.getName()
1403                             + " to service interfaces");
1404 
1405                     ClientCorbaClassesHolder corbaClasses
1406                         = new ClientCorbaClassesHolder();
1407 
1408                     corbaClasses.setCorbaObjectClass(clazz);
1409                     corbaClasses.setOperationsClass(operationsClass);
1410                     corbaClasses.setHelperClass(helperClass);                    
1411 
1412                     LOG.debug("result.add - corbaClasses=" + corbaClasses);
1413                     result.add(corbaClasses);
1414                 } catch (ClassNotFoundException e) {
1415                     String className = clazz.getName();
1416 
1417                     LOG.error("CRB000527_Class_Helper_or_Operations_not_found",
1418                               new Object[] { className, className }, e);
1419                 }
1420             } else {
1421             	LOG.debug("CRB000565_Class_not_referred_to_corba_client", 
1422                 		new Object[]{clazz.getName()});
1423             }
1424         }
1425         LOG.debug("<<<<<<<<<< filterInterfaceClasses - end. result:" + result);
1426         return result;
1427     }
1428 
1429   
1430 
1431   
1432 
1433 
1434     public URLClassLoader getUrlClassLoader() {
1435         return urlClassLoader;
1436     }
1437     
1438    
1439 
1440 
1441     public ChildFirstClassLoader getOriginalClassLoader() {
1442         return originalClassLoader;
1443     }
1444 
1445 
1446 
1447 
1448 
1449   protected void addEnumClass(String workdirsrc) throws ClassGenerationException {
1450     String enumFileName
1451       = "/it/imolinfo/jbi4corba/test/webservice/generator/EchoComplexEnum.java";
1452 
1453     String filename = workdirsrc + enumFileName;
1454 
1455     String enumSource
1456       = "package it.imolinfo.jbi4corba.test.webservice.generator; "
1457       + "\n\n"
1458       + "public enum EchoComplexEnum implements org.omg.CORBA.portable.IDLEntity { "
1459       + "\n\n"
1460       + "E1, E2, E3;"
1461       + "\n\n"
1462       + "public int value() {return ordinal();} "
1463       + "\n\n"
1464       + "public static EchoComplexEnum from_int (int value) { return EchoComplexEnum.values()[value]; } "
1465       + "\n\n"
1466       + "}"
1467       ;
1468 
1469     try {
1470       PrintWriter pw
1471         = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
1472       pw.println(enumSource);
1473       pw.flush();
1474       pw.close();
1475     } catch (IOException e) {
1476     	String msg=MESSAGES.getString("CRB000566_Failure_in_stopping_endpoint", 
1477     			new Object[] {e.getMessage()});
1478         LOG.error(msg,e);
1479         throw new ClassGenerationException(msg,e);
1480 
1481     }
1482   }
1483   
1484 
1485   
1486   
1487 }