001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * SelectJars.java
044: *
045: * Created on May 23, 2005, 11:26 AM
046: */
047:
048: package selectjars;
049:
050: import java.io.File;
051: import java.io.FileOutputStream;
052: import java.io.IOException;
053: import java.io.PrintStream;
054: import java.util.ArrayList;
055: import java.util.Collections;
056:
057: import java.util.Enumeration;
058: import java.util.HashSet;
059: import java.util.Iterator;
060: import java.util.LinkedList;
061: import java.util.List;
062: import java.util.Set;
063: import java.util.StringTokenizer;
064: import java.util.Vector;
065: import java.util.jar.JarFile;
066: import java.util.zip.ZipEntry;
067:
068: import org.apache.tools.ant.*;
069: import org.apache.tools.ant.types.*;
070:
071: /**
072: * @author mk97936
073: */
074: public class SelectJars extends Task {
075:
076: /* Set for storing final list of included jars */
077: private Set includedJars = new HashSet();
078:
079: /* Name of the property for storing ant reference */
080: private String pathRefProp;
081:
082: /* Array of included packages */
083: private String[] inclPackages;
084:
085: /* Original string with selected packages */
086: private String inclPackagesOrig;
087:
088: /* List of all jar files to choose from */
089: private List filesets = new LinkedList();
090:
091: /* PrintStream for printing out logging info */
092: private PrintStream logStream = null;
093:
094: public void setPathrefprop(String s) {
095: pathRefProp = s;
096: }
097:
098: public void setInclpackages(String s) {
099: inclPackagesOrig = new String(s);
100: Vector vecIncluded = new Vector();
101: StringTokenizer st = new StringTokenizer(s, ",");
102: while (st.hasMoreTokens()) {
103: String incPkg = st.nextToken();
104: // cut off last two characters if ends with '.*' or one if with '.'
105: if (incPkg.endsWith(".*")) {
106: incPkg = incPkg.substring(0, incPkg.length() - 2);
107: } else if (incPkg.endsWith(".")) {
108: incPkg = incPkg.substring(0, incPkg.length() - 1);
109: }
110: log("Included package: " + incPkg, Project.MSG_VERBOSE);
111: vecIncluded.add(incPkg);
112: }
113: inclPackages = (String[]) vecIncluded.toArray(new String[] {});
114: }
115:
116: public void addFileset(FileSet fs) {
117: filesets.add(fs);
118: }
119:
120: public void setLogfile(File lgfl) {
121: try {
122: logStream = new PrintStream(new FileOutputStream(lgfl));
123: } catch (IOException ioe) {
124: throw new BuildException(ioe.getLocalizedMessage());
125: }
126: }
127:
128: public void execute() throws BuildException {
129:
130: Iterator it = filesets.iterator();
131: while (it.hasNext()) {
132:
133: FileSet fs = (FileSet) it.next();
134: DirectoryScanner ds = fs.getDirectoryScanner(project);
135: File basedir = ds.getBasedir();
136: String[] files = ds.getIncludedFiles();
137:
138: for (int i = 0; i < files.length; i++) {
139:
140: // scanning each jar file for required packages
141: String jarFileName = basedir + File.separator
142: + files[i];
143: JarFile jarFile = null;
144: try {
145: jarFile = new JarFile(jarFileName);
146: } catch (IOException ioe) {
147: ioe.printStackTrace();
148: }
149:
150: log(jarFileName, Project.MSG_DEBUG);
151: Enumeration entries = jarFile.entries();
152: while (entries.hasMoreElements()) {
153: ZipEntry zipEntry = (ZipEntry) entries
154: .nextElement();
155: if (zipEntry.isDirectory()) {
156: log(zipEntry.toString(), Project.MSG_DEBUG);
157: String pkgFromJar = zipEntry.toString()
158: .replace('/', '.');
159: // removing trailing '.' to enable compare
160: if (pkgFromJar.endsWith(".")) {
161: pkgFromJar = pkgFromJar.substring(0,
162: pkgFromJar.length() - 1);
163: }
164: for (int j = 0; j < inclPackages.length; j++) {
165: if (inclPackages[j].compareTo(pkgFromJar) == 0) {
166: log("Selected jar: " + jarFileName,
167: Project.MSG_VERBOSE);
168: log(" " + inclPackages[j] + " == "
169: + pkgFromJar,
170: Project.MSG_VERBOSE);
171: includedJars.add(jarFileName);
172: }
173: }
174: }
175: }
176:
177: }
178:
179: }
180:
181: List listIncluded = new ArrayList(includedJars);
182: Collections.sort(listIncluded);
183: Iterator iter = listIncluded.iterator();
184:
185: log("Found " + includedJars.size() + " jar files.",
186: Project.MSG_VERBOSE);
187:
188: if (logStream != null) {
189: logStream.println("Instrumented Packages:");
190: logStream.println(inclPackagesOrig);
191: logStream
192: .println("--------------------------------------------------------------------------------");
193: logStream.println("Included JAR files:");
194: }
195:
196: Path path = new Path(getProject());
197: getProject().addReference(pathRefProp, path);
198: while (iter.hasNext()) {
199: String fileName = (String) iter.next();
200: path.createPathElement().setLocation(new File(fileName));
201: if (logStream != null) {
202: logStream.println(fileName);
203: }
204: }
205: if (logStream != null) {
206: logStream.println("Found " + includedJars.size()
207: + " jar files.");
208: logStream.close();
209: }
210: }
211:
212: }
|