Search in sources :

Example 1 with JCodeModel

use of org.apache.cxf.common.jaxb.JAXBUtils.JCodeModel in project cxf by apache.

the class DynamicClientFactory method createClient.

public Client createClient(String wsdlUrl, QName service, ClassLoader classLoader, QName port, List<String> bindingFiles) {
    if (classLoader == null) {
        classLoader = Thread.currentThread().getContextClassLoader();
    }
    LOG.log(Level.FINE, "Creating client from WSDL " + wsdlUrl);
    WSDLServiceFactory sf = (service == null) ? (new WSDLServiceFactory(bus, wsdlUrl)) : (new WSDLServiceFactory(bus, wsdlUrl, service));
    sf.setAllowElementRefs(allowRefs);
    Service svc = sf.create();
    // all SI's should have the same schemas
    SchemaCollection schemas = svc.getServiceInfos().get(0).getXmlSchemaCollection();
    SchemaCompiler compiler = createSchemaCompiler();
    InnerErrorListener listener = new InnerErrorListener(wsdlUrl);
    Object elForRun = ReflectionInvokationHandler.createProxyWrapper(listener, JAXBUtils.getParamClass(compiler, "setErrorListener"));
    compiler.setErrorListener(elForRun);
    OASISCatalogManager catalog = bus.getExtension(OASISCatalogManager.class);
    hackInNewInternalizationLogic(compiler, catalog);
    addSchemas(compiler.getOptions(), compiler, svc.getServiceInfos(), schemas);
    addBindingFiles(bindingFiles, compiler);
    applySchemaCompilerOptions(compiler);
    S2JJAXBModel intermediateModel = compiler.bind();
    listener.throwException();
    JCodeModel codeModel = intermediateModel.generateCode(null, elForRun);
    StringBuilder sb = new StringBuilder();
    boolean firstnt = false;
    for (Iterator<JPackage> packages = codeModel.packages(); packages.hasNext(); ) {
        JPackage jpackage = packages.next();
        if (!isValidPackage(jpackage)) {
            continue;
        }
        if (firstnt) {
            sb.append(':');
        } else {
            firstnt = true;
        }
        sb.append(jpackage.name());
    }
    JAXBUtils.logGeneratedClassNames(LOG, codeModel);
    String packageList = sb.toString();
    // our hashcode + timestamp ought to be enough.
    String stem = toString() + "-" + System.currentTimeMillis();
    File src = new File(tmpdir, stem + "-src");
    if (!src.mkdir()) {
        throw new IllegalStateException("Unable to create working directory " + src.getPath());
    }
    try {
        Object writer = JAXBUtils.createFileCodeWriter(src);
        codeModel.build(writer);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to write generated Java files for schemas: " + e.getMessage(), e);
    }
    File classes = new File(tmpdir, stem + "-classes");
    if (!classes.mkdir()) {
        throw new IllegalStateException("Unable to create working directory " + classes.getPath());
    }
    StringBuilder classPath = new StringBuilder();
    try {
        setupClasspath(classPath, classLoader);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    List<File> srcFiles = FileUtils.getFilesRecurseUsingSuffix(src, ".java");
    if (!srcFiles.isEmpty() && !compileJavaSrc(classPath.toString(), srcFiles, classes.toString())) {
        LOG.log(Level.SEVERE, new Message("COULD_NOT_COMPILE_SRC", LOG, wsdlUrl).toString());
    }
    FileUtils.removeDir(src);
    final URL[] urls;
    try {
        urls = new URL[] { classes.toURI().toURL() };
    } catch (MalformedURLException mue) {
        throw new IllegalStateException("Internal error; a directory returns a malformed URL: " + mue.getMessage(), mue);
    }
    final ClassLoader cl = ClassLoaderUtils.getURLClassLoader(urls, classLoader);
    JAXBContext context;
    Map<String, Object> contextProperties = jaxbContextProperties;
    if (contextProperties == null) {
        contextProperties = Collections.emptyMap();
    }
    try {
        if (StringUtils.isEmpty(packageList)) {
            context = JAXBContext.newInstance(new Class[0], contextProperties);
        } else {
            context = JAXBContext.newInstance(packageList, cl, contextProperties);
        }
    } catch (JAXBException jbe) {
        throw new IllegalStateException("Unable to create JAXBContext for generated packages: " + jbe.getMessage(), jbe);
    }
    JAXBDataBinding databinding = new JAXBDataBinding();
    databinding.setContext(context);
    svc.setDataBinding(databinding);
    ClientImpl client = new DynamicClientImpl(bus, svc, port, getEndpointImplFactory(), cl);
    ServiceInfo svcfo = client.getEndpoint().getEndpointInfo().getService();
    // Setup the new classloader!
    ClassLoaderUtils.setThreadContextClassloader(cl);
    TypeClassInitializer visitor = new TypeClassInitializer(bus, svcfo, intermediateModel, allowWrapperOps());
    visitor.walk();
    // delete the classes files
    FileUtils.removeDir(classes);
    return client;
}
Also used : MalformedURLException(java.net.MalformedURLException) Message(org.apache.cxf.common.i18n.Message) JAXBContext(javax.xml.bind.JAXBContext) SchemaCompiler(org.apache.cxf.common.jaxb.JAXBUtils.SchemaCompiler) URL(java.net.URL) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) OASISCatalogManager(org.apache.cxf.catalog.OASISCatalogManager) URLClassLoader(java.net.URLClassLoader) S2JJAXBModel(org.apache.cxf.common.jaxb.JAXBUtils.S2JJAXBModel) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) WSDLServiceFactory(org.apache.cxf.wsdl11.WSDLServiceFactory) JAXBException(javax.xml.bind.JAXBException) JPackage(org.apache.cxf.common.jaxb.JAXBUtils.JPackage) Service(org.apache.cxf.service.Service) ClientImpl(org.apache.cxf.endpoint.ClientImpl) JCodeModel(org.apache.cxf.common.jaxb.JAXBUtils.JCodeModel) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) DOMException(org.w3c.dom.DOMException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) JDefinedClass(org.apache.cxf.common.jaxb.JAXBUtils.JDefinedClass) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 2 with JCodeModel

use of org.apache.cxf.common.jaxb.JAXBUtils.JCodeModel in project fabric8 by jboss-fuse.

the class DynamicXJC method compileSchemas.

public CompileResults compileSchemas() {
    SchemaCompiler compiler = createSchemaCompiler();
    // our hashcode + timestamp ought to be enough.
    String stem = toString().replaceAll("@", "_") + "-" + System.currentTimeMillis();
    File src = new File(tmpdir, stem + "-src");
    if (!src.mkdir()) {
        throw new IllegalStateException("Unable to create working directory " + src.getPath());
    }
    boolean first = true;
    StringBuilder sb = new StringBuilder();
    for (String rawUrl : getSchemaUrls()) {
        String schemaUrl = resolveUrl(rawUrl);
        InnerErrorListener listener = new InnerErrorListener(schemaUrl);
        Object elForRun = ReflectionInvokationHandler.createProxyWrapper(listener, JAXBUtils.getParamClass(compiler, "setErrorListener"));
        compiler.setErrorListener(elForRun);
        compiler.parseSchema(new InputSource(schemaUrl));
        S2JJAXBModel intermediateModel = compiler.bind();
        listener.throwException();
        JCodeModel codeModel = intermediateModel.generateCode(null, elForRun);
        for (Iterator<JPackage> packages = codeModel.packages(); packages.hasNext(); ) {
            JPackage jpackage = packages.next();
            if (!isValidPackage(jpackage)) {
                continue;
            }
            if (first) {
                first = false;
            } else {
                sb.append(':');
            }
            sb.append(jpackage.name());
        }
        JAXBUtils.logGeneratedClassNames(LOG, codeModel);
        try {
            Object writer = JAXBUtils.createFileCodeWriter(src);
            codeModel.build(writer);
        } catch (Exception e) {
            throw new IllegalStateException("Unable to write generated Java files for schemas: " + e.getMessage(), e);
        }
    }
    String packageList = sb.toString();
    File classes = new File(tmpdir, stem + "-classes");
    if (!classes.mkdir()) {
        throw new IllegalStateException("Unable to create working directory " + classes.getPath());
    }
    StringBuilder classPath = new StringBuilder();
    try {
        setupClasspath(classPath, classLoader);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    List<File> srcFiles = FileUtils.getFilesRecurse(src, ".+\\.java$");
    if (!compileJavaSrc(classPath.toString(), srcFiles, classes.toString())) {
        LOG.log(Level.SEVERE, new Message("COULD_NOT_COMPILE_SRC", LOG, getSchemaUrls().toString()).toString());
    }
    FileUtils.removeDir(src);
    URL[] urls;
    try {
        urls = new URL[] { classes.toURI().toURL() };
    } catch (MalformedURLException mue) {
        throw new IllegalStateException("Internal error; a directory returns a malformed URL: " + mue.getMessage(), mue);
    }
    ClassLoader cl = ClassLoaderUtils.getURLClassLoader(urls, classLoader);
    JAXBContext context;
    Map<String, Object> contextProperties = jaxbContextProperties;
    if (contextProperties == null) {
        contextProperties = Collections.emptyMap();
    }
    try {
        if (StringUtils.isEmpty(packageList)) {
            context = JAXBContext.newInstance(new Class[0], contextProperties);
        } else {
            context = JAXBContext.newInstance(packageList, cl, contextProperties);
        }
    } catch (JAXBException jbe) {
        throw new IllegalStateException("Unable to create JAXBContext for generated packages: " + jbe.getMessage(), jbe);
    }
    // keep around for class loader discovery later
    classes.deleteOnExit();
    // FileUtils.removeDir(classes);
    return new CompileResults(cl, context);
}
Also used : InputSource(org.xml.sax.InputSource) MalformedURLException(java.net.MalformedURLException) Message(org.apache.cxf.common.i18n.Message) JAXBContext(javax.xml.bind.JAXBContext) SchemaCompiler(org.apache.cxf.common.jaxb.JAXBUtils.SchemaCompiler) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) S2JJAXBModel(org.apache.cxf.common.jaxb.JAXBUtils.S2JJAXBModel) JAXBException(javax.xml.bind.JAXBException) JPackage(org.apache.cxf.common.jaxb.JAXBUtils.JPackage) JCodeModel(org.apache.cxf.common.jaxb.JAXBUtils.JCodeModel) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXParseException(org.xml.sax.SAXParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JDefinedClass(org.apache.cxf.common.jaxb.JAXBUtils.JDefinedClass) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 3 with JCodeModel

use of org.apache.cxf.common.jaxb.JAXBUtils.JCodeModel in project cxf by apache.

the class SourceGenerator method createCodeModel.

private JCodeModel createCodeModel(List<SchemaInfo> schemaElements, Set<String> type) {
    SchemaCompiler compiler = createCompiler(type);
    Object elForRun = ReflectionInvokationHandler.createProxyWrapper(new InnerErrorListener(), JAXBUtils.getParamClass(compiler, "setErrorListener"));
    compiler.setErrorListener(elForRun);
    compiler.setEntityResolver(OASISCatalogManager.getCatalogManager(bus).getEntityResolver());
    if (!compilerArgs.isEmpty()) {
        compiler.getOptions().addGrammar(new InputSource("null"));
        compiler.getOptions().parseArguments(compilerArgs.toArray(new String[0]));
    }
    addSchemas(schemaElements, compiler);
    for (InputSource is : bindingFiles) {
        compiler.getOptions().addBindFile(is);
    }
    S2JJAXBModel intermediateModel = compiler.bind();
    JCodeModel codeModel = intermediateModel.generateCode(null, elForRun);
    JAXBUtils.logGeneratedClassNames(LOG, codeModel);
    return codeModel;
}
Also used : InputSource(org.xml.sax.InputSource) S2JJAXBModel(org.apache.cxf.common.jaxb.JAXBUtils.S2JJAXBModel) SchemaCompiler(org.apache.cxf.common.jaxb.JAXBUtils.SchemaCompiler) JCodeModel(org.apache.cxf.common.jaxb.JAXBUtils.JCodeModel)

Aggregations

JCodeModel (org.apache.cxf.common.jaxb.JAXBUtils.JCodeModel)3 S2JJAXBModel (org.apache.cxf.common.jaxb.JAXBUtils.S2JJAXBModel)3 SchemaCompiler (org.apache.cxf.common.jaxb.JAXBUtils.SchemaCompiler)3 File (java.io.File)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 JarFile (java.util.jar.JarFile)2 JAXBContext (javax.xml.bind.JAXBContext)2 JAXBException (javax.xml.bind.JAXBException)2 Message (org.apache.cxf.common.i18n.Message)2 JDefinedClass (org.apache.cxf.common.jaxb.JAXBUtils.JDefinedClass)2 JPackage (org.apache.cxf.common.jaxb.JAXBUtils.JPackage)2 InputSource (org.xml.sax.InputSource)2 SAXParseException (org.xml.sax.SAXParseException)2 XMLStreamException (javax.xml.stream.XMLStreamException)1 OASISCatalogManager (org.apache.cxf.catalog.OASISCatalogManager)1