1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
129
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 }