001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter;
020:
021: // N.B. this must only use standard Java packages
022: import java.io.File;
023: import java.io.FilenameFilter;
024: import java.io.IOException;
025: import java.lang.reflect.Method;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.util.LinkedList;
029: import java.util.List;
030: import java.util.StringTokenizer;
031:
032: /**
033: * Main class for JMeter - sets up initial classpath.
034: *
035: * @author Michael Stover
036: */
037: public final class NewDriver {
038: /** The class loader to use for loading JMeter classes. */
039: private static DynamicClassLoader loader;
040:
041: /** The directory JMeter is installed in. */
042: private static String jmDir;
043:
044: static {
045: List jars = new LinkedList();
046: String cp = System.getProperty("java.class.path");
047:
048: // Find JMeter home dir
049: StringTokenizer tok = new StringTokenizer(cp,
050: File.pathSeparator);
051: if (tok.countTokens() == 1
052: || (tok.countTokens() == 2 && System.getProperty(
053: "os.name").toLowerCase().startsWith("mac os x"))) {
054: File jar = new File(tok.nextToken());
055: try {
056: jmDir = jar.getCanonicalFile().getParentFile()
057: .getParent();
058: } catch (IOException e) {
059: }
060: } else {// e.g. started from IDE with full classpath
061: jmDir = System.getProperty("jmeter.home", "");// Allow override
062: if (jmDir.length() == 0) {
063: File userDir = new File(System.getProperty("user.dir"));
064: jmDir = userDir.getAbsoluteFile().getParent();
065: }
066: }
067:
068: /*
069: * Does the system support UNC paths? If so, may need to fix them up
070: * later
071: */
072: boolean usesUNC = System.getProperty("os.name").startsWith(
073: "Windows");
074:
075: StringBuffer classpath = new StringBuffer();
076: File[] libDirs = new File[] {
077: new File(jmDir + File.separator + "lib"),
078: new File(jmDir + File.separator + "lib"
079: + File.separator + "ext"),
080: new File(jmDir + File.separator + "lib"
081: + File.separator + "junit") };
082: for (int a = 0; a < libDirs.length; a++) {
083: File[] libJars = libDirs[a].listFiles(new FilenameFilter() {
084: public boolean accept(File dir, String name) {
085: return name.endsWith(".jar");
086: }
087: });
088: if (libJars == null) {
089: new Throwable("Could not access " + libDirs[a])
090: .printStackTrace();
091: continue;
092: }
093: for (int i = 0; i < libJars.length; i++) {
094: try {
095: String s = libJars[i].getPath();
096:
097: // Fix path to allow the use of UNC URLs
098: if (usesUNC) {
099: if (s.startsWith("\\\\")
100: && !s.startsWith("\\\\\\")) {
101: s = "\\\\" + s;
102: } else if (s.startsWith("//")
103: && !s.startsWith("///")) {
104: s = "//" + s;
105: }
106: } // usesUNC
107:
108: jars.add(new URL("file", "", s));
109: classpath.append(System
110: .getProperty("path.separator"));
111: classpath.append(s);
112: } catch (MalformedURLException e) {
113: e.printStackTrace();
114: }
115: }
116: }
117:
118: // ClassFinder needs this
119: System.setProperty("java.class.path", System
120: .getProperty("java.class.path")
121: + classpath.toString());
122: loader = new DynamicClassLoader((URL[]) jars
123: .toArray(new URL[0]));
124: }
125:
126: /**
127: * Prevent instantiation.
128: */
129: private NewDriver() {
130: }
131:
132: public static void addURL(String url) {
133: File furl = new File(url);
134: try {
135: loader.addURL(furl.toURL());
136: } catch (MalformedURLException e) {
137: e.printStackTrace();
138: }
139: }
140:
141: public static void addURL(URL url) {
142: loader.addURL(url);
143: }
144:
145: public static void addPath(String path)
146: throws MalformedURLException {
147: URL url = new URL("file", "", path);
148: loader.addURL(url);
149: StringBuffer sb = new StringBuffer(System
150: .getProperty("java.class.path"));
151: sb.append(System.getProperty("path.separator"));
152: sb.append(path);
153: // ClassFinder needs this
154: System.setProperty("java.class.path", sb.toString());
155: }
156:
157: /**
158: * Get the directory where JMeter is installed. This is the absolute path
159: * name.
160: *
161: * @return the directory where JMeter is installed.
162: */
163: public static String getJMeterDir() {
164: return jmDir;
165: }
166:
167: /**
168: * The main program which actually runs JMeter.
169: *
170: * @param args
171: * the command line arguments
172: */
173: public static void main(String[] args) {
174: Thread.currentThread().setContextClassLoader(loader);
175: if (System.getProperty("log4j.configuration") == null) {
176: File conf = new File(jmDir, "bin" + File.separator
177: + "log4j.conf");
178: System.setProperty("log4j.configuration", "file:" + conf);
179: }
180:
181: if (args != null && args.length > 0 && args[0].equals("report")) {
182: try {
183: Class JMeterReport = loader
184: .loadClass("org.apache.jmeter.JMeterReport");
185: Object instance = JMeterReport.newInstance();
186: Method startup = JMeterReport.getMethod("start",
187: new Class[] { (new String[0]).getClass() });
188: startup.invoke(instance, new Object[] { args });
189:
190: } catch (Exception e) {
191: e.printStackTrace();
192: System.out
193: .println("JMeter home directory was detected as: "
194: + jmDir);
195: System.exit(1);
196: }
197: } else {
198: try {
199: Class JMeter = loader
200: .loadClass("org.apache.jmeter.JMeter");
201: Object instance = JMeter.newInstance();
202: Method startup = JMeter.getMethod("start",
203: new Class[] { (new String[0]).getClass() });
204: startup.invoke(instance, new Object[] { args });
205:
206: } catch (Exception e) {
207: e.printStackTrace();
208: System.out
209: .println("JMeter home directory was detected as: "
210: + jmDir);
211: System.exit(1);
212: }
213: }
214: }
215: }
|