Search in sources :

Example 1 with Compiler

use of org.apache.cxf.common.util.Compiler in project cxf by apache.

the class JavaToWSTest method testJaxwsFrontend.

@Test
public void testJaxwsFrontend() throws Exception {
    File wsdlFile = outputFile("tmp.wsdl");
    String[] args = new String[] { "-wsdl", "-o", output.getPath() + "/tmp.wsdl", "-s", output.getPath(), "-frontend", "jaxws", "-client", "-server", "-address", "http://localhost:1234/test", "org.apache.hello_world_doc_lit.Greeter" };
    JavaToWS.main(args);
    // checkStdErr();
    assertTrue("Failed to generate WSDL file", wsdlFile.exists());
    String str = FileUtils.getStringFromFile(wsdlFile);
    assertTrue("Port address in generated wsdl is not correct", str.indexOf("http://localhost:1234/test") > -1);
    File client = outputFile("org/apache/hello_world_doc_lit/GreeterClient.java");
    str = FileUtils.getStringFromFile(client);
    assertTrue("Address generated in client side code is not correct", str.indexOf("http://localhost:1234/test") > -1);
    File server = outputFile("org/apache/hello_world_doc_lit/GreeterServer.java");
    str = FileUtils.getStringFromFile(server);
    assertTrue("Address generated in server side code is not correct", str.indexOf("http://localhost:1234/test") > -1);
    File impl = outputFile("org/apache/hello_world_doc_lit/GreeterImpl.java");
    Compiler compiler = new Compiler();
    String[] files = new String[] { client.getAbsoluteFile().toString(), server.getAbsoluteFile().toString(), impl.getAbsoluteFile().toString() };
    compiler.setOutputDir(this.classDir);
    compiler.compileFiles(files);
}
Also used : Compiler(org.apache.cxf.common.util.Compiler) File(java.io.File) Test(org.junit.Test)

Example 2 with Compiler

use of org.apache.cxf.common.util.Compiler in project cxf by apache.

the class BeanGenerator method generateAndCompile.

public void generateAndCompile(Collection<JavaClass> wrapperClasses, File dir) {
    VelocityGenerator generator = new VelocityGenerator(false);
    generator.setBaseDir(dir.toString());
    List<File> generatedFiles = new ArrayList<>();
    try {
        for (JavaClass wrapperClass : wrapperClasses) {
            generator.setCommonAttributes();
            generator.setAttributes("bean", wrapperClass);
            File file = generator.parseOutputName(wrapperClass.getPackageName(), wrapperClass.getName());
            generatedFiles.add(file);
            generator.doWrite(TEMPLATE, new FileWriterUtil(file.getParent(), getOutputStreamCreator()).getWriter(file, (String) getToolContext().get(ToolConstants.CFG_ENCODING)));
            generator.clearAttributes();
        }
        // compile the classes
        Compiler compiler = new Compiler();
        compiler.setOutputDir(compileToDir);
        List<String> files = new ArrayList<>(generatedFiles.size());
        for (File file : generatedFiles) {
            files.add(file.getAbsolutePath());
        }
        compiler.compileFiles(files.toArray(new String[files.size()]));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Compiler(org.apache.cxf.common.util.Compiler) VelocityGenerator(org.apache.cxf.tools.common.VelocityGenerator) JavaClass(org.apache.cxf.tools.common.model.JavaClass) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) ArrayList(java.util.ArrayList) File(java.io.File)

Example 3 with Compiler

use of org.apache.cxf.common.util.Compiler 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 4 with Compiler

use of org.apache.cxf.common.util.Compiler in project cxf by apache.

the class JavaToWSTest method testSimpleFrontend.

@Test
public void testSimpleFrontend() throws Exception {
    String[] args = new String[] { "-wsdl", "-o", output.getPath() + "/tmp.wsdl", "-s", output.getPath(), "-frontend", "simple", "-client", "-server", "-address", "http://localhost:1234/test", "org.apache.cxf.tools.fortest.simple.Hello" };
    JavaToWS.main(args);
    File client = outputFile("org/apache/cxf/tools/fortest/simple/HelloPortTypeClient.java");
    File server = outputFile("org/apache/cxf/tools/fortest/simple/HelloPortTypeServer.java");
    File impl = outputFile("org/apache/cxf/tools/fortest/simple/HelloPortTypeImpl.java");
    File wsdl = outputFile("tmp.wsdl");
    assertTrue("Failed to generate client file for simple front end ", client.exists());
    assertTrue("Failed to generate server file for simple front end ", server.exists());
    assertTrue("Failed to generate impl file for simple front end ", impl.exists());
    assertTrue("Failed to generate wsdl file for simple front end ", wsdl.exists());
    String str = FileUtils.getStringFromFile(client);
    assertTrue("Address generated in client side code is not correct", str.indexOf("http://localhost:1234/test") > -1);
    str = FileUtils.getStringFromFile(server);
    assertTrue("Address generated in server side code is not correct", str.indexOf("http://localhost:1234/test") > -1);
    str = FileUtils.getStringFromFile(wsdl);
    assertTrue("Address generated in wsdl is not correct", str.indexOf("http://localhost:1234/test") > -1);
    Compiler compiler = new Compiler();
    String[] files = new String[] { client.getAbsoluteFile().toString(), server.getAbsoluteFile().toString(), impl.getAbsoluteFile().toString() };
    compiler.setOutputDir(this.classDir);
    compiler.compileFiles(files);
}
Also used : Compiler(org.apache.cxf.common.util.Compiler) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)4 Compiler (org.apache.cxf.common.util.Compiler)4 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Message (org.apache.cxf.common.i18n.Message)1 VelocityGenerator (org.apache.cxf.tools.common.VelocityGenerator)1 JavaClass (org.apache.cxf.tools.common.model.JavaClass)1 ClassCollector (org.apache.cxf.tools.util.ClassCollector)1 FileWriterUtil (org.apache.cxf.tools.util.FileWriterUtil)1