You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2020/12/06 07:04:14 UTC
[karaf] branch karaf-4.2.x updated: Improve CDI example with API
bundle
This is an automated email from the ASF dual-hosted git repository.
jbonofre pushed a commit to branch karaf-4.2.x
in repository https://gitbox.apache.org/repos/asf/karaf.git
The following commit(s) were added to refs/heads/karaf-4.2.x by this push:
new 377eede Improve CDI example with API bundle
377eede is described below
commit 377eede66b193c2e5baa3735d486a3fa587a744a
Author: jbonofre <jb...@apache.org>
AuthorDate: Sun Dec 6 07:59:27 2020 +0100
Improve CDI example with API bundle
---
examples/karaf-cdi-example/README.md | 3 ++-
.../{ => karaf-cdi-example-api}/pom.xml | 23 ++++++++++++----------
.../apache/karaf/examples/cdi/api/MyService.java} | 14 +++----------
.../karaf-cdi-example-consumer/pom.xml | 2 +-
.../examples/cdi/consumer/MyConsumerBean.java | 4 ++--
.../src/main/feature/feature.xml | 1 +
.../karaf-cdi-example-provider/pom.xml | 5 +++++
.../karaf/examples/cdi/provider/MyServiceBean.java | 4 +++-
examples/karaf-cdi-example/pom.xml | 1 +
.../karaf/itests/examples/CdiExampleTest.java | 2 +-
10 files changed, 32 insertions(+), 27 deletions(-)
diff --git a/examples/karaf-cdi-example/README.md b/examples/karaf-cdi-example/README.md
index 35b7f90..a93da91 100644
--- a/examples/karaf-cdi-example/README.md
+++ b/examples/karaf-cdi-example/README.md
@@ -29,6 +29,7 @@ In this example, we are using Aries CDI and OpenWebBeans as CDI container.
## Artifacts
+* `karaf-cdi-example-api` provides API (interface) shared between provider and consumer bundles. It allows a decoupling between interface and bean implementation.
* `karaf-cdi-example-provider` provides an application scope bean and using the `@Service` annotation to register the bean in the Karaf service registry (can be used in another bundle).
* `karaf-cdi-example-consumer` creates another bean and inject the provider bean from the registry (thanks to the `@Reference` annotation).
* `karaf-cdi-example-features` provides a Karaf features XML to easily install the example application.
@@ -65,4 +66,4 @@ When you install the consumer feature, you will see:
```
Hello world
-```
\ No newline at end of file
+```
diff --git a/examples/karaf-cdi-example/pom.xml b/examples/karaf-cdi-example/karaf-cdi-example-api/pom.xml
similarity index 76%
copy from examples/karaf-cdi-example/pom.xml
copy to examples/karaf-cdi-example/karaf-cdi-example-api/pom.xml
index 75f35d2..032d349 100644
--- a/examples/karaf-cdi-example/pom.xml
+++ b/examples/karaf-cdi-example/karaf-cdi-example-api/pom.xml
@@ -23,19 +23,22 @@
<parent>
<groupId>org.apache.karaf.examples</groupId>
- <artifactId>apache-karaf-examples</artifactId>
+ <artifactId>karaf-cdi-example</artifactId>
<version>4.2.11-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
- <artifactId>karaf-cdi-example</artifactId>
- <name>Apache Karaf :: Examples :: CDI</name>
- <packaging>pom</packaging>
-
- <modules>
- <module>karaf-cdi-example-provider</module>
- <module>karaf-cdi-example-consumer</module>
- <module>karaf-cdi-example-features</module>
- </modules>
+ <artifactId>karaf-cdi-example-api</artifactId>
+ <name>Apache Karaf :: Examples :: CDI :: API</name>
+ <packaging>bundle</packaging>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
</project>
diff --git a/examples/karaf-cdi-example/karaf-cdi-example-provider/src/main/java/org/apache/karaf/examples/cdi/provider/MyServiceBean.java b/examples/karaf-cdi-example/karaf-cdi-example-api/src/main/java/org/apache/karaf/examples/cdi/api/MyService.java
similarity index 73%
copy from examples/karaf-cdi-example/karaf-cdi-example-provider/src/main/java/org/apache/karaf/examples/cdi/provider/MyServiceBean.java
copy to examples/karaf-cdi-example/karaf-cdi-example-api/src/main/java/org/apache/karaf/examples/cdi/api/MyService.java
index 8a5b141..50f9bdb 100644
--- a/examples/karaf-cdi-example/karaf-cdi-example-provider/src/main/java/org/apache/karaf/examples/cdi/provider/MyServiceBean.java
+++ b/examples/karaf-cdi-example/karaf-cdi-example-api/src/main/java/org/apache/karaf/examples/cdi/api/MyService.java
@@ -13,18 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.karaf.examples.cdi.provider;
+package org.apache.karaf.examples.cdi.api;
-import org.osgi.service.cdi.annotations.Service;
+public interface MyService {
-import javax.enterprise.context.ApplicationScoped;
-
-@Service
-@ApplicationScoped
-public class MyServiceBean {
-
- public String greeting(String message) {
- return "Hello " + message;
- }
+ String greeting(String message);
}
diff --git a/examples/karaf-cdi-example/karaf-cdi-example-consumer/pom.xml b/examples/karaf-cdi-example/karaf-cdi-example-consumer/pom.xml
index acad1ae..f397989 100644
--- a/examples/karaf-cdi-example/karaf-cdi-example-consumer/pom.xml
+++ b/examples/karaf-cdi-example/karaf-cdi-example-consumer/pom.xml
@@ -35,7 +35,7 @@
<dependencies>
<dependency>
<groupId>org.apache.karaf.examples</groupId>
- <artifactId>karaf-cdi-example-provider</artifactId>
+ <artifactId>karaf-cdi-example-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
diff --git a/examples/karaf-cdi-example/karaf-cdi-example-consumer/src/main/java/org/apache/karaf/examples/cdi/consumer/MyConsumerBean.java b/examples/karaf-cdi-example/karaf-cdi-example-consumer/src/main/java/org/apache/karaf/examples/cdi/consumer/MyConsumerBean.java
index c8ef478..09483ee 100644
--- a/examples/karaf-cdi-example/karaf-cdi-example-consumer/src/main/java/org/apache/karaf/examples/cdi/consumer/MyConsumerBean.java
+++ b/examples/karaf-cdi-example/karaf-cdi-example-consumer/src/main/java/org/apache/karaf/examples/cdi/consumer/MyConsumerBean.java
@@ -15,7 +15,7 @@
*/
package org.apache.karaf.examples.cdi.consumer;
-import org.apache.karaf.examples.cdi.provider.MyServiceBean;
+import org.apache.karaf.examples.cdi.api.MyService;
import org.osgi.service.cdi.annotations.Reference;
import javax.enterprise.context.ApplicationScoped;
@@ -28,7 +28,7 @@ public class MyConsumerBean {
@Reference
@Inject
- private MyServiceBean service;
+ private MyService service;
public void onStart(@Observes @Initialized(ApplicationScoped.class) Object start) {
System.out.println(service.greeting("world"));
diff --git a/examples/karaf-cdi-example/karaf-cdi-example-features/src/main/feature/feature.xml b/examples/karaf-cdi-example/karaf-cdi-example-features/src/main/feature/feature.xml
index e7fbbbb..00305c1 100644
--- a/examples/karaf-cdi-example/karaf-cdi-example-features/src/main/feature/feature.xml
+++ b/examples/karaf-cdi-example/karaf-cdi-example-features/src/main/feature/feature.xml
@@ -22,6 +22,7 @@
<feature name="karaf-cdi-example-provider" version="${project.version}">
<feature prerequisite="true">osgi-cdi-owb</feature>
+ <bundle>mvn:org.apache.karaf.examples/karaf-cdi-example-api/${project.version}</bundle>
<bundle>mvn:org.apache.karaf.examples/karaf-cdi-example-provider/${project.version}</bundle>
</feature>
diff --git a/examples/karaf-cdi-example/karaf-cdi-example-provider/pom.xml b/examples/karaf-cdi-example/karaf-cdi-example-provider/pom.xml
index b6633e3..8f61179 100644
--- a/examples/karaf-cdi-example/karaf-cdi-example-provider/pom.xml
+++ b/examples/karaf-cdi-example/karaf-cdi-example-provider/pom.xml
@@ -34,6 +34,11 @@
<dependencies>
<dependency>
+ <groupId>org.apache.karaf.examples</groupId>
+ <artifactId>karaf-cdi-example-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.cdi</artifactId>
<version>1.0.0</version>
diff --git a/examples/karaf-cdi-example/karaf-cdi-example-provider/src/main/java/org/apache/karaf/examples/cdi/provider/MyServiceBean.java b/examples/karaf-cdi-example/karaf-cdi-example-provider/src/main/java/org/apache/karaf/examples/cdi/provider/MyServiceBean.java
index 8a5b141..f404dc5 100644
--- a/examples/karaf-cdi-example/karaf-cdi-example-provider/src/main/java/org/apache/karaf/examples/cdi/provider/MyServiceBean.java
+++ b/examples/karaf-cdi-example/karaf-cdi-example-provider/src/main/java/org/apache/karaf/examples/cdi/provider/MyServiceBean.java
@@ -15,14 +15,16 @@
*/
package org.apache.karaf.examples.cdi.provider;
+import org.apache.karaf.examples.cdi.api.MyService;
import org.osgi.service.cdi.annotations.Service;
import javax.enterprise.context.ApplicationScoped;
@Service
@ApplicationScoped
-public class MyServiceBean {
+public class MyServiceBean implements MyService {
+ @Override
public String greeting(String message) {
return "Hello " + message;
}
diff --git a/examples/karaf-cdi-example/pom.xml b/examples/karaf-cdi-example/pom.xml
index 75f35d2..5828dc6 100644
--- a/examples/karaf-cdi-example/pom.xml
+++ b/examples/karaf-cdi-example/pom.xml
@@ -33,6 +33,7 @@
<packaging>pom</packaging>
<modules>
+ <module>karaf-cdi-example-api</module>
<module>karaf-cdi-example-provider</module>
<module>karaf-cdi-example-consumer</module>
<module>karaf-cdi-example-features</module>
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/examples/CdiExampleTest.java b/itests/test/src/test/java/org/apache/karaf/itests/examples/CdiExampleTest.java
index 9493b0b..7425f7d 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/examples/CdiExampleTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/examples/CdiExampleTest.java
@@ -31,7 +31,7 @@ public class CdiExampleTest extends BaseTest {
installAndAssertFeature("karaf-cdi-example-provider");
- Object object = getOsgiService("org.apache.karaf.examples.cdi.provider.MyServiceBean");
+ Object object = getOsgiService("org.apache.karaf.examples.cdi.api.MyService");
Assert.assertNotNull(object);