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 Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.tools.ws.ant;
037:
038: import org.apache.tools.ant.BuildException;
039: import org.apache.tools.ant.DirectoryScanner;
040: import org.apache.tools.ant.Project;
041: import org.apache.tools.ant.Task;
042: import org.apache.tools.ant.taskdefs.Execute;
043: import org.apache.tools.ant.taskdefs.LogStreamHandler;
044: import org.apache.tools.ant.types.*;
045:
046: import java.io.*;
047: import java.util.ArrayList;
048: import java.util.HashSet;
049: import java.util.List;
050: import java.util.Set;
051:
052: /**
053: * apt task for use with the JAXWS project.
054: */
055: public class Apt extends Task {
056:
057: /** -classpath option */
058: protected Path compileClasspath = null;
059:
060: public Path getClasspath() {
061: return compileClasspath;
062: }
063:
064: public void setClasspath(Path classpath) {
065: if (compileClasspath == null) {
066: compileClasspath = classpath;
067: } else {
068: compileClasspath.append(classpath);
069: }
070: }
071:
072: /**
073: * Creates a nested classpath element.
074: */
075: public Path createClasspath() {
076: if (compileClasspath == null) {
077: compileClasspath = new Path(project);
078: }
079: return compileClasspath.createPath();
080: }
081:
082: /**
083: * Adds a reference to a CLASSPATH defined elsewhere.
084: */
085: public void setClasspathRef(Reference r) {
086: createClasspath().setRefid(r);
087: }
088:
089: /** -d option: directory to output processor and javac generated class files */
090: private File destDir = null;
091:
092: public File getDestdir() {
093: return this .destDir;
094: }
095:
096: public void setDestdir(File base) {
097: this .destDir = base;
098: }
099:
100: /** -s option: directory to place processor generated source files */
101: private File sourceDestDir;
102:
103: public void setSourcedestdir(File sourceBase) {
104: this .sourceDestDir = sourceBase;
105: }
106:
107: public File getSourcedestdir() {
108: return sourceDestDir;
109: }
110:
111: /** -A option */
112: protected List<Option> options = new ArrayList<Option>();
113:
114: public List<Option> getOptions() {
115: return options;
116: }
117:
118: public Option createOption() {
119: Option option = new Option();
120: options.add(option);
121: return option;
122: }
123:
124: /** -J<flag> option: Pass <flag> directly to the runtime */
125: protected List<Jvmarg> jvmargs = new ArrayList<Jvmarg>();
126:
127: public List<Jvmarg> getJvmargs() {
128: return jvmargs;
129: }
130:
131: public Jvmarg createJvmarg() {
132: Jvmarg jvmarg = new Jvmarg();
133: jvmargs.add(jvmarg);
134: return jvmarg;
135: }
136:
137: /** -nocompile option */
138: private boolean noCompile = false;
139:
140: public boolean isNocompile() {
141: return noCompile;
142: }
143:
144: public void setNocompile(boolean noCompile) {
145: this .noCompile = noCompile;
146: }
147:
148: /******************** -print option **********************/
149: private boolean print = false;
150:
151: public boolean isPrint() {
152: return print;
153: }
154:
155: public void setPrint(boolean print) {
156: this .print = print;
157: }
158:
159: /******************** -factorypath option **********************/
160: private File factoryPath = null;
161:
162: public File getFactorypath() {
163: return factoryPath;
164: }
165:
166: public void setFactorypath(File factoryPath) {
167: this .factoryPath = factoryPath;
168: }
169:
170: /******************** -factory option **********************/
171: private String factory = null;
172:
173: public String getFactory() {
174: return factory;
175: }
176:
177: public void setFactory(String factory) {
178: this .factory = factory;
179: }
180:
181: /******************** -XListAnnotationTypes option **********************/
182: private boolean xListAnnotationTypes = false;
183:
184: public boolean isXlistannotationtypes() {
185: return xListAnnotationTypes;
186: }
187:
188: public void setXlistannotationtypes(boolean xListAnnotationTypes) {
189: this .xListAnnotationTypes = xListAnnotationTypes;
190: }
191:
192: /******************** -XListDeclarations option **********************/
193: private boolean xListDeclarations = false;
194:
195: public boolean isXlistdeclarations() {
196: return xListDeclarations;
197: }
198:
199: public void setXlistdeclarations(boolean xListDeclarations) {
200: this .xListDeclarations = xListDeclarations;
201: }
202:
203: /******************** -XPrintAptRounds option **********************/
204: private boolean xPrintAptRounds = false;
205:
206: public boolean isXprintaptrounds() {
207: return xPrintAptRounds;
208: }
209:
210: public void setXprintaptrounds(boolean xPrintAptRounds) {
211: this .xPrintAptRounds = xPrintAptRounds;
212: }
213:
214: /******************** -XPrintFactoryInfo option **********************/
215: private boolean xPrintFactoryInfo = false;
216:
217: public boolean isXprintfactoryinfo() {
218: return xPrintFactoryInfo;
219: }
220:
221: public void setXprintfactoryinfo(boolean xPrintFactoryInfo) {
222: this .xPrintFactoryInfo = xPrintFactoryInfo;
223: }
224:
225: /******************** -XclassesAsDecls option **********************/
226: private boolean xClassesAsDecls = false;
227:
228: public boolean isXclassesasdecls() {
229: return xClassesAsDecls;
230: }
231:
232: public void setXclassesasdecls(boolean xClassesAsDecls) {
233: this .xClassesAsDecls = xClassesAsDecls;
234: }
235:
236: /** Inherited from javac */
237:
238: /** -g option: debugging info */
239: protected boolean debug = false;
240:
241: public boolean isDebug() {
242: return debug;
243: }
244:
245: public void setDebug(boolean debug) {
246: this .debug = debug;
247: }
248:
249: /** debug level */
250: protected String debugLevel = null;
251:
252: public String getDebuglevel() {
253: return debugLevel;
254: }
255:
256: public void setDebuglevel(String debugLevel) {
257: this .debugLevel = debugLevel;
258: }
259:
260: /** -nowarn option: generate no warnings */
261: protected boolean nowarn = false;
262:
263: public boolean isNowarn() {
264: return nowarn;
265: }
266:
267: public void setNowarn(boolean nowarn) {
268: this .nowarn = nowarn;
269: }
270:
271: /** -deprecation option: output source locations where deprecated APIs are used */
272: protected boolean deprecation = false;
273:
274: public boolean isDeprecation() {
275: return deprecation;
276: }
277:
278: public void setDeprecation(boolean deprecation) {
279: this .deprecation = deprecation;
280: }
281:
282: /** -bootclasspath option: override location of bootstrap class files */
283: protected Path bootclassPath = null;
284:
285: public Path getBootclasspath() {
286: return bootclassPath;
287: }
288:
289: public void setBootclasspath(Path bootclassPath) {
290: this .bootclassPath = bootclassPath;
291: }
292:
293: /** -extdirs option: override location of installed extensions */
294: protected String extdirs = null;
295:
296: public String getExtdirs() {
297: return extdirs;
298: }
299:
300: public void setExtdirs(String extdirs) {
301: this .extdirs = extdirs;
302: }
303:
304: /** -endorseddirs option: override location of endorsed standards path */
305: protected String endorseddirs = null;
306:
307: public String getEndorseddirs() {
308: return endorseddirs;
309: }
310:
311: public void setEndorseddirs(String endorseddirs) {
312: this .endorseddirs = endorseddirs;
313: }
314:
315: /** -verbose option: output messages about what the compiler is doing */
316: protected boolean verbose = false;
317:
318: public boolean isVerbose() {
319: return verbose;
320: }
321:
322: public void setVerbose(boolean verbose) {
323: this .verbose = verbose;
324: }
325:
326: /** -sourcepath option: Specify where to find input source files */
327: protected Path sourcePath = null;
328:
329: public Path getSourcepath() {
330: return sourcePath;
331: }
332:
333: public void setSourcepath(Path sourcePath) {
334: this .sourcePath = sourcePath;
335: }
336:
337: /** -encoding option: character encoding used by the source files */
338: protected String encoding = null;
339:
340: public String getEncoding() {
341: return encoding;
342: }
343:
344: public void setEncoding(String encoding) {
345: this .encoding = encoding;
346: }
347:
348: /** -target option: generate class files for specific VM version */
349: protected String targetVM = null;
350:
351: public String getTarget() {
352: return targetVM;
353: }
354:
355: public void setTarget(String target) {
356: this .targetVM = target;
357: }
358:
359: /** Others */
360:
361: /** -fork option: */
362: protected boolean fork = false;
363:
364: public boolean isFork() {
365: return fork;
366: }
367:
368: public void setFork(boolean fork) {
369: this .fork = fork;
370: }
371:
372: protected List<FileSet> sourceFileset = new ArrayList<FileSet>();
373:
374: public void addConfiguredSource(FileSet fileset) {
375: sourceFileset.add(fileset);
376: }
377:
378: private Commandline setupAptCommand() {
379: Commandline cmd = setupAptArgs();
380:
381: // classpath option (cp option just uses classpath option)
382: Path classpath = getClasspath();
383:
384: if (classpath != null && !classpath.toString().equals("")) {
385: cmd.createArgument().setValue("-classpath");
386: cmd.createArgument().setPath(classpath);
387: }
388: return cmd;
389: }
390:
391: private Commandline setupAptForkCommand() {
392: CommandlineJava forkCmd = new CommandlineJava();
393:
394: Path classpath = getClasspath();
395: forkCmd.createClasspath(getProject()).append(classpath);
396: forkCmd.setClassname("com.sun.tools.apt.Main");
397: if (null != getJvmargs()) {
398: for (Jvmarg jvmarg : jvmargs) {
399: forkCmd.createVmArgument().setLine(jvmarg.getValue());
400: }
401: }
402:
403: Commandline cmd = setupAptArgs();
404: cmd.createArgument(true).setLine(forkCmd.toString());
405: return cmd;
406: }
407:
408: private Commandline setupAptArgs() {
409: Commandline cmd = new Commandline();
410:
411: if (null != getDestdir() && !getDestdir().getName().equals("")) {
412: cmd.createArgument().setValue("-d");
413: cmd.createArgument().setFile(getDestdir());
414: }
415:
416: if (null != getSourcedestdir()
417: && !getSourcedestdir().getName().equals("")) {
418: cmd.createArgument().setValue("-s");
419: cmd.createArgument().setFile(getSourcedestdir());
420: }
421:
422: if (getSourcepath() == null)
423: throw new BuildException(
424: "\"sourcePath\" attribute must be set.");
425:
426: if (getSourcepath() != null
427: && !getSourcepath().toString().equals("")) {
428: cmd.createArgument().setValue("-sourcepath");
429: cmd.createArgument().setValue(getSourcepath().toString());
430: }
431:
432: if (getBootclasspath() != null
433: && !getBootclasspath().toString().equals("")) {
434: cmd.createArgument().setValue("-bootclasspath");
435: cmd.createArgument()
436: .setValue(getBootclasspath().toString());
437: }
438:
439: if (getExtdirs() != null && !getExtdirs().equals("")) {
440: cmd.createArgument().setValue("-extdirs");
441: cmd.createArgument().setValue(getExtdirs());
442: }
443:
444: if (getEndorseddirs() != null && !getEndorseddirs().equals("")) {
445: cmd.createArgument().setValue("-endorseddirs");
446: cmd.createArgument().setValue(getEndorseddirs());
447: }
448:
449: if (isDebug()) {
450: String debugOption = "";
451: debugOption = "-g";
452: if (getDebuglevel() != null && !getDebuglevel().equals(""))
453: debugOption += ":" + getDebuglevel();
454: cmd.createArgument().setValue(debugOption);
455: } else
456: cmd.createArgument().setValue("-g:none");
457:
458: if (isVerbose())
459: cmd.createArgument().setValue("-verbose");
460:
461: if (getEncoding() != null && !getEncoding().equals("")) {
462: cmd.createArgument().setValue("-encoding");
463: cmd.createArgument().setValue(getEncoding());
464: }
465:
466: if (getTarget() != null && !getTarget().equals("")) {
467: cmd.createArgument().setValue("-target");
468: cmd.createArgument().setValue(getTarget());
469: }
470:
471: for (Jvmarg jvmarg : jvmargs) {
472: cmd.createArgument().setValue("-J" + jvmarg.getValue());
473: }
474:
475: for (Option option : options) {
476: cmd.createArgument().setValue(
477: "-A" + option.getKey() + "=" + option.getValue());
478: }
479:
480: if (isNowarn()) {
481: cmd.createArgument().setValue("-nowarn");
482: }
483:
484: if (isNocompile()) {
485: cmd.createArgument().setValue("-nocompile");
486: }
487:
488: if (isDeprecation()) {
489: cmd.createArgument().setValue("-deprecation");
490: }
491:
492: if (isPrint()) {
493: cmd.createArgument().setValue("-print");
494: }
495:
496: if (getFactorypath() != null) {
497: cmd.createArgument().setValue("-factorypath");
498: cmd.createArgument().setValue(getFactorypath().toString());
499: }
500:
501: if (getFactory() != null) {
502: cmd.createArgument().setValue("-factory");
503: cmd.createArgument().setValue(getFactory());
504: }
505:
506: if (isXlistannotationtypes()) {
507: cmd.createArgument().setValue("-XListAnnotationTypes");
508: }
509:
510: if (isXlistdeclarations()) {
511: cmd.createArgument().setValue("-XListDeclarations");
512: }
513:
514: if (isXprintaptrounds()) {
515: cmd.createArgument().setValue("-XPrintAptRounds");
516: }
517:
518: if (isXprintfactoryinfo()) {
519: cmd.createArgument().setValue("-XPrintFactoryInfo");
520: }
521:
522: if (isXprintfactoryinfo()) {
523: cmd.createArgument().setValue("-XclassesAsDecls");
524: }
525:
526: Set<File> sourceFiles = new HashSet<File>();
527: prepareSourceList(sourceFiles);
528:
529: if (!sourceFiles.isEmpty()) {
530: for (File source : sourceFiles) {
531: cmd.createArgument().setFile(source);
532: }
533: }
534:
535: return cmd;
536: }
537:
538: void prepareSourceList(Set<File> sourceFiles) throws BuildException {
539: if (sourceFileset != null) {
540: for (FileSet fileset : sourceFileset) {
541: DirectoryScanner ds = fileset
542: .getDirectoryScanner(getProject());
543: String[] includedFiles = ds.getIncludedFiles();
544: File baseDir = ds.getBasedir();
545: for (int i = 0; i < includedFiles.length; ++i) {
546: sourceFiles
547: .add(new File(baseDir, includedFiles[i]));
548: }
549: }
550: }
551: }
552:
553: /** Called by the project to let the task do it's work **/
554: public void execute() throws BuildException {
555:
556: PrintWriter writer = null;
557: boolean ok = false;
558: try {
559: Commandline cmd = fork ? setupAptForkCommand()
560: : setupAptCommand();
561:
562: if (verbose) {
563: log("command line: " + "apt " + cmd.toString());
564: }
565: int status = 0;
566: if (fork) {
567: status = run(cmd.getCommandline());
568: } else {
569: ByteArrayOutputStream baos = new ByteArrayOutputStream();
570: writer = new PrintWriter(baos);
571:
572: ClassLoader old = Thread.currentThread()
573: .getContextClassLoader();
574: Thread.currentThread().setContextClassLoader(
575: this .getClass().getClassLoader());
576: try {
577: com.sun.tools.apt.Main aptTool = new com.sun.tools.apt.Main();
578: status = aptTool
579: .process(writer, cmd.getArguments());
580: writer.flush();
581: if (verbose || baos.size() != 0)
582: log(baos.toString());
583: } finally {
584: Thread.currentThread().setContextClassLoader(old);
585: }
586: }
587: ok = (status == 0) ? true : false;
588: if (!ok) {
589: if (!verbose) {
590: log("Command invoked: " + "apt " + cmd.toString());
591: }
592: throw new BuildException("apt failed", location);
593: }
594: } catch (Exception ex) {
595: if (ex instanceof BuildException) {
596: throw (BuildException) ex;
597: } else {
598: throw new BuildException("Error starting apt: ", ex,
599: getLocation());
600: }
601: } finally {
602: if (writer != null) {
603: writer.close();
604: }
605: }
606: }
607:
608: /**
609: * Executes the given classname with the given arguments in a separate VM.
610: */
611: private int run(String[] command) throws BuildException {
612: FileOutputStream fos = null;
613: Execute exe = null;
614: LogStreamHandler logstr = new LogStreamHandler(this ,
615: Project.MSG_INFO, Project.MSG_WARN);
616: exe = new Execute(logstr);
617: exe.setAntRun(project);
618: exe.setCommandline(command);
619: try {
620: int rc = exe.execute();
621: if (exe.killedProcess()) {
622: log("Timeout: killed the sub-process", Project.MSG_WARN);
623: }
624: return rc;
625: } catch (IOException e) {
626: throw new BuildException(e, location);
627: }
628: }
629:
630: public static class Option {
631: protected String key;
632: protected String value;
633:
634: public String getKey() {
635: return key;
636: }
637:
638: public void setKey(String key) {
639: this .key = key;
640: }
641:
642: public String getValue() {
643: return value;
644: }
645:
646: public void setValue(String value) {
647: this .value = value;
648: }
649: }
650:
651: public static class Jvmarg {
652: protected String value;
653:
654: public String getValue() {
655: return value;
656: }
657:
658: public void setValue(String value) {
659: this.value = value;
660: }
661: }
662: }
|