f2par0 commented on code in PR #340:
URL: https://github.com/apache/camel-karaf/pull/340#discussion_r1637966725


##########
components/camel-jcache/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManager.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.camel.component.jcache.osgi;
+
+import org.apache.camel.component.jcache.JCacheConfiguration;
+import org.apache.camel.component.jcache.JCacheHelper;
+import org.apache.camel.component.jcache.JCacheManager;
+import org.apache.camel.component.jcache.JCacheProvider;
+import org.apache.camel.component.jcache.JCacheProviders;
+import org.apache.camel.util.ObjectHelper;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.wiring.BundleWiring;
+
+import javax.cache.Cache;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.Enumeration;
+
+public final class OSGiCacheManager<K, V> extends JCacheManager {
+    public OSGiCacheManager(JCacheConfiguration configuration) {
+        super(configuration);
+    }
+
+    @Override
+    public synchronized Cache<K, V> doGetCache(JCacheProvider provider) throws 
Exception {
+        final ClassLoader jcl = getClassLoader(provider.className());
+        final ClassLoader tccl = 
Thread.currentThread().getContextClassLoader();
+
+        try {
+            if (jcl != null) {
+                Thread.currentThread().setContextClassLoader(jcl);
+            }
+
+            Cache<K, V> cache = super.doGetCache(provider);
+            if (provider == JCacheProviders.hazelcast && jcl != null) {
+                cache = JCacheHelper.tcclProxy(cache, Cache.class, jcl);
+            }
+
+            return cache;
+        } finally {
+            Thread.currentThread().setContextClassLoader(tccl);
+        }
+    }
+
+    private ClassLoader getClassLoader(String providerName) throws Exception {
+        if (providerName == null || !getConfiguration().isLookupProviders()) {
+            return null;
+        }
+
+        final BundleContext bc = 
FrameworkUtil.getBundle(JCacheHelper.class).getBundleContext();
+        final ClassLoader bcl = 
bc.getBundle().adapt(BundleWiring.class).getClassLoader();
+        final ClassLoader acl = 
getConfiguration().getApplicationContextClassLoader();
+
+        for (final Bundle bundle : bc.getBundles()) {
+            URL spi = 
bundle.getResource("META-INF/services/javax.cache.spi.CachingProvider");
+            if (spi != null) {
+                try (BufferedReader in = new BufferedReader(new 
InputStreamReader(spi.openStream()))) {
+                    String currentLine;
+                    while( (currentLine = in.readLine()) != null) {
+                        if (ObjectHelper.equal(providerName, currentLine)) {
+                            return new ClassLoader(bcl) {
+
+                                @Override
+                                protected Class<?> findClass(String name) 
throws ClassNotFoundException {
+                                    try {
+                                        return acl.loadClass(name);
+                                    } catch (ClassNotFoundException e) {
+                                        return bundle.loadClass(name);
+                                    }
+                                }
+
+                                @Override
+                                protected URL findResource(String name) {
+                                    URL resource = acl.getResource(name);
+                                    if (resource == null) {
+                                        resource = bundle.getResource(name);
+                                    }
+                                    return resource;
+                                }
+
+                                @Override
+                                protected Enumeration findResources(String 
name) throws IOException {
+                                    try {
+                                        return acl.getResources(name);
+                                    } catch (IOException e) {
+                                        return bundle.getResources(name);
+                                    }
+                                }
+                            };

Review Comment:
   Tried to use the ServiceLoader to find the implementation, and then get to 
the bundle classloader.
   It works with hazelcast but not with ecache. 
   The Serviceloader works , but then when creating the cache, I get this 
exception which I could not resolve.
   
   ```
   2024-06-13T11:37:37,104 | INFO  | RMI TCP Connection(3)-127.0.0.1 | 
CamelSuppliedRouteLauncher       | 46 - camel-integration-test - 4.6.0.SNAPSHOT 
| CamelRouteSupplier service registered: {service.id=233, service.bundleid=150, 
objectClass=[Ljava.lang.String;@3233b911, service.scope=bundle, 
component.name=karaf-camel-jcache-eh-test, 
osgi.ds.satisfying.condition.target=(osgi.condition.id=true), component.id=25} 
from the class org.apache.karaf.camel.test.CamelJcacheEhRouteSupplier
   2024-06-13T11:40:33,762 | ERROR | RMI TCP Connection(3)-127.0.0.1 | Felix    
                        | 4 - org.ops4j.pax.logging.pax-logging-api - 2.2.7 | 
Bundle camel-integration-test [46] EventDispatcher: Error during dispatch. 
(java.lang.RuntimeException: org.apache.camel.FailedToStartRouteException: 
Failed to start route producer-camel-jcache-eh-test because of 
java.lang.IllegalStateException: Duplicate service implementation(s) found for 
class 
org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider\n      
        - interface org.ehcache.spi.serialization.SerializationProvider already 
has class org.ehcache.impl.internal.spi.serialization.D
   java.lang.RuntimeException: org.apache.camel.FailedToStartRouteException: 
Failed to start route producer-camel-jcache-eh-test because of 
java.lang.IllegalStateException: Duplicate service implementation(s) found for 
class org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider
                - interface org.ehcache.spi.serialization.SerializationProvider 
already has class 
org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider
                - class 
org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider 
already has class 
org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider
        at 
org.apache.karaf.camel.itests.CamelSuppliedRouteLauncher.addRoutes(CamelSuppliedRouteLauncher.java:151)
 ~[?:?]
        at 
org.apache.karaf.camel.itests.CamelSuppliedRouteLauncher.serviceChanged(CamelSuppliedRouteLauncher.java:111)
 ~[?:?]```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to