KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > apiscan > packaging > Archive


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Archive.java
26  *
27  * Created on September 6, 2004, 9:10 AM
28  */

29
30 package com.sun.enterprise.tools.verifier.apiscan.packaging;
31
32 import java.io.File JavaDoc;
33 import java.io.FileFilter JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.StringTokenizer JavaDoc;
41 import java.util.jar.Attributes JavaDoc;
42 import java.util.jar.JarFile JavaDoc;
43 import java.util.jar.Manifest JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47
48 /**
49  * @author Sanjeeb.Sahoo@Sun.COM
50  */

51 public class Archive {
52     private Manifest JavaDoc manifest;
53     private File JavaDoc path;
54     private static String JavaDoc resourceBundleName = "com.sun.enterprise.tools.verifier.apiscan.LocalStrings";
55     private static Logger JavaDoc logger = Logger.getLogger("apiscan.archive", resourceBundleName); // NOI18N
56
private static final String JavaDoc myClassName = "Archive"; // NOI18N
57
private static Archive[] allOptPkgsInstalledInJRE;//note this is per JVM. It is lazily instantiated
58
private static String JavaDoc thisClassName = "com.sun.enterprise.tools.verifier.apiscan.packaging.Archive"; // NOI18N
59

60     //returns a unmodifiable collection of installed optional packages.
61
//once the method is called, for subsequent calls it retuns the same list
62
//even if new pkg is installed in JVM lib ext dir. This is in line with JVM operations.
63
public static Archive[] getAllOptPkgsInstalledInJRE() {
64         if (allOptPkgsInstalledInJRE != null) return allOptPkgsInstalledInJRE;
65         final ArrayList JavaDoc<Archive> allPkgs = new ArrayList JavaDoc<Archive>();
66         synchronized (Archive.class) {
67             if (allOptPkgsInstalledInJRE == null) {//double if check to avoid race condition
68
List JavaDoc ext_dirs = listAllExtDirs();
69                 for (Iterator JavaDoc iter = ext_dirs.iterator(); iter.hasNext();) {
70                     File JavaDoc ext_dir = new File JavaDoc((String JavaDoc) iter.next());
71                     ext_dir.listFiles(new FileFilter JavaDoc() {
72                         public boolean accept(File JavaDoc f) {
73                             if (!f.isDirectory()) {
74                                 try {
75                                     allPkgs.add(new Archive(new JarFile JavaDoc(f)));
76                                     logger.logp(Level.FINE, myClassName,
77                                             "getAllOptPkgsInstalledInJRE", // NOI18N
78
"Found an installed opt pkg " + // NOI18N
79
f.getAbsolutePath());
80                                     return true;
81                                 } catch (Exception JavaDoc e) {
82                                     logger.logp(Level.INFO, myClassName,
83                                             "getAllOptPkgsInstalledInJRE", // NOI18N
84
thisClassName + ".exception1", new Object JavaDoc[]{f.toString()});
85                                     logger.log(Level.INFO, "", e);
86                                 }
87                             }
88                             return false;
89                         }//accept()
90
});
91                 }
92             }//if null
93
allOptPkgsInstalledInJRE = new Archive[allPkgs.size()];
94             allPkgs.toArray(allOptPkgsInstalledInJRE);
95         }//synchronized
96
return allOptPkgsInstalledInJRE;
97     }
98
99     private static List JavaDoc listAllExtDirs() {
100         String JavaDoc ext_dirStr = new String JavaDoc(System.getProperty("java.ext.dirs"));
101         logger.fine("Extension Dir Path is " + ext_dirStr); // NOI18N
102
ArrayList JavaDoc<String JavaDoc> ext_dirs = new ArrayList JavaDoc<String JavaDoc>();
103         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(ext_dirStr,
104                 File.pathSeparator);
105         while (st.hasMoreTokens()) {
106             String JavaDoc next = st.nextToken();
107             ext_dirs.add(next);
108         }
109         return ext_dirs;
110     }
111
112     /**
113      * Creates a new instance of Archive
114      */

115     public Archive(JarFile JavaDoc jar) throws IOException JavaDoc {
116         manifest = jar.getManifest();
117         path = new File JavaDoc(jar.getName());
118     }
119
120     //path represnets either a dir or a jar file path
121
public Archive(File JavaDoc path) throws IOException JavaDoc {
122         this.path = path.getCanonicalFile();
123     }
124
125     public String JavaDoc getClassPath() throws IOException JavaDoc {
126         String JavaDoc cp = getManifest().getMainAttributes().getValue(
127                 Attributes.Name.CLASS_PATH);
128         if (cp != null)
129             return cp;
130         else
131             return "";
132     }
133
134     //lazy initialisation
135
public synchronized Manifest JavaDoc getManifest() throws IOException JavaDoc {
136         if (manifest == null) {
137             if (path.isDirectory()) {
138                 File JavaDoc file = new File JavaDoc(
139                         path.getPath() + File.separator + JarFile.MANIFEST_NAME);
140                 if (file.exists()) {
141                     InputStream JavaDoc mis = new FileInputStream JavaDoc(file);
142                     manifest = new Manifest JavaDoc(mis);
143                     mis.close();
144                 }
145             } else {
146                 JarFile JavaDoc jar = new JarFile JavaDoc(path);
147                 try {
148                     manifest = jar.getManifest();
149                 } finally {
150                     jar.close();
151                 }
152             }
153             if (manifest == null)
154                 manifest = new Manifest JavaDoc();
155         }
156         return manifest;
157     }
158
159     /**
160      * @return the absolute path of this package. Depdnding on whether it was
161      * constructed from a dir or a jar file, the returned path
162      * represents either a file or a dir.
163      */

164     public String JavaDoc getPath() {
165         return path.getAbsolutePath();
166     }
167
168     public Archive[] getBundledArchives() throws IOException JavaDoc {
169         ArrayList JavaDoc<Archive> list = new ArrayList JavaDoc<Archive>();
170         String JavaDoc parent = path.getParent() + File.separator;
171         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(getClassPath());
172              st.hasMoreTokens();) {
173             String JavaDoc nextEntry = st.nextToken();
174             String JavaDoc entryPath = parent + nextEntry;
175             if (!new File JavaDoc(entryPath).exists()) {
176                 logger.logp(Level.FINE, myClassName, "getBundledArchives", // NOI18N
177
entryPath +
178                         " does not exist, will try to see if this is a module whose name has been changed when archive was exploded."); // NOI18N
179
String JavaDoc newNextEntry;
180                 //account for the fact that Class-Path may be specified as ./a.jar
181
if (nextEntry.startsWith("./") && nextEntry.length() > 2) // NOI18N
182
newNextEntry =
183                             nextEntry.substring("./".length()).replaceAll( // NOI18N
184
"\\.", "_"); // NOI18N
185
else
186                     newNextEntry = nextEntry.replaceAll("\\.", "_"); // NOI18N
187

188                 if (new File JavaDoc(parent, newNextEntry).exists()) {
189                     logger.logp(Level.FINE, myClassName, "getBundledArchives", // NOI18N
190
"Using " + newNextEntry + " instead of " + nextEntry); // NOI18N
191
entryPath = parent + newNextEntry;
192                     list.add(new Archive(new File JavaDoc(entryPath)));
193                 } else {
194                     logger.logp(Level.WARNING, myClassName,
195                             "getBundledArchives", // NOI18N
196
thisClassName + ".error1", new Object JavaDoc[]{getPath(), nextEntry});
197                 }
198             }
199             list.add(new Archive(new File JavaDoc(entryPath)));
200         }
201         return (Archive[]) list.toArray(new Archive[0]);
202     }
203
204     /**
205      * @return the list of installed optional packages that this package depends
206      * on.
207      */

208     public ExtensionRef[] getExtensionRefs() throws IOException JavaDoc {
209         ExtensionRef[] refs = new ExtensionRef[0];
210         Manifest JavaDoc manifest = getManifest();
211         String JavaDoc extensions = manifest.getMainAttributes().getValue(
212                 Attributes.Name.EXTENSION_LIST);
213         ArrayList JavaDoc<ExtensionRef> extensionList = new ArrayList JavaDoc<ExtensionRef>();
214         if (extensions != null) {
215             for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(extensions);
216                  st.hasMoreTokens();) {
217                 String JavaDoc 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 JavaDoc toString() {
227         return getPath();
228     }
229 }
230
Popular Tags