Search in sources :

Example 6 with ClassCollector

use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.

the class JAXRSContainerTest method testOnewayMethod.

@Test
public void testOnewayMethod() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);
    final String onewayMethod = "deleteRepository";
    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/test.xml"));
    context.put(WadlToolConstants.CFG_COMPILE, "true");
    context.put(WadlToolConstants.CFG_ONEWAY, onewayMethod);
    container.setContext(context);
    container.execute();
    assertNotNull(output.list());
    ClassCollector cc = context.get(ClassCollector.class);
    assertEquals(1, cc.getServiceClassNames().size());
    try (URLClassLoader loader = new URLClassLoader(new URL[] { output.toURI().toURL() })) {
        final Class<?> generatedClass = loader.loadClass(cc.getServiceClassNames().values().iterator().next());
        Method m = generatedClass.getMethod(onewayMethod, String.class);
        assertNotNull(m.getAnnotation(Oneway.class));
    }
}
Also used : Oneway(org.apache.cxf.jaxrs.ext.Oneway) ClassCollector(org.apache.cxf.tools.util.ClassCollector) URLClassLoader(java.net.URLClassLoader) ToolContext(org.apache.cxf.tools.common.ToolContext) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 7 with ClassCollector

use of org.apache.cxf.tools.util.ClassCollector 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));
    JarOutputStream jarout = null;
    try {
        jarout = new JarOutputStream(Files.newOutputStream(clientJarFile.toPath()), new Manifest());
        createClientJar(tmpDir, jarout);
        jarout.close();
    } 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 8 with ClassCollector

use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.

the class ClassUtils method compile.

public void compile(ToolContext context) throws ToolException {
    Compiler compiler = (Compiler) context.get(ToolConstants.COMPILER);
    if (compiler == null) {
        compiler = new Compiler();
    }
    if (context.isVerbose()) {
        compiler.setVerbose(true);
    }
    compiler.setEncoding((String) context.get(ToolConstants.CFG_ENCODING));
    if (context.get(ToolConstants.CFG_CLASSDIR) != null) {
        compiler.setOutputDir((String) context.get(ToolConstants.CFG_CLASSDIR));
    }
    String javaClasspath = System.getProperty("java.class.path");
    if (javaClasspath != null) {
        if (context.get(ToolConstants.CFG_OUTPUTDIR) != null) {
            compiler.setClassPath(javaClasspath + File.pathSeparatorChar + context.get(ToolConstants.CFG_OUTPUTDIR));
        } else {
            compiler.setClassPath(javaClasspath);
        }
    }
    String outPutDir = (String) context.get(ToolConstants.CFG_OUTPUTDIR);
    Set<String> dirSet = new HashSet<>();
    ClassCollector classCollector = context.get(ClassCollector.class);
    List<String> fileList = new ArrayList<>();
    Iterator<String> ite = classCollector.getGeneratedFileInfo().iterator();
    while (ite.hasNext()) {
        String fileName = ite.next();
        fileName = fileName.replace('.', File.separatorChar);
        String dirName = fileName.substring(0, fileName.lastIndexOf(File.separator) + 1);
        String path = outPutDir + File.separator + dirName;
        if (!dirSet.contains(path)) {
            dirSet.add(path);
            File file = new File(path);
            if (file.isDirectory() && file.list() != null) {
                for (String str : file.list()) {
                    if (str.endsWith("java")) {
                        fileList.add(path + str);
                    } else {
                        // copy generated xml file or others to class directory
                        File otherFile = new File(path + File.separator + str);
                        if (otherFile.isFile() && str.toLowerCase().endsWith("xml") && context.get(ToolConstants.CFG_CLASSDIR) != null) {
                            String targetDir = (String) context.get(ToolConstants.CFG_CLASSDIR);
                            File targetFile = new File(targetDir + File.separator + dirName + File.separator + str);
                            try {
                                copyXmlFile(otherFile, targetFile);
                            } catch (IOException e) {
                                Message msg = new Message("FAIL_TO_COPY_GENERATED_RESOURCE_FILE", LOG);
                                throw new ToolException(msg, e);
                            }
                        }
                    }
                }
                // JAXB plugins will generate extra files under the runtime directory
                // Those files can not be allocated into the ClassCollector
                File jaxbRuntime = new File(path, "runtime");
                if (jaxbRuntime.isDirectory() && jaxbRuntime.exists()) {
                    List<File> files = FileUtils.getFiles(jaxbRuntime, ".+\\.java$");
                    for (File f : files) {
                        fileList.add(f.toString());
                    }
                }
            }
        }
    }
    if (!compiler.compileFiles(fileList.toArray(new String[fileList.size()]))) {
        Message msg = new Message("FAIL_TO_COMPILE_GENERATE_CODES", LOG);
        throw new ToolException(msg);
    }
}
Also used : Compiler(org.apache.cxf.common.util.Compiler) Message(org.apache.cxf.common.i18n.Message) ClassCollector(org.apache.cxf.tools.util.ClassCollector) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) HashSet(java.util.HashSet)

Example 9 with ClassCollector

use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.

the class JAXBDataBinding method initialize.

public void initialize(ToolContext c) throws ToolException {
    this.context = c;
    checkEncoding(c);
    SchemaCompiler schemaCompiler = XJC.createSchemaCompiler();
    Bus bus = context.get(Bus.class);
    OASISCatalogManager catalog = bus.getExtension(OASISCatalogManager.class);
    Options opts = null;
    opts = getOptions(schemaCompiler);
    hackInNewInternalizationLogic(schemaCompiler, catalog, opts);
    ClassCollector classCollector = context.get(ClassCollector.class);
    ClassNameAllocatorImpl allocator = new ClassNameAllocatorImpl(classCollector, c.optionSet(ToolConstants.CFG_AUTORESOLVE));
    schemaCompiler.setClassNameAllocator(allocator);
    listener = new JAXBBindErrorListener(context.isVerbose(), context.getErrorListener());
    schemaCompiler.setErrorListener(listener);
    // Collection<SchemaInfo> schemas = serviceInfo.getSchemas();
    List<InputSource> jaxbBindings = context.getJaxbBindingFile();
    SchemaCollection schemas = (SchemaCollection) context.get(ToolConstants.XML_SCHEMA_COLLECTION);
    List<String> args = new ArrayList<>();
    if (context.get(ToolConstants.CFG_NO_ADDRESS_BINDING) == null) {
        // hard code to enable jaxb extensions
        args.add("-extension");
        String name = "/org/apache/cxf/tools/common/jaxb/W3CEPRJaxbBinding.xml";
        if (isJAXB22()) {
            name = "/org/apache/cxf/tools/common/jaxb/W3CEPRJaxbBinding_jaxb22.xml";
        }
        URL bindingFileUrl = getClass().getResource(name);
        InputSource ins = new InputSource(bindingFileUrl.toString());
        opts.addBindFile(ins);
    }
    if (context.get(ToolConstants.CFG_XJC_ARGS) != null) {
        Object o = context.get(ToolConstants.CFG_XJC_ARGS);
        if (o instanceof String) {
            o = new String[] { (String) o };
        }
        String[] xjcArgss = (String[]) o;
        for (String xjcArgs : xjcArgss) {
            StringTokenizer tokenizer = new StringTokenizer(xjcArgs, ",", false);
            while (tokenizer.hasMoreTokens()) {
                String arg = tokenizer.nextToken();
                args.add(arg);
                LOG.log(Level.FINE, "xjc arg:" + arg);
            }
        }
    }
    if (context.get(ToolConstants.CFG_NO_ADDRESS_BINDING) == null || context.get(ToolConstants.CFG_XJC_ARGS) != null) {
        try {
            // keep parseArguments happy, supply dummy required command-line
            // opts
            opts.addGrammar(new InputSource("null"));
            opts.parseArguments(args.toArray(new String[args.size()]));
        } catch (BadCommandLineException e) {
            StringBuilder msg = new StringBuilder("XJC reported 'BadCommandLineException' for -xjc argument:");
            for (String arg : args) {
                msg.append(arg + " ");
            }
            LOG.log(Level.FINE, msg.toString(), e);
            if (opts != null) {
                String pluginUsage = getPluginUsageString(opts);
                msg.append(System.getProperty("line.separator"));
                if (args.contains("-X")) {
                    throw new ToolException(pluginUsage, e);
                }
                msg.append(pluginUsage);
            }
            throw new ToolException(msg.toString(), e);
        }
    }
    if (context.optionSet(ToolConstants.CFG_MARK_GENERATED)) {
        // '-mark-generated' attribute to jaxb xjc.
        try {
            opts.parseArgument(new String[] { "-mark-generated" }, 0);
        } catch (BadCommandLineException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new ToolException(e);
        }
    }
    addSchemas(opts, schemaCompiler, schemas);
    addBindingFiles(opts, jaxbBindings, schemas);
    parseSchemas(schemaCompiler);
    rawJaxbModelGenCode = schemaCompiler.bind();
    addedEnumClassToCollector(schemas, allocator);
    if (context.get(ToolConstants.CFG_DEFAULT_VALUES) != null) {
        String cname = (String) context.get(ToolConstants.CFG_DEFAULT_VALUES);
        if (StringUtils.isEmpty(cname)) {
            defaultValues = new RandomValueProvider();
        } else {
            if (cname.charAt(0) == '=') {
                cname = cname.substring(1);
            }
            try {
                defaultValues = (DefaultValueProvider) Class.forName(cname).newInstance();
            } catch (Exception e) {
                LOG.log(Level.SEVERE, e.getMessage());
                throw new ToolException(e);
            }
        }
    }
    initialized = true;
}
Also used : Bus(org.apache.cxf.Bus) Options(com.sun.tools.xjc.Options) BadCommandLineException(com.sun.tools.xjc.BadCommandLineException) InputSource(org.xml.sax.InputSource) ClassCollector(org.apache.cxf.tools.util.ClassCollector) ArrayList(java.util.ArrayList) SchemaCompiler(com.sun.tools.xjc.api.SchemaCompiler) URL(java.net.URL) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) SAXException(org.xml.sax.SAXException) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) DOMException(org.w3c.dom.DOMException) BadCommandLineException(com.sun.tools.xjc.BadCommandLineException) SAXParseException(org.xml.sax.SAXParseException) StringTokenizer(java.util.StringTokenizer) OASISCatalogManager(org.apache.cxf.catalog.OASISCatalogManager) ToolException(org.apache.cxf.tools.common.ToolException) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) RandomValueProvider(org.apache.cxf.tools.wsdlto.core.RandomValueProvider)

Example 10 with ClassCollector

use of org.apache.cxf.tools.util.ClassCollector in project cxf by apache.

the class JAXBDataBinding method generate.

public void generate(ToolContext c) throws ToolException {
    if (!initialized) {
        initialize(c);
    }
    if (rawJaxbModelGenCode == null) {
        return;
    }
    if (c.getErrorListener().getErrorCount() > 0) {
        return;
    }
    try {
        String dir = (String) context.get(ToolConstants.CFG_OUTPUTDIR);
        TypesCodeWriter fileCodeWriter = new TypesCodeWriter(new File(dir), context.getExcludePkgList(), (String) context.get(ToolConstants.CFG_ENCODING), context.get(OutputStreamCreator.class));
        S2JJAXBModel schem2JavaJaxbModel = rawJaxbModelGenCode;
        ClassCollector classCollector = context.get(ClassCollector.class);
        for (JClass cls : schem2JavaJaxbModel.getAllObjectFactories()) {
            classCollector.getTypesPackages().add(cls._package().name());
        }
        JCodeModel jcodeModel = schem2JavaJaxbModel.generateCode(null, null);
        if (!isSuppressCodeGen()) {
            jcodeModel.build(fileCodeWriter);
        }
        context.put(JCodeModel.class, jcodeModel);
        for (String str : fileCodeWriter.getExcludeFileList()) {
            context.getExcludeFileList().add(str);
        }
        return;
    } catch (IOException e) {
        Message msg = new Message("FAIL_TO_GENERATE_TYPES", LOG);
        throw new ToolException(msg, e);
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) ClassCollector(org.apache.cxf.tools.util.ClassCollector) JClass(com.sun.codemodel.JClass) OutputStreamCreator(org.apache.cxf.tools.util.OutputStreamCreator) S2JJAXBModel(com.sun.tools.xjc.api.S2JJAXBModel) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) File(java.io.File) JCodeModel(com.sun.codemodel.JCodeModel)

Aggregations

ClassCollector (org.apache.cxf.tools.util.ClassCollector)19 ToolException (org.apache.cxf.tools.common.ToolException)6 File (java.io.File)4 IOException (java.io.IOException)4 Message (org.apache.cxf.common.i18n.Message)4 ArrayList (java.util.ArrayList)3 QName (javax.xml.namespace.QName)3 URL (java.net.URL)2 HashSet (java.util.HashSet)2 SchemaCollection (org.apache.cxf.common.xmlschema.SchemaCollection)2 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)2 ClassUtils (org.apache.cxf.tools.common.ClassUtils)2 JavaInterface (org.apache.cxf.tools.common.model.JavaInterface)2 JavaModel (org.apache.cxf.tools.common.model.JavaModel)2 JavaPort (org.apache.cxf.tools.common.model.JavaPort)2 JavaServiceClass (org.apache.cxf.tools.common.model.JavaServiceClass)2 JAXWSBinding (org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBinding)2 Test (org.junit.Test)2 InputSource (org.xml.sax.InputSource)2 JClass (com.sun.codemodel.JClass)1