Convert other starters to new plugin system boot-plugin
authorChristian Schneider <chris@die-schneider.net>
Wed, 25 May 2016 15:25:26 +0000 (17:25 +0200)
committerChristian Schneider <chris@die-schneider.net>
Wed, 25 May 2016 15:25:26 +0000 (17:25 +0200)
16 files changed:
starters/karaf-boot-starter-blueprint/pom.xml
starters/karaf-boot-starter-blueprint/src/main/java/org/apache/karaf/boot/blueprint/impl/BlueprintProcessor.java
starters/karaf-boot-starter-blueprint/src/main/resources/META-INF/services/javax.annotation.processing.Processor [deleted file]
starters/karaf-boot-starter-cdi/pom.xml
starters/karaf-boot-starter-cdi/src/main/java/org/apache/karaf/boot/cdi/impl/CdiProcessor.java
starters/karaf-boot-starter-cdi/src/main/resources/META-INF/services/javax.annotation.processing.Processor [deleted file]
starters/karaf-boot-starter-karaf/pom.xml [deleted file]
starters/karaf-boot-starter-karaf/src/main/java/org/apache/karaf/boot/karaf/impl/KarafProcessor.java [deleted file]
starters/karaf-boot-starter-karaf/src/main/resources/META-INF/services/javax.annotation.processing.Processor [deleted file]
starters/karaf-boot-starter-shell/pom.xml
starters/karaf-boot-starter-shell/src/main/java/org/apache/karaf/boot/shell/impl/ShellProcessor.java
starters/karaf-boot-starter-shell/src/main/resources/META-INF/services/javax.annotation.processing.Processor [deleted file]
starters/karaf-boot-starter-web/pom.xml
starters/plugin-api/src/main/java/org/apache/karaf/boot/plugin/api/StreamFactory.java
starters/pom.xml
tools/karaf-boot-maven-plugin/src/main/java/org/apache/karaf/boot/maven/GenerateMojo.java

index e958058..06f0948 100644 (file)
@@ -1,23 +1,17 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 
-    <!--
-
-        Licensed to the Apache Software Foundation (ASF) under one or more
-        contributor license agreements.  See the NOTICE file distributed with
-        this work for additional information regarding copyright ownership.
-        The ASF licenses this file to You under the Apache License, Version 2.0
-        (the "License"); you may not use this file except in compliance with
-        the License.  You may obtain a copy of the License at
-
-           http://www.apache.org/licenses/LICENSE-2.0
-
-        Unless required by applicable law or agreed to in writing, software
-        distributed under the License is distributed on an "AS IS" BASIS,
-        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-        See the License for the specific language governing permissions and
-        limitations under the License.
-    -->
+    <!-- Licensed to the Apache Software Foundation (ASF) under one or more 
+        contributor license agreements. See the NOTICE file distributed with this 
+        work for additional information regarding copyright ownership. The ASF licenses 
+        this file to You under the Apache License, Version 2.0 (the "License"); you 
+        may not use this file except in compliance with the License. You may obtain 
+        a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless 
+        required by applicable law or agreed to in writing, software distributed 
+        under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 
+        OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
+        the specific language governing permissions and limitations under the License. -->
 
     <modelVersion>4.0.0</modelVersion>
 
             <artifactId>geronimo-atinject_1.0_spec</artifactId>
             <version>1.0</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.boot</groupId>
+            <artifactId>karaf-boot-plugin-api</artifactId>
+            <version>${project.version}</version>
+        </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
index aebcfca..9a8829e 100644 (file)
@@ -1,77 +1,37 @@
 package org.apache.karaf.boot.blueprint.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 org.apache.aries.blueprint.annotation.Bean;
-
-import java.io.ByteArrayOutputStream;
-import java.io.CharArrayWriter;
+import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintWriter;
-import java.io.Reader;
-import java.io.Writer;
-import java.util.HashSet;
-import java.util.Set;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
-public class BlueprintProcessor extends AbstractProcessor {
-
-    boolean hasRun;
-
-    public BlueprintProcessor() {
-    }
+import org.apache.aries.blueprint.annotation.Bean;
+import org.apache.karaf.boot.plugin.api.BootPlugin;
+import org.apache.karaf.boot.plugin.api.StreamFactory;
 
+public class BlueprintProcessor implements BootPlugin {
+    
     @Override
-    public Set<String> getSupportedAnnotationTypes() {
-        Set<String> set = new HashSet<String>();
-        set.add(Bean.class.getName());
-        return set;
+    public Class<? extends Annotation> getAnnotation() {
+        return Bean.class;
     }
 
     @Override
-    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
-        if (!hasRun) {
-            hasRun = true;
-            // Add the blueprint requirement
-            try (PrintWriter w = appendResource("META-INF/org.apache.karaf.boot.bnd")) {
-                w.println("Bundle-Blueprint-Annotation: true");
-            } catch (Exception e) {
-                processingEnv.getMessager().printMessage(Kind.ERROR, "Error: " + e.getMessage());
-            }
-        }
-        return true;
-    }
-
-    private PrintWriter appendResource(String resource) throws IOException {
+    public Map<String, List<String>> enhance(List<Class<?>> annotated, File generatedDir,
+                                             StreamFactory streamFactory) {
+        OutputStream beansOS = streamFactory.create(new File (generatedDir, "META-INF/beans.xml"));
         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;
-            }
+            beansOS.close();
+        } catch (IOException e) {
+            throw new RuntimeException("Error closing stream", e);
         }
+        Map<String, List<String>> headers = new HashMap<>();
+        headers.put("Bundle-Blueprint-Annotation", Arrays.asList("true"));
+        return headers;
     }
+
 }
diff --git a/starters/karaf-boot-starter-blueprint/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/starters/karaf-boot-starter-blueprint/src/main/resources/META-INF/services/javax.annotation.processing.Processor
deleted file mode 100644 (file)
index fab44d1..0000000
+++ /dev/null
@@ -1 +0,0 @@
-org.apache.karaf.boot.blueprint.impl.BlueprintProcessor
index 3f33d28..3d46fe7 100644 (file)
@@ -1,23 +1,17 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
-    <!--
-
-        Licensed to the Apache Software Foundation (ASF) under one or more
-        contributor license agreements.  See the NOTICE file distributed with
-        this work for additional information regarding copyright ownership.
-        The ASF licenses this file to You under the Apache License, Version 2.0
-        (the "License"); you may not use this file except in compliance with
-        the License.  You may obtain a copy of the License at
-
-           http://www.apache.org/licenses/LICENSE-2.0
-
-        Unless required by applicable law or agreed to in writing, software
-        distributed under the License is distributed on an "AS IS" BASIS,
-        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-        See the License for the specific language governing permissions and
-        limitations under the License.
-    -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+    <!-- Licensed to the Apache Software Foundation (ASF) under one or more 
+        contributor license agreements. See the NOTICE file distributed with this 
+        work for additional information regarding copyright ownership. The ASF licenses 
+        this file to You under the Apache License, Version 2.0 (the "License"); you 
+        may not use this file except in compliance with the License. You may obtain 
+        a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless 
+        required by applicable law or agreed to in writing, software distributed 
+        under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 
+        OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
+        the specific language governing permissions and limitations under the License. -->
 
     <modelVersion>4.0.0</modelVersion>
 
             <artifactId>geronimo-atinject_1.0_spec</artifactId>
             <version>1.0</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.boot</groupId>
+            <artifactId>karaf-boot-plugin-api</artifactId>
+            <version>${project.version}</version>
+        </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
index 44cc571..8c6d0ed 100644 (file)
@@ -1,82 +1,38 @@
 package org.apache.karaf.boot.cdi.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.ByteArrayOutputStream;
-import java.io.CharArrayWriter;
+import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintWriter;
-import java.io.Reader;
-import java.io.Writer;
-import java.util.HashSet;
-import java.util.Set;
-
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.karaf.boot.plugin.api.BootPlugin;
+import org.apache.karaf.boot.plugin.api.StreamFactory;
 import org.ops4j.pax.cdi.api.OsgiServiceProvider;
 
-public class CdiProcessor extends AbstractProcessor {
-
-    boolean hasRun;
-
-    public CdiProcessor() {
-    }
-
+public class CdiProcessor implements BootPlugin {
+    
     @Override
-    public Set<String> getSupportedAnnotationTypes() {
-        Set<String> set = new HashSet<String>();
-        set.add(OsgiServiceProvider.class.getName());
-        return set;
+    public Class<? extends Annotation> getAnnotation() {
+        return OsgiServiceProvider.class;
     }
 
     @Override
-    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
-        if (!hasRun) {
-            hasRun = true;
-            // Make sure we have a META-INF/beans.xml file present
-            try (PrintWriter w = appendResource("META-INF/beans.xml")) {
-                processingEnv.getMessager().printMessage(Kind.NOTE, "Generated META-INF/beans.xml");
-            } catch (Exception e) {
-                processingEnv.getMessager().printMessage(Kind.ERROR, "Error: " + e.getMessage());
-            }
-            // Add the CDI requirement
-            try (PrintWriter w = appendResource("META-INF/org.apache.karaf.boot.bnd")) {
-                w.println("Require-Capability: osgi.extender; filter:=\"(osgi.extender=pax.cdi)\"");
-            } catch (Exception e) {
-                processingEnv.getMessager().printMessage(Kind.ERROR, "Error: " + e.getMessage());
-            }
-        }
-        return true;
-    }
-
-    private PrintWriter appendResource(String resource) throws IOException {
+    public Map<String, List<String>> enhance(List<Class<?>> annotated, File generatedDir,
+                                             StreamFactory streamFactory) {
+        OutputStream beansOS = streamFactory.create(new File (generatedDir, "META-INF/beans.xml"));
         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;
-            }
+            beansOS.close();
+        } catch (IOException e) {
+            throw new RuntimeException("Error closing stream", e);
         }
+        Map<String, List<String>> headers = new HashMap<>();
+        headers.put("Require-Capability", Arrays.asList("osgi.extender; filter:=\"(osgi.extender=pax.cdi)\""));
+        return headers;
     }
+
+
 }
diff --git a/starters/karaf-boot-starter-cdi/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/starters/karaf-boot-starter-cdi/src/main/resources/META-INF/services/javax.annotation.processing.Processor
deleted file mode 100644 (file)
index a6cd494..0000000
+++ /dev/null
@@ -1 +0,0 @@
-org.apache.karaf.boot.cdi.impl.CdiProcessor
diff --git a/starters/karaf-boot-starter-karaf/pom.xml b/starters/karaf-boot-starter-karaf/pom.xml
deleted file mode 100644 (file)
index 32bec48..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
-    <!--
-
-        Licensed to the Apache Software Foundation (ASF) under one or more
-        contributor license agreements.  See the NOTICE file distributed with
-        this work for additional information regarding copyright ownership.
-        The ASF licenses this file to You under the Apache License, Version 2.0
-        (the "License"); you may not use this file except in compliance with
-        the License.  You may obtain a copy of the License at
-
-           http://www.apache.org/licenses/LICENSE-2.0
-
-        Unless required by applicable law or agreed to in writing, software
-        distributed under the License is distributed on an "AS IS" BASIS,
-        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-        See the License for the specific language governing permissions and
-        limitations under the License.
-    -->
-
-    <modelVersion>4.0.0</modelVersion>
-
-    <parent>
-        <groupId>org.apache.karaf.boot</groupId>
-        <artifactId>karaf-boot-starters</artifactId>
-        <version>1.0.0-SNAPSHOT</version>
-        <relativePath>../pom.xml</relativePath>
-    </parent>
-
-    <artifactId>karaf-boot-starter-karaf</artifactId>
-
-    <dependencies>
-        <dependency>
-            <groupId>org.osgi</groupId>
-            <artifactId>org.osgi.core</artifactId>
-            <version>${osgi.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.karaf</groupId>
-            <artifactId>org.apache.karaf.util</artifactId>
-            <version>${karaf.version}</version>
-        </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/starters/karaf-boot-starter-karaf/src/main/java/org/apache/karaf/boot/karaf/impl/KarafProcessor.java b/starters/karaf-boot-starter-karaf/src/main/java/org/apache/karaf/boot/karaf/impl/KarafProcessor.java
deleted file mode 100644 (file)
index e51380e..0000000
+++ /dev/null
@@ -1,154 +0,0 @@
-package org.apache.karaf.boot.karaf.impl;
-
-import javax.annotation.processing.AbstractProcessor;
-import javax.annotation.processing.RoundEnvironment;
-import javax.lang.model.element.AnnotationMirror;
-import javax.lang.model.element.AnnotationValue;
-import javax.lang.model.element.Element;
-import javax.lang.model.element.ExecutableElement;
-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.OutputStream;
-import java.io.PrintWriter;
-import java.io.Reader;
-import java.io.Writer;
-import java.lang.annotation.Annotation;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-
-import org.apache.karaf.util.tracker.annotation.Managed;
-import org.apache.karaf.util.tracker.annotation.ProvideService;
-import org.apache.karaf.util.tracker.annotation.RequireService;
-import org.apache.karaf.util.tracker.annotation.Services;
-
-public class KarafProcessor extends AbstractProcessor {
-
-    boolean hasRun;
-
-    public KarafProcessor() {
-    }
-
-    @Override
-    public Set<String> getSupportedAnnotationTypes() {
-        Set<String> set = new HashSet<String>();
-        set.add(Services.class.getName());
-        set.add(Managed.class.getName());
-        return set;
-    }
-
-    @Override
-    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
-        List<String> instructions = new ArrayList<>();
-        Properties props = new Properties();
-
-        for (Element elem : roundEnv.getElementsAnnotatedWith(Services.class)) {
-            for (AnnotationMirror mirror : elem.getAnnotationMirrors()) {
-                if (Services.class.getName().equals(((TypeElement) mirror.getAnnotationType().asElement()).getQualifiedName().toString())) {
-                    Map<String, Object> values = getAnnotationValues(mirror);
-                    if (values.containsKey("provides")) {
-                        for (AnnotationMirror p : (List<AnnotationMirror>) values.get("provides")) {
-                            Map<String, Object> pv = getAnnotationValues(p);
-                            String n = pv.get("value").toString();
-                            instructions.add("Provide-Capability: osgi.service;effective:=active;objectClass=" + n);
-                        }
-                    }
-                    if (values.containsKey("requires")) {
-                        for (AnnotationMirror r : (List<AnnotationMirror>) values.get("requires")) {
-                            Map<String, Object> rv = getAnnotationValues(r);
-                            String value = rv.get("value").toString();
-                            String filter = (String) rv.getOrDefault("filter", "");
-                            boolean opt = ((Boolean) rv.getOrDefault("optional", false));
-
-                            String fltWithClass = combine(filter, "(objectClass=" + value + ")");
-                            instructions.add("Require-Capability: osgi.service;effective:=active;filter:=\"" + fltWithClass + "\"");
-                            props.setProperty(value, filter);
-                        }
-                    }
-                }
-            }
-            instructions.add("Bundle-Activator: " + ((TypeElement) elem).getQualifiedName().toString());
-
-            Managed managed = elem.getAnnotation(Managed.class);
-            if (managed != null) {
-                props.setProperty("pid", managed.value());
-            }
-
-            String name = "OSGI-INF/karaf-tracker/" + ((TypeElement) elem).getQualifiedName().toString();
-            try (OutputStream os = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", name).openOutputStream()) {
-                props.store(os, null);
-            } catch (IOException e) {
-                processingEnv.getMessager().printMessage(Kind.ERROR, "Error writing to " + name + ": " + e.getMessage());
-            }
-        }
-
-        instructions.add("Private-Package: org.apache.karaf.util.tracker");
-        instructions.add("PREPEND-Import-Package: !org.apache.karaf.util.tracker.annotation");
-
-        if (!hasRun) {
-            hasRun = true;
-            // Add the Karaf embedded package
-            try (PrintWriter w = appendResource("META-INF/org.apache.karaf.boot.bnd")) {
-                for (String instr : instructions) {
-                    w.println(instr);
-                }
-            } catch (Exception e) {
-                processingEnv.getMessager().printMessage(Kind.ERROR, "Error writing to META-INF/org.apache.karaf.boot.bnd: " + e.getMessage());
-            }
-        }
-
-        return true;
-    }
-
-    private Map<String, Object> getAnnotationValues(AnnotationMirror mirror) {
-        Map<String, Object> map = new HashMap<>();
-        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : mirror.getElementValues().entrySet()) {
-            map.put(entry.getKey().getSimpleName().toString(), entry.getValue().getValue());
-        }
-        return map;
-    }
-
-    private String combine(String filter1, String filter2) {
-        if (filter1!=null && !filter1.isEmpty()) {
-            return "(&" + filter2 + filter1 + ")";
-        } else {
-            return filter2;
-        }
-    }
-
-    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/starters/karaf-boot-starter-karaf/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/starters/karaf-boot-starter-karaf/src/main/resources/META-INF/services/javax.annotation.processing.Processor
deleted file mode 100644 (file)
index c45090b..0000000
+++ /dev/null
@@ -1 +0,0 @@
-org.apache.karaf.boot.karaf.impl.KarafProcessor
index 28523c7..e9734a3 100644 (file)
@@ -1,23 +1,17 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
-    <!--
-
-        Licensed to the Apache Software Foundation (ASF) under one or more
-        contributor license agreements.  See the NOTICE file distributed with
-        this work for additional information regarding copyright ownership.
-        The ASF licenses this file to You under the Apache License, Version 2.0
-        (the "License"); you may not use this file except in compliance with
-        the License.  You may obtain a copy of the License at
-
-           http://www.apache.org/licenses/LICENSE-2.0
-
-        Unless required by applicable law or agreed to in writing, software
-        distributed under the License is distributed on an "AS IS" BASIS,
-        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-        See the License for the specific language governing permissions and
-        limitations under the License.
-    -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+    <!-- Licensed to the Apache Software Foundation (ASF) under one or more 
+        contributor license agreements. See the NOTICE file distributed with this 
+        work for additional information regarding copyright ownership. The ASF licenses 
+        this file to You under the Apache License, Version 2.0 (the "License"); you 
+        may not use this file except in compliance with the License. You may obtain 
+        a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless 
+        required by applicable law or agreed to in writing, software distributed 
+        under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 
+        OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
+        the specific language governing permissions and limitations under the License. -->
 
     <modelVersion>4.0.0</modelVersion>
 
             <artifactId>org.apache.karaf.shell.core</artifactId>
             <version>${karaf.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.boot</groupId>
+            <artifactId>karaf-boot-plugin-api</artifactId>
+            <version>${project.version}</version>
+        </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
index 3de3bfb..cd8f859 100644 (file)
@@ -1,84 +1,37 @@
 package org.apache.karaf.boot.shell.impl;
 
-import javax.annotation.processing.AbstractProcessor;
-import javax.annotation.processing.RoundEnvironment;
-import javax.lang.model.element.Element;
-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 static java.util.stream.Collectors.toCollection;
+
+import java.io.File;
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
 
+import org.apache.karaf.boot.plugin.api.BootPlugin;
+import org.apache.karaf.boot.plugin.api.StreamFactory;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 
-public class ShellProcessor extends AbstractProcessor {
-
-    boolean hasRun;
-
-    public ShellProcessor() {
-    }
-
+public class ShellProcessor implements BootPlugin {
+    
     @Override
-    public Set<String> getSupportedAnnotationTypes() {
-        Set<String> set = new HashSet<String>();
-        set.add(Service.class.getName());
-        return set;
+    public Class<? extends Annotation> getAnnotation() {
+        return Service.class;
     }
 
     @Override
-    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
-        Set<String> packages = new TreeSet<>();
-        for (Element elem : roundEnv.getElementsAnnotatedWith(Service.class)) {
-            packages.add(elem.getEnclosingElement().toString());
-        }
-
-        if (!packages.isEmpty()) {
-            if (!hasRun) {
-                hasRun = true;
-                // Add the Karaf embedded package
-                try (PrintWriter w = appendResource("META-INF/org.apache.karaf.boot.bnd")) {
-                    w.println("Karaf-Commands: " + String.join(",", packages));
-                } catch (Exception e) {
-                    processingEnv.getMessager().printMessage(Kind.ERROR, "Error writing to META-INF/org.apache.karaf.boot.bnd: " + e.getMessage());
-                }
-            }
-        }
-
-        return true;
+    public Map<String, List<String>> enhance(List<Class<?>> annotated, File generatedDir,
+                                             StreamFactory streamFactory) {
+        Set<String> packages = annotated.stream() //
+            .map(clazz -> clazz.getPackage().getName()) //
+            .collect(toCollection(TreeSet::new)); 
+            new TreeSet<>();
+        Map<String, List<String>> headers = new HashMap<>();
+        headers.put("Require-Capability", new ArrayList<>(packages));
+        return headers;
     }
 
-    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/starters/karaf-boot-starter-shell/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/starters/karaf-boot-starter-shell/src/main/resources/META-INF/services/javax.annotation.processing.Processor
deleted file mode 100644 (file)
index 3ebe726..0000000
+++ /dev/null
@@ -1 +0,0 @@
-org.apache.karaf.boot.shell.impl.ShellProcessor
index 54bc3c5..a249383 100644 (file)
             <artifactId>javax.servlet-api</artifactId>
             <version>3.1.0</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.boot</groupId>
+            <artifactId>karaf-boot-plugin-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
 
 </project>
\ No newline at end of file
index b272d78..769ef82 100644 (file)
@@ -1,9 +1,8 @@
 package org.apache.karaf.boot.plugin.api;
 
 import java.io.File;
-import java.io.IOException;
 import java.io.OutputStream;
 
 public interface StreamFactory {
-    OutputStream create(File file) throws IOException;
+    OutputStream create(File file);
 }
index f39b2b8..3d9d602 100644 (file)
@@ -33,7 +33,7 @@
 
     <modules>
         <module>karaf-boot-starter</module>
-        <module>karaf-boot-starter-karaf</module>
+        
         <module>karaf-boot-starter-shell</module>
         <module>karaf-boot-starter-web</module>
         <module>karaf-boot-starter-jpa</module>
index 6fd7566..b6573f5 100644 (file)
@@ -46,9 +46,13 @@ public class GenerateMojo extends AbstractMojo {
 
     private final class BuildStreamFactory implements StreamFactory {
         @Override
-        public OutputStream create(File file) throws IOException {
-            file.getParentFile().mkdirs();
-            return buildContext.newFileOutputStream(file);
+        public OutputStream create(File file) {
+            try {
+                file.getParentFile().mkdirs();
+                return buildContext.newFileOutputStream(file);
+            } catch (IOException e) {
+                throw new RuntimeException("Error creating file " + file, e);
+            }
         }
     }