Search in sources :

Example 1 with EnunciateModule

use of com.webcohesion.enunciate.module.EnunciateModule in project enunciate by stoicflame.

the class TestEnunciateIDLModule method testAgainstFullAPI.

/**
 * Tests the xml artifact generation against the "full" API.
 */
public void testAgainstFullAPI() throws Exception {
    Map<String, String> prefixes = new HashMap<String, String>();
    prefixes.put(FULL_NAMESPACE, "full");
    prefixes.put(DATA_NAMESPACE, "data");
    prefixes.put(CITE_NAMESPACE, "cite");
    Properties testProperties = new Properties();
    testProperties.load(TestEnunciateIDLModule.class.getResourceAsStream("/test.properties"));
    String samplePath = testProperties.getProperty("api.sample.dir");
    assertNotNull(samplePath);
    File sampleDir = new File(samplePath);
    assertTrue(sampleDir.exists());
    Enunciate engine = new Enunciate().addSourceDir(sampleDir).loadConfiguration(TestEnunciateIDLModule.class.getResourceAsStream("test-idl-module-config.xml")).loadDiscoveredModules();
    String cp = System.getProperty("java.class.path");
    String[] path = cp.split(File.pathSeparator);
    List<File> classpath = new ArrayList<File>(path.length);
    for (String element : path) {
        File entry = new File(element);
        if (entry.exists() && !new File(entry, "test.properties").exists()) {
            classpath.add(entry);
        }
    }
    engine.setClasspath(classpath);
    engine.run();
    IDLModule idlModule = null;
    for (EnunciateModule candidate : engine.getModules()) {
        if (candidate instanceof IDLModule) {
            idlModule = (IDLModule) candidate;
            break;
        }
    }
    assertNotNull(idlModule);
    assertNotNull(idlModule.jaxbModule);
    assertNotNull(idlModule.jaxwsModule);
    assertNotNull(idlModule.jaxrsModule);
    final Map<String, String> schemas = new HashMap<String, String>();
    for (SchemaInfo schemaInfo : idlModule.jaxbModule.getJaxbContext().getSchemas().values()) {
        assertNotNull(schemaInfo.getSchemaFile());
        assertTrue(schemaInfo.getSchemaFile() instanceof JaxbSchemaFile);
        String filename = ((JaxbSchemaFile) schemaInfo.getSchemaFile()).filename;
        assertNotNull(filename);
        if (prefixes.containsKey(schemaInfo.getNamespace())) {
            assertEquals(prefixes.get(schemaInfo.getNamespace()) + ".xsd", filename);
        }
        StringWriter schemaOut = new StringWriter();
        ((JaxbSchemaFile) schemaInfo.getSchemaFile()).writeTo(schemaOut);
        schemaOut.flush();
        schemas.put(filename, schemaOut.toString());
    }
    WsdlInfo fullWsdlInfo = idlModule.jaxwsModule.getJaxwsContext().getWsdls().get(FULL_NAMESPACE);
    assertNotNull(fullWsdlInfo);
    assertEquals("full.wsdl", fullWsdlInfo.getFilename());
    assertNotNull(fullWsdlInfo.getWsdlFile());
    assertTrue(fullWsdlInfo.getWsdlFile() instanceof JaxwsWsdlFile);
    JaxwsWsdlFile fullWsdl = (JaxwsWsdlFile) fullWsdlInfo.getWsdlFile();
    assertEquals("full.wsdl", fullWsdl.filename);
    final StringWriter output = new StringWriter();
    fullWsdl.writeTo(output);
    output.flush();
    // make sure the wsdl is built correctly
    WSDLReader wsdlReader = WSDLFactory.newInstance().newWSDLReader();
    Definition definition = wsdlReader.readWSDL(new InMemoryWSDLLocator(output, schemas));
    assertEquals(FULL_NAMESPACE, definition.getTargetNamespace());
    Types types = definition.getTypes();
    List extensibilityElements = types.getExtensibilityElements();
    assertEquals(1, extensibilityElements.size());
    ExtensibilityElement ee = (ExtensibilityElement) extensibilityElements.get(0);
    assertEquals(new QName(W3C_XML_SCHEMA_NS_URI, "schema"), ee.getElementType());
    Schema schema = (Schema) ee;
    Map imports = schema.getImports();
    assertEquals(3, imports.size());
    assertNotNull(imports.get(DATA_NAMESPACE));
    assertNotNull(imports.get(CITE_NAMESPACE));
    assertNotNull(imports.get(null));
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    Element schemaElement = schema.getElement();
    // these namespaces need to be explicitly added because the transformer won't see any references to them...
    schemaElement.setAttribute("xmlns:data", DATA_NAMESPACE);
    schemaElement.setAttribute("xmlns:cite", CITE_NAMESPACE);
    schemaElement.setAttribute("xmlns:full", FULL_NAMESPACE);
    // write out the wsdl schema to its xml form so we can parse it with XSOM...
    DOMSource source = new DOMSource(schemaElement);
    StringWriter fullSchemaOutput = new StringWriter();
    StreamResult result = new StreamResult(fullSchemaOutput);
    transformer.transform(source, result);
    fullSchemaOutput.flush();
    // set up the XSOM Parser.
    final InputSource fullSource = new InputSource(new StringReader(fullSchemaOutput.toString()));
    fullSource.setSystemId("file:/");
    XSOMParser parser = new XSOMParser(new JAXPParser());
    // throw all errors and warnings.
    parser.setErrorHandler(new ThrowEverythingHandler());
    parser.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            String xsd = schemas.get(systemId.substring("file:/".length()));
            if (xsd == null) {
                return null;
            }
            InputSource source = new InputSource(new StringReader(xsd));
            source.setSystemId("file:/");
            return source;
        }
    });
    // make sure the schema included in the wsdl is correct.
    parser.parse(fullSource);
    XSSchemaSet schemaSet = parser.getResult();
    XSSchema wsdlSchema = schemaSet.getSchema(FULL_NAMESPACE);
    assertNotNull(wsdlSchema);
    // make sure the data schema is imported and correct.
    XSSchema dataSchema = schemaSet.getSchema(DATA_NAMESPACE);
    assertNotNull(dataSchema);
    assertDataSchemaStructure(dataSchema);
    // make sure the cite schema is imported and correct.
    XSSchema citeSchema = schemaSet.getSchema(CITE_NAMESPACE);
    assertNotNull(citeSchema);
    assertCiteSchemaStructure(citeSchema);
    // now verify the rest of the WSDL...
    assertWebServiceDefinition(definition);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) Schema(javax.wsdl.extensions.schema.Schema) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) Element(org.w3c.dom.Element) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) Enunciate(com.webcohesion.enunciate.Enunciate) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) SchemaInfo(com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo) EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) TransformerFactory(javax.xml.transform.TransformerFactory) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) StreamResult(javax.xml.transform.stream.StreamResult) QName(javax.xml.namespace.QName) JAXPParser(com.sun.xml.xsom.parser.JAXPParser) IOException(java.io.IOException) File(java.io.File) WsdlInfo(com.webcohesion.enunciate.modules.jaxws.WsdlInfo) WSDLReader(javax.wsdl.xml.WSDLReader)

Example 2 with EnunciateModule

use of com.webcohesion.enunciate.module.EnunciateModule in project enunciate by stoicflame.

the class EnunciateTask method execute.

/**
 * Executes the enunciate task.
 */
@Override
public void execute() throws BuildException {
    if (basedir == null) {
        throw new BuildException("A base directory must be specified.");
    }
    if (buildDir == null) {
        throw new BuildException("A build directory must be specified.");
    }
    try {
        Enunciate enunciate = new Enunciate();
        // set up the logger.
        enunciate.setLogger(new AntEnunciateLogger());
        // set the build dir.
        this.buildDir.mkdirs();
        enunciate.setBuildDir(this.buildDir);
        // add the source files.
        DirectoryScanner scanner = getDirectoryScanner(basedir);
        scanner.scan();
        Set<File> sourceFiles = new TreeSet<File>();
        for (String file : scanner.getIncludedFiles()) {
            sourceFiles.add(new File(basedir, file));
        }
        enunciate.setSourceFiles(sourceFiles);
        // load the config.
        if (this.configFile != null && this.configFile.exists()) {
            getProject().log("[ENUNCIATE] Using enunciate configuration at " + this.configFile.getAbsolutePath());
            ExpandProperties reader = new ExpandProperties(new FileReader(this.configFile));
            reader.setProject(getProject());
            enunciate.loadConfiguration(reader);
            enunciate.getConfiguration().setConfigFile(this.configFile);
        }
        if (classpath != null) {
            String[] filenames = this.classpath.list();
            List<File> cp = new ArrayList<File>(filenames.length);
            for (String filename : filenames) {
                File file = new File(filename);
                if (file.exists()) {
                    cp.add(file);
                }
            }
            enunciate.setClasspath(cp);
            // set up the classloader for the Enunciate invocation.
            AntClassLoader loader = new AntClassLoader(Enunciate.class.getClassLoader(), getProject(), this.classpath, true);
            Thread.currentThread().setContextClassLoader(loader);
            ServiceLoader<EnunciateModule> moduleLoader = ServiceLoader.load(EnunciateModule.class, loader);
            for (EnunciateModule module : moduleLoader) {
                enunciate.addModule(module);
            }
        }
        if (sourcepath != null) {
            String[] filenames = this.sourcepath.list();
            List<File> cp = new ArrayList<File>(filenames.length);
            for (String filename : filenames) {
                File file = new File(filename);
                if (file.exists()) {
                    cp.add(file);
                }
            }
            enunciate.setSourcepath(cp);
        }
        List<String> compilerArgs = new ArrayList<String>();
        String sourceVersion = this.javacSourceVersion;
        if (sourceVersion != null) {
            compilerArgs.add("-source");
            compilerArgs.add(sourceVersion);
        }
        String targetVersion = this.javacTargetVersion;
        if (targetVersion != null) {
            compilerArgs.add("-target");
            compilerArgs.add(targetVersion);
        }
        String sourceEncoding = this.encoding;
        if (sourceEncoding != null) {
            compilerArgs.add("-encoding");
            compilerArgs.add(sourceEncoding);
        }
        enunciate.getCompilerArgs().addAll(compilerArgs);
        for (JavacArgument javacArgument : this.javacArguments) {
            enunciate.getCompilerArgs().add(javacArgument.getArgument());
        }
        for (Export export : exports) {
            enunciate.addExport(export.getArtifactId(), export.getDestination());
        }
        enunciate.run();
    } catch (IOException e) {
        throw new BuildException(e);
    } catch (EnunciateException e) {
        throw new BuildException(e);
    }
}
Also used : EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) AntClassLoader(org.apache.tools.ant.AntClassLoader) IOException(java.io.IOException) ExpandProperties(org.apache.tools.ant.filters.ExpandProperties) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileReader(java.io.FileReader) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 3 with EnunciateModule

use of com.webcohesion.enunciate.module.EnunciateModule in project enunciate by stoicflame.

the class AggregatedApiRegistry method getServiceApis.

@Override
public List<ServiceApi> getServiceApis(ApiRegistrationContext context) {
    ArrayList<ServiceApi> serviceApis = new ArrayList<ServiceApi>();
    List<EnunciateModule> modules = enunciate.getModules();
    for (EnunciateModule module : modules) {
        if (module.isEnabled() && module instanceof ApiRegistryProviderModule) {
            serviceApis.addAll(((ApiRegistryProviderModule) module).getApiRegistry().getServiceApis(context));
        }
    }
    return serviceApis;
}
Also used : ApiRegistryProviderModule(com.webcohesion.enunciate.module.ApiRegistryProviderModule) EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) ServiceApi(com.webcohesion.enunciate.api.services.ServiceApi) ArrayList(java.util.ArrayList)

Example 4 with EnunciateModule

use of com.webcohesion.enunciate.module.EnunciateModule in project enunciate by stoicflame.

the class AggregatedApiRegistry method getResourceApis.

@Override
public List<ResourceApi> getResourceApis(ApiRegistrationContext context) {
    ArrayList<ResourceApi> resourceApis = new ArrayList<ResourceApi>();
    List<EnunciateModule> modules = enunciate.getModules();
    for (EnunciateModule module : modules) {
        if (module.isEnabled() && module instanceof ApiRegistryProviderModule) {
            resourceApis.addAll(((ApiRegistryProviderModule) module).getApiRegistry().getResourceApis(context));
        }
    }
    return resourceApis;
}
Also used : ApiRegistryProviderModule(com.webcohesion.enunciate.module.ApiRegistryProviderModule) ResourceApi(com.webcohesion.enunciate.api.resources.ResourceApi) EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) ArrayList(java.util.ArrayList)

Example 5 with EnunciateModule

use of com.webcohesion.enunciate.module.EnunciateModule in project enunciate by stoicflame.

the class Enunciate method buildModuleGraph.

protected Graph<String, DefaultEdge> buildModuleGraph(Map<String, ? extends EnunciateModule> modules) {
    Graph<String, DefaultEdge> graph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
    for (String moduleName : modules.keySet()) {
        graph.addVertex(moduleName);
    }
    for (EnunciateModule module : modules.values()) {
        List<DependencySpec> dependencies = module.getDependencySpecifications();
        if (dependencies != null && !dependencies.isEmpty()) {
            for (DependencySpec dependency : dependencies) {
                for (EnunciateModule other : modules.values()) {
                    if (dependency.accept(other)) {
                        graph.addEdge(other.getName(), module.getName());
                    }
                }
                if (!dependency.isFulfilled()) {
                    throw new EnunciateException(String.format("Unfulfilled dependency %s of module %s.", dependency.toString(), module.getName()));
                }
            }
        }
    }
    for (EnunciateModule module : modules.values()) {
        if (module instanceof DependingModuleAwareModule) {
            Set<DefaultEdge> edges = graph.outgoingEdgesOf(module.getName());
            Set<String> dependingModules = new TreeSet<String>();
            for (DefaultEdge edge : edges) {
                dependingModules.add(graph.getEdgeTarget(edge));
            }
            ((DependingModuleAwareModule) module).acknowledgeDependingModules(dependingModules);
        }
        if (module instanceof ApiRegistryAwareModule) {
            ((ApiRegistryAwareModule) module).setApiRegistry(this.apiRegistry);
        }
    }
    CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(graph);
    Set<String> modulesInACycle = cycleDetector.findCycles();
    if (!modulesInACycle.isEmpty()) {
        StringBuilder errorMessage = new StringBuilder("Module cycle detected: ");
        java.util.Iterator<String> subcycle = cycleDetector.findCyclesContainingVertex(modulesInACycle.iterator().next()).iterator();
        while (subcycle.hasNext()) {
            String next = subcycle.next();
            errorMessage.append(next);
            if (subcycle.hasNext()) {
                errorMessage.append(" --> ");
            }
        }
        throw new EnunciateException(errorMessage.toString());
    }
    return graph;
}
Also used : EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) DefaultEdge(org.jgrapht.graph.DefaultEdge) ApiRegistryAwareModule(com.webcohesion.enunciate.module.ApiRegistryAwareModule) CycleDetector(org.jgrapht.alg.cycle.CycleDetector) DependingModuleAwareModule(com.webcohesion.enunciate.module.DependingModuleAwareModule) TreeSet(java.util.TreeSet) DependencySpec(com.webcohesion.enunciate.module.DependencySpec)

Aggregations

EnunciateModule (com.webcohesion.enunciate.module.EnunciateModule)9 ApiRegistryProviderModule (com.webcohesion.enunciate.module.ApiRegistryProviderModule)3 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Enunciate (com.webcohesion.enunciate.Enunciate)2 TreeSet (java.util.TreeSet)2 JAXPParser (com.sun.xml.xsom.parser.JAXPParser)1 XSOMParser (com.sun.xml.xsom.parser.XSOMParser)1 EnunciateConfiguration (com.webcohesion.enunciate.EnunciateConfiguration)1 EnunciateException (com.webcohesion.enunciate.EnunciateException)1 Syntax (com.webcohesion.enunciate.api.datatype.Syntax)1 ResourceApi (com.webcohesion.enunciate.api.resources.ResourceApi)1 ServiceApi (com.webcohesion.enunciate.api.services.ServiceApi)1 AnnotationMirrorDecoration (com.webcohesion.enunciate.javac.decorations.AnnotationMirrorDecoration)1 DecoratedProcessingEnvironment (com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)1 ElementDecoration (com.webcohesion.enunciate.javac.decorations.ElementDecoration)1 TypeMirrorDecoration (com.webcohesion.enunciate.javac.decorations.TypeMirrorDecoration)1 ApiRegistryAwareModule (com.webcohesion.enunciate.module.ApiRegistryAwareModule)1 ContextModifyingModule (com.webcohesion.enunciate.module.ContextModifyingModule)1