KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ClassPathBuilder.java
26  *
27  * Created on August 13, 2004, 6:52 PM
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.IOException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37 import java.util.jar.JarFile JavaDoc;
38 import java.util.logging.ConsoleHandler JavaDoc;
39 import java.util.logging.Handler JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * This is a stand alone utility that recurssively computes the optional package
45  * dependency of a jar file. Then it makes a classpath out of the same
46  * information.
47  *
48  * @author Sanjeeb.Sahoo@Sun.COM
49  */

50 public class ClassPathBuilder {
51     private static String JavaDoc resourceBundleName = "com.sun.enterprise.tools.verifier.apiscan.LocalStrings";
52     public static Logger JavaDoc logger = Logger.getLogger("apiscan.packaging", resourceBundleName); // NOI18N
53
private static final String JavaDoc myClassName = "ClassPathBuilder"; // NOI18N
54

55     private static final String JavaDoc thisClassName = "com.sun.enterprise.tools.verifier.apiscan.packaging.ClassPathBuilder"; // NOI18N
56

57     public static String JavaDoc buildClassPathForJar(JarFile JavaDoc jar) throws IOException JavaDoc {
58         return buildClassPathForJar(new Archive(jar));
59     }
60
61     public static String JavaDoc buildClassPathForJar(File JavaDoc file) throws IOException JavaDoc {
62         return buildClassPathForJar(new Archive(file));
63     }
64
65     public static String JavaDoc buildClassPathForRar(File JavaDoc file) throws IOException JavaDoc {
66         return buildClassPathForRar(new Archive(file));
67     }
68
69     //It adds the incoming jar file to the classpath.
70
public static String JavaDoc buildClassPathForJar(Archive jar) throws IOException JavaDoc {
71         logger.entering(myClassName, "buildClassPathForJar", jar); // NOI18N
72
StringBuffer JavaDoc classpath = new StringBuffer JavaDoc(
73                 jar.getPath() + File.pathSeparator);
74
75         Archive[] archives = jar.getBundledArchives();
76         classpath.append(convertToClassPath(archives));
77         //now resolve installed exts.
78
classpath.append(File.pathSeparator).append(
79                 convertToClassPath(getInstalledArchivesForJar(jar)));
80         String JavaDoc result = removeDuplicates(classpath).toString();
81         logger.exiting(myClassName, "buildClassPathForJar", result); // NOI18N
82
return result;
83     }
84
85     //It adds the incoming jar file to the classpath.
86
public static String JavaDoc buildClassPathForRar(Archive rar) throws IOException JavaDoc {
87         logger.entering(myClassName, "buildClassPathForRar", rar); // NOI18N
88
final StringBuffer JavaDoc classpath = new StringBuffer JavaDoc();
89
90         // all the jar's inside the rar should be available in the classpath
91
new File JavaDoc(rar.getPath()).listFiles(new FileFilter JavaDoc() {
92             public boolean accept(File JavaDoc file) {
93                 if (file.getName().endsWith(".jar") && file.isFile()) { // NOI18N
94
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         //now resolve installed exts.
104
classpath.append(File.pathSeparator).append(
105                 convertToClassPath(getInstalledArchivesForJar(rar)));
106         String JavaDoc result = removeDuplicates(classpath).toString();
107         logger.exiting(myClassName, "buildClassPathForRar", result); // NOI18N
108
return result;
109     }
110
111     private static Archive[] getInstalledArchivesForJar(Archive jar)
112             throws IOException JavaDoc {
113         ArrayList JavaDoc<Archive> list = new ArrayList JavaDoc<Archive>();
114         ExtensionRef[] extRefs = jar.getExtensionRefs();
115         Archive[] bundled = jar.getBundledArchives();
116         Archive[] allOptPkgs = Archive.getAllOptPkgsInstalledInJRE();
117
118         //first go over all thebundled ones.
119
for (int i = 0; i < extRefs.length; ++i) {
120             ExtensionRef ref = extRefs[i];
121             logger.logp(Level.FINE, myClassName, "getInstalledArchivesForJar", // NOI18N
122
"Trying to find an optional package for \n" + ref); // NOI18N
123
logger.logp(Level.FINE, myClassName, "getInstalledArchivesForJar", // NOI18N
124
"Searching in the bundled optional package list."); // NOI18N
125
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", // NOI18N
132
thisClassName + ".info1", new Object JavaDoc[]{satisfyingPkg.getPath()});
133                         break;
134                     }
135                 } catch (IOException JavaDoc e) {
136                     logger.logp(Level.WARNING, myClassName,
137                             "getInstalledArchivesForJar", // NOI18N
138
thisClassName + ".exception1", new Object JavaDoc[]{bundled[j].getPath()});
139                     logger.log(Level.WARNING, "", e);
140                 }
141             }
142             logger.logp(Level.FINE, myClassName, "getInstalledArchivesForJar", // NOI18N
143
"Searching in the installed optional package list."); // NOI18N
144
//now go over installed refs.
145
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", // NOI18N
152
"Found a matching installed optional package " + // NOI18N
153
satisfyingPkg.getPath());
154                             break;
155                         }
156                     } catch (IOException JavaDoc e) {
157                         logger.logp(Level.WARNING, myClassName,
158                                 "getInstalledArchivesForJar", // NOI18N
159
thisClassName + ".exception1", new Object JavaDoc[]{allOptPkgs[j].getPath()});
160                         logger.log(Level.WARNING, "", e);
161                     }
162                 }//for
163
}//if
164
if (satisfyingPkg != null) {
165                 list.add(satisfyingPkg);
166                 //TODO We are not supporting if the satisfyingPkg depends on some optional package.
167
} else {
168                 logger.logp(Level.WARNING, myClassName, "buildClassPathForEar", thisClassName + ".warning1", new Object JavaDoc[]{ref.toString()});// NOI18N
169
}
170         }//for each ref
171
return (Archive[]) list.toArray(new Archive[0]);
172     }
173
174     public static String JavaDoc buildClassPathForWar(JarFile JavaDoc war) throws IOException JavaDoc {
175         return buildClassPathForWar(new Archive(war));
176     }
177
178     public static String JavaDoc buildClassPathForWar(File JavaDoc file) throws IOException JavaDoc {
179         return buildClassPathForWar(new Archive(file));
180     }
181
182     public static String JavaDoc buildClassPathForWar(Archive war) throws IOException JavaDoc {
183         final StringBuffer JavaDoc cp = new StringBuffer JavaDoc();
184         if (new File JavaDoc(war.getPath()).isDirectory()) {
185             final String JavaDoc explodedDir = war.getPath();
186             //As per section#9.5 of Servlet 2.4 spec, WEB-INF/classes must be
187
// ahead of WEB-INF/lib/*.jar in class-path.
188
cp.append(
189                     explodedDir + File.separator + "WEB-INF" + File.separator +
190                     "classes" +
191                     File.separator);
192             File JavaDoc[] jarFiles = new File JavaDoc(
193                     explodedDir + File.separator + "WEB-INF" + File.separator +
194                     "lib").listFiles(new FileFilter JavaDoc() {
195                         public boolean accept(File JavaDoc 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);//We must explode the war. TBD. Anyway never reached from verifier.
208
}
209
210         Archive[] archives = war.getBundledArchives();
211         cp.append(File.pathSeparator).append(convertToClassPath(archives));
212
213         //now resolve installed exts.
214
cp.append(File.pathSeparator).append(
215                 convertToClassPath(getInstalledArchivesForJar(war)));
216         String JavaDoc result = removeDuplicates(cp).toString();
217         logger.exiting(myClassName, "buildClassPathForWar", result);
218         return result;
219     }
220
221     public static String JavaDoc buildClassPathForEar(JarFile JavaDoc ear) throws IOException JavaDoc {
222         return buildClassPathForEar(new Archive(ear));
223     }
224
225     public static String JavaDoc buildClassPathForEar(File JavaDoc fileOrDir)
226             throws IOException JavaDoc {
227         return buildClassPathForEar(new Archive(fileOrDir));
228     }
229
230     public static String JavaDoc buildClassPathForEar(Archive jar) throws IOException JavaDoc {
231         logger.entering(myClassName, "buildClassPathForEar", jar);
232         StringBuffer JavaDoc classpath = new StringBuffer JavaDoc();
233         //See we do not care about bundled opt packages, as for an ear file
234
//there should not be any Class-Path entry.
235
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 JavaDoc depCP = buildClassPathForJar(satisfyingPkg);
258                     classpath.append(File.pathSeparator).append(depCP);
259                 } catch (IOException JavaDoc 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         }//for each ref
270
File JavaDoc[] archives =
271                 new File JavaDoc(jar.getPath()).listFiles(new FileFilter JavaDoc() {
272                     public boolean accept(File JavaDoc pathname) {
273                         return pathname.getName().endsWith("_rar");
274                     }
275                 });
276         for (File JavaDoc archive : archives) {
277             String JavaDoc rarCP = buildClassPathForRar(archive);
278             classpath.append(File.pathSeparator).append(rarCP);
279         }
280         String JavaDoc result = removeDuplicates(classpath).toString();
281         logger.exiting(myClassName, "buildClassPath", result);
282         return result;
283     }
284
285     private static StringBuffer JavaDoc convertToClassPath(Archive[] archives) {
286         StringBuffer JavaDoc cp = new StringBuffer JavaDoc();
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 JavaDoc removeDuplicates(StringBuffer JavaDoc cp) {
295         ArrayList JavaDoc<String JavaDoc> tokens = new ArrayList JavaDoc<String JavaDoc>();
296         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(cp.toString(),
297                 File.pathSeparator);
298              st.hasMoreTokens();) {
299             String JavaDoc next = st.nextToken();
300             if (!tokens.contains(next)) tokens.add(next);
301         }
302         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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 JavaDoc[] 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 JavaDoc logger = Logger.getLogger("apiscan");
317         Handler JavaDoc h = new ConsoleHandler JavaDoc();
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 JavaDoc jarFileName = args[i];
324             try {
325                 System.out.println("Building CLASSPATH for " + jarFileName);
326                 String JavaDoc classPath;
327                 if (jarFileName.endsWith(".ear"))
328                     classPath =
329                             ClassPathBuilder.buildClassPathForEar(
330                                     new Archive(new File JavaDoc(jarFileName)));
331                 else
332                     classPath =
333                             ClassPathBuilder.buildClassPathForJar(
334                                     new Archive(new File JavaDoc(jarFileName)));
335                 System.out.println(
336                         "CLASSPATH for For " + jarFileName + "\n [" + classPath +
337                         "]");
338             } catch (Exception JavaDoc e) {
339                 System.out.println(
340                         "For " + jarFileName + " got the following exception");
341                 e.printStackTrace();
342             }
343         }
344     }
345 }
346
Popular Tags