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.FileInputStream ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.util.ArrayList ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.StringTokenizer ; 41 import java.util.jar.Attributes ; 42 import java.util.jar.JarFile ; 43 import java.util.jar.Manifest ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 48 51 public class Archive { 52 private Manifest manifest; 53 private File path; 54 private static String resourceBundleName = "com.sun.enterprise.tools.verifier.apiscan.LocalStrings"; 55 private static Logger logger = Logger.getLogger("apiscan.archive", resourceBundleName); private static final String myClassName = "Archive"; private static Archive[] allOptPkgsInstalledInJRE; private static String thisClassName = "com.sun.enterprise.tools.verifier.apiscan.packaging.Archive"; 60 public static Archive[] getAllOptPkgsInstalledInJRE() { 64 if (allOptPkgsInstalledInJRE != null) return allOptPkgsInstalledInJRE; 65 final ArrayList <Archive> allPkgs = new ArrayList <Archive>(); 66 synchronized (Archive.class) { 67 if (allOptPkgsInstalledInJRE == null) { List ext_dirs = listAllExtDirs(); 69 for (Iterator iter = ext_dirs.iterator(); iter.hasNext();) { 70 File ext_dir = new File ((String ) iter.next()); 71 ext_dir.listFiles(new FileFilter () { 72 public boolean accept(File f) { 73 if (!f.isDirectory()) { 74 try { 75 allPkgs.add(new Archive(new JarFile (f))); 76 logger.logp(Level.FINE, myClassName, 77 "getAllOptPkgsInstalledInJRE", "Found an installed opt pkg " + f.getAbsolutePath()); 80 return true; 81 } catch (Exception e) { 82 logger.logp(Level.INFO, myClassName, 83 "getAllOptPkgsInstalledInJRE", thisClassName + ".exception1", new Object []{f.toString()}); 85 logger.log(Level.INFO, "", e); 86 } 87 } 88 return false; 89 } }); 91 } 92 } allOptPkgsInstalledInJRE = new Archive[allPkgs.size()]; 94 allPkgs.toArray(allOptPkgsInstalledInJRE); 95 } return allOptPkgsInstalledInJRE; 97 } 98 99 private static List listAllExtDirs() { 100 String ext_dirStr = new String (System.getProperty("java.ext.dirs")); 101 logger.fine("Extension Dir Path is " + ext_dirStr); ArrayList <String > ext_dirs = new ArrayList <String >(); 103 StringTokenizer st = new StringTokenizer (ext_dirStr, 104 File.pathSeparator); 105 while (st.hasMoreTokens()) { 106 String next = st.nextToken(); 107 ext_dirs.add(next); 108 } 109 return ext_dirs; 110 } 111 112 115 public Archive(JarFile jar) throws IOException { 116 manifest = jar.getManifest(); 117 path = new File (jar.getName()); 118 } 119 120 public Archive(File path) throws IOException { 122 this.path = path.getCanonicalFile(); 123 } 124 125 public String getClassPath() throws IOException { 126 String cp = getManifest().getMainAttributes().getValue( 127 Attributes.Name.CLASS_PATH); 128 if (cp != null) 129 return cp; 130 else 131 return ""; 132 } 133 134 public synchronized Manifest getManifest() throws IOException { 136 if (manifest == null) { 137 if (path.isDirectory()) { 138 File file = new File ( 139 path.getPath() + File.separator + JarFile.MANIFEST_NAME); 140 if (file.exists()) { 141 InputStream mis = new FileInputStream (file); 142 manifest = new Manifest (mis); 143 mis.close(); 144 } 145 } else { 146 JarFile jar = new JarFile (path); 147 try { 148 manifest = jar.getManifest(); 149 } finally { 150 jar.close(); 151 } 152 } 153 if (manifest == null) 154 manifest = new Manifest (); 155 } 156 return manifest; 157 } 158 159 164 public String getPath() { 165 return path.getAbsolutePath(); 166 } 167 168 public Archive[] getBundledArchives() throws IOException { 169 ArrayList <Archive> list = new ArrayList <Archive>(); 170 String parent = path.getParent() + File.separator; 171 for (StringTokenizer st = new StringTokenizer (getClassPath()); 172 st.hasMoreTokens();) { 173 String nextEntry = st.nextToken(); 174 String entryPath = parent + nextEntry; 175 if (!new File (entryPath).exists()) { 176 logger.logp(Level.FINE, myClassName, "getBundledArchives", entryPath + 178 " does not exist, will try to see if this is a module whose name has been changed when archive was exploded."); String newNextEntry; 180 if (nextEntry.startsWith("./") && nextEntry.length() > 2) newNextEntry = 183 nextEntry.substring("./".length()).replaceAll( "\\.", "_"); else 186 newNextEntry = nextEntry.replaceAll("\\.", "_"); 188 if (new File (parent, newNextEntry).exists()) { 189 logger.logp(Level.FINE, myClassName, "getBundledArchives", "Using " + newNextEntry + " instead of " + nextEntry); entryPath = parent + newNextEntry; 192 list.add(new Archive(new File (entryPath))); 193 } else { 194 logger.logp(Level.WARNING, myClassName, 195 "getBundledArchives", thisClassName + ".error1", new Object []{getPath(), nextEntry}); 197 } 198 } 199 list.add(new Archive(new File (entryPath))); 200 } 201 return (Archive[]) list.toArray(new Archive[0]); 202 } 203 204 208 public ExtensionRef[] getExtensionRefs() throws IOException { 209 ExtensionRef[] refs = new ExtensionRef[0]; 210 Manifest manifest = getManifest(); 211 String extensions = manifest.getMainAttributes().getValue( 212 Attributes.Name.EXTENSION_LIST); 213 ArrayList <ExtensionRef> extensionList = new ArrayList <ExtensionRef>(); 214 if (extensions != null) { 215 for (StringTokenizer st = new StringTokenizer (extensions); 216 st.hasMoreTokens();) { 217 String extName = st.nextToken(); 218 ExtensionRef ref = new ExtensionRef(manifest, extName); 219 extensionList.add(ref); 220 } 221 } 222 refs = (ExtensionRef[]) extensionList.toArray(refs); 223 return refs; 224 } 225 226 public String toString() { 227 return getPath(); 228 } 229 } 230 | Popular Tags |