use of org.apache.cxf.tools.wsdlto.WSDLToJava in project cxf by apache.
the class WSDL2JavaMojo method generate.
@Override
protected Bus generate(GenericWsdlOption genericWsdlOption, Bus bus, Set<URI> classPath) throws MojoExecutionException {
WsdlOption wsdlOption = (WsdlOption) genericWsdlOption;
File outputDirFile = wsdlOption.getOutputDir();
outputDirFile.mkdirs();
URI basedir = project.getBasedir().toURI();
URI wsdlURI = wsdlOption.getWsdlURI(basedir);
File doneFile = getDoneFile(basedir, wsdlURI, "java");
if (!shouldRun(wsdlOption, doneFile, wsdlURI)) {
return bus;
}
doneFile.delete();
try {
File file = new File(getBaseFileURI(wsdlURI));
if (file.exists()) {
buildContext.removeMessages(file);
}
} catch (Throwable t) {
// ignore
}
if (wsdlOption.getDependencies() != null) {
for (URI dependency : wsdlOption.getDependencyURIs(project.getBasedir().toURI())) {
URI baseDependency = getBaseFileURI(dependency);
if ("file".equals(baseDependency.getScheme())) {
buildContext.removeMessages(new File(baseDependency));
}
}
}
List<String> list = wsdlOption.generateCommandLine(outputDirFile, basedir, wsdlURI, getLog().isDebugEnabled());
if (encoding != null) {
list.add(0, "-encoding");
list.add(1, encoding);
}
String[] args = list.toArray(new String[0]);
getLog().debug("Calling wsdl2java with args: " + Arrays.toString(args));
if (!"false".equals(fork)) {
Set<URI> artifactsPath = new LinkedHashSet<>();
for (Artifact a : pluginArtifacts) {
File file = a.getFile();
if (file == null) {
throw new MojoExecutionException("Unable to find (null) file for artifact " + a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion());
}
artifactsPath.add(file.toURI());
}
addPluginArtifact(artifactsPath);
artifactsPath.addAll(classPath);
runForked(artifactsPath, WSDLToJava.class.getName(), args);
} else {
if (bus == null) {
bus = BusFactory.newInstance().createBus();
BusFactory.setThreadDefaultBus(bus);
}
try {
ToolContext ctx = new ToolContext();
final List<File> files = new ArrayList<>();
final List<File> errorfiles = new ArrayList<>();
ctx.put(OutputStreamCreator.class, new OutputStreamCreator() {
public OutputStream createOutputStream(File file) throws IOException {
files.add(file);
return buildContext.newFileOutputStream(file);
}
});
ctx.setErrorListener(new MavenToolErrorListener(errorfiles));
new WSDLToJava(args).run(ctx);
List<File> oldFiles = CastUtils.cast((List<?>) buildContext.getValue("cxf.file.list." + doneFile.getName()));
if (oldFiles != null) {
for (File f : oldFiles) {
if (!files.contains(f)) {
f.delete();
buildContext.refresh(f);
}
}
}
buildContext.setValue("cxf.file.list." + doneFile.getName(), files);
} catch (Throwable e) {
buildContext.setValue("cxf.file.list." + doneFile.getName(), null);
getLog().debug(e);
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new MojoExecutionException(e.getMessage(), e);
}
}
try {
createMarkerFile(wsdlOption, doneFile, wsdlURI);
buildContext.refresh(doneFile);
} catch (Throwable e) {
getLog().warn("Could not create marker file " + doneFile.getAbsolutePath());
getLog().debug(e);
throw new MojoExecutionException("Failed to create marker file " + doneFile.getAbsolutePath());
}
if (project != null && getGeneratedSourceRoot() != null && getGeneratedSourceRoot().exists()) {
project.addCompileSourceRoot(getGeneratedSourceRoot().getAbsolutePath());
buildContext.refresh(getGeneratedSourceRoot().getAbsoluteFile());
}
if (project != null && getGeneratedTestRoot() != null && getGeneratedTestRoot().exists()) {
project.addTestCompileSourceRoot(getGeneratedTestRoot().getAbsolutePath());
buildContext.refresh(getGeneratedTestRoot().getAbsoluteFile());
}
return bus;
}
use of org.apache.cxf.tools.wsdlto.WSDLToJava in project cxf by apache.
the class CodeGenBugTest method testExcludeNSWithoutPackageName.
@Test
public void testExcludeNSWithoutPackageName() throws Exception {
String[] args = new String[] { "-d", output.getCanonicalPath(), "-nexclude", "http://apache.org/test/types", getLocation("/wsdl2java_wsdl/hello_world_exclude.wsdl") };
CommandInterfaceUtils.commandCommonMain();
WSDLToJava w2j = new WSDLToJava(args);
w2j.run(new ToolContext());
assertNotNull(output);
File com = new File(output, "test");
assertFalse("Generated file has been excluded", com.exists());
}
use of org.apache.cxf.tools.wsdlto.WSDLToJava in project cxf by apache.
the class CodeGenBugTest method testMultiXjcArgs.
@Test
public void testMultiXjcArgs() throws Exception {
String[] args = new String[] { "-d", output.getCanonicalPath(), "-xjc-Xlocator", "-xjc-Xsync-methods", getLocation("/wsdl2java_wsdl/hello_world.wsdl") };
CommandInterfaceUtils.commandCommonMain();
WSDLToJava w2j = new WSDLToJava(args);
w2j.run(new ToolContext());
File file = new File(output, "org/apache/cxf/w2j/hello_world_soap_http/types/SayHi.java");
assertTrue(file.exists());
String str = TestFileUtils.getStringFromFile(file);
assertTrue(str.contains("@XmlLocation"));
assertTrue(str.contains("synchronized"));
}
use of org.apache.cxf.tools.wsdlto.WSDLToJava in project cxf by apache.
the class CodeGenBugTest method testBug305924ForExternalBinding.
@Test
public void testBug305924ForExternalBinding() {
try {
String[] args = new String[] { "-all", "-compile", "-classdir", output.getCanonicalPath() + "/classes", "-d", output.getCanonicalPath(), "-b", getLocation("/wsdl2java_wsdl/bug305924/binding1.xml"), getLocation("/wsdl2java_wsdl/bug305924/hello_world.wsdl") };
CommandInterfaceUtils.commandCommonMain();
WSDLToJava w2j = new WSDLToJava(args);
w2j.run(new ToolContext());
} catch (Exception e) {
e.printStackTrace(System.err);
}
try {
Class<?> clz = classLoader.loadClass("org.apache.cxf.w2j.hello_world_soap_http.types.CreateProcess$MyProcess");
assertNotNull("Customization binding code should be generated", clz);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
use of org.apache.cxf.tools.wsdlto.WSDLToJava in project cxf by apache.
the class CodeGenBugTest method testBug305924ForNestedBinding.
@Test
public void testBug305924ForNestedBinding() {
try {
String[] args = new String[] { "-all", "-compile", "-classdir", output.getCanonicalPath() + "/classes", "-d", output.getCanonicalPath(), "-b", getLocation("/wsdl2java_wsdl/bug305924/binding2.xml"), getLocation("/wsdl2java_wsdl/bug305924/hello_world.wsdl") };
CommandInterfaceUtils.commandCommonMain();
WSDLToJava w2j = new WSDLToJava(args);
w2j.run(new ToolContext());
} catch (Exception e) {
}
try {
Class<?> clz = classLoader.loadClass("org.apache.cxf.w2j.hello_world_soap_http.types.CreateProcess$MyProcess");
assertNotNull("Customization binding code should be generated", clz);
} catch (ClassNotFoundException e) {
fail("Can not load the inner class MyProcess, the customization failed: \n" + e.getMessage());
}
}
Aggregations