You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2016/05/09 14:48:38 UTC

[19/35] karaf-boot git commit: [cdi] Provides a CDI starter and example

[cdi] Provides a CDI starter and example


Project: http://git-wip-us.apache.org/repos/asf/karaf-boot/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf-boot/commit/1d2b7741
Tree: http://git-wip-us.apache.org/repos/asf/karaf-boot/tree/1d2b7741
Diff: http://git-wip-us.apache.org/repos/asf/karaf-boot/diff/1d2b7741

Branch: refs/heads/master
Commit: 1d2b774123ed025d52bf08c1ec6462b637400506
Parents: 93af001
Author: Guillaume Nodet <gn...@apache.org>
Authored: Fri Apr 15 16:39:32 2016 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Fri Apr 15 16:39:32 2016 +0200

----------------------------------------------------------------------
 .../karaf-boot-sample-cdi/README.md             | 23 ++++++
 .../karaf-boot-sample-cdi/pom.xml               | 47 +++++++++++
 .../main/java/sample/cdi/IceCreamService.java   |  6 ++
 .../java/sample/cdi/impl/VanillaService.java    | 45 +++++++++++
 .../karaf-boot-starter-cdi/pom.xml              | 67 ++++++++++++++++
 .../karaf/boot/cdi/impl/CdiProcessor.java       | 82 ++++++++++++++++++++
 .../javax.annotation.processing.Processor       |  1 +
 karaf-boot-starters/pom.xml                     |  1 +
 pom.xml                                         |  1 +
 9 files changed, 273 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/1d2b7741/karaf-boot-samples/karaf-boot-sample-cdi/README.md
----------------------------------------------------------------------
diff --git a/karaf-boot-samples/karaf-boot-sample-cdi/README.md b/karaf-boot-samples/karaf-boot-sample-cdi/README.md
new file mode 100644
index 0000000..1f98cac
--- /dev/null
+++ b/karaf-boot-samples/karaf-boot-sample-cdi/README.md
@@ -0,0 +1,23 @@
+== karaf-boot-sample-cdi ==
+
+This sample shows how to define a CDI bundle.
+
+= Design
+
+TODO.
+
+= Build
+
+To build, simply do:
+
+  mvn clean install
+
+= Deploy
+
+To deploy i:
+
+* you can drop the generated jar file (target/karaf-boot-sample-cdi-1.0.jar) in the
+Karaf deploy folder
+* in the Karaf shell console, do:
+
+  bundle:install -s mvn:org.apache.karaf.boot/karaf-boot-sample-cdi/1.0
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/1d2b7741/karaf-boot-samples/karaf-boot-sample-cdi/pom.xml
----------------------------------------------------------------------
diff --git a/karaf-boot-samples/karaf-boot-sample-cdi/pom.xml b/karaf-boot-samples/karaf-boot-sample-cdi/pom.xml
new file mode 100644
index 0000000..d627b6e
--- /dev/null
+++ b/karaf-boot-samples/karaf-boot-sample-cdi/pom.xml
@@ -0,0 +1,47 @@
+<?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>
+
+    <groupId>org.apache.karaf.boot</groupId>
+    <artifactId>karaf-boot-sample-cdi</artifactId>
+    <version>1.0.0-SNAPSHOT</version>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.karaf.boot</groupId>
+            <artifactId>karaf-boot-starter-cdi</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.karaf.boot</groupId>
+                <artifactId>karaf-boot-maven-plugin</artifactId>
+                <version>${project.version}</version>
+                <extensions>true</extensions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/1d2b7741/karaf-boot-samples/karaf-boot-sample-cdi/src/main/java/sample/cdi/IceCreamService.java
----------------------------------------------------------------------
diff --git a/karaf-boot-samples/karaf-boot-sample-cdi/src/main/java/sample/cdi/IceCreamService.java b/karaf-boot-samples/karaf-boot-sample-cdi/src/main/java/sample/cdi/IceCreamService.java
new file mode 100644
index 0000000..5f9cfac
--- /dev/null
+++ b/karaf-boot-samples/karaf-boot-sample-cdi/src/main/java/sample/cdi/IceCreamService.java
@@ -0,0 +1,6 @@
+package sample.cdi;
+
+public interface IceCreamService {
+
+    String getFlavour();
+}

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/1d2b7741/karaf-boot-samples/karaf-boot-sample-cdi/src/main/java/sample/cdi/impl/VanillaService.java
----------------------------------------------------------------------
diff --git a/karaf-boot-samples/karaf-boot-sample-cdi/src/main/java/sample/cdi/impl/VanillaService.java b/karaf-boot-samples/karaf-boot-sample-cdi/src/main/java/sample/cdi/impl/VanillaService.java
new file mode 100644
index 0000000..3007035
--- /dev/null
+++ b/karaf-boot-samples/karaf-boot-sample-cdi/src/main/java/sample/cdi/impl/VanillaService.java
@@ -0,0 +1,45 @@
+/**
+ *  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.
+ */
+package sample.cdi.impl;
+
+import javax.annotation.PostConstruct;
+import javax.enterprise.context.ApplicationScoped;
+
+import org.ops4j.pax.cdi.api.OsgiServiceProvider;
+import org.ops4j.pax.cdi.api.Properties;
+import org.ops4j.pax.cdi.api.Property;
+import sample.cdi.IceCreamService;
+
+@OsgiServiceProvider(classes = { VanillaService.class, IceCreamService.class })
+@Properties(@Property(name = "flavour", value = "vanilla"))
+@ApplicationScoped
+class VanillaService implements IceCreamService {
+
+    private boolean initialized;
+
+    @PostConstruct
+    public void init() {
+        initialized = true;
+    }
+
+    public String getFlavour() {
+        if (!initialized) {
+            throw new AssertionError("VanillaService is not initialized");
+        }
+        return "Vanilla";
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/1d2b7741/karaf-boot-starters/karaf-boot-starter-cdi/pom.xml
----------------------------------------------------------------------
diff --git a/karaf-boot-starters/karaf-boot-starter-cdi/pom.xml b/karaf-boot-starters/karaf-boot-starter-cdi/pom.xml
new file mode 100644
index 0000000..3f33d28
--- /dev/null
+++ b/karaf-boot-starters/karaf-boot-starter-cdi/pom.xml
@@ -0,0 +1,67 @@
+<?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-cdi</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.cmpn</artifactId>
+            <version>${osgi.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.cdi</groupId>
+            <artifactId>pax-cdi-api</artifactId>
+            <version>${pax.cdi.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>javax.enterprise</groupId>
+            <artifactId>cdi-api</artifactId>
+            <version>1.2</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-atinject_1.0_spec</artifactId>
+            <version>1.0</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

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/1d2b7741/karaf-boot-starters/karaf-boot-starter-cdi/src/main/java/org/apache/karaf/boot/cdi/impl/CdiProcessor.java
----------------------------------------------------------------------
diff --git a/karaf-boot-starters/karaf-boot-starter-cdi/src/main/java/org/apache/karaf/boot/cdi/impl/CdiProcessor.java b/karaf-boot-starters/karaf-boot-starter-cdi/src/main/java/org/apache/karaf/boot/cdi/impl/CdiProcessor.java
new file mode 100644
index 0000000..44cc571
--- /dev/null
+++ b/karaf-boot-starters/karaf-boot-starter-cdi/src/main/java/org/apache/karaf/boot/cdi/impl/CdiProcessor.java
@@ -0,0 +1,82 @@
+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.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 org.ops4j.pax.cdi.api.OsgiServiceProvider;
+
+public class CdiProcessor extends AbstractProcessor {
+
+    boolean hasRun;
+
+    public CdiProcessor() {
+    }
+
+    @Override
+    public Set<String> getSupportedAnnotationTypes() {
+        Set<String> set = new HashSet<String>();
+        set.add(OsgiServiceProvider.class.getName());
+        return set;
+    }
+
+    @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 {
+        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;
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/1d2b7741/karaf-boot-starters/karaf-boot-starter-cdi/src/main/resources/META-INF/services/javax.annotation.processing.Processor
----------------------------------------------------------------------
diff --git a/karaf-boot-starters/karaf-boot-starter-cdi/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/karaf-boot-starters/karaf-boot-starter-cdi/src/main/resources/META-INF/services/javax.annotation.processing.Processor
new file mode 100644
index 0000000..a6cd494
--- /dev/null
+++ b/karaf-boot-starters/karaf-boot-starter-cdi/src/main/resources/META-INF/services/javax.annotation.processing.Processor
@@ -0,0 +1 @@
+org.apache.karaf.boot.cdi.impl.CdiProcessor

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/1d2b7741/karaf-boot-starters/pom.xml
----------------------------------------------------------------------
diff --git a/karaf-boot-starters/pom.xml b/karaf-boot-starters/pom.xml
index 8a22ae1..e50f6c2 100644
--- a/karaf-boot-starters/pom.xml
+++ b/karaf-boot-starters/pom.xml
@@ -37,6 +37,7 @@
         <module>karaf-boot-starter-shell</module>
         <module>karaf-boot-starter-web</module>
         <module>karaf-boot-starter-jpa</module>
+        <module>karaf-boot-starter-cdi</module>
     </modules>
 
 </project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/1d2b7741/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 5e1e5c8..ca1f26e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -32,6 +32,7 @@
     <properties>
         <karaf.version>4.0.2</karaf.version>
         <osgi.version>6.0.0</osgi.version>
+        <pax.cdi.version>1.0.0.RC1</pax.cdi.version>
     </properties>
 
     <modules>