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 com.sun.tools.ws.wscompile.WsgenTool;
039: import org.apache.tools.ant.AntClassLoader;
040: import org.apache.tools.ant.BuildException;
041: import org.apache.tools.ant.Project;
042: import org.apache.tools.ant.taskdefs.Execute;
043: import org.apache.tools.ant.taskdefs.LogOutputStream;
044: import org.apache.tools.ant.taskdefs.LogStreamHandler;
045: import org.apache.tools.ant.taskdefs.MatchingTask;
046: import org.apache.tools.ant.types.Commandline;
047: import org.apache.tools.ant.types.CommandlineJava;
048: import org.apache.tools.ant.types.Path;
049: import org.apache.tools.ant.types.Reference;
050:
051: import java.io.File;
052: import java.io.FileOutputStream;
053: import java.io.IOException;
054: import java.io.PrintWriter;
055: import java.io.StringWriter;
056:
057: /**
058: * wsgen task for use with the JAXWS project.
059: *
060: */
061: public class WsGen2 extends MatchingTask {
062: private CommandlineJava cmd = new CommandlineJava();
063: /************************* -classpath option *************************/
064: protected Path compileClasspath = null;
065:
066: /**
067: * Gets the classpath.
068: */
069: public Path getClasspath() {
070: return compileClasspath;
071: }
072:
073: /**
074: * Set the classpath to be used for this compilation.
075: */
076: public void setClasspath(Path classpath) {
077: if (compileClasspath == null) {
078: compileClasspath = classpath;
079: } else {
080: compileClasspath.append(classpath);
081: }
082: }
083:
084: /**
085: * Creates a nested classpath element.
086: */
087: public Path createClasspath() {
088: if (compileClasspath == null) {
089: compileClasspath = new Path(getProject());
090: }
091: return compileClasspath.createPath();
092: }
093:
094: /**
095: * Adds a reference to a CLASSPATH defined elsewhere.
096: */
097: public void setClasspathRef(Reference r) {
098: createClasspath().setRefid(r);
099: }
100:
101: /************************* -cp option *************************/
102: /**
103: * Gets the classpath.
104: */
105: public Path getCP() {
106: return getClasspath();
107: }
108:
109: /**
110: * Set the classpath to be used for this compilation.
111: */
112: public void setCP(Path classpath) {
113: setClasspath(classpath);
114: }
115:
116: /************************* -d option *************************/
117: private File destDir = null;
118:
119: /** Gets the base directory to output generated class. **/
120: public File getDestdir() {
121: return this .destDir;
122: }
123:
124: /** Sets the base directory to output generated class. **/
125: public void setDestdir(File base) {
126: this .destDir = base;
127: }
128:
129: /********************* failonerror option ***********************/
130: /**
131: * False to continue the build even if the compilation fails.
132: */
133: private boolean failonerror = true;
134:
135: /**
136: * Mostly for our SQE teams and not to be advertized.
137: */
138: public void setFailonerror(boolean value) {
139: failonerror = value;
140: }
141:
142: /**
143: * Adds a JVM argument.
144: *
145: * @return JVM argument created
146: */
147: public Commandline.Argument createJvmarg() {
148: return cmd.createVmArgument();
149: }
150:
151: /******************** -extensions option **********************/
152: protected boolean extension;
153:
154: /** Gets the "extension" flag. **/
155: public boolean getExtension() {
156: return extension;
157: }
158:
159: /** Sets the "extension" flag. **/
160: public void setExtension(boolean extension) {
161: this .extension = extension;
162: }
163:
164: /************************* -keep option *************************/
165: private boolean keep = false;
166:
167: /** Gets the "keep" flag. **/
168: public boolean getKeep() {
169: return keep;
170: }
171:
172: /** Sets the "keep" flag. **/
173: public void setKeep(boolean keep) {
174: this .keep = keep;
175: }
176:
177: /************************* -fork option *************************/
178: private boolean fork = false;
179:
180: /** Gets the "fork" flag. **/
181: public boolean getFork() {
182: return fork;
183: }
184:
185: /** Sets the "fork" flag. **/
186: public void setFork(boolean fork) {
187: this .fork = fork;
188: }
189:
190: /************************* -r option *************************/
191: private File resourceDestDir = null;
192:
193: /** Gets the directory for non-class generated files. **/
194: public File getResourcedestdir() {
195: return this .resourceDestDir;
196: }
197:
198: /** Sets the directory for non-class generated files. **/
199: public void setResourcedestdir(File resourceDir) {
200: this .resourceDestDir = resourceDir;
201: }
202:
203: /************************* -O option *************************/
204: private boolean optimize = false;
205:
206: /** Gets the optimize flag. **/
207: public boolean getOptimize() {
208: return optimize;
209: }
210:
211: /** Sets the optimize flag. **/
212: public void setOptimize(boolean optimize) {
213: this .optimize = optimize;
214: }
215:
216: /************************* -s option *************************/
217: private File sourceDestDir;
218:
219: /** Sets the directory to place generated source java files. **/
220: public void setSourcedestdir(File sourceBase) {
221: keep = true;
222: this .sourceDestDir = sourceBase;
223: }
224:
225: /** Gets the directory to place generated source java files. **/
226: public File getSourcedestdir() {
227: return sourceDestDir;
228: }
229:
230: /************************* -verbose option *************************/
231: protected boolean verbose = false;
232:
233: /** Gets the "verbose" flag. **/
234: public boolean getVerbose() {
235: return verbose;
236: }
237:
238: /** Sets the "verbose" flag. **/
239: public void setVerbose(boolean verbose) {
240: this .verbose = verbose;
241: }
242:
243: /************************* -g option *************************/
244: private boolean debug = false;
245:
246: /** Gets the debug flag. **/
247: public boolean getDebug() {
248: return debug;
249: }
250:
251: /** Sets the debug flag. **/
252: public void setDebug(boolean debug) {
253: this .debug = debug;
254: }
255:
256: /************************* -wsdl option *************************/
257: private boolean genWsdl = false;
258:
259: /** Gets the genWsdl flag. **/
260: public boolean getGenwsdl() {
261: return genWsdl;
262: }
263:
264: /** Sets the genWsdl flag. **/
265: public void setGenwsdl(boolean genWsdl) {
266: this .genWsdl = genWsdl;
267: }
268:
269: /************* protocol option used only with -wsdl option*****************/
270: private String protocol = "";
271:
272: /** Gets the protocol. **/
273: public String getProtocol() {
274: return protocol;
275: }
276:
277: /** Sets the protocol. **/
278: public void setProtocol(String protocol) {
279: this .protocol = protocol;
280: }
281:
282: /************* serviceName option used only with -wsdl option*****************/
283: private String serviceName = null;
284:
285: /** Gets the serviceName. **/
286: public String getServicename() {
287: return serviceName;
288: }
289:
290: /** Sets the serviceName. **/
291: public void setServicename(String name) {
292: this .serviceName = name;
293: }
294:
295: /************* portName option used only with -wsdl option*****************/
296: private String portName = null;
297:
298: /** Gets the portName. **/
299: public String getPortname() {
300: return portName;
301: }
302:
303: /** Sets the serviceName. **/
304: public void setPortname(String name) {
305: this .portName = name;
306: }
307:
308: /*********************** include ant runtime **********************/
309: /** not sure if these methods are needed */
310: private boolean includeAntRuntime = false;
311:
312: /**
313: * Include ant's own classpath in this task's classpath?
314: */
315: public void setIncludeantruntime(boolean include) {
316: includeAntRuntime = include;
317: }
318:
319: /**
320: * Gets whether or not the ant classpath is to be included in the
321: * task's classpath.
322: */
323: public boolean getIncludeantruntime() {
324: return includeAntRuntime;
325: }
326:
327: /*********************** include java runtime **********************/
328: /** not sure if these methods are needed */
329: private boolean includeJavaRuntime = false;
330:
331: /**
332: * Sets whether or not to include the java runtime libraries to this
333: * task's classpath.
334: */
335: public void setIncludejavaruntime(boolean include) {
336: includeJavaRuntime = include;
337: }
338:
339: /**
340: * Gets whether or not the java runtime should be included in this
341: * task's classpath.
342: */
343: public boolean getIncludejavaruntime() {
344: return includeJavaRuntime;
345: }
346:
347: private String sei;
348:
349: /**
350: * @return Returns the sei.
351: */
352: public String getSei() {
353: return sei;
354: }
355:
356: public void setSei(String endpointImplementationClass) {
357: this .sei = endpointImplementationClass;
358: }
359:
360: private void setupWscompileCommand() {
361: Path classpath = getClasspath();
362: if (classpath != null && !classpath.toString().equals("")) {
363: cmd.createArgument().setValue("-classpath");
364: cmd.createArgument().setPath(classpath);
365: }
366: setupWscompileArgs();
367:
368: }
369:
370: private void setupWscompileForkCommand() {
371:
372: ClassLoader loader = this .getClass().getClassLoader();
373: while (loader != null && !(loader instanceof AntClassLoader)) {
374: loader = loader.getParent();
375: }
376:
377: if (loader != null)
378: cmd.createClasspath(getProject()).append(
379: new Path(getProject(), ((AntClassLoader) loader)
380: .getClasspath()));
381:
382: cmd.createClasspath(getProject()).append(getClasspath());
383: cmd.setClassname("com.sun.tools.ws.WsGen");
384: setupWscompileArgs();
385: //cmd.createArgument(true).setLine(forkCmd.toString());
386:
387: }
388:
389: private void setupWscompileArgs() {
390:
391: // d option
392: if (null != getDestdir() && !getDestdir().getName().equals("")) {
393: cmd.createArgument().setValue("-d");
394: cmd.createArgument().setFile(getDestdir());
395: }
396:
397: // g option
398: if (getDebug()) {
399: cmd.createArgument().setValue("-g");
400: }
401:
402: // extension flag
403: if (getExtension()) {
404: cmd.createArgument().setValue("-extension");
405: }
406:
407: // keep option
408: if (getKeep()) {
409: cmd.createArgument().setValue("-keep");
410: }
411:
412: if (getGenwsdl()) {
413: String tmp = "-wsdl";
414: if (protocol.length() > 0)
415: tmp += ":" + protocol;
416: cmd.createArgument().setValue(tmp);
417:
418: if (serviceName != null && serviceName.length() > 0) {
419: cmd.createArgument().setValue("-servicename");
420: cmd.createArgument().setValue(serviceName);
421: }
422:
423: if (portName != null && portName.length() > 0) {
424: cmd.createArgument().setValue("-portname");
425: cmd.createArgument().setValue(portName);
426: }
427: }
428:
429: // r option
430: if (null != getResourcedestdir()
431: && !getResourcedestdir().getName().equals("")) {
432: cmd.createArgument().setValue("-r");
433: cmd.createArgument().setFile(getResourcedestdir());
434: }
435:
436: // optimize option
437: if (getOptimize()) {
438: cmd.createArgument().setValue("-O");
439: }
440:
441: // s option
442: if (null != getSourcedestdir()
443: && !getSourcedestdir().getName().equals("")) {
444: cmd.createArgument().setValue("-s");
445: cmd.createArgument().setFile(getSourcedestdir());
446: }
447:
448: // verbose option
449: if (getVerbose()) {
450: cmd.createArgument().setValue("-verbose");
451: }
452:
453: if (getSei() != null) {
454: cmd.createArgument().setValue(getSei());
455: }
456:
457: }
458:
459: /** Called by the project to let the task do it's work **/
460: public void execute() throws BuildException {
461: /* Create an instance of the rmic, redirecting output to
462: * the project log
463: */
464: LogOutputStream logstr = null;
465: boolean ok = false;
466: try {
467: if (fork) {
468: setupWscompileForkCommand();
469: } else {
470: if (cmd.getVmCommand().size() > 1) {
471: log("JVM args ignored when same JVM is used.",
472: Project.MSG_WARN);
473: }
474: setupWscompileCommand();
475: }
476: if (fork) {
477: if (verbose) { // Fix for CR 6444561
478: log("command line: " + "wsgen " + cmd.toString());
479: }
480: int status = run(cmd.getCommandline());
481: ok = (status == 0);
482: } else {
483: if (verbose) { // Fix for CR 6444561
484: log("command line: " + "wsgen "
485: + cmd.getJavaCommand().toString());
486: }
487: logstr = new LogOutputStream(this , Project.MSG_WARN);
488:
489: ClassLoader old = Thread.currentThread()
490: .getContextClassLoader();
491: Thread.currentThread().setContextClassLoader(
492: this .getClass().getClassLoader());
493: try {
494: WsgenTool compTool = new WsgenTool(logstr);
495: ok = compTool.run(cmd.getJavaCommand()
496: .getArguments());
497: } finally {
498: Thread.currentThread().setContextClassLoader(old);
499: }
500: }
501: if (!ok) {
502: if (!verbose) {
503: log("Command invoked: " + "wsgen " + cmd.toString());
504: }
505: throw new BuildException("wsgen failed", location);
506: }
507: } catch (Exception ex) {
508: if (failonerror) {
509: if (ex instanceof BuildException) {
510: throw (BuildException) ex;
511: } else {
512: throw new BuildException("Error starting wsgen: ",
513: ex, getLocation());
514: }
515: } else {
516: StringWriter sw = new StringWriter();
517: ex.printStackTrace(new PrintWriter(sw));
518: getProject().log(sw.toString(), Project.MSG_WARN);
519: // continue
520: }
521: } finally {
522: try {
523: if (logstr != null) {
524: logstr.close();
525: }
526: } catch (IOException e) {
527: throw new BuildException(e);
528: }
529: }
530: }
531:
532: /**
533: * Executes the given classname with the given arguments in a separate VM.
534: */
535: private int run(String[] command) throws BuildException {
536: FileOutputStream fos = null;
537: Execute exe = null;
538: LogStreamHandler logstr = new LogStreamHandler(this ,
539: Project.MSG_INFO, Project.MSG_WARN);
540: exe = new Execute(logstr);
541: exe.setAntRun(project);
542: exe.setCommandline(command);
543: try {
544: int rc = exe.execute();
545: if (exe.killedProcess()) {
546: log("Timeout: killed the sub-process", Project.MSG_WARN);
547: }
548: return rc;
549: } catch (IOException e) {
550: throw new BuildException(e, location);
551: }
552: }
553: }
|