001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.ant.extensions;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.util.HashSet;
023: import java.util.Set;
024:
025: import org.apache.cxf.common.util.StringUtils;
026: import org.apache.cxf.tools.wsdlto.WSDLToJava;
027: import org.apache.tools.ant.AntClassLoader;
028: import org.apache.tools.ant.BuildException;
029: import org.apache.tools.ant.DirectoryScanner;
030: import org.apache.tools.ant.Project;
031: import org.apache.tools.ant.taskdefs.Execute;
032: import org.apache.tools.ant.taskdefs.LogStreamHandler;
033: import org.apache.tools.ant.types.FileSet;
034: import org.apache.tools.ant.types.Path;
035:
036: public class WSDL2JavaTask extends CxfAntTask {
037:
038: private String wsdlLocation;
039: private String wsdl;
040: private Set<File> bindingFiles = new HashSet<File>();
041:
042: public void setWsdlLocation(String w) {
043: wsdlLocation = w;
044: }
045:
046: public void setWsdl(String w) {
047: wsdl = w;
048: }
049:
050: public void addConfiguredBinding(FileSet fs) {
051: DirectoryScanner ds = fs.getDirectoryScanner(getProject());
052: String[] includedFiles = ds.getIncludedFiles();
053: File baseDir = ds.getBasedir();
054: for (int i = 0; i < includedFiles.length; ++i) {
055: bindingFiles.add(new File(baseDir, includedFiles[i]));
056: }
057: }
058:
059: public void execute() throws BuildException {
060: buildCommandLine();
061:
062: LogStreamHandler log = new LogStreamHandler(this ,
063: Project.MSG_INFO, Project.MSG_WARN);
064: Execute exe = new Execute(log);
065: exe.setAntRun(getProject());
066: exe.setCommandline(cmd.getCommandline());
067: try {
068: int rc = exe.execute();
069: if (exe.killedProcess() || rc != 0) {
070: throw new BuildException("wsdl2java failed",
071: getLocation());
072: }
073: } catch (IOException e) {
074: throw new BuildException(e, getLocation());
075: }
076: }
077:
078: private void buildCommandLine() {
079: ClassLoader loader = this .getClass().getClassLoader();
080: Path classpath = new Path(getProject());
081: if (loader instanceof AntClassLoader) {
082: classpath = new Path(getProject(),
083: ((AntClassLoader) loader).getClasspath());
084: }
085: cmd.createClasspath(getProject()).append(classpath);
086: cmd.createVmArgument().setLine(
087: "-Djava.util.logging.config.file=");
088:
089: cmd.setClassname(WSDLToJava.class.getName());
090:
091: if (null != classesDir
092: && !StringUtils.isEmpty(classesDir.getName())) {
093: cmd.createArgument().setValue("-classdir");
094: cmd.createArgument().setFile(classesDir);
095: cmd.createArgument().setValue("-compile");
096: }
097: if (null != sourcesDir
098: && !StringUtils.isEmpty(sourcesDir.getName())) {
099: cmd.createArgument().setValue("-d");
100: cmd.createArgument().setFile(sourcesDir);
101: }
102:
103: // verbose option
104: if (verbose) {
105: cmd.createArgument().setValue("-verbose");
106: }
107:
108: if (!bindingFiles.isEmpty()) {
109: for (File b : bindingFiles) {
110: cmd.createArgument().setValue("-b");
111: cmd.createArgument().setFile(b);
112: }
113: }
114:
115: if (!StringUtils.isEmpty(wsdlLocation)) {
116: cmd.createArgument().setValue("-wsdlLocation");
117: cmd.createArgument().setValue(wsdlLocation);
118: }
119:
120: // wsdl
121: if (!StringUtils.isEmpty(wsdl)) {
122: cmd.createArgument().setValue(wsdl);
123: }
124: }
125: }
|