2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.nbbuild;
45
import java.io.FileWriter;
46
import java.io.IOException;
47
import java.util.StringTokenizer;
48
import java.util.zip.ZipFile;
49
import org.apache.tools.ant.BuildException;
50
import org.apache.tools.ant.Task;
51
import org.apache.tools.ant.taskdefs.Java;
52
import org.apache.tools.ant.types.Commandline;
53
import org.apache.tools.ant.types.EnumeratedAttribute;
54
import org.apache.tools.ant.types.FileSet;
55
import org.apache.tools.ant.types.Path;
56
import org.apache.tools.ant.types.Reference;
58
/** Invokes signature tests.
59
* @author Michal Zlamal
61
public class Sigtest extends Task {
68
boolean failOnError = true;
71
public void setFileName(File f) {
75
public void setPackages(String s) {
79
public void setAction(ActionType s) {
83
public void setClasspath(Path p) {
84
if (classpath == null) {
90
public Path createClasspath () {
91
if (classpath == null) {
92
classpath = new Path(getProject());
94
return classpath.createPath();
96
public void setClasspathRef(Reference r) {
97
createClasspath().setRefid(r);
99
public void setVersion(String v) {
103
public void setSigtestJar(File f) {
107
public void setFailOnError(boolean b) {
112
public void execute() throws BuildException {
113
if (fileName == null) {
114
throw new BuildException("FileName has to filed", getLocation());
116
if (packages == null) {
117
throw new BuildException("Packages has to filed", getLocation());
119
if (action == null) {
120
throw new BuildException("Action has to filed", getLocation());
122
if (classpath == null) {
123
throw new BuildException("Classpath has to filed", getLocation());
125
if (sigtestJar == null) {
126
throw new BuildException("SigtestJar has to filed", getLocation());
129
if (packages.equals("-")) {
130
log("No public packages, skipping");
134
if (!sigtestJar.exists()) {
135
throw new BuildException("Cannot find JAR with testing infrastructure: " + sigtestJar);
139
ZipFile zip = new ZipFile(sigtestJar);
140
String c1 = "com/sun/tdk/signaturetest/Setup.class";
141
if (zip.getEntry(c1) != null) {
146
String c2 = "org/netbeans/apitest/Main.class";
147
if (zip.getEntry(c2) != null) {
154
throw new BuildException("Cannot find " + c1 + " nor " + c2 + " in " + sigtestJar);
155
} catch (IOException ex) {
156
throw new BuildException(ex);
162
Java java = new Java();
163
java.setProject(getProject());
164
Path sigtestPath = new Path(getProject());
165
sigtestPath.setLocation(sigtestJar);
167
java.setClasspath(sigtestPath);
169
if ("strictcheck".equals(action.getValue())) { // NOI18N
170
a = "SignatureTest"; // NOI18N
172
if ("generate".equals(action.getValue())) { // NOI18N
173
a = "Setup"; // NOI18N
176
throw new BuildException("Unsupported action " + action + " use: strictcheck or generate");
178
java.setClassname("com.sun.tdk.signaturetest." + a);
179
Commandline.Argument arg;
180
arg = java.createArg();
181
arg.setValue("-FileName");
182
arg = java.createArg();
183
arg.setValue(fileName.getAbsolutePath());
184
arg = java.createArg();
185
arg.setValue("-Classpath");
186
arg = java.createArg();
188
Path extracp = new Path(getProject());
189
extracp.add(classpath);
190
FileSet jdk = new FileSet();
191
jdk.setDir(new File(new File(System.getProperty("java.home")), "lib"));
192
jdk.setIncludes("*.jar");
193
extracp.addFileset(jdk);
194
arg.setPath(extracp);
197
File outputFile = null;
198
String s = getProject().getProperty("sigtest.output.dir");
200
File dir = getProject().resolveFile(s);
202
outputFile = new File(dir, fileName.getName().replace(".sig", "").replace("-", "."));
203
log(outputFile.toString());
204
String email = getProject().getProperty("sigtest.mail");
207
FileWriter w = new FileWriter(outputFile);
212
} catch (IOException ex) {
213
throw new BuildException(ex);
217
java.setAppend(true);
218
java.setOutput(outputFile);
223
arg = java.createArg();
224
arg.setLine("-static");
225
log("Packages: " + packages);
226
StringTokenizer packagesTokenizer = new StringTokenizer(packages,",");
227
while (packagesTokenizer.hasMoreTokens()) {
228
String p = packagesTokenizer.nextToken().trim();
229
String prefix = "-PackageWithoutSubpackages "; // NOI18N
230
//Strip the ending ".*"
231
int idx = p.lastIndexOf(".*");
233
p = p.substring(0, idx);
235
idx = p.lastIndexOf(".**");
237
prefix = "-Package "; // NOI18N
238
p = p.substring(0, idx);
242
arg = java.createArg();
243
arg.setLine(prefix + p);
245
int returnCode = java.executeJava();
246
if (returnCode != 95) {
247
if (failOnError && outputFile == null) {
248
throw new BuildException("Signature tests return code is wrong (" + returnCode + "), check the messages above. For more info see http://wiki.netbeans.org/wiki/view/SignatureTest", getLocation());
251
log("Signature tests return code is wrong (" + returnCode + "), check the messages above");
254
if (outputFile != null) {
260
private void apitest() {
261
Java java = new Java();
262
java.setProject(getProject());
263
java.setTaskName(getTaskName());
265
Path sigtestPath = new Path(getProject());
266
sigtestPath.setLocation(sigtestJar);
269
java.setClasspath(sigtestPath);
270
java.setClassname("org.netbeans.apitest.Main");
271
Commandline.Argument arg;
272
arg = java.createArg();
273
arg.setValue("-FileName");
274
arg = java.createArg();
275
arg.setValue(fileName.getAbsolutePath());
276
arg = java.createArg();
277
arg.setValue("-Classpath");
278
arg = java.createArg();
279
arg.setPath(classpath);
280
if (action.getValue().equals("generate")) {
281
arg = java.createArg();
282
arg.setValue("-setup");
283
} else if (action.getValue().equals("binarycheck")) {
284
arg = java.createArg();
285
arg.setValue("-extensibleinterfaces");
286
} else if (action.getValue().equals("check")) {
287
// no special arg for check
288
} else if (action.getValue().equals("strictcheck")) {
289
arg = java.createArg();
290
arg.setValue("-maintenance");
292
throw new BuildException("Unknown action: " + action);
294
if (version != null) {
295
arg = java.createArg();
296
arg.setValue("-Version");
297
arg = java.createArg();
298
arg.setValue(version);
302
arg = java.createJvmarg();
303
arg.setValue("-XX:PermSize=32m");
304
arg = java.createJvmarg();
305
arg.setValue("-XX:MaxPermSize=200m");
307
File outputFile = null;
308
String s = getProject().getProperty("sigtest.output.dir");
310
File dir = getProject().resolveFile(s);
312
outputFile = new File(dir, fileName.getName().replace(".sig", "").replace("-", "."));
313
log(outputFile.toString());
314
java.setOutput(outputFile);
318
log("Packages: " + packages);
319
StringTokenizer packagesTokenizer = new StringTokenizer(packages,",");
320
while (packagesTokenizer.hasMoreTokens()) {
321
String p = packagesTokenizer.nextToken().trim();
322
String prefix = "-PackageWithoutSubpackages "; // NOI18N
323
//Strip the ending ".*"
324
int idx = p.lastIndexOf(".*");
326
p = p.substring(0, idx);
328
idx = p.lastIndexOf(".**");
330
prefix = "-Package "; // NOI18N
331
p = p.substring(0, idx);
335
arg = java.createArg();
336
arg.setLine(prefix + p);
338
int returnCode = java.executeJava();
339
if (returnCode != 0) {
340
if (failOnError && outputFile == null) {
341
throw new BuildException("Signature tests return code is wrong (" + returnCode + "), check the messages above. To disable signature tests, run your built with -Dsigtest.skip.check=true property", getLocation());
344
log("Signature tests return code is wrong (" + returnCode + "), check the messages above");
347
if (outputFile != null) {
353
public static final class ActionType extends EnumeratedAttribute {
354
public String[] getValues () {
355
return new String[] {