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:41 UTC

[22/35] karaf-boot git commit: [blueprint] Add blueprint annotation support

[blueprint] Add blueprint annotation support

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

Branch: refs/heads/master
Commit: 18a2cfab13be3acfbf72ce6fe014831d20173490
Parents: 52bc684
Author: Guillaume Nodet <gn...@apache.org>
Authored: Fri Apr 15 20:48:32 2016 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Fri Apr 15 20:48:32 2016 +0200

----------------------------------------------------------------------
 .../README.md                                   | 21 ++++++
 .../pom.xml                                     | 49 +++++++++++++
 .../service/provider/HelloService.java          | 25 +++++++
 .../service/provider/HelloServiceImpl.java      | 35 +++++++++
 .../karaf-boot-starter-blueprint/pom.xml        | 57 +++++++++++++++
 .../boot/blueprint/impl/BlueprintProcessor.java | 76 ++++++++++++++++++++
 .../javax.annotation.processing.Processor       |  1 +
 karaf-boot-starters/pom.xml                     |  1 +
 pom.xml                                         |  2 +
 9 files changed, 267 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/18a2cfab/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/README.md
----------------------------------------------------------------------
diff --git a/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/README.md b/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/README.md
new file mode 100644
index 0000000..02f255e
--- /dev/null
+++ b/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/README.md
@@ -0,0 +1,21 @@
+== karaf-boot-sample-service-blueprint-annotation ==
+
+This sample exposes an OSGi service using blueprint annotations.
+
+= Design
+
+TODO
+
+= Build
+
+To build, simply do:
+
+  mvn clean install
+
+= Deploy
+
+* you can drop the generated jar file (target/karaf-boot-sample-service-blueprint-annotation-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-service-blueprint-annotation/1.0

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/18a2cfab/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/pom.xml
----------------------------------------------------------------------
diff --git a/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/pom.xml b/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/pom.xml
new file mode 100644
index 0000000..cc629dc
--- /dev/null
+++ b/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/pom.xml
@@ -0,0 +1,49 @@
+<?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-service-blueprint-annotation</artifactId>
+    <version>1.0.0-SNAPSHOT</version>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.karaf.boot</groupId>
+            <artifactId>karaf-boot-starter-blueprint</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/18a2cfab/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/src/main/java/sample/blueprint/service/provider/HelloService.java
----------------------------------------------------------------------
diff --git a/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/src/main/java/sample/blueprint/service/provider/HelloService.java b/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/src/main/java/sample/blueprint/service/provider/HelloService.java
new file mode 100644
index 0000000..2555069
--- /dev/null
+++ b/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/src/main/java/sample/blueprint/service/provider/HelloService.java
@@ -0,0 +1,25 @@
+/**
+ *  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.blueprint.service.provider;
+
+public interface HelloService {
+
+    public String hello(String message);
+
+    public void startUp();
+
+}

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/18a2cfab/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/src/main/java/sample/blueprint/service/provider/HelloServiceImpl.java
----------------------------------------------------------------------
diff --git a/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/src/main/java/sample/blueprint/service/provider/HelloServiceImpl.java b/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/src/main/java/sample/blueprint/service/provider/HelloServiceImpl.java
new file mode 100644
index 0000000..f46b5da
--- /dev/null
+++ b/karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation/src/main/java/sample/blueprint/service/provider/HelloServiceImpl.java
@@ -0,0 +1,35 @@
+/**
+ *  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.blueprint.service.provider;
+
+import org.apache.aries.blueprint.annotation.Bean;
+import org.apache.aries.blueprint.annotation.Service;
+
+@Bean(id = "HelloServiceImpl")
+@Service(autoExport = "interfaces")
+public class HelloServiceImpl implements HelloService{
+
+    public String hello(String message) {
+        return "Hello " + message + " !";
+    }
+
+    public void startUp() {
+        System.out.println("I'm starting up!");
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/18a2cfab/karaf-boot-starters/karaf-boot-starter-blueprint/pom.xml
----------------------------------------------------------------------
diff --git a/karaf-boot-starters/karaf-boot-starter-blueprint/pom.xml b/karaf-boot-starters/karaf-boot-starter-blueprint/pom.xml
new file mode 100644
index 0000000..9f48c60
--- /dev/null
+++ b/karaf-boot-starters/karaf-boot-starter-blueprint/pom.xml
@@ -0,0 +1,57 @@
+<?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-blueprint</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.aries.blueprint</groupId>
+            <artifactId>org.apache.aries.blueprint.api</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.aries.blueprint</groupId>
+            <artifactId>org.apache.aries.blueprint.annotation.api</artifactId>
+            <version>1.0.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/18a2cfab/karaf-boot-starters/karaf-boot-starter-blueprint/src/main/java/org/apache/karaf/boot/blueprint/impl/BlueprintProcessor.java
----------------------------------------------------------------------
diff --git a/karaf-boot-starters/karaf-boot-starter-blueprint/src/main/java/org/apache/karaf/boot/blueprint/impl/BlueprintProcessor.java b/karaf-boot-starters/karaf-boot-starter-blueprint/src/main/java/org/apache/karaf/boot/blueprint/impl/BlueprintProcessor.java
new file mode 100644
index 0000000..896a161
--- /dev/null
+++ b/karaf-boot-starters/karaf-boot-starter-blueprint/src/main/java/org/apache/karaf/boot/blueprint/impl/BlueprintProcessor.java
@@ -0,0 +1,76 @@
+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 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.apache.aries.blueprint.annotation.Bean;
+
+public class BlueprintProcessor extends AbstractProcessor {
+
+    boolean hasRun;
+
+    public BlueprintProcessor() {
+    }
+
+    @Override
+    public Set<String> getSupportedAnnotationTypes() {
+        Set<String> set = new HashSet<String>();
+        set.add(Bean.class.getName());
+        return set;
+    }
+
+    @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 {
+        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/18a2cfab/karaf-boot-starters/karaf-boot-starter-blueprint/src/main/resources/META-INF/services/javax.annotation.processing.Processor
----------------------------------------------------------------------
diff --git a/karaf-boot-starters/karaf-boot-starter-blueprint/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/karaf-boot-starters/karaf-boot-starter-blueprint/src/main/resources/META-INF/services/javax.annotation.processing.Processor
new file mode 100644
index 0000000..fab44d1
--- /dev/null
+++ b/karaf-boot-starters/karaf-boot-starter-blueprint/src/main/resources/META-INF/services/javax.annotation.processing.Processor
@@ -0,0 +1 @@
+org.apache.karaf.boot.blueprint.impl.BlueprintProcessor

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/18a2cfab/karaf-boot-starters/pom.xml
----------------------------------------------------------------------
diff --git a/karaf-boot-starters/pom.xml b/karaf-boot-starters/pom.xml
index 127874e..57dbd39 100644
--- a/karaf-boot-starters/pom.xml
+++ b/karaf-boot-starters/pom.xml
@@ -39,6 +39,7 @@
         <module>karaf-boot-starter-web</module>
         <module>karaf-boot-starter-jpa</module>
         <module>karaf-boot-starter-cdi</module>
+        <module>karaf-boot-starter-blueprint</module>
     </modules>
 
 </project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/karaf-boot/blob/18a2cfab/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index ca1f26e..eea3146 100644
--- a/pom.xml
+++ b/pom.xml
@@ -74,6 +74,8 @@
                 <!-- blueprint -->
                 <module>karaf-boot-samples/karaf-boot-sample-service-provider-blueprint</module>
                 <module>karaf-boot-samples/karaf-boot-sample-service-consumer-blueprint</module>
+                <!-- blueprint annotations -->
+                <module>karaf-boot-samples/karaf-boot-sample-service-blueprint-annotation</module>
                 <!-- cdi -->
                 <module>karaf-boot-samples/karaf-boot-sample-cdi</module>
                 <!-- shell -->