KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > selectjars > SelectJars


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * SelectJars.java
22  *
23  * Created on May 23, 2005, 11:26 AM
24  */

25
26 package selectjars;
27
28 import java.io.File JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.PrintStream JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collections JavaDoc;
34
35 import java.util.Enumeration JavaDoc;
36 import java.util.HashSet JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.LinkedList JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Set JavaDoc;
41 import java.util.StringTokenizer JavaDoc;
42 import java.util.Vector JavaDoc;
43 import java.util.jar.JarFile JavaDoc;
44 import java.util.zip.ZipEntry JavaDoc;
45
46 import org.apache.tools.ant.*;
47 import org.apache.tools.ant.types.*;
48
49 /**
50  * @author mk97936
51  */

52 public class SelectJars extends Task {
53     
54     /* Set for storing final list of included jars */
55     private Set JavaDoc includedJars = new HashSet JavaDoc();
56     
57     /* Name of the property for storing ant reference */
58     private String JavaDoc pathRefProp;
59     
60     /* Array of included packages */
61     private String JavaDoc[] inclPackages;
62     
63     /* Original string with selected packages */
64     private String JavaDoc inclPackagesOrig;
65     
66     /* List of all jar files to choose from */
67     private List JavaDoc filesets = new LinkedList JavaDoc();
68     
69     /* PrintStream for printing out logging info */
70     private PrintStream JavaDoc logStream = null;
71     
72     public void setPathrefprop(String JavaDoc s) {
73         pathRefProp = s;
74     }
75     
76     public void setInclpackages(String JavaDoc s) {
77         inclPackagesOrig = new String JavaDoc(s);
78         Vector JavaDoc vecIncluded = new Vector JavaDoc();
79         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, ",");
80         while (st.hasMoreTokens()) {
81             String JavaDoc incPkg = st.nextToken();
82             // cut off last two characters if ends with '.*' or one if with '.'
83
if (incPkg.endsWith(".*")) {
84                 incPkg = incPkg.substring(0, incPkg.length() - 2);
85             } else if (incPkg.endsWith(".")) {
86                 incPkg = incPkg.substring(0, incPkg.length() - 1);
87             }
88             log("Included package: " + incPkg, Project.MSG_VERBOSE);
89             vecIncluded.add(incPkg);
90         }
91         inclPackages = (String JavaDoc[]) vecIncluded.toArray(new String JavaDoc[] {});
92     }
93     
94     public void addFileset(FileSet fs) {
95         filesets.add(fs);
96     }
97     
98     public void setLogfile(File JavaDoc lgfl) {
99         try {
100             logStream = new PrintStream JavaDoc(new FileOutputStream JavaDoc(lgfl));
101         } catch (IOException JavaDoc ioe) {
102             throw new BuildException(ioe.getLocalizedMessage());
103         }
104     }
105     
106     public void execute() throws BuildException {
107         
108         Iterator JavaDoc it = filesets.iterator();
109         while (it.hasNext()) {
110             
111             FileSet fs = (FileSet) it.next();
112             DirectoryScanner ds = fs.getDirectoryScanner(project);
113             File JavaDoc basedir = ds.getBasedir();
114             String JavaDoc[] files = ds.getIncludedFiles();
115             
116             for (int i = 0; i < files.length; i++) {
117                 
118                 // scanning each jar file for required packages
119
String JavaDoc jarFileName = basedir + File.separator + files[i];
120                 JarFile JavaDoc jarFile = null;
121                 try {
122                     jarFile = new JarFile JavaDoc(jarFileName);
123                 } catch (IOException JavaDoc ioe) {
124                     ioe.printStackTrace();
125                 }
126                 
127                 log(jarFileName, Project.MSG_DEBUG);
128                 Enumeration JavaDoc entries = jarFile.entries();
129                 while (entries.hasMoreElements()) {
130                     ZipEntry JavaDoc zipEntry = (ZipEntry JavaDoc) entries.nextElement();
131                     if (zipEntry.isDirectory()) {
132                         log(zipEntry.toString(), Project.MSG_DEBUG);
133                         String JavaDoc pkgFromJar = zipEntry.toString().replace('/', '.');
134                         // removing trailing '.' to enable compare
135
if (pkgFromJar.endsWith(".")) {
136                             pkgFromJar = pkgFromJar.substring(0, pkgFromJar.length() - 1);
137                         }
138                         for (int j = 0; j < inclPackages.length; j++) {
139                             if (inclPackages[j].compareTo(pkgFromJar) == 0) {
140                                 log("Selected jar: " + jarFileName , Project.MSG_VERBOSE);
141                                 log(" " + inclPackages[j] + " == " + pkgFromJar, Project.MSG_VERBOSE);
142                                 includedJars.add(jarFileName);
143                             }
144                         }
145                     }
146                 }
147                 
148             }
149             
150         }
151         
152         List JavaDoc listIncluded = new ArrayList JavaDoc(includedJars);
153         Collections.sort(listIncluded);
154         Iterator JavaDoc iter = listIncluded.iterator();
155         
156         log("Found " + includedJars.size() + " jar files.", Project.MSG_VERBOSE);
157                 
158         if (logStream != null) {
159             logStream.println("Instrumented Packages:");
160             logStream.println(inclPackagesOrig);
161             logStream.println("--------------------------------------------------------------------------------");
162             logStream.println("Included JAR files:");
163         }
164         
165         Path path = new Path(getProject());
166         getProject().addReference(pathRefProp, path);
167         while (iter.hasNext()) {
168             String JavaDoc fileName = (String JavaDoc) iter.next();
169             path.createPathElement().setLocation(new File JavaDoc(fileName));
170             if (logStream != null) {
171                 logStream.println(fileName);
172             }
173         }
174         if (logStream != null) {
175             logStream.println("Found " + includedJars.size() + " jar files.");
176             logStream.close();
177         }
178     }
179     
180 }
181
Popular Tags