1 23 24 28 29 package com.sun.enterprise.util.diagnostics; 30 31 import java.io.*; 32 import java.util.*; 33 import java.util.jar.*; 34 import java.util.zip.*; 35 import java.util.logging.Logger ; 37 import java.util.logging.Level ; 38 import com.sun.logging.LogDomains; 39 41 public class JWhich 42 { 43 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 45 public static void main(String [] args) 47 { 48 if(args == null || args.length == 0) 49 { 50 usage(); 51 return; 52 } 53 54 int argNum = 0; 55 56 JWhich jwhich; 57 58 if(args[0].toLowerCase().equals("-classpath")) { 60 if(args.length != 3) 61 { 62 usage(); 63 return; 64 } 65 66 jwhich = new JWhich(args[2], args[1]); 67 } 68 else 69 jwhich = new JWhich(args[0]); 70 } 71 72 74 public JWhich(String classname, String classpathArg) 75 { 76 this.classpathArg = classpathArg; 77 ctor(classname); 78 } 79 80 82 public JWhich(String classname) 83 { 84 ctor(classname); 85 } 86 87 89 public String getResult() 90 { 91 return result; 92 } 93 94 96 private void ctor(String classname) 97 { 98 this.classname = classname; 99 100 103 initClasspath(); 104 fixClassname(); 105 String [] locations = findClass(); 106 107 pr(""); 109 if(locations == null || locations.length <= 0) 110 { 111 pr("Can't find class"); return; 113 } 114 115 for(int i = 0; i < locations.length; i++) 116 pr(classname + " located in " + locations[i]); 118 } 121 122 124 private static void usage() 125 { 126 System.out.println("Usage: java " + JWhich.class.getName() + " [-classpath a_classpath] classname"); } 128 129 131 private void initClasspath() 132 { 133 String cp; 134 135 if(classpathArg == null) 136 cp = System.getProperty("java.class.path"); else 138 cp = classpathArg; 139 140 StringTokenizer tokens = new StringTokenizer(cp, ";", false); int nTokens = tokens.countTokens(); 142 143 classpath = new String [nTokens]; 144 145 debug("" + nTokens + " tokens."); 147 for(int i = 0; tokens.hasMoreTokens(); i++) 148 { 149 String s = tokens.nextToken(); 150 debug(s); 151 classpath[i] = s; 152 } 153 } 154 155 157 private void fixClassname() 158 { 159 164 debug("old classname: " + classname); jarClassname = classname; 166 167 classname = classname.replace('.', File.separatorChar); 168 169 if(File.separatorChar != '/') 170 classname = classname.replace('/', File.separatorChar); 171 172 if(File.separatorChar != '\\') 173 classname = classname.replace('\\', File.separatorChar); 174 175 177 jarClassname = jarClassname.replace('.', '/'); 178 jarClassname = jarClassname.replace('\\', '/'); 179 180 classname = classname + ".class"; jarClassname = jarClassname + ".class"; 183 debug("new classname: " + classname); debug("new jarClassname: " + jarClassname); } 186 187 189 private String [] findClass() 190 { 191 ArrayList names = new ArrayList(); 192 193 for(int i = 0; i < classpath.length; i++) 194 { 195 String path = classpath[i]; 196 197 if(findClass(path)) 198 { 199 names.add(path); 200 debug("FOUND IT: " + path); } 202 } 203 204 int num = names.size(); 205 206 debug("Found it in " + num + " places"); 208 if(num <= 0) 209 { 210 return null; 211 } 212 213 String [] ss = new String [num]; 214 ss = (String [])names.toArray(ss); 215 return ss; 216 } 217 218 220 private boolean findClass(String path) 221 { 222 if(path.toLowerCase().endsWith(".jar")) { 224 return findClassInJar(path); 225 } 226 227 File f = new File(path + File.separator + classname); 228 debug("Looking for " + f); 230 return f.exists(); 231 } 232 233 235 private boolean findClassInJar(String path) 236 { 237 ZipInputStream zin = null; 238 239 try 240 { 241 zin = new ZipInputStream(new FileInputStream(path)); 242 ZipEntry entry; 243 244 while((entry = zin.getNextEntry()) != null) 245 { 246 String name = entry.getName(); 247 zin.closeEntry(); 248 249 if(name.equals(jarClassname)) 250 { 251 zin.close(); 252 return true; 253 } 254 } 255 zin.close(); 256 } 257 catch(IOException e) 258 { 259 debug("" + e + " " + path); } 261 262 return false; 263 } 264 265 267 private void debug(String s) 268 { 269 if(debug_) 270 pr(s); 271 } 272 273 275 private void pr(String s) 276 { 277 _logger.log(Level.FINE,s); 280 result += s; 282 } 283 284 286 private String [] classpath = null; 287 private String classpathArg = null; 288 private String classname = null; 289 private String jarClassname = null; 290 private boolean doReflect = false; 291 private boolean doExhaustive = true; 292 private boolean debug_ = false; 293 private String result = new String (); 294 } 295 | Popular Tags |