essobedo commented on code in PR #340: URL: https://github.com/apache/camel-karaf/pull/340#discussion_r1634981134
########## components/camel-jcache/src/main/java/org/apache/camel/component/jcache/osgi/Activator.java: ########## @@ -0,0 +1,41 @@ +/* + * 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.JCacheManagerFactory; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +public class Activator implements BundleActivator { + private ServiceRegistration<?> registration; + + @Override + public void start(BundleContext context) throws Exception { + Object factory = new OSGiCacheManagerFactory(); + registration = context.registerService(JCacheManagerFactory.class.getName(), factory, null); + } + + @Override + public void stop(BundleContext context) throws Exception { + // release the reference + if (registration != null) { + context.ungetService(registration.getReference()); Review Comment: I think it should rather be registration.unregister() ########## tests/features/camel-jcache/src/main/java/org/apache/karaf/camel/test/CamelJcacheEhRouteSupplier.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 org.apache.karaf.camel.test; + +import org.apache.karaf.camel.itests.CamelRouteSupplier; +import org.osgi.service.component.annotations.Component; + +@Component( + name = "karaf-camel-jcache-eh-test", + immediate = true, + service = CamelRouteSupplier.class +) +public class CamelJcacheEhRouteSupplier extends AbstractCamelJcacheRouteSupplier { + + public static final String PROVIDER_CLASS = "org.ehcache.jsr107.EhcacheCachingProvider"; Review Comment: Can we use `EhcacheCachingProvider.class.getName()` instead? ########## tests/features/camel-jcache/src/main/java/org/apache/karaf/camel/test/CamelJcacheHzRouteSupplier.java: ########## @@ -0,0 +1,41 @@ +/* + * 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.karaf.camel.test; + +import org.apache.camel.CamelContext; +import org.apache.karaf.camel.itests.CamelRouteSupplier; +import org.osgi.service.component.annotations.Component; + +@Component( + name = "karaf-camel-jcache-hz-test", + immediate = true, + service = CamelRouteSupplier.class +) +public class CamelJcacheHzRouteSupplier extends AbstractCamelJcacheRouteSupplier { + + public static final String PROVIDER_CLASS = "com.hazelcast.cache.HazelcastCachingProvider"; + + @Override + public void configure(CamelContext camelContext) { + System.setProperty("hazelcast.jcache.provider.type", "member"); Review Comment: Use `ClusterProperty.JCACHE_PROVIDER_TYPE.setSystemProperty("member")` instead ########## 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: This should not be needed as SPI is managed by spifly ########## tests/features/camel-jcache/src/main/java/org/apache/karaf/camel/test/CamelJcacheHzRouteSupplier.java: ########## @@ -0,0 +1,41 @@ +/* + * 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.karaf.camel.test; + +import org.apache.camel.CamelContext; +import org.apache.karaf.camel.itests.CamelRouteSupplier; +import org.osgi.service.component.annotations.Component; + +@Component( + name = "karaf-camel-jcache-hz-test", + immediate = true, + service = CamelRouteSupplier.class +) +public class CamelJcacheHzRouteSupplier extends AbstractCamelJcacheRouteSupplier { + + public static final String PROVIDER_CLASS = "com.hazelcast.cache.HazelcastCachingProvider"; Review Comment: ditto, let's get the name from the class instead of hard coding it -- 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