Search in sources :

Example 96 with ToolException

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

the class IDLToWSDLProcessor method addTypeMapSchemaImports.

public void addTypeMapSchemaImports(Definition def, WSDLASTVisitor visitor) {
    List<CorbaType> types = visitor.getTypeMap().getStructOrExceptionOrUnion();
    ModuleToNSMapper mapper = visitor.getModuleToNSMapper();
    WSDLSchemaManager manager = visitor.getManager();
    Collection<String> namespaces = CastUtils.cast(def.getNamespaces().values());
    Set<Map.Entry<String, String>> userModuleMappings = mapper.getUserMapping().entrySet();
    if (types != null) {
        for (int i = 0; i < types.size(); i++) {
            CorbaType type = types.get(i);
            QName schemaType = type.getType();
            if (schemaType != null) {
                String typeNamespace = schemaType.getNamespaceURI();
                try {
                    // a remote location.
                    if (!namespaces.contains(typeNamespace) && typeNamespace.equals(ReferenceConstants.WSADDRESSING_NAMESPACE)) {
                        // build up the ws-addressing schema import
                        Schema wsdlSchema = (Schema) def.getExtensionRegistry().createExtension(Types.class, new QName(Constants.URI_2001_SCHEMA_XSD, "schema"));
                        SchemaImport schemaimport = wsdlSchema.createImport();
                        schemaimport.setNamespaceURI(ReferenceConstants.WSADDRESSING_NAMESPACE);
                        schemaimport.setSchemaLocationURI(ReferenceConstants.WSADDRESSING_LOCATION);
                        wsdlSchema.addImport(schemaimport);
                        // add the import and the prefix to the definition
                        def.getTypes().addExtensibilityElement(wsdlSchema);
                        CastUtils.cast(def.getNamespaces(), String.class, String.class).put(ReferenceConstants.WSADDRESSING_PREFIX, typeNamespace);
                    } else if (!namespaces.contains(typeNamespace)) {
                        String prefix = getModulePrefixForNamespace(userModuleMappings, mapper, typeNamespace);
                        // prefix = mapper.mapNSToPrefix(typeNamespace);
                        XmlSchema schema = manager.getXmlSchema(typeNamespace);
                        // TODO: REVISIT - Is this the only way we can create the file name for the
                        // imported schema?
                        String importFile = visitor.getOutputDir() + System.getProperty("file.separator") + prefix + ".xsd";
                        manager.addWSDLSchemaImport(def, typeNamespace, importFile);
                        manager.getImportedXmlSchemas().put(new File(importFile), schema);
                        CastUtils.cast(def.getNamespaces(), String.class, String.class).put(prefix, typeNamespace);
                    }
                } catch (Exception ex) {
                    throw new ToolException("Unable to add schema import for namespace" + typeNamespace);
                }
            }
        }
    }
}
Also used : Types(javax.wsdl.Types) SchemaImport(javax.wsdl.extensions.schema.SchemaImport) QName(javax.xml.namespace.QName) XmlSchema(org.apache.ws.commons.schema.XmlSchema) Schema(javax.wsdl.extensions.schema.Schema) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ToolException(org.apache.cxf.tools.common.ToolException) Entry(java.util.Map.Entry) CorbaType(org.apache.cxf.binding.corba.wsdl.CorbaType) XmlSchema(org.apache.ws.commons.schema.XmlSchema) ToolException(org.apache.cxf.tools.common.ToolException) File(java.io.File)

Example 97 with ToolException

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

the class IDLToWSDLProcessor method generateCORBAService.

public void generateCORBAService(Definition def, Binding[] bindings, boolean isDefaultMapping) throws Exception {
    Map<String, Service> serviceMap = new HashMap<>();
    Map<String, String> serviceNames = getServiceNames(bindings, isDefaultMapping);
    for (int i = 0; i < bindings.length; i++) {
        QName portTypeName = bindings[i].getPortType().getQName();
        Service service;
        if (isDefaultMapping) {
            service = def.createService();
            service.setQName(new QName(def.getTargetNamespace(), portTypeName.getLocalPart() + "CORBAService"));
            def.addService(service);
        } else {
            String ns = portTypeName.getNamespaceURI();
            String serviceName = serviceNames.get(ns);
            service = serviceMap.get(ns);
            if (service == null) {
                service = def.createService();
                serviceMap.put(ns, service);
                String[] serviceTokens = serviceName.split("\\.");
                String serviceToken = serviceTokens[serviceTokens.length - 1];
                QName serviceQName = new QName(def.getTargetNamespace(), serviceToken);
                Service existingService = def.getService(serviceQName);
                if (existingService != null) {
                    String existingServiceNS = ((Port) existingService.getPorts().values().iterator().next()).getBinding().getPortType().getQName().getNamespaceURI();
                    existingService.setQName(new QName(def.getTargetNamespace(), serviceNames.get(existingServiceNS)));
                    serviceMap.put(existingServiceNS, existingService);
                    service.setQName(new QName(def.getTargetNamespace(), serviceName));
                } else {
                    service.setQName(serviceQName);
                }
                def.addService(service);
            }
        }
        Port port = def.createPort();
        port.setName(portTypeName.getLocalPart() + "CORBAPort");
        AddressType address = (AddressType) def.getExtensionRegistry().createExtension(Port.class, CorbaConstants.NE_CORBA_ADDRESS);
        String addr = null;
        String addrFileName = (String) env.get(ToolCorbaConstants.CFG_ADDRESSFILE);
        if (addrFileName != null) {
            BufferedReader bufferedReader = null;
            try {
                File addrFile = new File(addrFileName);
                FileReader fileReader = new FileReader(addrFile);
                bufferedReader = new BufferedReader(fileReader);
                addr = bufferedReader.readLine();
            } catch (Exception ex) {
                throw new ToolException(ex.getMessage(), ex);
            } finally {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            }
        } else {
            addr = (String) env.get(ToolCorbaConstants.CFG_ADDRESS);
        }
        if (addr == null) {
            addr = "IOR:";
        }
        address.setLocation(addr);
        port.addExtensibilityElement((ExtensibilityElement) address);
        service.addPort(port);
        port.setBinding(bindings[i]);
    }
}
Also used : HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Port(javax.wsdl.Port) Service(javax.wsdl.Service) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ToolException(org.apache.cxf.tools.common.ToolException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ToolException(org.apache.cxf.tools.common.ToolException) AddressType(org.apache.cxf.binding.corba.wsdl.AddressType) File(java.io.File)

Example 98 with ToolException

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

the class IDLToWSDLProcessor method parseIDL.

public void parseIDL(AST idlTree) throws Exception {
    if (env.isVerbose()) {
        System.out.println(idlTree.toStringTree());
    }
    // target namespace
    String tns = (String) env.get(ToolCorbaConstants.CFG_TNS);
    if (tns == null) {
        tns = CorbaConstants.WSDL_NS_URI + idl;
    }
    // XmlSchema namespace
    String schemans = (String) env.get(ToolCorbaConstants.CFG_SCHEMA_NAMESPACE);
    // corba typemap namespace
    String corbatypemaptns = (String) env.get(ToolCorbaConstants.CFG_CORBATYPEMAP_NAMESPACE);
    outputDir = ".";
    try {
        WSDLASTVisitor visitor = new WSDLASTVisitor(tns, schemans, corbatypemaptns, preprocessor.getPragmaPrefix());
        visitor.getManager().setIgnoreImports(ignoreImports);
        if (env.optionSet(ToolConstants.CFG_OUTPUTDIR)) {
            outputDir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
        }
        visitor.setOutputDir(outputDir);
        Definition def = visitor.getDefinition();
        if (env.optionSet(ToolCorbaConstants.CFG_SEQUENCE_OCTET_TYPE)) {
            visitor.setSequenceOctetType((String) env.get(ToolCorbaConstants.CFG_SEQUENCE_OCTET_TYPE));
        }
        if (env.optionSet(ToolCorbaConstants.CFG_SCHEMA_NAMESPACE)) {
            // visitor.getDefinition()
            def.addNamespace(ToolCorbaConstants.CFG_SCHEMA_NAMESPACE_PREFIX, (String) env.get(ToolCorbaConstants.CFG_SCHEMA_NAMESPACE));
        }
        if (env.optionSet(ToolCorbaConstants.CFG_BOUNDEDSTRINGS)) {
            visitor.setBoundedStringOverride(true);
        }
        if (env.optionSet(ToolCorbaConstants.CFG_MODULETONS)) {
            String mapping = (String) env.get(ToolCorbaConstants.CFG_MODULETONS);
            // parse the mapping & set a map of module to namespace mapping in the visitor
            visitor.setModuleToNSMapping(getModuleToNSMapping(mapping));
        }
        if (env.optionSet(ToolCorbaConstants.CFG_QUALIFIED)) {
            visitor.setQualified(true);
        }
        if (env.optionSet(ToolCorbaConstants.CFG_POLYMORPHIC_FACTORIES)) {
            visitor.setSupportPolymorphicFactories(true);
        }
        if (env.optionSet(ToolCorbaConstants.CFG_SCHEMA)) {
            visitor.setSchemaGenerated(true);
            // generate default namespace for schema if -T is used alone.
            if (env.get(ToolCorbaConstants.CFG_SCHEMA_NAMESPACE) == null) {
                visitor.updateSchemaNamespace(def.getTargetNamespace() + "-types");
                def.addNamespace(ToolCorbaConstants.CFG_SCHEMA_NAMESPACE_PREFIX, def.getTargetNamespace() + "-types");
            }
        }
        if (env.optionSet(ToolCorbaConstants.CFG_EXCLUDEMODULES)) {
            String modules = (String) env.get(ToolCorbaConstants.CFG_EXCLUDEMODULES);
            // parse the mapping & set a map of module to namespace mapping in the visitor
            visitor.setExcludedModules(getExcludedModules(modules));
        }
        visitor.visit(idlTree);
        cleanUpTypeMap(visitor.getTypeMap());
        Binding[] bindings = visitor.getCorbaBindings();
        generateCORBAService(def, bindings, visitor.getModuleToNSMapper().isDefaultMapping());
        writeDefinitions(visitor);
    } catch (Exception ex) {
        throw new ToolException(ex.getMessage(), ex);
    }
}
Also used : Binding(javax.wsdl.Binding) Definition(javax.wsdl.Definition) ToolException(org.apache.cxf.tools.common.ToolException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ToolException(org.apache.cxf.tools.common.ToolException)

Example 99 with ToolException

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

the class WSDLToService method execute.

public void execute(boolean exitOnFinish) {
    WSDLToServiceProcessor processor = new WSDLToServiceProcessor();
    try {
        super.execute(exitOnFinish);
        if (!hasInfoOption()) {
            ToolContext env = new ToolContext();
            env.setParameters(getParametersMap(getArrayKeys()));
            if (isVerboseOn()) {
                env.put(ToolConstants.CFG_VERBOSE, Boolean.TRUE);
            }
            env.put(ToolConstants.CFG_CMD_ARG, getArgument());
            validate(env);
            processor.setEnvironment(env);
            processor.process();
        }
    } catch (ToolException ex) {
        if (ex.getCause() instanceof BadUsageException) {
            printUsageException(TOOL_NAME, (BadUsageException) ex.getCause());
        }
        err.println();
        err.println("WSDLToService Error : " + ex.getMessage());
        if (isVerboseOn()) {
            ex.printStackTrace(err);
        }
    } catch (Exception ex) {
        err.println();
        err.println("WSDLToService Error : " + ex.getMessage());
        if (isVerboseOn()) {
            ex.printStackTrace(err);
        }
    } finally {
        tearDown();
    }
}
Also used : WSDLToServiceProcessor(org.apache.cxf.tools.misc.processor.WSDLToServiceProcessor) BadUsageException(org.apache.cxf.tools.common.toolspec.parser.BadUsageException) ToolContext(org.apache.cxf.tools.common.ToolContext) ToolException(org.apache.cxf.tools.common.ToolException) ToolException(org.apache.cxf.tools.common.ToolException) BadUsageException(org.apache.cxf.tools.common.toolspec.parser.BadUsageException)

Example 100 with ToolException

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

the class WSDLToSoap method execute.

public void execute(boolean exitOnFinish) {
    WSDLToSoapProcessor processor = new WSDLToSoapProcessor();
    try {
        super.execute(exitOnFinish);
        if (!hasInfoOption()) {
            ToolContext env = new ToolContext();
            env.setParameters(getParametersMap(getArrayKeys()));
            if (isVerboseOn()) {
                env.put(ToolConstants.CFG_VERBOSE, Boolean.TRUE);
            }
            env.put(ToolConstants.CFG_CMD_ARG, getArgument());
            validate(env);
            setEnvParamDefValues(env);
            processor.setEnvironment(env);
            processor.process();
        }
    } catch (ToolException ex) {
        if (ex.getCause() instanceof BadUsageException) {
            printUsageException(TOOL_NAME, (BadUsageException) ex.getCause());
        }
        err.println();
        err.println("WSDLToSoap Error : " + ex.getMessage());
        if (isVerboseOn()) {
            ex.printStackTrace(err);
        }
    } catch (Exception ex) {
        err.println();
        err.println("WSDLToSoap Error : " + ex.getMessage());
        if (isVerboseOn()) {
            ex.printStackTrace(err);
        }
    } finally {
        tearDown();
    }
}
Also used : BadUsageException(org.apache.cxf.tools.common.toolspec.parser.BadUsageException) ToolContext(org.apache.cxf.tools.common.ToolContext) ToolException(org.apache.cxf.tools.common.ToolException) WSDLToSoapProcessor(org.apache.cxf.tools.misc.processor.WSDLToSoapProcessor) ToolException(org.apache.cxf.tools.common.ToolException) BadUsageException(org.apache.cxf.tools.common.toolspec.parser.BadUsageException)

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