View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.launch;
20  
21  import java.io.File;
22  
23  import java.net.URL;
24  import java.net.URLClassLoader;
25  import java.net.MalformedURLException;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.StringTokenizer;
30  
31  import org.apache.tools.ant.launch.Locator;
32  
33  /***
34   * This is the launcher for Jameleon. It loads in all required
35   * libraries.
36   * NOTE: I started this code from Apache Ant Launcher as an
37   * example.
38   */
39  public class JameleonLauncher {
40      /***
41       * The system property name that is used to specify the location
42       * of the jameleon installation
43       */ 
44      public static final String JAMELEON_HOME_PROPERTY = "jameleon.home";
45      public static final String MAIN_CLASS = "net.sf.jameleon.ui.JameleonUI";
46  
47      public static void main (String args[]) throws Exception{
48          JameleonLauncher jameleon = new JameleonLauncher();
49          try{
50              jameleon.launch();
51          }catch (JameleonLaunchException jle){
52              System.err.println(jle.getMessage());
53          }catch (Throwable t){
54              t.printStackTrace();
55          }
56      }
57  
58      public void launch() throws JameleonLaunchException, MalformedURLException{
59          File jameleonHome = calculateJameleonHome();
60          List pathUrls = new ArrayList();
61          addPath(jameleonHome.getAbsolutePath()+File.separator+"lib", true, pathUrls);
62  
63          URL[] jars = (URL[]) pathUrls.toArray(new URL[0]);
64  
65          StringBuffer baseClassPath
66              = new StringBuffer(System.getProperty("java.class.path"));
67          if (baseClassPath.charAt(baseClassPath.length() - 1)
68                  == File.pathSeparatorChar) {
69              baseClassPath.setLength(baseClassPath.length() - 1);
70          }
71  
72          for (int i = 0; i < jars.length; ++i) {
73              baseClassPath.append(File.pathSeparatorChar);
74              baseClassPath.append(Locator.fromURI(jars[i].toString()));
75          }
76  
77          System.setProperty("java.class.path", baseClassPath.toString());
78  
79          URLClassLoader loader = new URLClassLoader(jars);
80          Thread.currentThread().setContextClassLoader(loader);
81          try {
82              Class mainClass = loader.loadClass(MAIN_CLASS);
83              JameleonMain main = (JameleonMain) mainClass.newInstance();
84              main.startJameleon();
85          } catch (Throwable t) {
86              t.printStackTrace();
87          }
88      }
89  
90      /***
91        * Add a CLASSPATH or -lib to lib path urls.
92        * This method was directly copied over from
93        * org.apache.tools.ant.launch.Launcher
94        * @param path        the classpath or lib path to add to the libPathULRLs
95        * @param getJars     if true and a path is a directory, add the jars in
96        *                    the directory to the path urls
97        * @param libPathURLs the list of paths to add to
98        */
99      private void addPath(String path, boolean getJars, List libPathURLs)
100         throws MalformedURLException {
101         StringTokenizer myTokenizer = 
102             new StringTokenizer(path, System.getProperty("path.separator"));
103 
104         while (myTokenizer.hasMoreElements()) {
105             String elementName = myTokenizer.nextToken();
106             File element = new File(elementName);
107             if (elementName.indexOf("%") != -1 && !element.exists()) {
108                 continue;
109             }
110             if (getJars && element.isDirectory()) {
111                 // add any jars in the directory
112                 URL[] dirURLs = Locator.getLocationURLs(element);
113                 for (int j = 0; j < dirURLs.length; ++j) {
114                     libPathURLs.add(dirURLs[j]);
115                 }
116             }
117 
118             libPathURLs.add(element.toURL());
119         }
120     }
121 
122     public File calculateJameleonHome() throws JameleonLaunchException{
123         String jameleonHomeValue = System.getProperty(JAMELEON_HOME_PROPERTY);
124         File jameleonHome = null;
125 
126         File launcherJar = Locator.getClassSource(getClass());
127         
128         //Default the lib directory to the location of the launcherJar because the class may not
129         //be in a jar file.
130         File libDir = launcherJar;
131         if (launcherJar.exists() && !launcherJar.isDirectory()){
132         	libDir = launcherJar.getParentFile();
133         }
134 
135         if (jameleonHomeValue != null) {
136             jameleonHome = new File(jameleonHomeValue);
137         }
138 
139         if (jameleonHome == null || !jameleonHome.exists()) {
140             jameleonHome = libDir.getParentFile();
141             System.setProperty(JAMELEON_HOME_PROPERTY, jameleonHome.getAbsolutePath());
142         }
143 
144         if (!jameleonHome.exists()) {
145             throw new JameleonLaunchException("Jameleon home '"+JAMELEON_HOME_PROPERTY+
146                                               "' is set incorrectly or Jameleon could not be located");
147         }
148         return jameleonHome;
149     }
150 }