~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to nbbuild/antsrc/org/netbeans/nbbuild/Sigtest.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.nbbuild;
 
43
 
 
44
import java.io.File;
 
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;
 
57
 
 
58
/** Invokes signature tests.
 
59
 * @author Michal Zlamal
 
60
 */
 
61
public class Sigtest extends Task {
 
62
 
 
63
    File fileName;
 
64
    Path classpath;
 
65
    String packages;
 
66
    ActionType action;
 
67
    File sigtestJar;
 
68
    boolean failOnError = true;
 
69
    String version;
 
70
    
 
71
    public void setFileName(File f) {
 
72
        fileName = f;
 
73
    }
 
74
    
 
75
    public void setPackages(String s) {
 
76
        packages = s;
 
77
    }
 
78
 
 
79
    public void setAction(ActionType s) {
 
80
        action = s;
 
81
    }
 
82
 
 
83
    public void setClasspath(Path p) {
 
84
        if (classpath == null) {
 
85
            classpath = p;
 
86
        } else {
 
87
            classpath.append(p);
 
88
        }
 
89
    }
 
90
    public Path createClasspath () {
 
91
        if (classpath == null) {
 
92
            classpath = new Path(getProject());
 
93
        }
 
94
        return classpath.createPath();
 
95
    }
 
96
    public void setClasspathRef(Reference r) {
 
97
        createClasspath().setRefid(r);
 
98
    }
 
99
    public void setVersion(String v) {
 
100
        version = v;
 
101
    }
 
102
 
 
103
    public void setSigtestJar(File f) {
 
104
        sigtestJar = f;
 
105
    }
 
106
 
 
107
    public void setFailOnError(boolean b) {
 
108
        failOnError = b;
 
109
    }
 
110
 
 
111
    @Override
 
112
    public void execute() throws BuildException {
 
113
        if (fileName == null) {
 
114
            throw new BuildException("FileName has to filed", getLocation());
 
115
        }
 
116
        if (packages == null) {
 
117
            throw new BuildException("Packages has to filed", getLocation());
 
118
        }
 
119
        if (action == null) {
 
120
            throw new BuildException("Action has to filed", getLocation());
 
121
        }
 
122
        if (classpath == null) {
 
123
            throw new BuildException("Classpath has to filed", getLocation());
 
124
        }
 
125
        if (sigtestJar == null) {
 
126
            throw new BuildException("SigtestJar has to filed", getLocation());
 
127
        }
 
128
        
 
129
        if (packages.equals("-")) {
 
130
            log("No public packages, skipping");
 
131
            return;
 
132
        }
 
133
        
 
134
        if (!sigtestJar.exists()) {
 
135
            throw new BuildException("Cannot find JAR with testing infrastructure: " + sigtestJar);
 
136
        }
 
137
        
 
138
        try {
 
139
            ZipFile zip = new ZipFile(sigtestJar);
 
140
            String c1 = "com/sun/tdk/signaturetest/Setup.class";
 
141
            if (zip.getEntry(c1) != null) {
 
142
                zip.close();
 
143
                tdk();
 
144
                return;
 
145
            }
 
146
            String c2 = "org/netbeans/apitest/Main.class";
 
147
            if (zip.getEntry(c2) != null) {
 
148
                zip.close();
 
149
                apitest();
 
150
                return;
 
151
            }
 
152
            zip.close();
 
153
            
 
154
            throw new BuildException("Cannot find " + c1 + " nor " + c2 + " in " + sigtestJar);
 
155
        } catch (IOException ex) {
 
156
            throw new BuildException(ex);
 
157
        }
 
158
        
 
159
    }
 
160
    
 
161
    private void tdk() {
 
162
        Java java = new Java();
 
163
        java.setProject(getProject());
 
164
        Path sigtestPath = new Path(getProject());
 
165
        sigtestPath.setLocation(sigtestJar);
 
166
        
 
167
        java.setClasspath(sigtestPath);
 
168
        String a = null;
 
169
        if ("strictcheck".equals(action.getValue())) { // NOI18N
 
170
            a = "SignatureTest"; // NOI18N
 
171
        }
 
172
        if ("generate".equals(action.getValue())) { // NOI18N
 
173
            a = "Setup"; // NOI18N
 
174
        }
 
175
        if (a == null) {
 
176
            throw new BuildException("Unsupported action " + action + " use: strictcheck or generate");
 
177
        }
 
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();
 
187
        {
 
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);
 
195
        }
 
196
        
 
197
        File outputFile = null;
 
198
        String s = getProject().getProperty("sigtest.output.dir");
 
199
        if (s != null) {
 
200
            File dir = getProject().resolveFile(s);
 
201
            dir.mkdirs();
 
202
            outputFile = new File(dir, fileName.getName().replace(".sig", "").replace("-", "."));
 
203
            log(outputFile.toString());
 
204
            String email = getProject().getProperty("sigtest.mail");
 
205
            if (email != null) {
 
206
                try {
 
207
                    FileWriter w = new FileWriter(outputFile);
 
208
                    w.write("email: ");
 
209
                    w.write(email);
 
210
                    w.write("\n");
 
211
                    w.close();
 
212
                } catch (IOException ex) {
 
213
                    throw new BuildException(ex);
 
214
                }
 
215
            }
 
216
 
 
217
            java.setAppend(true);
 
218
            java.setOutput(outputFile);
 
219
            java.setFork(true);
 
220
        }
 
221
        
 
222
        
 
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(".*");
 
232
            if (idx > 0) {
 
233
                p = p.substring(0, idx);
 
234
            } else {
 
235
                idx = p.lastIndexOf(".**");
 
236
                if (idx > 0) {
 
237
                    prefix = "-Package "; // NOI18N
 
238
                    p = p.substring(0, idx);
 
239
                }
 
240
            }
 
241
            
 
242
            arg = java.createArg();
 
243
            arg.setLine(prefix + p);
 
244
        }
 
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());
 
249
            }
 
250
            else {
 
251
                log("Signature tests return code is wrong (" + returnCode + "), check the messages above");
 
252
            }
 
253
        } else {
 
254
            if (outputFile != null) {
 
255
                outputFile.delete();
 
256
            }
 
257
        }
 
258
    }
 
259
    
 
260
    private void apitest() {
 
261
        Java java = new Java();
 
262
        java.setProject(getProject());
 
263
        java.setTaskName(getTaskName());
 
264
        java.setFork(true);
 
265
        Path sigtestPath = new Path(getProject());
 
266
        sigtestPath.setLocation(sigtestJar);
 
267
        
 
268
        
 
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");
 
291
        } else {
 
292
            throw new BuildException("Unknown action: " + action);
 
293
        }
 
294
        if (version != null) {
 
295
            arg = java.createArg();
 
296
            arg.setValue("-Version");
 
297
            arg = java.createArg();
 
298
            arg.setValue(version);
 
299
        }
 
300
        
 
301
        
 
302
        arg = java.createJvmarg();
 
303
        arg.setValue("-XX:PermSize=32m");
 
304
        arg = java.createJvmarg();
 
305
        arg.setValue("-XX:MaxPermSize=200m");
 
306
            
 
307
        File outputFile = null;
 
308
        String s = getProject().getProperty("sigtest.output.dir");
 
309
        if (s != null) {
 
310
            File dir = getProject().resolveFile(s);
 
311
            dir.mkdirs();
 
312
            outputFile = new File(dir, fileName.getName().replace(".sig", "").replace("-", "."));
 
313
            log(outputFile.toString());
 
314
            java.setOutput(outputFile);
 
315
        }
 
316
        
 
317
        
 
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(".*");
 
325
            if (idx > 0) {
 
326
                p = p.substring(0, idx);
 
327
            } else {
 
328
                idx = p.lastIndexOf(".**");
 
329
                if (idx > 0) {
 
330
                    prefix = "-Package "; // NOI18N
 
331
                    p = p.substring(0, idx);
 
332
                }
 
333
            }
 
334
            
 
335
            arg = java.createArg();
 
336
            arg.setLine(prefix + p);
 
337
        }
 
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());
 
342
            }
 
343
            else {
 
344
                log("Signature tests return code is wrong (" + returnCode + "), check the messages above");
 
345
            }
 
346
        } else {
 
347
            if (outputFile != null) {
 
348
                outputFile.delete();
 
349
            }
 
350
        }
 
351
    }
 
352
 
 
353
    public static final class ActionType extends EnumeratedAttribute {
 
354
        public String[] getValues () {
 
355
            return new String[] { 
 
356
                "generate",
 
357
                "check",
 
358
                "strictcheck",
 
359
                "binarycheck",
 
360
            };
 
361
        }
 
362
    }
 
363
 
 
364
}