Search in sources :

Example 1 with OutputStreamCreator

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;
}
Also used : GenericWsdlOption(org.apache.cxf.maven_plugin.GenericWsdlOption) LinkedHashSet(java.util.LinkedHashSet) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) WSDLToJava(org.apache.cxf.tools.wsdlto.WSDLToJava) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) ToolContext(org.apache.cxf.tools.common.ToolContext) IOException(java.io.IOException) URI(java.net.URI) Artifact(org.apache.maven.artifact.Artifact) OutputStreamCreator(org.apache.cxf.tools.util.OutputStreamCreator) File(java.io.File)

Example 2 with OutputStreamCreator

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;
}
Also used : OutputStreamCreator(org.apache.cxf.tools.util.OutputStreamCreator)

Example 3 with OutputStreamCreator

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);
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) Element(org.w3c.dom.Element) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) Definition(javax.wsdl.Definition) WSDLWriter(javax.wsdl.xml.WSDLWriter) BadUsageException(org.apache.cxf.tools.common.toolspec.parser.BadUsageException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) XmlSchema(org.apache.ws.commons.schema.XmlSchema) List(java.util.List) ArrayList(java.util.ArrayList) OutputStreamCreator(org.apache.cxf.tools.util.OutputStreamCreator) ToolException(org.apache.cxf.tools.common.ToolException) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Writer(java.io.Writer) WSDLWriter(javax.wsdl.xml.WSDLWriter)

Aggregations

OutputStreamCreator (org.apache.cxf.tools.util.OutputStreamCreator)3 File (java.io.File)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 BufferedOutputStream (java.io.BufferedOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 Writer (java.io.Writer)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 JarOutputStream (java.util.jar.JarOutputStream)1 Definition (javax.wsdl.Definition)1 WSDLWriter (javax.wsdl.xml.WSDLWriter)1 Message (org.apache.cxf.common.i18n.Message)1 SchemaCollection (org.apache.cxf.common.xmlschema.SchemaCollection)1 LoadingByteArrayOutputStream (org.apache.cxf.helpers.LoadingByteArrayOutputStream)1 GenericWsdlOption (org.apache.cxf.maven_plugin.GenericWsdlOption)1