use of org.apache.karaf.util.tracker.annotation.Services 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);
}
}
Aggregations