1 23 24 29 30 package com.sun.enterprise.tools.verifier.apiscan.packaging; 31 32 import java.io.File ; 33 import java.io.FileFilter ; 34 import java.io.IOException ; 35 import java.util.ArrayList ; 36 import java.util.StringTokenizer ; 37 import java.util.jar.JarFile ; 38 import java.util.logging.ConsoleHandler ; 39 import java.util.logging.Handler ; 40 import java.util.logging.Level ; 41 import java.util.logging.Logger ; 42 43 50 public class ClassPathBuilder { 51 private static String resourceBundleName = "com.sun.enterprise.tools.verifier.apiscan.LocalStrings"; 52 public static Logger logger = Logger.getLogger("apiscan.packaging", resourceBundleName); private static final String myClassName = "ClassPathBuilder"; 55 private static final String thisClassName = "com.sun.enterprise.tools.verifier.apiscan.packaging.ClassPathBuilder"; 57 public static String buildClassPathForJar(JarFile jar) throws IOException { 58 return buildClassPathForJar(new Archive(jar)); 59 } 60 61 public static String buildClassPathForJar(File file) throws IOException { 62 return buildClassPathForJar(new Archive(file)); 63 } 64 65 public static String buildClassPathForRar(File file) throws IOException { 66 return buildClassPathForRar(new Archive(file)); 67 } 68 69 public static String buildClassPathForJar(Archive jar) throws IOException { 71 logger.entering(myClassName, "buildClassPathForJar", jar); StringBuffer classpath = new StringBuffer ( 73 jar.getPath() + File.pathSeparator); 74 75 Archive[] archives = jar.getBundledArchives(); 76 classpath.append(convertToClassPath(archives)); 77 classpath.append(File.pathSeparator).append( 79 convertToClassPath(getInstalledArchivesForJar(jar))); 80 String result = removeDuplicates(classpath).toString(); 81 logger.exiting(myClassName, "buildClassPathForJar", result); return result; 83 } 84 85 public static String buildClassPathForRar(Archive rar) throws IOException { 87 logger.entering(myClassName, "buildClassPathForRar", rar); final StringBuffer classpath = new StringBuffer (); 89 90 new File (rar.getPath()).listFiles(new FileFilter () { 92 public boolean accept(File file) { 93 if (file.getName().endsWith(".jar") && file.isFile()) { classpath.append(File.pathSeparator).append(file.getAbsolutePath()); 95 return true; 96 } 97 return false; 98 } 99 }); 100 101 Archive[] archives = rar.getBundledArchives(); 102 classpath.append(convertToClassPath(archives)); 103 classpath.append(File.pathSeparator).append( 105 convertToClassPath(getInstalledArchivesForJar(rar))); 106 String result = removeDuplicates(classpath).toString(); 107 logger.exiting(myClassName, "buildClassPathForRar", result); return result; 109 } 110 111 private static Archive[] getInstalledArchivesForJar(Archive jar) 112 throws IOException { 113 ArrayList <Archive> list = new ArrayList <Archive>(); 114 ExtensionRef[] extRefs = jar.getExtensionRefs(); 115 Archive[] bundled = jar.getBundledArchives(); 116 Archive[] allOptPkgs = Archive.getAllOptPkgsInstalledInJRE(); 117 118 for (int i = 0; i < extRefs.length; ++i) { 120 ExtensionRef ref = extRefs[i]; 121 logger.logp(Level.FINE, myClassName, "getInstalledArchivesForJar", "Trying to find an optional package for \n" + ref); logger.logp(Level.FINE, myClassName, "getInstalledArchivesForJar", "Searching in the bundled optional package list."); Archive satisfyingPkg = null; 126 for (int j = 0; j < bundled.length; ++j) { 127 try { 128 if (ref.isSatisfiedBy(bundled[j])) { 129 satisfyingPkg = bundled[j]; 130 logger.logp(Level.INFO, myClassName, 131 "getInstalledArchivesForJar", thisClassName + ".info1", new Object []{satisfyingPkg.getPath()}); 133 break; 134 } 135 } catch (IOException e) { 136 logger.logp(Level.WARNING, myClassName, 137 "getInstalledArchivesForJar", thisClassName + ".exception1", new Object []{bundled[j].getPath()}); 139 logger.log(Level.WARNING, "", e); 140 } 141 } 142 logger.logp(Level.FINE, myClassName, "getInstalledArchivesForJar", "Searching in the installed optional package list."); if (satisfyingPkg == null) { 146 for (int j = 0; j < allOptPkgs.length; ++j) { 147 try { 148 if (ref.isSatisfiedBy(allOptPkgs[j])) { 149 satisfyingPkg = allOptPkgs[j]; 150 logger.logp(Level.FINE, myClassName, 151 "buildClassPathForJar", "Found a matching installed optional package " + satisfyingPkg.getPath()); 154 break; 155 } 156 } catch (IOException e) { 157 logger.logp(Level.WARNING, myClassName, 158 "getInstalledArchivesForJar", thisClassName + ".exception1", new Object []{allOptPkgs[j].getPath()}); 160 logger.log(Level.WARNING, "", e); 161 } 162 } } if (satisfyingPkg != null) { 165 list.add(satisfyingPkg); 166 } else { 168 logger.logp(Level.WARNING, myClassName, "buildClassPathForEar", thisClassName + ".warning1", new Object []{ref.toString()}); } 170 } return (Archive[]) list.toArray(new Archive[0]); 172 } 173 174 public static String buildClassPathForWar(JarFile war) throws IOException { 175 return buildClassPathForWar(new Archive(war)); 176 } 177 178 public static String buildClassPathForWar(File file) throws IOException { 179 return buildClassPathForWar(new Archive(file)); 180 } 181 182 public static String buildClassPathForWar(Archive war) throws IOException { 183 final StringBuffer cp = new StringBuffer (); 184 if (new File (war.getPath()).isDirectory()) { 185 final String explodedDir = war.getPath(); 186 cp.append( 189 explodedDir + File.separator + "WEB-INF" + File.separator + 190 "classes" + 191 File.separator); 192 File [] jarFiles = new File ( 193 explodedDir + File.separator + "WEB-INF" + File.separator + 194 "lib").listFiles(new FileFilter () { 195 public boolean accept(File file) { 196 if (file.getName().endsWith(".jar") && 197 file.isFile()) { 198 cp.append(File.pathSeparator).append( 199 file.getAbsolutePath()); 200 return true; 201 } 202 return false; 203 } 204 }); 205 206 } else { 207 assert(false); } 209 210 Archive[] archives = war.getBundledArchives(); 211 cp.append(File.pathSeparator).append(convertToClassPath(archives)); 212 213 cp.append(File.pathSeparator).append( 215 convertToClassPath(getInstalledArchivesForJar(war))); 216 String result = removeDuplicates(cp).toString(); 217 logger.exiting(myClassName, "buildClassPathForWar", result); 218 return result; 219 } 220 221 public static String buildClassPathForEar(JarFile ear) throws IOException { 222 return buildClassPathForEar(new Archive(ear)); 223 } 224 225 public static String buildClassPathForEar(File fileOrDir) 226 throws IOException { 227 return buildClassPathForEar(new Archive(fileOrDir)); 228 } 229 230 public static String buildClassPathForEar(Archive jar) throws IOException { 231 logger.entering(myClassName, "buildClassPathForEar", jar); 232 StringBuffer classpath = new StringBuffer (); 233 ExtensionRef[] extRefs = jar.getExtensionRefs(); 236 Archive[] allOptPkgs = Archive.getAllOptPkgsInstalledInJRE(); 237 for (int i = 0; i < extRefs.length; ++i) { 238 ExtensionRef ref = extRefs[i]; 239 logger.logp(Level.FINE, myClassName, "buildClassPathForEar", 240 "Finding an installed optional package matching extension ref\n" + 241 ref); 242 Archive satisfyingPkg = null; 243 for (int j = 0; j < allOptPkgs.length; ++j) { 244 if (ref.isSatisfiedBy(allOptPkgs[j])) { 245 satisfyingPkg = allOptPkgs[j]; 246 break; 247 } 248 } 249 if (satisfyingPkg != null) { 250 logger.logp(Level.FINE, myClassName, "buildClassPathForEar", 251 "Found an installed optional package " + 252 satisfyingPkg.getPath()); 253 if (classpath.length() != 0) 254 classpath.append(File.pathSeparator); 255 classpath.append(satisfyingPkg.getPath()); 256 try { 257 String depCP = buildClassPathForJar(satisfyingPkg); 258 classpath.append(File.pathSeparator).append(depCP); 259 } catch (IOException e) { 260 logger.logp(Level.WARNING, myClassName, 261 "buildClassPathForEar", 262 "Ignoring " + satisfyingPkg.getPath(), e); 263 } 264 } else { 265 logger.logp(Level.WARNING, myClassName, "buildClassPathForEar", 266 "Could not find an installed optional package for \n" + 267 ref); 268 } 269 } File [] archives = 271 new File (jar.getPath()).listFiles(new FileFilter () { 272 public boolean accept(File pathname) { 273 return pathname.getName().endsWith("_rar"); 274 } 275 }); 276 for (File archive : archives) { 277 String rarCP = buildClassPathForRar(archive); 278 classpath.append(File.pathSeparator).append(rarCP); 279 } 280 String result = removeDuplicates(classpath).toString(); 281 logger.exiting(myClassName, "buildClassPath", result); 282 return result; 283 } 284 285 private static StringBuffer convertToClassPath(Archive[] archives) { 286 StringBuffer cp = new StringBuffer (); 287 for (int i = 0; i < archives.length; ++i) { 288 if (i != 0) cp.append(File.pathSeparatorChar); 289 cp.append(archives[i].getPath()); 290 } 291 return cp; 292 } 293 294 private static StringBuffer removeDuplicates(StringBuffer cp) { 295 ArrayList <String > tokens = new ArrayList <String >(); 296 for (StringTokenizer st = new StringTokenizer (cp.toString(), 297 File.pathSeparator); 298 st.hasMoreTokens();) { 299 String next = st.nextToken(); 300 if (!tokens.contains(next)) tokens.add(next); 301 } 302 StringBuffer result = new StringBuffer (); 303 for (int j = 0; j < tokens.size(); ++j) { 304 if (j != 0) result.append(File.pathSeparator); 305 result.append(tokens.get(j)); 306 } 307 return result; 308 } 309 310 public static void main(String [] args) { 311 if (args.length < 1) { 312 System.out.println( 313 "Usage : java " + ClassPathBuilder.class.getName() + 314 " <path(s) to jar files>"); 315 } 316 Logger logger = Logger.getLogger("apiscan"); 317 Handler h = new ConsoleHandler (); 318 h.setLevel(Level.ALL); 319 logger.addHandler(h); 320 logger.setLevel(Level.ALL); 321 322 for (int i = 0; i < args.length; i++) { 323 String jarFileName = args[i]; 324 try { 325 System.out.println("Building CLASSPATH for " + jarFileName); 326 String classPath; 327 if (jarFileName.endsWith(".ear")) 328 classPath = 329 ClassPathBuilder.buildClassPathForEar( 330 new Archive(new File (jarFileName))); 331 else 332 classPath = 333 ClassPathBuilder.buildClassPathForJar( 334 new Archive(new File (jarFileName))); 335 System.out.println( 336 "CLASSPATH for For " + jarFileName + "\n [" + classPath + 337 "]"); 338 } catch (Exception e) { 339 System.out.println( 340 "For " + jarFileName + " got the following exception"); 341 e.printStackTrace(); 342 } 343 } 344 } 345 } 346 | Popular Tags |