1 18 19 package org.apache.jmeter; 20 21 import java.io.File ; 22 import java.io.FilenameFilter ; 23 import java.io.IOException ; 24 import java.lang.reflect.Method ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.net.URLClassLoader ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.StringTokenizer ; 31 32 36 public final class NewDriver 37 { 38 39 private static URLClassLoader loader; 40 41 42 private static String jmDir; 43 44 static { 45 List jars = new LinkedList (); 46 String cp = System.getProperty("java.class.path"); 47 48 StringTokenizer tok = new StringTokenizer (cp, File.pathSeparator); 50 if (tok.countTokens() == 1) 51 { 52 File jar = new File (tok.nextToken()); 53 try 54 { 55 jmDir = jar.getCanonicalFile().getParentFile().getParent(); 56 } 57 catch (IOException e) 58 { 59 } 60 } 61 else 62 { 63 File userDir = new File (System.getProperty("user.dir")); 64 jmDir = userDir.getAbsoluteFile().getParent(); 65 } 66 67 71 boolean usesUNC = System.getProperty("os.name").startsWith("Windows"); 72 73 StringBuffer classpath = new StringBuffer (); 74 File [] libDirs = 75 new File [] { 76 new File (jmDir + File.separator + "lib"), 77 new File ( 78 jmDir + File.separator + "lib" + File.separator + "ext")}; 79 for (int a = 0; a < libDirs.length; a++) 80 { 81 File [] libJars = libDirs[a].listFiles(new FilenameFilter () 82 { 83 public boolean accept(File dir, String name) 84 { 85 return name.endsWith(".jar"); 86 } 87 }); 88 if (libJars == null){ 89 new Throwable ("Could not access "+libDirs[a]).printStackTrace(); 90 continue; 91 } 92 for (int i = 0; i < libJars.length; i++) 93 { 94 try 95 { 96 String s = libJars[i].getPath(); 97 98 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 } 114 jars.add(new URL ("file", "", s)); 115 classpath.append(System.getProperty("path.separator")); 116 classpath.append(s); 117 } 118 catch (MalformedURLException 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 ((URL []) jars.toArray(new URL [0])); 129 130 } 131 132 135 private NewDriver() 136 { 137 } 138 139 145 public static String getJMeterDir() 146 { 147 return jmDir; 148 } 149 150 155 public static void main(String [] args) 156 { 157 Thread.currentThread().setContextClassLoader(loader); 158 if (System.getProperty("log4j.configuration") == null) 159 { 160 File conf = new File (jmDir, "bin" + File.separator + "log4j.conf"); 161 System.setProperty("log4j.configuration", "file:" + conf); 162 } 163 164 try 165 { 166 Class JMeter = loader.loadClass("org.apache.jmeter.JMeter"); 167 Object instance = JMeter.newInstance(); 168 Method startup = 169 JMeter.getMethod( 170 "start", 171 new Class [] {(new String [0]).getClass()}); 172 startup.invoke(instance, new Object [] { args }); 173 174 } 175 catch (Exception e) 176 { 177 e.printStackTrace(); 178 } 179 } 180 } 181 | Popular Tags |