use of org.apache.cxf.tools.util.OutputStreamCreator 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.util.OutputStreamCreator in project cxf by apache.
the class AbstractGenerator method getOutputStreamCreator.
protected OutputStreamCreator getOutputStreamCreator() {
OutputStreamCreator osc = context.get(OutputStreamCreator.class);
if (osc == null) {
osc = new OutputStreamCreator();
context.put(OutputStreamCreator.class, osc);
}
return osc;
}
use of org.apache.cxf.tools.util.OutputStreamCreator in project cxf by apache.
the class WSDLToJavaContainer method generateLocalWSDL.
@SuppressWarnings("unchecked")
private void generateLocalWSDL(ToolContext context) {
String outputdir = (String) context.get(ToolConstants.CFG_CLASSDIR);
File wsdlFile = new File(outputdir, (String) context.get(ToolConstants.CFG_WSDLLOCATION));
Definition def = context.get(Definition.class);
try {
// get imported schemas
int xsdCount = 0;
SchemaCollection schemas = (SchemaCollection) context.get(ToolConstants.XML_SCHEMA_COLLECTION);
Map<String, String> sourceMap = new HashMap<>();
for (XmlSchema imp : schemas.getXmlSchemas()) {
if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
String schemaFileName = "schema" + (++xsdCount) + ".xsd";
File sourceFile = new File(imp.getSourceURI());
sourceMap.put(createSchemaFileKey(imp.getTargetNamespace(), sourceFile.getName()), schemaFileName);
}
}
// get imported wsdls
int wsdlImportCount = 0;
List<Definition> defs = (List<Definition>) context.get(ToolConstants.IMPORTED_DEFINITION);
Map<String, String> importWSDLMap = new HashMap<>();
for (Definition importDef : defs) {
File importedWsdlFile;
if (!StringUtils.isEmpty(importDef.getDocumentBaseURI())) {
importedWsdlFile = new File(importDef.getDocumentBaseURI());
} else {
importedWsdlFile = new File(importDef.getQName().getLocalPart() + ".wsdl");
}
if (!FileUtils.isValidFileName(importedWsdlFile.getName())) {
importedWsdlFile = new File("import" + (++wsdlImportCount) + ".wsdl");
}
importWSDLMap.put(importDef.getTargetNamespace(), importedWsdlFile.getName());
}
final OutputStreamCreator outputStreamCreator;
if (context.get(OutputStreamCreator.class) != null) {
outputStreamCreator = context.get(OutputStreamCreator.class);
} else {
outputStreamCreator = new OutputStreamCreator();
context.put(OutputStreamCreator.class, outputStreamCreator);
}
for (XmlSchema imp : schemas.getXmlSchemas()) {
if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
File sourceFile = new File(imp.getSourceURI());
String schemaKey = createSchemaFileKey(imp.getTargetNamespace(), sourceFile.getName());
String schemaFileName = sourceMap.get(schemaKey);
File impfile = new File(wsdlFile.getParentFile(), schemaFileName);
Element el = imp.getSchemaDocument().getDocumentElement();
updateImports(el, sourceMap);
updateIncludes(el, sourceMap);
try (Writer os = new FileWriterUtil(impfile.getParent(), outputStreamCreator).getWriter(impfile, StandardCharsets.UTF_8.name())) {
StaxUtils.writeTo(el, os, 2);
}
}
}
WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
// change the import location in wsdl file
try (OutputStream wsdloutput = new BufferedOutputStream(Files.newOutputStream(wsdlFile.toPath()))) {
LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
wsdlWriter.writeWSDL(def, bout);
Element defEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
List<Element> xsdElements = DOMUtils.findAllElementsByTagNameNS(defEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
for (Element xsdEle : xsdElements) {
updateImports(xsdEle, sourceMap);
updateIncludes(xsdEle, sourceMap);
}
updateWSDLImports(defEle, importWSDLMap);
StaxUtils.writeTo(defEle, wsdloutput);
}
for (Definition importDef : defs) {
File importWsdlFile = new File(outputdir, importWSDLMap.get(importDef.getTargetNamespace()));
try (OutputStream wsdlOs = new BufferedOutputStream(Files.newOutputStream(importWsdlFile.toPath()))) {
LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
wsdlWriter.writeWSDL(importDef, bout);
Element importEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
List<Element> xsdElements = DOMUtils.findAllElementsByTagNameNS(importEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
for (Element xsdEle : xsdElements) {
updateImports(xsdEle, sourceMap);
updateIncludes(xsdEle, sourceMap);
}
updateWSDLImports(importEle, importWSDLMap);
StaxUtils.writeTo(importEle, wsdlOs);
}
}
} catch (Exception ex) {
LOG.log(Level.SEVERE, "FAILED_TO_GEN_LOCAL_WSDL", ex);
Message msg = new Message("FAILED_TO_GEN_LOCAL_WSDL", LOG);
throw new ToolException(msg, ex);
}
}
Aggregations