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