Search in sources :

Example 86 with ToolException

use of org.apache.cxf.tools.common.ToolException in project cxf by apache.

the class WSDLToJavaContainer method createClientJar.

private void createClientJar(File tmpDirectory, JarOutputStream jarout) {
    try {
        URI parentFile = new File((String) context.get(ToolConstants.CFG_CLASSDIR)).toURI();
        File[] files = tmpDirectory.listFiles();
        if (files != null) {
            for (File file : files) {
                URI relativePath = parentFile.relativize(file.toURI());
                String name = relativePath.toString();
                if (file.isDirectory()) {
                    if (!StringUtils.isEmpty(name)) {
                        if (!name.endsWith("/")) {
                            name += "/";
                        }
                        JarEntry entry = new JarEntry(name);
                        entry.setTime(file.lastModified());
                        jarout.putNextEntry(entry);
                        jarout.closeEntry();
                    }
                    createClientJar(file, jarout);
                    continue;
                }
                JarEntry entry = new JarEntry(name);
                entry.setTime(file.lastModified());
                jarout.putNextEntry(entry);
                InputStream input = new BufferedInputStream(Files.newInputStream(file.toPath()));
                IOUtils.copy(input, jarout);
                input.close();
                jarout.closeEntry();
            }
        }
    } catch (Exception e) {
        Message msg = new Message("FAILED_ADD_JARENTRY", LOG);
        throw new ToolException(msg, e);
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ToolException(org.apache.cxf.tools.common.ToolException) JarEntry(java.util.jar.JarEntry) URI(java.net.URI) File(java.io.File) BadUsageException(org.apache.cxf.tools.common.toolspec.parser.BadUsageException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException)

Example 87 with ToolException

use of org.apache.cxf.tools.common.ToolException 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";
                sourceMap.put(imp.getTargetNamespace(), 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 = null;
            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());
        }
        OutputStreamCreator outputStreamCreator = null;
        if (context.get(OutputStreamCreator.class) != null) {
            outputStreamCreator = context.get(OutputStreamCreator.class);
        } else {
            outputStreamCreator = new OutputStreamCreator();
            context.put(OutputStreamCreator.class, outputStreamCreator);
        }
        Writer os = null;
        for (XmlSchema imp : schemas.getXmlSchemas()) {
            if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
                String schemaFileName = sourceMap.get(imp.getTargetNamespace());
                File impfile = new File(wsdlFile.getParentFile(), schemaFileName);
                Element el = imp.getSchemaDocument().getDocumentElement();
                updateImports(el, sourceMap);
                os = new FileWriterUtil(impfile.getParent(), context.get(OutputStreamCreator.class)).getWriter(impfile, StandardCharsets.UTF_8.name());
                StaxUtils.writeTo(el, os, 2);
                os.close();
            }
        }
        // change the import location in wsdl file
        OutputStream wsdloutput = new BufferedOutputStream(Files.newOutputStream(wsdlFile.toPath()));
        WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
        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);
        }
        updateWSDLImports(defEle, importWSDLMap);
        StaxUtils.writeTo(defEle, wsdloutput);
        wsdloutput.close();
        for (Definition importDef : defs) {
            File importWsdlFile = new File(outputdir, importWSDLMap.get(importDef.getTargetNamespace()));
            OutputStream wsdlOs = new BufferedOutputStream(Files.newOutputStream(importWsdlFile.toPath()));
            bout = new LoadingByteArrayOutputStream();
            wsdlWriter.writeWSDL(importDef, bout);
            Element importEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
            xsdElements = DOMUtils.findAllElementsByTagNameNS(importEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
            for (Element xsdEle : xsdElements) {
                updateImports(xsdEle, sourceMap);
            }
            updateWSDLImports(importEle, importWSDLMap);
            StaxUtils.writeTo(importEle, wsdlOs);
            wsdlOs.close();
        }
    } 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)

Example 88 with ToolException

use of org.apache.cxf.tools.common.ToolException in project cxf by apache.

the class PluginLoader method init.

private void init() {
    try {
        jaxbContext = JAXBContext.newInstance("org.apache.cxf.tools.plugin");
        loadPlugins(ClassLoaderUtils.getResources(PLUGIN_FILE_NAME, getClass()));
    } catch (JAXBException e) {
        Message msg = new Message("JAXB_CONTEXT_INIT_FAIL", LOG);
        LOG.log(Level.SEVERE, msg.toString());
        throw new ToolException(msg);
    } catch (IOException ioe) {
        Message msg = new Message("LOAD_PLUGIN_EXCEPTION", LOG);
        LOG.log(Level.SEVERE, msg.toString());
        throw new ToolException(msg);
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) JAXBException(javax.xml.bind.JAXBException) ToolException(org.apache.cxf.tools.common.ToolException) IOException(java.io.IOException)

Example 89 with ToolException

use of org.apache.cxf.tools.common.ToolException in project cxf by apache.

the class PluginLoader method loadContainerClass.

private Class<? extends ToolContainer> loadContainerClass(String fullClzName) {
    Class<?> clz = null;
    try {
        clz = ClassLoaderUtils.loadClass(fullClzName, getClass());
    } catch (Exception e) {
        Message msg = new Message("LOAD_CONTAINER_CLASS_FAILED", LOG, fullClzName);
        LOG.log(Level.SEVERE, msg.toString());
        throw new ToolException(msg, e);
    }
    if (!ToolContainer.class.isAssignableFrom(clz)) {
        Message message = new Message("CLZ_SHOULD_IMPLEMENT_INTERFACE", LOG, clz.getName());
        LOG.log(Level.SEVERE, message.toString());
        throw new ToolException(message);
    }
    return clz.asSubclass(ToolContainer.class);
}
Also used : Message(org.apache.cxf.common.i18n.Message) ToolContainer(org.apache.cxf.tools.common.toolspec.ToolContainer) ToolException(org.apache.cxf.tools.common.ToolException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) ToolException(org.apache.cxf.tools.common.ToolException)

Example 90 with ToolException

use of org.apache.cxf.tools.common.ToolException in project cxf by apache.

the class PluginLoader method getPlugin.

protected Plugin getPlugin(String resource) throws JAXBException, FileNotFoundException {
    Plugin plugin = plugins.get(resource);
    if (plugin == null) {
        InputStream is = null;
        if (new File(resource).exists()) {
            is = new BufferedInputStream(new FileInputStream(new File(resource)));
        } else {
            is = getClass().getResourceAsStream(resource);
        }
        if (is == null) {
            Message msg = new Message("PLUGIN_MISSING", LOG, resource);
            LOG.log(Level.SEVERE, msg.toString());
            throw new ToolException(msg);
        }
        plugin = getPlugin(is);
        if (plugin == null || StringUtils.isEmpty(plugin.getName())) {
            Message msg = new Message("PLUGIN_LOAD_FAIL", LOG, resource);
            LOG.log(Level.SEVERE, msg.toString());
            throw new ToolException(msg);
        }
        plugins.put(resource, plugin);
    }
    return plugin;
}
Also used : Message(org.apache.cxf.common.i18n.Message) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ToolException(org.apache.cxf.tools.common.ToolException) File(java.io.File) FileInputStream(java.io.FileInputStream) Plugin(org.apache.cxf.tools.plugin.Plugin)

Aggregations

ToolException (org.apache.cxf.tools.common.ToolException)129 Message (org.apache.cxf.common.i18n.Message)69 IOException (java.io.IOException)38 File (java.io.File)30 QName (javax.xml.namespace.QName)19 WSDLException (javax.wsdl.WSDLException)18 BadUsageException (org.apache.cxf.tools.common.toolspec.parser.BadUsageException)16 ToolContext (org.apache.cxf.tools.common.ToolContext)15 XMLStreamException (javax.xml.stream.XMLStreamException)14 FileNotFoundException (java.io.FileNotFoundException)12 Test (org.junit.Test)12 Element (org.w3c.dom.Element)10 InputStream (java.io.InputStream)9 Writer (java.io.Writer)9 URISyntaxException (java.net.URISyntaxException)9 URL (java.net.URL)9 HashMap (java.util.HashMap)9 ArrayList (java.util.ArrayList)8 SoapBinding (org.apache.cxf.binding.soap.wsdl.extensions.SoapBinding)8 WSDLWriter (javax.wsdl.xml.WSDLWriter)7