Search in sources :

Example 1 with ClassUtils

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

the class WSDLToJavaContainer method processClientJar.

private void processClientJar(ToolContext context) {
    ClassCollector oldCollector = context.get(ClassCollector.class);
    ClassCollector newCollector = new ClassCollector();
    String oldClassDir = (String) context.get(ToolConstants.CFG_CLASSDIR);
    File tmpDir = FileUtils.createTmpDir();
    context.put(ToolConstants.CFG_CLASSDIR, tmpDir.getAbsolutePath());
    newCollector.setTypesClassNames(oldCollector.getTypesClassNames());
    newCollector.setSeiClassNames(oldCollector.getSeiClassNames());
    newCollector.setExceptionClassNames(oldCollector.getExceptionClassNames());
    newCollector.setServiceClassNames(oldCollector.getServiceClassNames());
    context.put(ClassCollector.class, newCollector);
    new ClassUtils().compile(context);
    generateLocalWSDL(context);
    File clientJarFile = new File((String) context.get(ToolConstants.CFG_OUTPUTDIR), (String) context.get(ToolConstants.CFG_CLIENT_JAR));
    try (JarOutputStream jarout = new JarOutputStream(Files.newOutputStream(clientJarFile.toPath()), new Manifest())) {
        createClientJar(tmpDir, jarout);
    } catch (Exception e) {
        LOG.log(Level.SEVERE, "FAILED_TO_CREAT_CLIENTJAR", e);
        Message msg = new Message("FAILED_TO_CREAT_CLIENTJAR", LOG);
        throw new ToolException(msg, e);
    }
    context.put(ToolConstants.CFG_CLASSDIR, oldClassDir);
    context.put(ClassCollector.class, oldCollector);
}
Also used : Message(org.apache.cxf.common.i18n.Message) ClassCollector(org.apache.cxf.tools.util.ClassCollector) JarOutputStream(java.util.jar.JarOutputStream) ToolException(org.apache.cxf.tools.common.ToolException) Manifest(java.util.jar.Manifest) File(java.io.File) ClassUtils(org.apache.cxf.tools.common.ClassUtils) BadUsageException(org.apache.cxf.tools.common.toolspec.parser.BadUsageException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException)

Example 2 with ClassUtils

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

the class WSDLToJavaContainer method processWsdl.

private void processWsdl() {
    validate(context);
    FrontEndProfile frontend = context.get(FrontEndProfile.class);
    if (frontend == null) {
        throw new ToolException(new Message("FOUND_NO_FRONTEND", LOG));
    }
    WSDLConstants.WSDLVersion version = getWSDLVersion();
    String wsdlURL = (String) context.get(ToolConstants.CFG_WSDLURL);
    @SuppressWarnings("unchecked") List<ServiceInfo> serviceList = (List<ServiceInfo>) context.get(ToolConstants.SERVICE_LIST);
    if (serviceList == null) {
        serviceList = new ArrayList<>();
        // Build the ServiceModel from the WSDLModel
        if (version == WSDLConstants.WSDLVersion.WSDL11) {
            AbstractWSDLBuilder builder = frontend.getWSDLBuilder();
            builder.setContext(context);
            builder.setBus(getBus());
            context.put(Bus.class, getBus());
            wsdlURL = URIParserUtil.getAbsoluteURI(wsdlURL);
            builder.build(wsdlURL);
            builder.customize();
            Definition definition = builder.getWSDLModel();
            context.put(Definition.class, definition);
            builder.validate(definition);
            WSDLServiceBuilder serviceBuilder = new WSDLServiceBuilder(getBus());
            if (context.isVerbose()) {
                serviceBuilder.setUnwrapLogLevel(Level.INFO);
            }
            serviceBuilder.setIgnoreUnknownBindings(true);
            String allowRefs = (String) context.get(ToolConstants.CFG_ALLOW_ELEMENT_REFS);
            if (!StringUtils.isEmpty(allowRefs) || context.optionSet(ToolConstants.CFG_ALLOW_ELEMENT_REFS)) {
                if (allowRefs.length() > 0 && allowRefs.charAt(0) == '=') {
                    allowRefs = allowRefs.substring(1);
                }
                if (StringUtils.isEmpty(allowRefs)) {
                    allowRefs = "true";
                }
                serviceBuilder.setAllowElementRefs(Boolean.valueOf(allowRefs));
            }
            String serviceName = (String) context.get(ToolConstants.CFG_SERVICENAME);
            if (serviceName != null) {
                List<ServiceInfo> services = serviceBuilder.buildServices(definition, getServiceQName(definition));
                serviceList.addAll(services);
            } else if (definition.getServices().size() > 0) {
                serviceList = serviceBuilder.buildServices(definition);
            } else {
                serviceList = serviceBuilder.buildMockServices(definition);
            }
            // remove definition from cache so that won't fail when encounter same wsdl file
            // name but different wsdl content(CXF-3340)
            getBus().getExtension(WSDLManager.class).removeDefinition(definition);
        } else {
        // TODO: wsdl2.0 support
        }
    }
    context.put(ToolConstants.SERVICE_LIST, serviceList);
    Map<String, InterfaceInfo> interfaces = new LinkedHashMap<>();
    ServiceInfo service0 = serviceList.get(0);
    SchemaCollection schemaCollection = service0.getXmlSchemaCollection();
    context.put(ToolConstants.XML_SCHEMA_COLLECTION, schemaCollection);
    context.put(ToolConstants.PORTTYPE_MAP, interfaces);
    context.put(ClassCollector.class, createClassCollector());
    Processor processor = frontend.getProcessor();
    if (processor instanceof ClassNameProcessor) {
        processor.setEnvironment(context);
        for (ServiceInfo service : serviceList) {
            context.put(ServiceInfo.class, service);
            ((ClassNameProcessor) processor).processClassNames();
            context.put(ServiceInfo.class, null);
        }
    }
    if (context.optionSet(ToolConstants.CFG_NO_TYPES)) {
        context.remove(ToolConstants.CFG_TYPES);
        context.remove(ToolConstants.CFG_ALL);
        context.remove(ToolConstants.CFG_COMPILE);
    }
    generateTypes();
    if (context.getErrorListener().getErrorCount() > 0) {
        return;
    }
    for (ServiceInfo service : serviceList) {
        context.put(ServiceInfo.class, service);
        if (context.basicValidateWSDL()) {
            validate(service);
        }
        if (context.getErrorListener().getErrorCount() == 0) {
            // Build the JavaModel from the ServiceModel
            processor.setEnvironment(context);
            processor.process();
        }
    }
    if (context.getErrorListener().getErrorCount() > 0) {
        return;
    }
    if (context.optionSet(ToolConstants.CFG_CLIENT_JAR)) {
        enforceWSDLLocation(context);
    }
    if (!isSuppressCodeGen()) {
        // Generate artifacts
        for (FrontEndGenerator generator : frontend.getGenerators()) {
            generator.generate(context);
        }
    }
    context.remove(ToolConstants.SERVICE_LIST);
    // Build projects: compile classes and copy resources etc.
    if (context.optionSet(ToolConstants.CFG_COMPILE)) {
        new ClassUtils().compile(context);
    }
    if (context.isExcludeNamespaceEnabled()) {
        try {
            removeExcludeFiles();
        } catch (IOException e) {
            throw new ToolException(e);
        }
    }
    if (context.optionSet(ToolConstants.CFG_CLIENT_JAR)) {
        processClientJar(context);
    }
}
Also used : AbstractWSDLBuilder(org.apache.cxf.tools.wsdlto.core.AbstractWSDLBuilder) ClassNameProcessor(org.apache.cxf.tools.common.ClassNameProcessor) Processor(org.apache.cxf.tools.common.Processor) Message(org.apache.cxf.common.i18n.Message) Definition(javax.wsdl.Definition) IOException(java.io.IOException) ClassUtils(org.apache.cxf.tools.common.ClassUtils) FrontEndProfile(org.apache.cxf.tools.wsdlto.core.FrontEndProfile) LinkedHashMap(java.util.LinkedHashMap) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) WSDLConstants(org.apache.cxf.wsdl.WSDLConstants) WSDLServiceBuilder(org.apache.cxf.wsdl11.WSDLServiceBuilder) FrontEndGenerator(org.apache.cxf.tools.common.FrontEndGenerator) WSDLManager(org.apache.cxf.wsdl.WSDLManager) ClassNameProcessor(org.apache.cxf.tools.common.ClassNameProcessor) List(java.util.List) ArrayList(java.util.ArrayList) ToolException(org.apache.cxf.tools.common.ToolException) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection)

Example 3 with ClassUtils

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

the class JAXRSContainer method processWadl.

private void processWadl() {
    File outDir = new File((String) context.get(WadlToolConstants.CFG_OUTPUTDIR));
    String wadlURL = getAbsoluteWadlURL();
    String authentication = (String) context.get(WadlToolConstants.CFG_AUTHENTICATION);
    SourceGenerator sg = new SourceGenerator();
    sg.setBus(getBus());
    boolean generateImpl = context.optionSet(WadlToolConstants.CFG_IMPL);
    sg.setGenerateImplementation(generateImpl);
    if (generateImpl) {
        sg.setGenerateInterfaces(context.optionSet(WadlToolConstants.CFG_INTERFACE));
    }
    sg.setPackageName((String) context.get(WadlToolConstants.CFG_PACKAGENAME));
    sg.setResourceName((String) context.get(WadlToolConstants.CFG_RESOURCENAME));
    sg.setEncoding((String) context.get(WadlToolConstants.CFG_ENCODING));
    sg.setAuthentication(authentication);
    String wadlNs = (String) context.get(WadlToolConstants.CFG_WADL_NAMESPACE);
    if (wadlNs != null) {
        sg.setWadlNamespace(wadlNs);
    }
    sg.setJaxbClassNameSuffix((String) context.get(WadlToolConstants.CFG_JAXB_CLASS_NAME_SUFFIX));
    sg.setSupportMultipleXmlReps(context.optionSet(WadlToolConstants.CFG_MULTIPLE_XML_REPS));
    sg.setSupportBeanValidation(context.optionSet(WadlToolConstants.CFG_BEAN_VALIDATION));
    sg.setCreateJavaDocs(context.optionSet(WadlToolConstants.CFG_CREATE_JAVA_DOCS));
    // set the base path
    sg.setWadlPath(wadlURL);
    CustomizationParser parser = new CustomizationParser(context);
    parser.parse(context);
    List<InputSource> bindingFiles = parser.getJaxbBindings();
    sg.setBindingFiles(bindingFiles);
    sg.setCompilerArgs(parser.getCompilerArgs());
    List<InputSource> schemaPackageFiles = parser.getSchemaPackageFiles();
    sg.setSchemaPackageFiles(schemaPackageFiles);
    sg.setSchemaPackageMap(context.getNamespacePackageMap());
    sg.setJavaTypeMap(DEFAULT_JAVA_TYPE_MAP);
    sg.setSchemaTypeMap(getSchemaTypeMap());
    sg.setMediaTypeMap(getMediaTypeMap());
    sg.setSuspendedAsyncMethods(getSuspendedAsyncMethods());
    sg.setResponseMethods(getResponseMethods());
    sg.setOnewayMethods(getOnewayMethods());
    sg.setGenerateEnums(context.optionSet(WadlToolConstants.CFG_GENERATE_ENUMS));
    sg.setValidateWadl(context.optionSet(WadlToolConstants.CFG_VALIDATE_WADL));
    sg.setRx(context.get(WadlToolConstants.CFG_RX, String.class));
    boolean inheritResourceParams = context.optionSet(WadlToolConstants.CFG_INHERIT_PARAMS);
    sg.setInheritResourceParams(inheritResourceParams);
    if (inheritResourceParams) {
        sg.setInheritResourceParamsFirst(isInheritResourceParamsFirst());
    }
    sg.setSkipSchemaGeneration(context.optionSet(WadlToolConstants.CFG_NO_TYPES));
    boolean noVoidForEmptyResponses = context.optionSet(WadlToolConstants.CFG_NO_VOID_FOR_EMPTY_RESPONSES);
    if (noVoidForEmptyResponses) {
        sg.setUseVoidForEmptyResponses(false);
    }
    sg.setGenerateResponseIfHeadersSet(context.optionSet(WadlToolConstants.CFG_GENERATE_RESPONSE_IF_HEADERS_SET));
    // generate
    String codeType = context.optionSet(WadlToolConstants.CFG_TYPES) ? SourceGenerator.CODE_TYPE_GRAMMAR : SourceGenerator.CODE_TYPE_PROXY;
    sg.generateSource(outDir, codeType);
    // compile
    if (context.optionSet(WadlToolConstants.CFG_COMPILE)) {
        ClassCollector collector = createClassCollector();
        List<String> generatedServiceClasses = sg.getGeneratedServiceClasses();
        for (String className : generatedServiceClasses) {
            int index = className.lastIndexOf('.');
            collector.addServiceClassName(className.substring(0, index), className.substring(index + 1), className);
        }
        List<String> generatedTypeClasses = sg.getGeneratedTypeClasses();
        for (String className : generatedTypeClasses) {
            int index = className.lastIndexOf('.');
            collector.addTypesClassName(className.substring(0, index), className.substring(index + 1), className);
        }
        context.put(ClassCollector.class, collector);
        new ClassUtils().compile(context);
    }
}
Also used : InputSource(org.xml.sax.InputSource) CustomizationParser(org.apache.cxf.tools.wadlto.jaxb.CustomizationParser) ClassCollector(org.apache.cxf.tools.util.ClassCollector) File(java.io.File) ClassUtils(org.apache.cxf.tools.common.ClassUtils)

Aggregations

ClassUtils (org.apache.cxf.tools.common.ClassUtils)3 File (java.io.File)2 IOException (java.io.IOException)2 Message (org.apache.cxf.common.i18n.Message)2 ToolException (org.apache.cxf.tools.common.ToolException)2 ClassCollector (org.apache.cxf.tools.util.ClassCollector)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 JarOutputStream (java.util.jar.JarOutputStream)1 Manifest (java.util.jar.Manifest)1 Definition (javax.wsdl.Definition)1 SchemaCollection (org.apache.cxf.common.xmlschema.SchemaCollection)1 InterfaceInfo (org.apache.cxf.service.model.InterfaceInfo)1 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)1 ClassNameProcessor (org.apache.cxf.tools.common.ClassNameProcessor)1 FrontEndGenerator (org.apache.cxf.tools.common.FrontEndGenerator)1 Processor (org.apache.cxf.tools.common.Processor)1 BadUsageException (org.apache.cxf.tools.common.toolspec.parser.BadUsageException)1 CustomizationParser (org.apache.cxf.tools.wadlto.jaxb.CustomizationParser)1