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   
9   package it.imolinfo.jbi4ejb.test;
10  
11  import it.imolinfo.jbi4ejb.exception.EJBWSDLGenerationException;
12  import it.imolinfo.jbi4ejb.jbi.wsdl.Jbi4EjbExtension;
13  import it.imolinfo.jbi4ejb.webservice.generator.EJBUtils;
14  import it.imolinfo.jbi4ejb.webservice.generator.JarUtil;
15  import it.imolinfo.jbi4ejb.webservice.generator.WSDLGenerator;
16  
17  import java.io.File;
18  import java.io.FileInputStream;
19  import java.io.FileOutputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.io.StringWriter;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Properties;
27  
28  import javax.wsdl.Definition;
29  import javax.wsdl.WSDLException;
30  import javax.wsdl.extensions.ExtensionRegistry;
31  import javax.wsdl.factory.WSDLFactory;
32  import javax.wsdl.xml.WSDLReader;
33  import javax.wsdl.xml.WSDLWriter;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  public class TestUtils {
39      
40      /** Websphere constants */ 
41      public static final String WEBSPHERE_6 = "ws6";
42      
43      /** Glassfish constants */
44      public static final String GLASSFISH_v2 = "gl6";
45          
46      /** Jboss constants */
47      public static final String JBOSS_V4 = "jboss4";
48          
49      /** Glassfish constants */
50      public static final String BEA_V8 = "bea8";    
51      
52      /** Glassfish constants */
53      public static final String JACORB = "jacorb";  
54  
55      /**
56       * Logger.
57       */
58      private static Log LOG = LogFactory.getLog(TestUtils.class);
59  
60      /**
61       * Returns the jar filename list to use to compile thx generated sources
62       * @param xFireVersion the xfire version
63       * @return
64       */
65      public static List<String> getJarFilesName(String xFireVersion)  {
66          String repodir = System.getProperty("localRepository");
67          LOG.debug("repodir=" + repodir);        
68  
69          List<String> jarFilesName = new ArrayList<String>();
70  
71          // adding needed compilation files
72          //xfire
73          jarFilesName.add(repodir +
74                  "/xfire/xfire-jsr181-api/1.0-M1/xfire-jsr181-api-1.0-M1.jar");
75          jarFilesName.add(repodir + "/org/codehaus/xfire/xfire-jaxb2/" +
76                  xFireVersion + "/xfire-jaxb2-" + xFireVersion + ".jar");
77          jarFilesName.add(repodir + "/org/codehaus/xfire/xfire-java5/" +
78                  xFireVersion + "/xfire-java5-" + xFireVersion + ".jar");
79          jarFilesName.add(repodir + "/org/codehaus/xfire/xfire-core/" +
80                  xFireVersion + "/xfire-core-" + xFireVersion + ".jar");
81          jarFilesName.add(repodir + "/org/codehaus/xfire/xfire-aegis/" +
82                  xFireVersion + "/xfire-aegis-" + xFireVersion + ".jar");
83          jarFilesName.add(repodir + "/org/codehaus/xfire/xfire-annotations/" +
84                  xFireVersion + "/xfire-annotations-" + xFireVersion + ".jar");
85  
86          //.../.m2/repository/commons-lang/commons-lang/2.1
87          String apacheCommonsVersion = "2.1";
88          jarFilesName.add(repodir + "/commons-lang/commons-lang/" +
89                  apacheCommonsVersion + "/commons-lang-" + apacheCommonsVersion +
90                  ".jar");
91  
92          //java
93          jarFilesName.add(repodir + "/javax/xml/jaxb-api/2.0/jaxb-api-2.0.jar");
94  
95          return jarFilesName;
96      }
97      
98      
99      public static Properties getOrbProperties(String asType) {
100         
101         Properties props = new Properties();
102         
103         if (WEBSPHERE_6.equals(asType)) {
104             LOG.debug("Setting Websphere6  ORB properties");
105             
106             // IBM Websphere 6 properties
107             /*
108             props.setProperty("org.omg.CORBA.ORBClass", "com.ibm.CORBA.iiop.ORB");
109             props.setProperty("org.omg.CORBA.ORBSingletonClass", "com.ibm.rmi.corba.ORBSingleton");
110             props.setProperty("javax.rmi.CORBA.StubClass", "com.ibm.rmi.javax.rmi.CORBA.StubDelegateImpl");
111             props.setProperty("javax.rmi.CORBA.PortableRemoteObjectClass", "com.ibm.rmi.javax.rmi.PortableRemoteObject");
112             props.setProperty("javax.rmi.CORBA.UtilClass", "com.ibm.ws.orb.WSUtilDelegateImpl");
113             props.setProperty("com.ibm.ws.orb.transport.ConnectionInterceptorName", "com.ibm.ISecurityLocalObjectBaseL13Impl.SecurityConnectionInterceptor");
114             props.setProperty("com.ibm.ws.orb.transport.WSSSLClientSocketFactoryName", "com.ibm.ws.security.orbssl.WSSSLClientSocketFactoryImpl");
115             props.setProperty("com.ibm.CORBA.enableLocateRequest", "true");
116             props.setProperty("com.ibm.CORBA.ORBCharEncoding", "UTF8");
117             props.setProperty("com.ibm.CORBA.TransportMode", "Pluggable");
118             */
119             
120             
121         } else if (GLASSFISH_v2.equals(asType)) {
122             // add nothing (Default SUN orb values)
123             props.setProperty("org.omg.CORBA.ORBClass", "com.sun.corba.ee.impl.orb.ORBImpl");
124         } else if (JACORB.equals(asType)) {
125             props.setProperty("org.omg.CORBA.ORBClass", "org.jacorb.orb.ORB");
126             props.setProperty("org.omg.CORBA.ORBSingletonClass", "org.jacorb.orb.ORBSingleton");
127             // props.setProperty("ORBInitRef.NameService", "corbaloc::localhost:12354/StandardNS/NameServer-POA/_root");   
128             //props.setProperty("org.omg.PortableInterceptor.ORBInitializerClass.TSServerInit", 
129             //              "org.jacorb.transaction.TransactionInitializer");            
130         } else if (JBOSS_V4.endsWith(asType)) {
131             props.setProperty("org.omg.CORBA.ORBClass", "org.jacorb.orb.ORB");
132             props.setProperty("org.omg.CORBA.ORBSingletonClass", "org.jacorb.orb.ORBSingleton");
133         }
134         return props;
135     }    
136     
137     public static Properties getJndiProperties(String asType) {
138         
139         Properties props = new Properties();
140         
141         if (WEBSPHERE_6.equals(asType)) {
142             LOG.debug("Setting Websphere6 JNDI properties");
143             // IBM Websphere 6 properties
144             props.setProperty("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
145             props.setProperty("java.naming.provider.url", "corbaname:iiop:127.0.0.1:2809/NameServiceServerRoot");
146 
147         } else if (GLASSFISH_v2.equals(asType)) {
148             // To use that, add appserv-rt.jar and javaee.jar from glassfish to the classpath
149             props.setProperty("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
150             props.setProperty("java.naming.factory.url.pkgs","com.sun.enterprise.naming");
151             props.setProperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
152             //props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
153             // props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");            
154         }
155         return props;
156     }    
157     
158     /**
159      * Gets the classes UIDs from a jar.
160      * 
161      * @param remoteInterfaceName
162      *          The remote interface name
163      * @param ejbJarPath
164      *          The jar path 
165      * @return the classes id from jar
166      * 
167      * @throws IOException
168      *              if some problem occurs
169      * @throws EJBWSDLGenerationException
170      *              if some problem occurs
171      */
172     public static Properties getClassesIdFromJar(String remoteInterfaceName, String ejbJarPath) throws IOException, EJBWSDLGenerationException{
173         File tempDir = File.createTempFile("EJBCLASSES_", null);        
174         tempDir.delete();
175         tempDir.mkdir();    
176         JarUtil.unjar(new File(ejbJarPath), tempDir);
177         Properties classesId = WSDLGenerator.getClassesID(remoteInterfaceName, tempDir);
178         tempDir.delete();
179         return classesId;
180     }
181     
182     /**
183      * Creates a temporary directory (for unit testing pourpose).
184      * @return
185      */
186     public static File createTempDir()  {
187         // Creates the working temp dir
188         File tempDir = null;
189         try {
190             tempDir = EJBUtils.createTempDir();
191         } catch (IOException e) {
192             // TODO i18n
193             LOG.error(e.getMessage());
194             e.printStackTrace();
195         }
196         return tempDir;
197     }
198     
199     /**
200      * Return the current test-classes directory.
201      * @return
202      */
203     public static File getTestClassesDir() {
204         return new File("." + File.separatorChar +"target" +  File.separatorChar + "test-classes");
205     }    
206     
207     
208     /**
209      * Gets the WSDL string from the WSDL definition.
210      * 
211      * @param wsdl
212      * 
213      * @return the WSDL string from definition
214      */
215     public static String getWSDLStringFromDefinition(Definition wsdl) throws WSDLException {
216         // Asserts...
217         StringWriter strWriter = new StringWriter();   
218         getExtendedWSDLWriter().writeWSDL(wsdl, strWriter);
219         return strWriter.toString();                
220     }
221     
222     /**
223      * Gets the Jbi4Ejb extended WSDL reader.
224      * 
225      * @return the extended WSDL reader
226      * 
227      * @throws WSDLException
228      */
229     public static WSDLReader getExtendedWSDLReader() throws WSDLException {    
230         WSDLFactory factory = WSDLFactory.newInstance();
231         WSDLReader reader = factory.newWSDLReader();        
232         ExtensionRegistry registry = factory.newPopulatedExtensionRegistry();            
233         Jbi4EjbExtension.register(registry);      
234         reader.setExtensionRegistry(registry);
235         return reader;
236     }
237     
238     /**
239      * Gets the Jbi4Ejb extended WSDL writer.
240      * 
241      * @return the extended WSDL writer
242      * 
243      * @throws WSDLException
244      */
245     public static WSDLWriter getExtendedWSDLWriter() throws WSDLException {    
246         WSDLFactory factory = WSDLFactory.newInstance();
247         WSDLWriter writer = factory.newWSDLWriter();  
248         ExtensionRegistry registry = factory.newPopulatedExtensionRegistry();            
249         Jbi4EjbExtension.register(registry);              
250         return writer;
251     }
252     
253     /**
254      * recursively removes all the directory
255      * @param dir
256      * @return
257      */
258     public static boolean deleteDir(File dir) {
259         if (dir.isDirectory()) {
260             String[] children = dir.list();
261             for (int i=0; i<children.length; i++) {
262                 boolean success = deleteDir(new File(dir, children[i]));
263                 if (!success) {
264                     return false;
265                 }
266             }
267         }    
268         // The directory is now empty so delete it
269         return dir.delete();
270     }
271     
272     /**
273      * Copy directory.
274      * S
275      * @param sourceLocation
276      * @param targetLocation
277      * 
278      * @throws IOException
279      */
280     public static void copyDirectory(File sourceLocation , File targetLocation)
281     throws IOException {
282 
283         if (sourceLocation.isDirectory()) {
284             if (!targetLocation.exists()) {
285                 targetLocation.mkdir();
286             }
287 
288             String[] children = sourceLocation.list();
289             for (int i=0; i<children.length; i++) {
290                 copyDirectory(new File(sourceLocation, children[i]),
291                         new File(targetLocation, children[i]));
292             }
293         } else {
294 
295             InputStream in = new FileInputStream(sourceLocation);
296             OutputStream out = new FileOutputStream(targetLocation);
297 
298             // Copy the bits from instream to outstream
299             byte[] buf = new byte[1024];
300             int len;
301             while ((len = in.read(buf)) > 0) {
302                 out.write(buf, 0, len);
303             }
304             in.close();
305             out.close();
306         }
307     }
308 
309 }
310