KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > NewDriver


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/NewDriver.java,v 1.7 2004/02/13 02:40:54 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter;
20
21 import java.io.File JavaDoc;
22 import java.io.FilenameFilter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLClassLoader JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 /**
33  * @author Michael Stover
34  * @version $Revision: 1.7 $
35  */

36 public final class NewDriver
37 {
38     /** The class loader to use for loading JMeter classes. */
39     private static URLClassLoader JavaDoc loader;
40     
41     /** The directory JMeter is installed in. */
42     private static String JavaDoc jmDir;
43
44     static {
45         List JavaDoc jars = new LinkedList JavaDoc();
46         String JavaDoc cp = System.getProperty("java.class.path");
47
48         //Find JMeter home dir
49
StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(cp, File.pathSeparator);
50         if (tok.countTokens() == 1)
51         {
52             File JavaDoc jar = new File JavaDoc(tok.nextToken());
53             try
54             {
55                 jmDir = jar.getCanonicalFile().getParentFile().getParent();
56             }
57             catch (IOException JavaDoc e)
58             {
59             }
60         }
61         else
62         {
63             File JavaDoc userDir = new File JavaDoc(System.getProperty("user.dir"));
64             jmDir = userDir.getAbsoluteFile().getParent();
65         }
66
67         /*
68          * Does the system support UNC paths?
69          * If so, may need to fix them up later
70          */

71         boolean usesUNC = System.getProperty("os.name").startsWith("Windows");
72         
73         StringBuffer JavaDoc classpath = new StringBuffer JavaDoc();
74         File JavaDoc[] libDirs =
75             new File JavaDoc[] {
76                 new File JavaDoc(jmDir + File.separator + "lib"),
77                 new File JavaDoc(
78                     jmDir + File.separator + "lib" + File.separator + "ext")};
79         for (int a = 0; a < libDirs.length; a++)
80         {
81             File JavaDoc[] libJars = libDirs[a].listFiles(new FilenameFilter JavaDoc()
82             {
83                 public boolean accept(File JavaDoc dir, String JavaDoc name)
84                 {
85                     return name.endsWith(".jar");
86                 }
87             });
88             if (libJars == null){
89                 new Throwable JavaDoc("Could not access "+libDirs[a]).printStackTrace();
90                 continue;
91             }
92             for (int i = 0; i < libJars.length; i++)
93             {
94                 try
95                 {
96                     String JavaDoc s = libJars[i].getPath();
97                     
98                     // Fix path to allow the use of UNC URLs
99
if (usesUNC){
100                         if (s.startsWith("\\\\") &&
101                            !s.startsWith("\\\\\\")
102                            )
103                         {
104                             s = "\\\\" + s;
105                         }
106                         else if (s.startsWith("//") &&
107                                 !s.startsWith("///")
108                                 )
109                          {
110                              s = "//" + s;
111                          }
112                     } // usesUNC
113

114                     jars.add(new URL JavaDoc("file", "", s));
115                     classpath.append(System.getProperty("path.separator"));
116                     classpath.append(s);
117                 }
118                 catch (MalformedURLException JavaDoc e)
119                 {
120                     e.printStackTrace();
121                 }
122             }
123         }
124
125         System.setProperty(
126             "java.class.path",
127             System.getProperty("java.class.path") + classpath.toString());
128         loader = new URLClassLoader JavaDoc((URL JavaDoc[]) jars.toArray(new URL JavaDoc[0]));
129
130     }
131
132     /**
133      * Prevent instantiation.
134      */

135     private NewDriver()
136     {
137     }
138
139     /**
140      * Get the directory where JMeter is installed. This is the absolute path
141      * name.
142      *
143      * @return the directory where JMeter is installed.
144      */

145     public static String JavaDoc getJMeterDir()
146     {
147         return jmDir;
148     }
149
150     /**
151      * The main program which actually runs JMeter.
152      *
153      * @param args the command line arguments
154      */

155     public static void main(String JavaDoc[] args)
156     {
157         Thread.currentThread().setContextClassLoader(loader);
158         if (System.getProperty("log4j.configuration") == null)
159         {
160             File JavaDoc conf = new File JavaDoc(jmDir, "bin" + File.separator + "log4j.conf");
161             System.setProperty("log4j.configuration", "file:" + conf);
162         }
163
164         try
165         {
166             Class JavaDoc JMeter = loader.loadClass("org.apache.jmeter.JMeter");
167             Object JavaDoc instance = JMeter.newInstance();
168             Method JavaDoc startup =
169                 JMeter.getMethod(
170                     "start",
171                     new Class JavaDoc[] {(new String JavaDoc[0]).getClass()});
172             startup.invoke(instance, new Object JavaDoc[] { args });
173
174         }
175         catch (Exception JavaDoc e)
176         {
177             e.printStackTrace();
178         }
179     }
180 }
181
Popular Tags