[ds] add ds processor
authorGuillaume Nodet <gnodet@apache.org>
Fri, 15 Apr 2016 16:27:18 +0000 (18:27 +0200)
committerGuillaume Nodet <gnodet@apache.org>
Fri, 15 Apr 2016 16:27:18 +0000 (18:27 +0200)
karaf-boot-starters/karaf-boot-starter-ds/pom.xml
karaf-boot-starters/karaf-boot-starter-ds/src/main/java/org/apache/karaf/boot/ds/impl/DsProcessor.java [new file with mode: 0644]
karaf-boot-starters/karaf-boot-starter-ds/src/main/resources/META-INF/services/javax.annotation.processing.Processor [new file with mode: 0644]

index 09eb983..331c8e2 100644 (file)
         </dependency>
     </dependencies>
 
+    <build>
+        <plugins>
+            <plugin>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <compilerArgument>-proc:none</compilerArgument>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
 </project>
\ No newline at end of file
diff --git a/karaf-boot-starters/karaf-boot-starter-ds/src/main/java/org/apache/karaf/boot/ds/impl/DsProcessor.java b/karaf-boot-starters/karaf-boot-starter-ds/src/main/java/org/apache/karaf/boot/ds/impl/DsProcessor.java
new file mode 100644 (file)
index 0000000..49baa70
--- /dev/null
@@ -0,0 +1,72 @@
+package org.apache.karaf.boot.ds.impl;
+
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.RoundEnvironment;
+import javax.lang.model.element.TypeElement;
+import javax.tools.Diagnostic.Kind;
+import javax.tools.FileObject;
+import javax.tools.StandardLocation;
+import java.io.CharArrayWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.HashSet;
+import java.util.Set;
+
+public class DsProcessor extends AbstractProcessor {
+
+    boolean hasRun;
+
+    public DsProcessor() {
+    }
+
+    @Override
+    public Set<String> getSupportedAnnotationTypes() {
+        Set<String> set = new HashSet<String>();
+        set.add(org.osgi.service.component.annotations.Component.class.getName());
+        return set;
+    }
+
+    @Override
+    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+        if (!hasRun) {
+            hasRun = true;
+            // Add the Karaf embedded package
+            try (PrintWriter w = appendResource("META-INF/org.apache.karaf.boot.bnd")) {
+                w.println("_dsannotations: *");
+            } catch (Exception e) {
+                processingEnv.getMessager().printMessage(Kind.ERROR, "Error: " + e.getMessage());
+            }
+        }
+        return true;
+    }
+
+    private PrintWriter appendResource(String resource) throws IOException {
+        try {
+            FileObject o = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", resource);
+            return new PrintWriter(o.openWriter());
+        } catch (Exception e) {
+            try {
+                FileObject o = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", resource);
+                CharArrayWriter baos = new CharArrayWriter();
+                try (Reader r = o.openReader(true)) {
+                    char[] buf = new char[4096];
+                    int l;
+                    while ((l = r.read(buf)) > 0) {
+                        baos.write(buf, 0, l);
+                    }
+                }
+                o.delete();
+                o = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", resource);
+                Writer w = o.openWriter();
+                w.write(baos.toCharArray());
+                return new PrintWriter(w);
+            } catch (Exception e2) {
+                e2.addSuppressed(e);
+                e2.printStackTrace();
+                throw e2;
+            }
+        }
+    }
+}
diff --git a/karaf-boot-starters/karaf-boot-starter-ds/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/karaf-boot-starters/karaf-boot-starter-ds/src/main/resources/META-INF/services/javax.annotation.processing.Processor
new file mode 100644 (file)
index 0000000..11ef8af
--- /dev/null
@@ -0,0 +1 @@
+org.apache.karaf.boot.ds.impl.DsProcessor