Search in sources :

Example 6 with EnunciateModule

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

the class EnunciateTest method testClasspathScanning.

@Test
public void testClasspathScanning() throws Exception {
    Enunciate enunciate = new Enunciate();
    enunciate.setModules(Collections.singletonList((EnunciateModule) new TestModule("test", new ArrayList<String>())));
    Reflections reflections = enunciate.loadApiReflections(buildTestClasspath());
    Set<String> scannedEntries = reflections.getStore().get(EnunciateReflectionsScanner.class.getSimpleName()).keySet();
    assertTrue(scannedEntries.contains("enunciate.Class1"));
    assertTrue(scannedEntries.contains("enunciate.Class2"));
    assertTrue(scannedEntries.contains("enunciate.Class3"));
    assertTrue(scannedEntries.contains("enunciate/Class1.java"));
    assertEquals(4, scannedEntries.size());
    assertFalse(scannedEntries.isEmpty());
}
Also used : EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) ArrayList(java.util.ArrayList) Reflections(org.reflections.Reflections) Test(org.junit.Test)

Example 7 with EnunciateModule

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

the class EnunciateAnnotationProcessor method init.

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
    super.init(processingEnv);
    // set up the processing environment.
    ArrayList<ElementDecoration> elementDecorations = new ArrayList<ElementDecoration>();
    ArrayList<TypeMirrorDecoration> typeMirrorDecorations = new ArrayList<TypeMirrorDecoration>();
    ArrayList<AnnotationMirrorDecoration> annotationMirrorDecorations = new ArrayList<AnnotationMirrorDecoration>();
    DecoratedProcessingEnvironment processingEnvironment = new DecoratedProcessingEnvironment(processingEnv, elementDecorations, typeMirrorDecorations, annotationMirrorDecorations);
    // construct a context.
    this.context = new EnunciateContext(processingEnvironment, this.enunciate.getLogger(), this.enunciate.getApiRegistry(), this.enunciate.getConfiguration(), this.enunciate.getIncludePatterns(), this.enunciate.getExcludePatterns(), this.enunciate.getClasspath());
    // initialize the modules.
    for (EnunciateModule module : this.enunciate.getModules()) {
        module.init(this.context);
        if (module instanceof ContextModifyingModule) {
            ContextModifyingModule contextModifier = (ContextModifyingModule) module;
            elementDecorations.addAll(contextModifier.getElementDecorations());
            typeMirrorDecorations.addAll(contextModifier.getTypeMirrorDecorations());
            annotationMirrorDecorations.addAll(contextModifier.getAnnotationMirrorDecorations());
        }
    }
}
Also used : EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) ContextModifyingModule(com.webcohesion.enunciate.module.ContextModifyingModule) ElementDecoration(com.webcohesion.enunciate.javac.decorations.ElementDecoration) AnnotationMirrorDecoration(com.webcohesion.enunciate.javac.decorations.AnnotationMirrorDecoration) TypeMirrorDecoration(com.webcohesion.enunciate.javac.decorations.TypeMirrorDecoration) DecoratedProcessingEnvironment(com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)

Example 8 with EnunciateModule

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

the class AggregatedApiRegistry method getSyntaxes.

@Override
public Set<Syntax> getSyntaxes(ApiRegistrationContext context) {
    Set<Syntax> syntaxes = new TreeSet<Syntax>();
    List<EnunciateModule> modules = enunciate.getModules();
    for (EnunciateModule module : modules) {
        if (module.isEnabled() && module instanceof ApiRegistryProviderModule) {
            syntaxes.addAll(((ApiRegistryProviderModule) module).getApiRegistry().getSyntaxes(context));
        }
    }
    return syntaxes;
}
Also used : ApiRegistryProviderModule(com.webcohesion.enunciate.module.ApiRegistryProviderModule) EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) TreeSet(java.util.TreeSet) Syntax(com.webcohesion.enunciate.api.datatype.Syntax)

Example 9 with EnunciateModule

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

the class ConfigMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    if (skipEnunciate) {
        getLog().info("[ENUNCIATE] Skipping enunciate per configuration.");
        return;
    }
    Enunciate enunciate = new Enunciate();
    // set up the logger.
    enunciate.setLogger(new MavenEnunciateLogger());
    // set the build dir.
    enunciate.setBuildDir(this.buildDir);
    // load the config.
    EnunciateConfiguration config = enunciate.getConfiguration();
    File configFile = this.configFile;
    if (configFile == null) {
        configFile = new File(project.getBasedir(), "enunciate.xml");
    }
    if (configFile.exists()) {
        getLog().info("[ENUNCIATE] Using enunciate configuration at " + configFile.getAbsolutePath());
        try {
            loadConfig(enunciate, configFile);
            config.setBase(configFile.getParentFile());
        } catch (Exception e) {
            throw new MojoExecutionException("Problem with enunciate config file " + configFile, e);
        }
    } else if (this.configFile != null) {
        throw new MojoFailureException("Enunciate config file \"" + this.configFile + "\" does not exist");
    } else {
        getLog().debug("[ENUNCIATE] Default config file does not exist");
    }
    // set the default configured label.
    config.setDefaultSlug(project.getArtifactId());
    if (StringUtils.isNotBlank(title)) {
        config.setDefaultTitle(title);
    }
    String defaultDescription = getDefaultDescription();
    if (defaultDescription != null) {
        config.setDefaultDescription(defaultDescription);
    }
    if (project.getVersion() != null && !"".equals(project.getVersion().trim())) {
        config.setDefaultVersion(project.getVersion());
    }
    List contributors = project.getContributors();
    if (contributors != null && !contributors.isEmpty()) {
        List<EnunciateConfiguration.Contact> contacts = new ArrayList<EnunciateConfiguration.Contact>(contributors.size());
        for (Object c : contributors) {
            Contributor contributor = (Contributor) c;
            contacts.add(new EnunciateConfiguration.Contact(contributor.getName(), contributor.getUrl(), contributor.getEmail()));
        }
        config.setDefaultContacts(contacts);
    }
    List licenses = project.getLicenses();
    if (licenses != null && !licenses.isEmpty()) {
        License license = (License) licenses.get(0);
        config.setDefaultApiLicense(new EnunciateConfiguration.License(license.getName(), license.getUrl(), null, null));
    }
    // set the class paths.
    setClasspathAndSourcepath(enunciate);
    // load any modules on the classpath.
    List<URL> pluginClasspath = buildPluginClasspath();
    ServiceLoader<EnunciateModule> moduleLoader = ServiceLoader.load(EnunciateModule.class, new URLClassLoader(pluginClasspath.toArray(new URL[pluginClasspath.size()]), Thread.currentThread().getContextClassLoader()));
    for (EnunciateModule module : moduleLoader) {
        enunciate.addModule(module);
    }
    // set the compiler arguments.
    List<String> compilerArgs = new ArrayList<String>();
    String sourceVersion = findSourceVersion();
    if (sourceVersion != null) {
        compilerArgs.add("-source");
        compilerArgs.add(sourceVersion);
    }
    String targetVersion = findTargetVersion();
    if (targetVersion != null) {
        compilerArgs.add("-target");
        compilerArgs.add(targetVersion);
    }
    String sourceEncoding = this.encoding;
    if (sourceEncoding != null) {
        compilerArgs.add("-encoding");
        compilerArgs.add(sourceEncoding);
    }
    if (this.compilerArgs != null) {
        compilerArgs.addAll(Arrays.asList(this.compilerArgs));
    }
    enunciate.getCompilerArgs().addAll(compilerArgs);
    // includes.
    if (this.includes != null) {
        for (String include : this.includes) {
            enunciate.addInclude(include);
        }
    }
    // excludes.
    if (this.excludes != null) {
        for (String exclude : this.excludes) {
            enunciate.addExclude(exclude);
        }
    }
    // exports.
    if (this.exports != null) {
        for (String exportId : this.exports.keySet()) {
            String filename = this.exports.get(exportId);
            if (filename == null || "".equals(filename)) {
                throw new MojoExecutionException("Invalid (empty or null) filename for export " + exportId + ".");
            }
            File exportFile = new File(filename);
            if (!exportFile.isAbsolute()) {
                exportFile = new File(this.exportsDir, filename);
            }
            enunciate.addExport(exportId, exportFile);
        }
    }
    Set<String> enunciateAddedSourceDirs = new TreeSet<String>();
    List<EnunciateModule> modules = enunciate.getModules();
    if (modules != null) {
        Set<String> projectExtensions = new TreeSet<String>(this.projectExtensions == null ? Collections.<String>emptyList() : Arrays.asList(this.projectExtensions));
        for (EnunciateModule module : modules) {
            // configure the project with the module project extensions.
            if (projectExtensions.contains(module.getName()) && module instanceof ProjectExtensionModule) {
                ProjectExtensionModule extensions = (ProjectExtensionModule) module;
                for (File projectSource : extensions.getProjectSources()) {
                    String sourceDir = projectSource.getAbsolutePath();
                    enunciateAddedSourceDirs.add(sourceDir);
                    if (!project.getCompileSourceRoots().contains(sourceDir)) {
                        getLog().debug("[ENUNCIATE] Adding '" + sourceDir + "' to the compile source roots.");
                        project.addCompileSourceRoot(sourceDir);
                    }
                }
                for (File testSource : extensions.getProjectTestSources()) {
                    project.addTestCompileSourceRoot(testSource.getAbsolutePath());
                }
                for (File resourceDir : extensions.getProjectResourceDirectories()) {
                    Resource restResource = new Resource();
                    restResource.setDirectory(resourceDir.getAbsolutePath());
                    project.addResource(restResource);
                }
                for (File resourceDir : extensions.getProjectTestResourceDirectories()) {
                    Resource resource = new Resource();
                    resource.setDirectory(resourceDir.getAbsolutePath());
                    project.addTestResource(resource);
                }
            }
            applyAdditionalConfiguration(module);
        }
    }
    // add any new source directories to the project.
    Set<File> sourceDirs = new HashSet<File>();
    Collection<String> sourcePaths = this.sources == null || this.sources.length == 0 ? (Collection<String>) project.getCompileSourceRoots() : Arrays.asList(this.sources);
    for (String sourcePath : sourcePaths) {
        File sourceDir = new File(sourcePath);
        if (!enunciateAddedSourceDirs.contains(sourceDir.getAbsolutePath())) {
            sourceDirs.add(sourceDir);
        } else {
            getLog().info("[ENUNCIATE] " + sourceDir + " appears to be added to the source roots by Enunciate.  Excluding from original source roots....");
        }
    }
    for (File sourceDir : sourceDirs) {
        enunciate.addSourceDir(sourceDir);
    }
    postProcessConfig(enunciate);
    try {
        enunciate.run();
    } catch (Exception e) {
        Throwable t = unwrap(e);
        if (t instanceof EnunciateException) {
            throw new MojoExecutionException(t.getMessage(), t);
        }
        throw new MojoExecutionException("Error invoking Enunciate.", e);
    }
    if (this.artifacts != null) {
        for (Artifact projectArtifact : artifacts) {
            if (projectArtifact.getEnunciateArtifactId() == null) {
                getLog().warn("[ENUNCIATE] No enunciate export id specified.  Skipping project artifact...");
                continue;
            }
            com.webcohesion.enunciate.artifacts.Artifact artifact = null;
            for (com.webcohesion.enunciate.artifacts.Artifact enunciateArtifact : enunciate.getArtifacts()) {
                if (projectArtifact.getEnunciateArtifactId().equals(enunciateArtifact.getId()) || enunciateArtifact.getAliases().contains(projectArtifact.getEnunciateArtifactId())) {
                    artifact = enunciateArtifact;
                    break;
                }
            }
            if (artifact != null) {
                try {
                    File tempExportFile = enunciate.createTempFile(project.getArtifactId() + "-" + projectArtifact.getClassifier(), projectArtifact.getArtifactType());
                    artifact.exportTo(tempExportFile, enunciate);
                    projectHelper.attachArtifact(project, projectArtifact.getArtifactType(), projectArtifact.getClassifier(), tempExportFile);
                } catch (IOException e) {
                    throw new MojoExecutionException("Error exporting Enunciate artifact.", e);
                }
            } else {
                getLog().warn("[ENUNCIATE] Enunciate artifact '" + projectArtifact.getEnunciateArtifactId() + "' not found in the project...");
            }
        }
    }
    postProcess(enunciate);
    getPluginContext().put(ConfigMojo.ENUNCIATE_PROPERTY, enunciate);
}
Also used : EnunciateConfiguration(com.webcohesion.enunciate.EnunciateConfiguration) License(org.apache.maven.model.License) Contributor(org.apache.maven.model.Contributor) ProjectExtensionModule(com.webcohesion.enunciate.module.ProjectExtensionModule) URL(java.net.URL) Enunciate(com.webcohesion.enunciate.Enunciate) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Resource(org.apache.maven.model.Resource) IOException(java.io.IOException) EnunciateException(com.webcohesion.enunciate.EnunciateException) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SAXException(org.xml.sax.SAXException) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

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