View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006, 2007 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.jbi4ejb.webservice.generator;
9   
10  import it.imolinfo.jbi4ejb.Logger;
11  import it.imolinfo.jbi4ejb.LoggerFactory;
12  import it.imolinfo.jbi4ejb.exception.ClassGenerationException;
13  import it.imolinfo.jbi4ejb.exception.EJBDeployException;
14  import it.imolinfo.jbi4ejb.exception.EJBWSDLGenerationException;
15  import it.imolinfo.jbi4ejb.jbi.Messages;
16  import it.imolinfo.jbi4ejb.runtime.ejbproxy.StatelessEJBProxyFactory;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.ObjectStreamClass;
21  import java.util.ArrayList;
22  import java.util.Hashtable;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Set;
26  
27  
28  /**
29   * Utility class to generate the service WSDL.
30   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
31   */
32  public final class DynamicEJBWSDLGenerator {        
33  
34      private static final Logger LOG
35          = LoggerFactory.getLogger(DynamicEJBWSDLGenerator.class);
36      private static final Messages MESSAGES
37      = Messages.getMessages(DynamicEJBWSDLGenerator.class);
38      
39      /**
40       * Private default constructor to avoid instantiation.
41       */
42      private DynamicEJBWSDLGenerator() {}
43      
44      /**
45       * Creates the WSDL for the remote interface, using the jar passed.
46       * 
47       * @param remoteInterfaceClassName
48       *            The rermote interface class name
49       * @param ejbJarPath
50       *            the ejb-jar path
51       * @param descriptor
52       *            The WSDLDescriptor
53       * @return the absolute WSDL path name
54       * 
55       * @throws EJBWSDLGenerationException
56       *             If some problem occurs
57       */
58      public static String generateWSDLFromRemoteInterface(String remoteInterfaceClassName, 
59                  String ejbJarPath, WSDLDescriptor descriptor) throws EJBWSDLGenerationException {
60                  
61          // Creates the working temp dir
62          File tempDir;
63          try {
64              tempDir = EJBUtils.createTempDir();
65          } catch (IOException e) {
66          	String msg=MESSAGES.getString("EJB001001_generateWSDLFromRemoteInterface", new Object[]{e.getMessage()});
67              LOG.error(msg,e);
68              throw new EJBWSDLGenerationException(msg,e);
69          }
70          String wsdlFileName = tempDir.getAbsolutePath() + File.separatorChar + remoteInterfaceClassName + ".wsdl";
71          File interfaceWSDL = WSDLGenerator.createWsdlFromJar(remoteInterfaceClassName, ejbJarPath, wsdlFileName, descriptor, tempDir);
72          return interfaceWSDL.getAbsolutePath();
73      }
74      
75      /**
76       * Gets the UID from the classes used by the remote interface. 
77       * 
78       * @param remoteInterface
79       *              The remote interface
80       * @param ejbJarPath
81       *              The complete ejb-jar path
82       * @return the classes ID
83       * 
84       * @throws EJBWSDLGenerationException
85       *              If some error occurs
86       */
87      @SuppressWarnings("unchecked")
88      public static Hashtable getClassesID(String remoteInterface, String ejbJarPath) throws EJBWSDLGenerationException {
89          
90          Hashtable classesID = new Hashtable();
91  
92          File tempDir;
93          try {
94              tempDir = File.createTempFile("EJBCLASSES_", null);
95          } catch (IOException e) {
96          	String msg=MESSAGES.getString("EJB001002_getClassesID", new Object[]{e.getMessage()});
97              LOG.error(msg,e);
98              throw new EJBWSDLGenerationException(msg,e);
99          } 
100         tempDir.delete();
101         tempDir.mkdir();
102 
103         try {
104             JarUtil.unjar(new File(ejbJarPath), tempDir);
105         } catch (IOException e) {
106         	String msg=MESSAGES.getString("EJB001002_getClassesID", new Object[]{e.getMessage()});
107             LOG.error(msg,e);
108             throw new EJBWSDLGenerationException(msg,e);
109         }
110 
111         String remoteInterfaceFileName = remoteInterface.replace('.',File.separatorChar);
112 
113         LOG.debug("remoteInterfaceFileName: " + remoteInterfaceFileName);
114         List<File> list = new ArrayList<File>();
115         list.add(new File(tempDir.getAbsolutePath() + File.separatorChar + remoteInterfaceFileName));
116 
117         // Find the classes used by the remote interface (with recursion).
118         Set<Class> classesToSerialize;
119         try {
120             classesToSerialize = Util.findClassUsed(tempDir.getAbsolutePath(), list);
121         } catch (ClassGenerationException e) {
122         	String msg=MESSAGES.getString("EJB001002_getClassesID", new Object[]{e.getMessage()});
123             LOG.error(msg,e);
124             throw new EJBWSDLGenerationException(msg,e);
125         }
126 
127         Iterator iter = classesToSerialize.iterator();
128         while (iter.hasNext()) {
129             Class classToSerialize = (Class)iter.next();
130             LOG.debug(classToSerialize.getName());
131                         
132             ObjectStreamClass objectStreamClass = ObjectStreamClass.lookup(classToSerialize);
133                         
134             long uid = objectStreamClass.getSerialVersionUID();
135              
136 /*
137             // With com.sun.corba.se.impl.io.ObjectStreamClass;
138             long uid = ObjectStreamClass.getSerialVersionUID(classToSerialize);
139 
140             if (uid == 0L) {
141                 uid = ObjectStreamClass.getActualSerialVersionUID(classToSerialize);
142             }
143 */            
144 
145             LOG.debug(classToSerialize.getName() + " uid: " + uid);            
146             classesID.put(classToSerialize.getName() , Long.valueOf(uid));
147         }            
148 
149         return classesID;
150         
151     }        
152     
153     
154 }