Search in sources :

Example 6 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class JavaJSONClientModule method compileClientSources.

protected File compileClientSources(File sourceDir) {
    File compileDir = getCompileDir();
    compileDir.mkdirs();
    // Compile the java files.
    if (!isDisableCompile()) {
        if (!isUpToDateWithSources(compileDir)) {
            List<File> sources = findJavaFiles(sourceDir);
            if (sources != null && !sources.isEmpty()) {
                String classpath = this.enunciate.writeClasspath(enunciate.getClasspath());
                JavaCompiler compiler = JavacTool.create();
                List<String> options = Arrays.asList("-source", getJavacSource(), "-target", getJavacTarget(), "-encoding", "UTF-8", "-cp", classpath, "-d", compileDir.getAbsolutePath(), "-nowarn");
                JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, options, null, compiler.getStandardFileManager(null, null, null).getJavaFileObjectsFromFiles(sources));
                if (!task.call()) {
                    throw new EnunciateException("Compile failed of Java JSON client-side classes.");
                }
            } else {
                debug("No Java JSON client classes to compile.");
            }
        } else {
            info("Skipping compilation of Java JSON client classes as everything appears up-to-date...");
        }
    }
    return compileDir;
}
Also used : EnunciateException(com.webcohesion.enunciate.EnunciateException) JavaCompiler(javax.tools.JavaCompiler)

Example 7 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class JavaXMLClientModule method compileClientSources.

protected File compileClientSources(File sourceDir) {
    File compileDir = getCompileDir();
    compileDir.mkdirs();
    // Compile the java files.
    if (!isDisableCompile()) {
        if (!isUpToDateWithSources(compileDir)) {
            List<File> sources = findJavaFiles(sourceDir);
            if (sources != null && !sources.isEmpty()) {
                String classpath = this.enunciate.writeClasspath(enunciate.getClasspath());
                JavaCompiler compiler = JavacTool.create();
                List<String> options = Arrays.asList("-source", getJavacSource(), "-target", getJavacTarget(), "-encoding", "UTF-8", "-cp", classpath, "-d", compileDir.getAbsolutePath(), "-nowarn");
                JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, options, null, compiler.getStandardFileManager(null, null, null).getJavaFileObjectsFromFiles(sources));
                if (!task.call()) {
                    throw new EnunciateException("Compile failed of Java client-side classes.");
                }
            } else {
                debug("No Java XML client classes to compile.");
            }
        } else {
            info("Skipping compilation of Java client classes as everything appears up-to-date...");
        }
    }
    return compileDir;
}
Also used : EnunciateException(com.webcohesion.enunciate.EnunciateException) JavaCompiler(javax.tools.JavaCompiler)

Example 8 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class JavaXMLClientModule method generateClientSources.

protected File generateClientSources() {
    File sourceDir = getSourceDir();
    sourceDir.mkdirs();
    Map<String, Object> model = new HashMap<String, Object>();
    Map<String, String> conversions = getClientPackageConversions();
    EnunciateJaxbContext jaxbContext = this.jaxbModule.getJaxbContext();
    model.put("packageFor", new ClientPackageForMethod(conversions, this.context));
    model.put("classnameFor", new ClientClassnameForMethod(conversions, jaxbContext));
    model.put("simpleNameFor", new SimpleNameForMethod(new ClientClassnameForMethod(conversions, jaxbContext, true)));
    model.put("file", new FileDirective(sourceDir, this.enunciate.getLogger()));
    model.put("generatedCodeLicense", this.enunciate.getConfiguration().readGeneratedCodeLicenseFile());
    model.put("annotationValue", new AnnotationValueMethod());
    Set<String> facetIncludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetIncludes());
    facetIncludes.addAll(getFacetIncludes());
    Set<String> facetExcludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetExcludes());
    facetExcludes.addAll(getFacetExcludes());
    FacetFilter facetFilter = new FacetFilter(facetIncludes, facetExcludes);
    model.put("isFacetExcluded", new IsFacetExcludedMethod(facetFilter));
    boolean upToDate = isUpToDateWithSources(sourceDir);
    if (!upToDate) {
        try {
            debug("Generating the Java client classes...");
            HashMap<String, WebFault> allFaults = new HashMap<String, WebFault>();
            AntPatternMatcher matcher = new AntPatternMatcher();
            matcher.setPathSeparator(".");
            if (this.jaxwsModule != null) {
                Set<String> seeAlsos = new TreeSet<String>();
                // for each endpoint interface.
                for (WsdlInfo wsdlInfo : this.jaxwsModule.getJaxwsContext().getWsdls().values()) {
                    for (EndpointInterface ei : wsdlInfo.getEndpointInterfaces()) {
                        if (facetFilter.accept(ei)) {
                            for (WebMethod webMethod : ei.getWebMethods()) {
                                if (facetFilter.accept(webMethod)) {
                                    for (WebMessage webMessage : webMethod.getMessages()) {
                                        if (webMessage instanceof RequestWrapper) {
                                            model.put("message", webMessage);
                                            processTemplate(getTemplateURL("client-request-bean.fmt"), model);
                                            seeAlsos.add(getBeanName(new ClientClassnameForMethod(conversions, jaxbContext), ((RequestWrapper) webMessage).getRequestBeanName()));
                                        } else if (webMessage instanceof ResponseWrapper) {
                                            model.put("message", webMessage);
                                            processTemplate(getTemplateURL("client-response-bean.fmt"), model);
                                            seeAlsos.add(getBeanName(new ClientClassnameForMethod(conversions, jaxbContext), ((ResponseWrapper) webMessage).getResponseBeanName()));
                                        } else if (webMessage instanceof WebFault) {
                                            WebFault fault = (WebFault) webMessage;
                                            allFaults.put(fault.getQualifiedName().toString(), fault);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                // gather the annotation information and process the possible beans for each web fault.
                for (WebFault webFault : allFaults.values()) {
                    boolean implicit = webFault.isImplicitSchemaElement();
                    String faultBean = implicit ? getBeanName(new ClientClassnameForMethod(conversions, jaxbContext), webFault.getImplicitFaultBeanQualifiedName()) : new ClientClassnameForMethod(conversions, jaxbContext).convert(webFault.getExplicitFaultBeanType());
                    seeAlsos.add(faultBean);
                    if (implicit) {
                        model.put("fault", webFault);
                        processTemplate(getTemplateURL("client-fault-bean.fmt"), model);
                    }
                }
                model.put("seeAlsoBeans", seeAlsos);
                model.put("baseUri", this.enunciate.getConfiguration().getApplicationRoot());
                for (WsdlInfo wsdlInfo : this.jaxwsModule.getJaxwsContext().getWsdls().values()) {
                    if (wsdlInfo.getWsdlFile() == null) {
                        throw new EnunciateException("WSDL " + wsdlInfo.getId() + " doesn't have a filename.");
                    }
                    for (EndpointInterface ei : wsdlInfo.getEndpointInterfaces()) {
                        if (facetFilter.accept(ei)) {
                            model.put("endpointInterface", ei);
                            model.put("wsdlFileName", wsdlInfo.getFilename());
                            processTemplate(getTemplateURL("client-endpoint-interface.fmt"), model);
                            processTemplate(getTemplateURL("client-soap-endpoint-impl.fmt"), model);
                        }
                    }
                }
                for (WebFault webFault : allFaults.values()) {
                    if (useServerSide(webFault, matcher)) {
                        copyServerSideType(sourceDir, webFault);
                    } else {
                        TypeElement superFault = (TypeElement) ((DeclaredType) webFault.getSuperclass()).asElement();
                        if (superFault != null && allFaults.containsKey(superFault.getQualifiedName().toString()) && allFaults.get(superFault.getQualifiedName().toString()).isImplicitSchemaElement()) {
                            model.put("superFault", allFaults.get(superFault.getQualifiedName().toString()));
                        } else {
                            model.remove("superFault");
                        }
                        model.put("fault", webFault);
                        processTemplate(getTemplateURL("client-web-fault.fmt"), model);
                    }
                }
            }
            for (SchemaInfo schemaInfo : this.jaxbModule.getJaxbContext().getSchemas().values()) {
                for (TypeDefinition typeDefinition : schemaInfo.getTypeDefinitions()) {
                    if (facetFilter.accept(typeDefinition)) {
                        if (useServerSide(typeDefinition, matcher)) {
                            copyServerSideType(sourceDir, typeDefinition);
                        } else {
                            model.put("rootEl", this.jaxbModule.getJaxbContext().findElementDeclaration(typeDefinition));
                            model.put("type", typeDefinition);
                            URL template = typeDefinition.isEnum() ? typeDefinition instanceof QNameEnumTypeDefinition ? getTemplateURL("client-qname-enum-type.fmt") : getTemplateURL("client-enum-type.fmt") : typeDefinition.isSimple() ? getTemplateURL("client-simple-type.fmt") : getTemplateURL("client-complex-type.fmt");
                            processTemplate(template, model);
                        }
                    }
                }
                for (Registry registry : schemaInfo.getRegistries()) {
                    model.put("registry", registry);
                    processTemplate(getTemplateURL("client-registry.fmt"), model);
                }
            }
        } catch (IOException e) {
            throw new EnunciateException(e);
        } catch (TemplateException e) {
            throw new EnunciateException(e);
        }
    } else {
        info("Skipping generation of Java client sources as everything appears up-to-date...");
    }
    context.setProperty(LIRBARY_DESCRIPTION_PROPERTY, readLibraryDescription(model));
    return sourceDir;
}
Also used : QNameEnumTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.QNameEnumTypeDefinition) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition) QNameEnumTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.QNameEnumTypeDefinition) EnunciateException(com.webcohesion.enunciate.EnunciateException) SchemaInfo(com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo) TemplateException(freemarker.template.TemplateException) TypeElement(javax.lang.model.element.TypeElement) Registry(com.webcohesion.enunciate.modules.jaxb.model.Registry) EnunciateJaxbContext(com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext) AntPatternMatcher(com.webcohesion.enunciate.util.AntPatternMatcher) JavaFileObject(javax.tools.JavaFileObject) WsdlInfo(com.webcohesion.enunciate.modules.jaxws.WsdlInfo)

Example 9 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class JavaXMLClientModule method readLibraryDescription.

/**
 * Reads a resource into string form.
 *
 * @return The string form of the resource.
 */
protected String readLibraryDescription(Map<String, Object> model) {
    model.put("sample_service_method", findExampleWebMethod());
    model.put("sample_resource", findExampleResourceMethod());
    model.put("mediaTypeFor", new MediaTypeForMethod());
    URL res = JavaXMLClientModule.class.getResource("library_description.fmt");
    try {
        return processTemplate(res, model);
    } catch (TemplateException e) {
        throw new EnunciateException(e);
    } catch (IOException e) {
        throw new EnunciateException(e);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) EnunciateException(com.webcohesion.enunciate.EnunciateException) URL(java.net.URL)

Example 10 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class EnumTypeDefinition method loadEnumValues.

protected List<EnumValue> loadEnumValues() {
    List<VariableElement> enumConstants = enumValues();
    List<EnumValue> enumValueMap = new ArrayList<EnumValue>();
    HashSet<String> enumValues = new HashSet<String>(enumConstants.size());
    for (VariableElement enumConstant : enumConstants) {
        if (isIgnored(enumConstant)) {
            continue;
        }
        String value = enumConstant.getSimpleName().toString();
        if (context.isHonorJaxb()) {
            XmlEnumValue enumValue = enumConstant.getAnnotation(XmlEnumValue.class);
            if (enumValue != null) {
                value = enumValue.value();
            }
        }
        if (context.isHonorGson()) {
            AnnotationValue av = JacksonUtil.findAnnotationValueByName(JacksonUtil.findAnnotationByClassName(enumConstant.getAnnotationMirrors(), JacksonUtil.GSON_SERIALIZED_NAME_CLASS), "value");
            if (av != null) {
                value = (String) av.getValue();
            }
        }
        JsonProperty jsonProperty = enumConstant.getAnnotation(JsonProperty.class);
        if (jsonProperty != null) {
            value = jsonProperty.value();
        }
        if (!enumValues.add(value)) {
            throw new EnunciateException(getQualifiedName() + ": duplicate enum value: " + value);
        }
        enumValueMap.add(new EnumValue(this, enumConstant, enumConstant.getSimpleName().toString(), value));
    }
    return enumValueMap;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) EnunciateException(com.webcohesion.enunciate.EnunciateException) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue) AnnotationValue(javax.lang.model.element.AnnotationValue) VariableElement(javax.lang.model.element.VariableElement) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue)

Aggregations

EnunciateException (com.webcohesion.enunciate.EnunciateException)53 URL (java.net.URL)21 TemplateException (freemarker.template.TemplateException)19 IOException (java.io.IOException)16 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)12 File (java.io.File)12 FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)11 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)8 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)7 TypeElement (javax.lang.model.element.TypeElement)7 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)6 FindRootElementMethod (com.webcohesion.enunciate.modules.jaxb.util.FindRootElementMethod)6 IsFacetExcludedMethod (com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod)6 VariableElement (javax.lang.model.element.VariableElement)6 EnunciateJacksonContext (com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext)5 TypeDefinition (com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)5 EnunciateJackson1Context (com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context)5 SchemaInfo (com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)5 AccessorOverridesAnotherMethod (com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod)5 FileDirective (com.webcohesion.enunciate.util.freemarker.FileDirective)5