Search in sources :

Example 11 with ClassFinder

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

the class BlueprintFileWriterTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    ClassFinder classFinder = new ClassFinder(BlueprintFileWriterTest.class.getClassLoader());
    long start = System.currentTimeMillis();
    Set<Class<?>> beanClasses = findClasses(classFinder, Arrays.asList(MyBean1.class.getPackage().getName(), ReferenceListenerToProduceWithoutAnnotation.class.getPackage().getName()));
    Set<String> namespaces = new HashSet<>(Arrays.asList(NS_JPA, NS_TX1));
    Map<String, String> customParameters = new HashMap<>();
    customParameters.put("ex.t", "1");
    customParameters.put("example.p1", "v1");
    customParameters.put("example.p2", "v2");
    BlueprintConfigurationImpl blueprintConfiguration = new BlueprintConfigurationImpl(namespaces, null, customParameters);
    Blueprint blueprint = new Blueprint(blueprintConfiguration, beanClasses);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    new BlueprintFileWriter(os).write(blueprint);
    xmlAsBytes = os.toByteArray();
    System.out.println("Generation took " + (System.currentTimeMillis() - start) + " millis");
    System.out.println(new String(xmlAsBytes, "UTF-8"));
    document = readToDocument(xmlAsBytes, false);
    xpath = XPathFactory.newInstance().newXPath();
}
Also used : HashMap(java.util.HashMap) Blueprint(org.apache.aries.blueprint.plugin.model.Blueprint) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ReferenceListenerToProduceWithoutAnnotation(org.apache.aries.blueprint.plugin.test.referencelistener.ReferenceListenerToProduceWithoutAnnotation) ClassFinder(org.apache.xbean.finder.ClassFinder) BeforeClass(org.junit.BeforeClass) MyBean1(org.apache.aries.blueprint.plugin.test.MyBean1) HashSet(java.util.HashSet) BeforeClass(org.junit.BeforeClass)

Example 12 with ClassFinder

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

the class GenerateMojo method createProjectScopeFinder.

private ClassFinder createProjectScopeFinder() throws MalformedURLException {
    List<URL> urls = new ArrayList<>();
    urls.add(new File(project.getBuild().getOutputDirectory()).toURI().toURL());
    for (Object artifactO : project.getArtifacts()) {
        Artifact artifact = (Artifact) artifactO;
        File file = artifact.getFile();
        if (file != null) {
            urls.add(file.toURI().toURL());
        }
    }
    ClassLoader loader = new URLClassLoader(urls.toArray(new URL[urls.size()]), getClass().getClassLoader());
    return new ClassFinder(loader, urls);
}
Also used : URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) ClassFinder(org.apache.xbean.finder.ClassFinder) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL) Artifact(org.apache.maven.artifact.Artifact)

Example 13 with ClassFinder

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

the class GenerateHelpMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        if (!FORMAT_DOCBX.equals(format) && !FORMAT_CONF.equals(format) && !FORMAT_ASCIIDOC.equals(format)) {
            throw new MojoFailureException("Unsupported format: " + format + ". Supported formats are: asciidoc, docbx, or conf.");
        }
        if (!targetFolder.exists()) {
            targetFolder.mkdirs();
        }
        ClassFinder finder = createFinder(classLoader);
        List<Class<?>> classes = finder.findAnnotatedClasses(Command.class);
        if (classes.isEmpty()) {
            throw new MojoFailureException("No command found");
        }
        CommandHelpPrinter helpPrinter = null;
        if (FORMAT_ASCIIDOC.equals(format)) {
            helpPrinter = new AsciiDoctorCommandHelpPrinter();
        }
        if (FORMAT_CONF.equals(format)) {
            helpPrinter = new UserConfCommandHelpPrinter();
        }
        if (FORMAT_DOCBX.equals(format)) {
            helpPrinter = new DocBookCommandHelpPrinter();
        }
        Map<String, Set<String>> commands = new TreeMap<>();
        String commandSuffix = null;
        if (FORMAT_ASCIIDOC.equals(format)) {
            commandSuffix = "adoc";
        }
        if (FORMAT_CONF.equals(format)) {
            commandSuffix = "conf";
        }
        if (FORMAT_DOCBX.equals(format)) {
            commandSuffix = "xml";
        }
        for (Class<?> clazz : classes) {
            try {
                Action action = (Action) clazz.newInstance();
                Command cmd = clazz.getAnnotation(Command.class);
                // skip the *-help command
                if (cmd.scope().equals("*"))
                    continue;
                File output = new File(targetFolder, cmd.scope() + "-" + cmd.name() + "." + commandSuffix);
                FileOutputStream outStream = new FileOutputStream(output);
                PrintStream out = new PrintStream(outStream);
                helpPrinter.printHelp(action, out, includeHelpOption);
                out.close();
                outStream.close();
                commands.computeIfAbsent(cmd.scope(), k -> new TreeSet<>()).add(cmd.name());
                getLog().info("Found command: " + cmd.scope() + ":" + cmd.name());
            } catch (Exception e) {
                getLog().warn("Unable to write help for " + clazz.getName(), e);
            }
        }
        String overViewSuffix = null;
        if (FORMAT_ASCIIDOC.equals(format)) {
            overViewSuffix = "adoc";
        }
        if (FORMAT_CONF.equals(format)) {
            overViewSuffix = "conf";
        }
        if (FORMAT_DOCBX.equals(format)) {
            overViewSuffix = "xml";
        }
        PrintStream writer = new PrintStream(new FileOutputStream(new File(targetFolder, "commands." + overViewSuffix)));
        helpPrinter.printOverview(commands, writer);
        writer.close();
    } catch (Exception e) {
        throw new MojoExecutionException("Error building commands help", e);
    }
}
Also used : PrintStream(java.io.PrintStream) URL(java.net.URL) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) Parameter(org.apache.maven.plugins.annotations.Parameter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ClassFinder(org.apache.xbean.finder.ClassFinder) File(java.io.File) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Command(org.apache.karaf.shell.api.action.Command) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Action(org.apache.karaf.shell.api.action.Action) Mojo(org.apache.maven.plugins.annotations.Mojo) URLClassLoader(java.net.URLClassLoader) List(java.util.List) TreeMap(java.util.TreeMap) MavenProject(org.apache.maven.project.MavenProject) Map(java.util.Map) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) ResolutionScope(org.apache.maven.plugins.annotations.ResolutionScope) AbstractMojo(org.apache.maven.plugin.AbstractMojo) PrintStream(java.io.PrintStream) Action(org.apache.karaf.shell.api.action.Action) Set(java.util.Set) TreeSet(java.util.TreeSet) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) TreeMap(java.util.TreeMap) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Command(org.apache.karaf.shell.api.action.Command) TreeSet(java.util.TreeSet) FileOutputStream(java.io.FileOutputStream) ClassFinder(org.apache.xbean.finder.ClassFinder) File(java.io.File)

Example 14 with ClassFinder

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

the class GenerateServiceMetadata method createFinder.

private ClassFinder createFinder(String classloaderType) throws Exception {
    ClassFinder finder;
    if ("project".equals(classloaderType)) {
        List<URL> urls = new ArrayList<>();
        urls.add(new File(project.getBuild().getOutputDirectory()).toURI().toURL());
        for (Artifact artifact : project.getArtifacts()) {
            File file = artifact.getFile();
            if (file != null) {
                urls.add(file.toURI().toURL());
                System.out.println("classpath: " + file);
            }
        }
        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) Artifact(org.apache.maven.artifact.Artifact)

Example 15 with ClassFinder

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

the class ManagedMBean method scan.

private void scan(final Object target, final String prefix) {
    final ClassFinder finder = new ClassFinder(Classes.ancestors(target.getClass()));
    final List<Field> fields = finder.findAnnotatedFields(Managed.class);
    for (final Field field : fields) {
        attribute(new FieldMember(field, target, prefix));
    }
    final List<Method> managed = finder.findAnnotatedMethods(Managed.class);
    for (final Method method : managed) {
        final MethodMember member = new MethodMember(method, target, prefix);
        if (!method.getName().matches("(get|is)([A-Z_].*|)")) {
            operationsMap.put(member.getName(), member);
        } else {
            attribute(new MethodMember(method, target, prefix));
        }
    }
    final List<Method> collections = finder.findAnnotatedMethods(ManagedCollection.class);
    for (final Method method : collections) {
        dynamic.add(new MethodMember(method, target, prefix));
    }
}
Also used : Field(java.lang.reflect.Field) ClassFinder(org.apache.xbean.finder.ClassFinder) Method(java.lang.reflect.Method)

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