geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject [geronimo-jcache-simple] branch master updated: GERONIMO-6761 enable geronimo-simple-jcache to work with osgi
Date Thu, 13 Feb 2020 17:20:11 GMT
This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/geronimo-jcache-simple.git


The following commit(s) were added to refs/heads/master by this push:
     new 1f35f4c  GERONIMO-6761 enable geronimo-simple-jcache to work with osgi
1f35f4c is described below

commit 1f35f4c244c987d2001b51dfb1e0fa7788f6d845
Author: Romain Manni-Bucau <rmannibucau@gmail.com>
AuthorDate: Thu Feb 13 18:19:55 2020 +0100

    GERONIMO-6761 enable geronimo-simple-jcache to work with osgi
---
 pom.xml                                            | 33 ++++++++++
 .../cdi/MakeJCacheCDIInterceptorFriendly.java      | 57 ++++------------
 .../jcache/simple/osgi/JCacheActivator.java        | 77 ++++++++++++++++++++++
 3 files changed, 121 insertions(+), 46 deletions(-)

diff --git a/pom.xml b/pom.xml
index 24e1982..da633f6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -56,6 +56,14 @@
 
   <dependencies>
     <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>osgi.core</artifactId>
+      <version>7.0.0</version>
+      <scope>provided</scope>
+      <optional>true</optional>
+    </dependency>
+
+    <dependency>
       <groupId>org.apache.geronimo.specs</groupId>
       <artifactId>geronimo-jcache_1.0_spec</artifactId>
       <version>1.0-alpha-1</version>
@@ -159,6 +167,11 @@
             <goals>
               <goal>jar</goal>
             </goals>
+            <configuration>
+              <archive>
+                <manifestFile>${project.build.directory}/manifest/global/MANIFEST.MF</manifestFile>
+              </archive>
+            </configuration>
           </execution>
           <execution> <!-- cdi only -->
             <id>cdi-jar</id>
@@ -245,6 +258,26 @@
           </systemPropertyVariables>
         </configuration>
       </plugin>
+
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <version>4.2.1</version>
+        <executions>
+          <execution>
+            <id>global-manifest</id>
+            <goals>
+              <goal>manifest</goal>
+            </goals>
+            <configuration>
+              <manifestLocation>${project.build.directory}/manifest/global</manifestLocation>
+              <instructions>
+                <Bundle-Activator>org.apache.geronimo.jcache.simple.osgi.JCacheActivator</Bundle-Activator>
+              </instructions>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
     </plugins>
   </build>
 
diff --git a/src/main/java/org/apache/geronimo/jcache/simple/cdi/MakeJCacheCDIInterceptorFriendly.java
b/src/main/java/org/apache/geronimo/jcache/simple/cdi/MakeJCacheCDIInterceptorFriendly.java
index d98a32c..42b3920 100644
--- a/src/main/java/org/apache/geronimo/jcache/simple/cdi/MakeJCacheCDIInterceptorFriendly.java
+++ b/src/main/java/org/apache/geronimo/jcache/simple/cdi/MakeJCacheCDIInterceptorFriendly.java
@@ -18,14 +18,13 @@
  */
 package org.apache.geronimo.jcache.simple.cdi;
 
-import static java.util.Arrays.asList;
-
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Stream;
 
 import javax.cache.annotation.CachePut;
 import javax.cache.annotation.CacheRemove;
@@ -46,7 +45,6 @@ import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.inject.spi.InjectionTarget;
 import javax.enterprise.inject.spi.PassivationCapable;
 import javax.enterprise.inject.spi.ProcessAnnotatedType;
-import javax.enterprise.util.AnnotationLiteral;
 
 // TODO: observe annotated type (or maybe sthg else) to cache data and inject this extension
(used as metadata cache)
 // to get class model and this way allow to add cache annotation on the fly - == avoid java
pure reflection to get metadata
@@ -59,35 +57,18 @@ public class MakeJCacheCDIInterceptorFriendly implements Extension {
 
     private boolean needHelper = true;
 
-    protected void discoverInterceptorBindings(final @Observes BeforeBeanDiscovery beforeBeanDiscoveryEvent,
+    protected void discoverInterceptorBindings(final @Observes BeforeBeanDiscovery beforeBeanDiscovery,
             final BeanManager bm) {
         if (SKIP) {
             return;
         }
-        // CDI 1.1 will just pick createAnnotatedType(X) as beans so we'll skip our HelperBean
-        // but CDI 1.0 needs our HelperBean + interceptors in beans.xml like:
-        /*
-         * <beans xmlns="http://java.sun.com/xml/ns/javaee"
-         * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         * xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
-         * http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
-         * <interceptors>
-         * <class>org.apache.geronimo.jcache.simple.cdi.CacheResultInterceptor</class>
-         * <class>org.apache.geronimo.jcache.simple.cdi.CacheRemoveAllInterceptor</class>
-         * <class>org.apache.geronimo.jcache.simple.cdi.CacheRemoveInterceptor</class>
-         * <class>org.apache.geronimo.jcache.simple.cdi.CachePutInterceptor</class>
-         * </interceptors>
-         * </beans>
-         */
-        bm.createAnnotatedType(CDIJCacheHelper.class);
-        for (final Class<?> interceptor : asList(CachePutInterceptor.class, CacheRemoveInterceptor.class,
-                CacheRemoveAllInterceptor.class, CacheResultInterceptor.class)) {
-            beforeBeanDiscoveryEvent.addAnnotatedType(bm.createAnnotatedType(interceptor));
-        }
-        for (final Class<? extends Annotation> interceptor : asList(CachePut.class,
CacheRemove.class, CacheRemoveAll.class,
-                CacheResult.class)) {
-            beforeBeanDiscoveryEvent.addInterceptorBinding(interceptor);
-        }
+        Stream.of(CachePut.class, CacheRemove.class, CacheRemoveAll.class, CacheResult.class)
+                .forEach(it -> beforeBeanDiscovery.addInterceptorBinding(bm.createAnnotatedType(it)));
+        Stream.of(
+                CDIJCacheHelper.class,
+                CachePutInterceptor.class, CacheRemoveInterceptor.class,
+                CacheRemoveAllInterceptor.class, CacheRemoveInterceptor.class)
+                .forEach(it -> beforeBeanDiscovery.addAnnotatedType(bm.createAnnotatedType(it)));
     }
 
     protected void addHelper(final @Observes AfterBeanDiscovery afterBeanDiscovery, final
BeanManager bm) {
@@ -97,20 +78,6 @@ public class MakeJCacheCDIInterceptorFriendly implements Extension {
         if (!needHelper) {
             return;
         }
-        /*
-         * CDI >= 1.1 only. Actually we shouldn't go here with CDI 1.1 since we defined
the annotated type for the helper
-         * final AnnotatedType<CDIJCacheHelper> annotatedType = bm.createAnnotatedType(CDIJCacheHelper.class);
-         * final BeanAttributes<CDIJCacheHelper> beanAttributes = bm.createBeanAttributes(annotatedType);
-         * final InjectionTarget<CDIJCacheHelper> injectionTarget = bm.createInjectionTarget(annotatedType);
-         * final Bean<CDIJCacheHelper> bean = bm.createBean(beanAttributes, CDIJCacheHelper.class,
new
-         * InjectionTargetFactory<CDIJCacheHelper>() {
-         * 
-         * @Override
-         * public InjectionTarget<CDIJCacheHelper> createInjectionTarget(Bean<CDIJCacheHelper>
bean) {
-         * return injectionTarget;
-         * }
-         * });
-         */
         final AnnotatedType<CDIJCacheHelper> annotatedType = bm.createAnnotatedType(CDIJCacheHelper.class);
         final InjectionTarget<CDIJCacheHelper> injectionTarget = bm.createInjectionTarget(annotatedType);
         final HelperBean bean = new HelperBean(annotatedType, injectionTarget, findIdSuffix());
@@ -153,10 +120,8 @@ public class MakeJCacheCDIInterceptorFriendly implements Extension {
             this.id = "JCache#CDIHelper#" + id;
 
             this.qualifiers = new HashSet<>();
-            this.qualifiers.add(new AnnotationLiteral<Default>() {
-            });
-            this.qualifiers.add(new AnnotationLiteral<Any>() {
-            });
+            this.qualifiers.add(Default.Literal.INSTANCE);
+            this.qualifiers.add(Any.Literal.INSTANCE);
         }
 
         @Override
diff --git a/src/main/java/org/apache/geronimo/jcache/simple/osgi/JCacheActivator.java b/src/main/java/org/apache/geronimo/jcache/simple/osgi/JCacheActivator.java
new file mode 100644
index 0000000..4f7efdc
--- /dev/null
+++ b/src/main/java/org/apache/geronimo/jcache/simple/osgi/JCacheActivator.java
@@ -0,0 +1,77 @@
+/*
+ * 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 org.apache.geronimo.jcache.simple.osgi;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Objects;
+import java.util.stream.Stream;
+
+import javax.cache.spi.CachingProvider;
+import javax.enterprise.inject.spi.Extension;
+
+import org.apache.geronimo.jcache.simple.SimpleProvider;
+import org.apache.geronimo.jcache.simple.cdi.MakeJCacheCDIInterceptorFriendly;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.PrototypeServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+
+public class JCacheActivator implements BundleActivator {
+    private ServiceRegistration<?> cacheProviderRegistration;
+    private ServiceRegistration<?> jcacheExtensionRegistration;
+
+    @Override
+    public void start(final BundleContext context) {
+        final Dictionary<String, Object> cachingProvider = new Hashtable<>();
+        cachingProvider.put("javax.cache.provider", CachingProvider.class.getName());
+        cacheProviderRegistration = context.registerService(
+                CachingProvider.class, new SimpleProvider(), cachingProvider);
+
+        final Dictionary<String, Object> jcacheExtension = new Hashtable<>();
+        jcacheExtension.put("osgi.cdi.extension", "implicit"); // always enable since it
just enable interceptors
+        jcacheExtensionRegistration = context.registerService(
+                Extension.class, new PrototypeServiceFactory<Extension>() {
+                    @Override
+                    public Extension getService(final Bundle bundle, final ServiceRegistration<Extension>
registration) {
+                        return new MakeJCacheCDIInterceptorFriendly();
+                    }
+
+                    @Override
+                    public void ungetService(final Bundle bundle, final ServiceRegistration<Extension>
registration,
+                                             final Extension service) {
+                        // no-op
+                    }
+                }, jcacheExtension);
+    }
+
+    @Override
+    public void stop(final BundleContext context) {
+        Stream.of(cacheProviderRegistration, jcacheExtensionRegistration)
+                .filter(Objects::nonNull)
+                .forEach(it -> {
+                    try {
+                        it.unregister();
+                    } catch (final IllegalStateException ise) {
+                        // no-op
+                    }
+                });
+    }
+}


Mime
View raw message