Search in sources :

Example 1 with ClassFinder

use of org.apache.xbean.finder.ClassFinder in project aries by apache.

the class EnableAnnotationTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    ClassFinder classFinder = new ClassFinder(EnableAnnotationTest.class.getClassLoader());
    beanClasses = findClasses(classFinder, Arrays.asList(TxBean.class.getPackage().getName()));
}
Also used : ClassFinder(org.apache.xbean.finder.ClassFinder) BeforeClass(org.junit.BeforeClass)

Example 2 with ClassFinder

use of org.apache.xbean.finder.ClassFinder in project aries by apache.

the class GenerateMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    List<String> toScan = getPackagesToScan();
    if (!sourcesChanged()) {
        getLog().info("Skipping blueprint generation because source files were not changed");
        return;
    }
    BlueprintConfigurationImpl blueprintConfiguration = new BlueprintConfigurationImpl(namespaces, defaultActivation, customParameters);
    try {
        ClassFinder classFinder = createProjectScopeFinder();
        Set<Class<?>> classes = FilteredClassFinder.findClasses(classFinder, toScan);
        Blueprint blueprint = new Blueprint(blueprintConfiguration, classes);
        writeBlueprintIfNeeded(blueprint);
    } catch (Exception e) {
        throw new MojoExecutionException("Error building commands help", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Blueprint(org.apache.aries.blueprint.plugin.model.Blueprint) ClassFinder(org.apache.xbean.finder.ClassFinder) MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 3 with ClassFinder

use of org.apache.xbean.finder.ClassFinder 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)

Example 4 with ClassFinder

use of org.apache.xbean.finder.ClassFinder in project karaf by apache.

the class GenerateHelpMojo method createFinder.

private ClassFinder createFinder(String classloaderType) throws Exception {
    ClassFinder finder;
    if ("project".equals(classloaderType)) {
        List<URL> urls = new ArrayList<>();
        for (Object object : project.getCompileClasspathElements()) {
            String path = (String) object;
            urls.add(new File(path).toURI().toURL());
        }
        ClassLoader loader = new URLClassLoader(urls.toArray(new URL[urls.size()]), getClass().getClassLoader());
        finder = new ClassFinder(loader, urls);
    } else if ("plugin".equals(classLoader)) {
        finder = new ClassFinder(getClass().getClassLoader());
    } else {
        throw new MojoFailureException("classLoader attribute must be 'project' or 'plugin'");
    }
    return finder;
}
Also used : URLClassLoader(java.net.URLClassLoader) ClassFinder(org.apache.xbean.finder.ClassFinder) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL)

Example 5 with ClassFinder

use of org.apache.xbean.finder.ClassFinder in project tomee by apache.

the class InterceptorData method scan.

public static InterceptorData scan(final Class<?> clazz) {
    final InterceptorData model = CACHE.get(clazz);
    if (model != null) {
        final InterceptorData data = new InterceptorData(clazz);
        data.aroundInvoke.addAll(model.getAroundInvoke());
        data.postConstruct.addAll(model.getPostConstruct());
        data.preDestroy.addAll(model.getPreDestroy());
        data.postActivate.addAll(model.getPostActivate());
        data.prePassivate.addAll(model.getPrePassivate());
        data.afterBegin.addAll(model.getAfterBegin());
        data.beforeCompletion.addAll(model.getBeforeCompletion());
        data.afterCompletion.addAll(model.getAfterCompletion());
        data.aroundTimeout.addAll(model.getAroundTimeout());
        return data;
    }
    final ClassFinder finder = new ClassFinder(clazz);
    final InterceptorData data = new InterceptorData(clazz);
    add(finder, data.aroundInvoke, AroundInvoke.class);
    add(finder, data.postConstruct, PostConstruct.class);
    add(finder, data.preDestroy, PreDestroy.class);
    add(finder, data.postActivate, PostActivate.class);
    add(finder, data.prePassivate, PrePassivate.class);
    add(finder, data.afterBegin, AfterBegin.class);
    add(finder, data.beforeCompletion, BeforeCompletion.class);
    add(finder, data.afterCompletion, AfterCompletion.class);
    add(finder, data.aroundTimeout, AroundTimeout.class);
    return data;
}
Also used : ClassFinder(org.apache.xbean.finder.ClassFinder)

Aggregations

ClassFinder (org.apache.xbean.finder.ClassFinder)24 Method (java.lang.reflect.Method)10 ArrayList (java.util.ArrayList)10 URL (java.net.URL)8 File (java.io.File)6 HashMap (java.util.HashMap)6 Field (java.lang.reflect.Field)5 List (java.util.List)5 MojoFailureException (org.apache.maven.plugin.MojoFailureException)5 URLClassLoader (java.net.URLClassLoader)4 Map (java.util.Map)4 Arrays.asList (java.util.Arrays.asList)3 LinkedList (java.util.LinkedList)3 Properties (java.util.Properties)3 MalformedURLException (java.net.MalformedURLException)2 Iterator (java.util.Iterator)2 TreeSet (java.util.TreeSet)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 EJB (javax.ejb.EJB)2 NamingException (javax.naming.NamingException)2