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);
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations