1 23 24 28 29 package com.sun.enterprise.tools.common.util; 30 31 import java.io.*; 32 import java.util.*; 33 import java.util.jar.*; 34 import java.util.zip.*; 35 import com.sun.enterprise.tools.common.util.diagnostics.Reporter; 36 37 public class JWhich 38 { 39 public static void main(String [] args) 40 { 41 if(args == null || args.length == 0) 42 { 43 usage(); 44 return; 45 } 46 47 int argNum = 0; 48 49 JWhich jwhich; 50 51 if(args[0].toLowerCase().equals("-classpath")) { 53 if(args.length != 3) 54 { 55 usage(); 56 return; 57 } 58 59 jwhich = new JWhich(args[2], args[1]); 60 } 61 else 62 jwhich = new JWhich(args[0]); 63 } 64 65 67 public JWhich(String classname, String classpathArg) 68 { 69 this.classpathArg = classpathArg; 70 ctor(classname); 71 } 72 73 75 public JWhich(String classname) 76 { 77 ctor(classname); 78 } 79 80 82 public String getResult() 83 { 84 return result; 85 } 86 87 89 private void ctor(String classname) 90 { 91 this.classname = classname; 92 93 96 initClasspath(); 97 fixClassname(); 98 String [] locations = findClass(); 99 100 pr(""); 102 if(locations == null || locations.length <= 0) 103 { 104 pr("Can't find class"); return; 106 } 107 108 for(int i = 0; i < locations.length; i++) 109 pr(classname + " located in " + locations[i]); 111 } 114 115 117 private static void usage() 118 { 119 System.out.println("Usage: java " + JWhich.class.getName() + " [-classpath a_classpath] classname"); } 121 122 124 private void initClasspath() 125 { 126 String cp; 127 128 if(classpathArg == null) 129 cp = System.getProperty("java.class.path"); else 131 cp = classpathArg; 132 133 StringTokenizer tokens = new StringTokenizer(cp, ";", false); int nTokens = tokens.countTokens(); 135 136 classpath = new String [nTokens]; 137 138 debug("" + nTokens + " tokens."); 140 for(int i = 0; tokens.hasMoreTokens(); i++) 141 { 142 String s = tokens.nextToken(); 143 debug(s); 144 classpath[i] = s; 145 } 146 } 147 148 150 private void fixClassname() 151 { 152 157 debug("old classname: " + classname); jarClassname = classname; 159 160 classname = classname.replace('.', File.separatorChar); 161 162 if(File.separatorChar != '/') 163 classname = classname.replace('/', File.separatorChar); 164 165 if(File.separatorChar != '\\') 166 classname = classname.replace('\\', File.separatorChar); 167 168 170 jarClassname = jarClassname.replace('.', '/'); 171 jarClassname = jarClassname.replace('\\', '/'); 172 173 classname = classname + ".class"; jarClassname = jarClassname + ".class"; 176 debug("new classname: " + classname); debug("new jarClassname: " + jarClassname); } 179 180 182 private String [] findClass() 183 { 184 ArrayList names = new ArrayList(); 185 186 for(int i = 0; i < classpath.length; i++) 187 { 188 String path = classpath[i]; 189 190 if(findClass(path)) 191 { 192 names.add(path); 193 debug("FOUND IT: " + path); } 195 } 196 197 int num = names.size(); 198 199 debug("Found it in " + num + " places"); 201 if(num <= 0) 202 { 203 return null; 204 } 205 206 String [] ss = new String [num]; 207 ss = (String [])names.toArray(ss); 208 return ss; 209 } 210 211 213 private boolean findClass(String path) 214 { 215 if(path.toLowerCase().endsWith(".jar")) { 217 return findClassInJar(path); 218 } 219 220 File f = new File(path + File.separator + classname); 221 debug("Looking for " + f); 223 return f.exists(); 224 } 225 226 228 private boolean findClassInJar(String path) 229 { 230 ZipInputStream zin = null; 231 232 try 233 { 234 zin = new ZipInputStream(new FileInputStream(path)); 235 ZipEntry entry; 236 237 while((entry = zin.getNextEntry()) != null) 238 { 239 String name = entry.getName(); 240 zin.closeEntry(); 241 242 if(name.equals(jarClassname)) 243 { 244 zin.close(); 245 return true; 246 } 247 } 248 zin.close(); 249 } 250 catch(IOException e) 251 { 252 debug("" + e + " " + path); } 254 255 return false; 256 } 257 258 260 private void debug(String s) 261 { 262 if(debug_) 263 pr(s); 264 } 265 266 268 private void pr(String s) 269 { 270 System.out.println(s); result += s; 272 } 273 274 276 private String [] classpath = null; 277 private String classpathArg = null; 278 private String classname = null; 279 private String jarClassname = null; 280 private boolean doReflect = false; 281 private boolean doExhaustive = true; 282 private boolean debug_ = false; 283 private String result = new String (); 284 } 285 | Popular Tags |