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-2007 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: package org.netbeans.nbbuild;
043:
044: import java.io.File;
045: import java.io.FileWriter;
046: import java.io.IOException;
047: import java.util.StringTokenizer;
048: import java.util.zip.ZipFile;
049: import org.apache.tools.ant.BuildException;
050: import org.apache.tools.ant.Task;
051: import org.apache.tools.ant.taskdefs.Java;
052: import org.apache.tools.ant.types.Commandline;
053: import org.apache.tools.ant.types.EnumeratedAttribute;
054: import org.apache.tools.ant.types.FileSet;
055: import org.apache.tools.ant.types.Path;
056: import org.apache.tools.ant.types.Reference;
057:
058: /** Invokes signature tests.
059: * @author Michal Zlamal
060: */
061: public class Sigtest extends Task {
062:
063: File fileName;
064: Path classpath;
065: String packages;
066: ActionType action;
067: File sigtestJar;
068: boolean failOnError = true;
069: String version;
070:
071: public void setFileName(File f) {
072: fileName = f;
073: }
074:
075: public void setPackages(String s) {
076: packages = s;
077: }
078:
079: public void setAction(ActionType s) {
080: action = s;
081: }
082:
083: public void setClasspath(Path p) {
084: if (classpath == null) {
085: classpath = p;
086: } else {
087: classpath.append(p);
088: }
089: }
090:
091: public Path createClasspath() {
092: if (classpath == null) {
093: classpath = new Path(getProject());
094: }
095: return classpath.createPath();
096: }
097:
098: public void setClasspathRef(Reference r) {
099: createClasspath().setRefid(r);
100: }
101:
102: public void setVersion(String v) {
103: version = v;
104: }
105:
106: public void setSigtestJar(File f) {
107: sigtestJar = f;
108: }
109:
110: public void setFailOnError(boolean b) {
111: failOnError = b;
112: }
113:
114: @Override
115: public void execute() throws BuildException {
116: if (fileName == null) {
117: throw new BuildException("FileName has to filed",
118: getLocation());
119: }
120: if (packages == null) {
121: throw new BuildException("Packages has to filed",
122: getLocation());
123: }
124: if (action == null) {
125: throw new BuildException("Action has to filed",
126: getLocation());
127: }
128: if (classpath == null) {
129: throw new BuildException("Classpath has to filed",
130: getLocation());
131: }
132: if (sigtestJar == null) {
133: throw new BuildException("SigtestJar has to filed",
134: getLocation());
135: }
136:
137: if (packages.equals("-")) {
138: log("No public packages, skipping");
139: return;
140: }
141:
142: if (!sigtestJar.exists()) {
143: throw new BuildException(
144: "Cannot find JAR with testing infrastructure: "
145: + sigtestJar);
146: }
147:
148: try {
149: ZipFile zip = new ZipFile(sigtestJar);
150: String c1 = "com/sun/tdk/signaturetest/Setup.class";
151: if (zip.getEntry(c1) != null) {
152: zip.close();
153: tdk();
154: return;
155: }
156: String c2 = "org/netbeans/apitest/Main.class";
157: if (zip.getEntry(c2) != null) {
158: zip.close();
159: apitest();
160: return;
161: }
162: zip.close();
163:
164: throw new BuildException("Cannot find " + c1 + " nor " + c2
165: + " in " + sigtestJar);
166: } catch (IOException ex) {
167: throw new BuildException(ex);
168: }
169:
170: }
171:
172: private void tdk() {
173: Java java = new Java();
174: java.setProject(getProject());
175: Path sigtestPath = new Path(getProject());
176: sigtestPath.setLocation(sigtestJar);
177:
178: java.setClasspath(sigtestPath);
179: String a = null;
180: if ("strictcheck".equals(action.getValue())) { // NOI18N
181: a = "SignatureTest"; // NOI18N
182: }
183: if ("generate".equals(action.getValue())) { // NOI18N
184: a = "Setup"; // NOI18N
185: }
186: if (a == null) {
187: throw new BuildException("Unsupported action " + action
188: + " use: strictcheck or generate");
189: }
190: java.setClassname("com.sun.tdk.signaturetest." + a);
191: Commandline.Argument arg;
192: arg = java.createArg();
193: arg.setValue("-FileName");
194: arg = java.createArg();
195: arg.setValue(fileName.getAbsolutePath());
196: arg = java.createArg();
197: arg.setValue("-Classpath");
198: arg = java.createArg();
199: {
200: Path extracp = new Path(getProject());
201: extracp.add(classpath);
202: FileSet jdk = new FileSet();
203: jdk.setDir(new File(new File(System
204: .getProperty("java.home")), "lib"));
205: jdk.setIncludes("*.jar");
206: extracp.addFileset(jdk);
207: arg.setPath(extracp);
208: }
209:
210: File outputFile = null;
211: String s = getProject().getProperty("sigtest.output.dir");
212: if (s != null) {
213: File dir = getProject().resolveFile(s);
214: dir.mkdirs();
215: outputFile = new File(dir, fileName.getName().replace(
216: ".sig", "").replace("-", "."));
217: log(outputFile.toString());
218: String email = getProject().getProperty("sigtest.mail");
219: if (email != null) {
220: try {
221: FileWriter w = new FileWriter(outputFile);
222: w.write("email: ");
223: w.write(email);
224: w.write("\n");
225: w.close();
226: } catch (IOException ex) {
227: throw new BuildException(ex);
228: }
229: }
230:
231: java.setAppend(true);
232: java.setOutput(outputFile);
233: java.setFork(true);
234: }
235:
236: arg = java.createArg();
237: arg.setLine("-static");
238: log("Packages: " + packages);
239: StringTokenizer packagesTokenizer = new StringTokenizer(
240: packages, ",");
241: while (packagesTokenizer.hasMoreTokens()) {
242: String p = packagesTokenizer.nextToken().trim();
243: String prefix = "-PackageWithoutSubpackages "; // NOI18N
244: //Strip the ending ".*"
245: int idx = p.lastIndexOf(".*");
246: if (idx > 0) {
247: p = p.substring(0, idx);
248: } else {
249: idx = p.lastIndexOf(".**");
250: if (idx > 0) {
251: prefix = "-Package "; // NOI18N
252: p = p.substring(0, idx);
253: }
254: }
255:
256: arg = java.createArg();
257: arg.setLine(prefix + p);
258: }
259: int returnCode = java.executeJava();
260: if (returnCode != 95) {
261: if (failOnError && outputFile == null) {
262: throw new BuildException(
263: "Signature tests return code is wrong ("
264: + returnCode
265: + "), check the messages above. For more info see http://wiki.netbeans.org/wiki/view/SignatureTest",
266: getLocation());
267: } else {
268: log("Signature tests return code is wrong ("
269: + returnCode + "), check the messages above");
270: }
271: } else {
272: if (outputFile != null) {
273: outputFile.delete();
274: }
275: }
276: }
277:
278: private void apitest() {
279: Java java = new Java();
280: java.setProject(getProject());
281: java.setTaskName(getTaskName());
282: java.setFork(true);
283: Path sigtestPath = new Path(getProject());
284: sigtestPath.setLocation(sigtestJar);
285:
286: java.setClasspath(sigtestPath);
287: java.setClassname("org.netbeans.apitest.Main");
288: Commandline.Argument arg;
289: arg = java.createArg();
290: arg.setValue("-FileName");
291: arg = java.createArg();
292: arg.setValue(fileName.getAbsolutePath());
293: arg = java.createArg();
294: arg.setValue("-Classpath");
295: arg = java.createArg();
296: arg.setPath(classpath);
297: if (action.getValue().equals("generate")) {
298: arg = java.createArg();
299: arg.setValue("-setup");
300: } else if (action.getValue().equals("binarycheck")) {
301: arg = java.createArg();
302: arg.setValue("-extensibleinterfaces");
303: } else if (action.getValue().equals("check")) {
304: // no special arg for check
305: } else if (action.getValue().equals("strictcheck")) {
306: arg = java.createArg();
307: arg.setValue("-maintenance");
308: } else {
309: throw new BuildException("Unknown action: " + action);
310: }
311: if (version != null) {
312: arg = java.createArg();
313: arg.setValue("-Version");
314: arg = java.createArg();
315: arg.setValue(version);
316: }
317:
318: arg = java.createJvmarg();
319: arg.setValue("-XX:PermSize=32m");
320: arg = java.createJvmarg();
321: arg.setValue("-XX:MaxPermSize=200m");
322:
323: File outputFile = null;
324: String s = getProject().getProperty("sigtest.output.dir");
325: if (s != null) {
326: File dir = getProject().resolveFile(s);
327: dir.mkdirs();
328: outputFile = new File(dir, fileName.getName().replace(
329: ".sig", "").replace("-", "."));
330: log(outputFile.toString());
331: java.setOutput(outputFile);
332: }
333:
334: log("Packages: " + packages);
335: StringTokenizer packagesTokenizer = new StringTokenizer(
336: packages, ",");
337: while (packagesTokenizer.hasMoreTokens()) {
338: String p = packagesTokenizer.nextToken().trim();
339: String prefix = "-PackageWithoutSubpackages "; // NOI18N
340: //Strip the ending ".*"
341: int idx = p.lastIndexOf(".*");
342: if (idx > 0) {
343: p = p.substring(0, idx);
344: } else {
345: idx = p.lastIndexOf(".**");
346: if (idx > 0) {
347: prefix = "-Package "; // NOI18N
348: p = p.substring(0, idx);
349: }
350: }
351:
352: arg = java.createArg();
353: arg.setLine(prefix + p);
354: }
355: int returnCode = java.executeJava();
356: if (returnCode != 0) {
357: if (failOnError && outputFile == null) {
358: throw new BuildException(
359: "Signature tests return code is wrong ("
360: + returnCode
361: + "), check the messages above. To disable signature tests, run your built with -Dsigtest.skip.check=true property",
362: getLocation());
363: } else {
364: log("Signature tests return code is wrong ("
365: + returnCode + "), check the messages above");
366: }
367: } else {
368: if (outputFile != null) {
369: outputFile.delete();
370: }
371: }
372: }
373:
374: public static final class ActionType extends EnumeratedAttribute {
375: public String[] getValues() {
376: return new String[] { "generate", "check", "strictcheck",
377: "binarycheck", };
378: }
379: }
380:
381: }
|