Search in sources :

Example 1 with Managed

use of org.apache.karaf.util.tracker.annotation.Managed in project karaf by apache.

the class GenerateServiceMetadata method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        boolean addSourceDirectory = false;
        StringBuilder requirements = new StringBuilder();
        StringBuilder capabilities = new StringBuilder();
        ClassFinder finder = createFinder(classLoader);
        List<Class<?>> classes = finder.findAnnotatedClasses(Services.class);
        List<Class<?>> activators = new ArrayList<>();
        for (Class<?> clazz : classes) {
            URL classUrl = clazz.getClassLoader().getResource(clazz.getName().replace('.', '/') + ".class");
            URL outputDirectoryUrl = new File(project.getBuild().getOutputDirectory()).toURI().toURL();
            if (classUrl == null || !classUrl.getPath().startsWith(outputDirectoryUrl.getPath())) {
                System.out.println("Ignoring " + classUrl);
                continue;
            }
            if (BundleActivator.class.isAssignableFrom(clazz)) {
                activators.add(clazz);
            }
            Properties props = new Properties();
            Services services = clazz.getAnnotation(Services.class);
            if (services != null) {
                for (RequireService req : services.requires()) {
                    String fltWithClass = combine(req.filter(), "(objectClass=" + req.value().getName() + ")");
                    addServiceReq(requirements, fltWithClass);
                    props.setProperty(req.value().getName(), req.filter());
                }
                for (ProvideService cap : services.provides()) {
                    addServiceCap(capabilities, cap);
                }
            }
            Managed managed = clazz.getAnnotation(Managed.class);
            if (managed != null) {
                props.setProperty("pid", managed.value());
            }
            File file = new File(outputDirectory, "OSGI-INF/karaf-tracker/" + clazz.getName());
            file.getParentFile().mkdirs();
            try (OutputStream os = buildContext.newFileOutputStream(file)) {
                props.store(os, null);
            }
            addSourceDirectory = true;
        }
        if (addSourceDirectory) {
            Resource resource = new Resource();
            resource.setDirectory(outputDirectory);
            project.addResource(resource);
        }
        project.getProperties().setProperty(requirementsProperty, requirements.toString());
        project.getProperties().setProperty(capabilitiesProperty, capabilities.toString());
        if (activators.size() == 1) {
            project.getProperties().setProperty(activatorProperty, activators.get(0).getName());
        }
        project.getProperties().setProperty("BNDExtension-Private-Package", "org.apache.karaf.util.tracker");
        project.getProperties().setProperty("BNDPrependExtension-Import-Package", "!org.apache.karaf.util.tracker.annotation");
        List<Class<?>> services = finder.findAnnotatedClasses(Service.class);
        Set<String> packages = new TreeSet<>();
        for (Class<?> clazz : services) {
            packages.add(clazz.getPackage().getName());
        }
        if (!packages.isEmpty()) {
            project.getProperties().setProperty("BNDExtension-Karaf-Commands", join(packages, ","));
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error building commands help", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) RequireService(org.apache.karaf.util.tracker.annotation.RequireService) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Resource(org.apache.maven.model.Resource) Properties(java.util.Properties) URL(java.net.URL) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Services(org.apache.karaf.util.tracker.annotation.Services) ProvideService(org.apache.karaf.util.tracker.annotation.ProvideService) TreeSet(java.util.TreeSet) ClassFinder(org.apache.xbean.finder.ClassFinder) File(java.io.File) Managed(org.apache.karaf.util.tracker.annotation.Managed)

Aggregations

File (java.io.File)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 TreeSet (java.util.TreeSet)1 Managed (org.apache.karaf.util.tracker.annotation.Managed)1 ProvideService (org.apache.karaf.util.tracker.annotation.ProvideService)1 RequireService (org.apache.karaf.util.tracker.annotation.RequireService)1 Services (org.apache.karaf.util.tracker.annotation.Services)1 Resource (org.apache.maven.model.Resource)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 ClassFinder (org.apache.xbean.finder.ClassFinder)1