You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/06/14 07:40:27 UTC
[camel] 04/07: CAMEL-16711: components - Move OSGi dependenct code
to camel-karaf
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
commit baa144cff4478a9e1e14a3e78421e708789ac4fe
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jun 14 07:30:49 2021 +0200
CAMEL-16711: components - Move OSGi dependenct code to camel-karaf
---
components/camel-jcache/pom.xml | 9 --
.../jcache/DefaultJCacheManagerFactory.java} | 19 ++--
.../camel/component/jcache/JCacheEndpoint.java | 2 +-
.../camel/component/jcache/JCacheHelper.java | 38 ++-----
.../component/jcache/JCacheManagerFactory.java} | 17 +---
.../component/jcache/osgi/OSGiCacheManager.java | 113 ---------------------
.../aggregate/JCacheAggregationRepository.java | 22 +++-
.../idempotent/JCacheIdempotentRepository.java | 22 +++-
.../JCacheAggregationRepositoryTestSupport.java | 1 +
.../idempotent/JCacheIdempotentRepositoryTest.java | 5 +-
10 files changed, 58 insertions(+), 190 deletions(-)
diff --git a/components/camel-jcache/pom.xml b/components/camel-jcache/pom.xml
index 122a517..7a9ecc5 100644
--- a/components/camel-jcache/pom.xml
+++ b/components/camel-jcache/pom.xml
@@ -47,15 +47,6 @@
<version>${jcache-bundle-version}</version>
</dependency>
- <!-- osgi -->
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>osgi.core</artifactId>
- <scope>provided</scope>
- <optional>true</optional>
- <version>6.0.0</version>
- </dependency>
-
<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
diff --git a/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTestSupport.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/DefaultJCacheManagerFactory.java
similarity index 54%
copy from components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTestSupport.java
copy to components/camel-jcache/src/main/java/org/apache/camel/component/jcache/DefaultJCacheManagerFactory.java
index fa655ca..2f3fbb2 100644
--- a/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTestSupport.java
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/DefaultJCacheManagerFactory.java
@@ -14,20 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.component.jcache.processor.aggregate;
+package org.apache.camel.component.jcache;
-import org.apache.camel.component.jcache.JCacheConfiguration;
-import org.apache.camel.component.jcache.support.HazelcastTest;
-import org.apache.camel.test.junit5.CamelTestSupport;
+public class DefaultJCacheManagerFactory implements JCacheManagerFactory {
-@HazelcastTest
-class JCacheAggregationRepositoryTestSupport extends CamelTestSupport {
-
- protected JCacheAggregationRepository createRepository(boolean optimistic) throws Exception {
- JCacheAggregationRepository repository = new JCacheAggregationRepository();
- repository.setConfiguration(new JCacheConfiguration("aggregation-repository"));
- repository.setOptimistic(optimistic);
-
- return repository;
+ @Override
+ public <K, V> JCacheManager<K, V> createManager(JCacheConfiguration configuration) {
+ return new JCacheManager<>(configuration);
}
+
}
diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheEndpoint.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheEndpoint.java
index a520c8a..034818d 100644
--- a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheEndpoint.java
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheEndpoint.java
@@ -66,7 +66,7 @@ public class JCacheEndpoint extends DefaultEndpoint {
@Override
protected void doStart() throws Exception {
- cacheManager = JCacheHelper.createManager(configuration);
+ cacheManager = JCacheHelper.createManager(getCamelContext(), configuration);
}
@Override
diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheHelper.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheHelper.java
index 55a27f8..327faa9 100644
--- a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheHelper.java
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheHelper.java
@@ -16,29 +16,22 @@
*/
package org.apache.camel.component.jcache;
-import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
-import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.CamelContext;
+import org.apache.camel.support.CamelContextHelper;
public final class JCacheHelper {
private JCacheHelper() {
}
- public static <K, V> JCacheManager<K, V> createManager(JCacheConfiguration configuration) {
- if (isOSGi()) {
- try {
- Class<?> type = Class.forName("org.apache.camel.component.jcache.osgi.OSGiCacheManager");
- Constructor<?> ctor = type.getConstructor(JCacheConfiguration.class);
-
- return (JCacheManager<K, V>) ctor.newInstance(configuration);
- } catch (Exception e) {
- throw new RuntimeCamelException(e);
- }
+ public static <K, V> JCacheManager<K, V> createManager(CamelContext camelContext, JCacheConfiguration configuration) {
+ JCacheManagerFactory factory = CamelContextHelper.findByType(camelContext, JCacheManagerFactory.class);
+ if (factory == null) {
+ factory = new DefaultJCacheManagerFactory();
}
-
- return new JCacheManager<>(configuration);
+ return factory.createManager(configuration);
}
@SuppressWarnings("unchecked")
@@ -59,21 +52,4 @@ public final class JCacheHelper {
});
}
- public static boolean isOSGi() {
- try {
- // Check if we are in an osgi container
- Class<?> fu = Class.forName("org.osgi.framework.FrameworkUtil");
- if (fu != null) {
- Method method = fu.getMethod("getBundle", Class.class);
- if (method != null) {
- return method.invoke(null, JCacheHelper.class) != null;
- }
- }
- } catch (ClassNotFoundException e) {
- } catch (Exception e) {
- throw new RuntimeCamelException(e);
- }
-
- return false;
- }
}
diff --git a/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTestSupport.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheManagerFactory.java
similarity index 54%
copy from components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTestSupport.java
copy to components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheManagerFactory.java
index fa655ca..2e7cd40 100644
--- a/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTestSupport.java
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheManagerFactory.java
@@ -14,20 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.component.jcache.processor.aggregate;
+package org.apache.camel.component.jcache;
-import org.apache.camel.component.jcache.JCacheConfiguration;
-import org.apache.camel.component.jcache.support.HazelcastTest;
-import org.apache.camel.test.junit5.CamelTestSupport;
+public interface JCacheManagerFactory {
-@HazelcastTest
-class JCacheAggregationRepositoryTestSupport extends CamelTestSupport {
-
- protected JCacheAggregationRepository createRepository(boolean optimistic) throws Exception {
- JCacheAggregationRepository repository = new JCacheAggregationRepository();
- repository.setConfiguration(new JCacheConfiguration("aggregation-repository"));
- repository.setOptimistic(optimistic);
-
- return repository;
- }
+ <K, V> JCacheManager<K, V> createManager(JCacheConfiguration configuration);
}
diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManager.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManager.java
deleted file mode 100644
index b08c5be..0000000
--- a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManager.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * 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 java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.URL;
-import java.util.Enumeration;
-
-import javax.cache.Cache;
-
-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;
-
-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()))) {
- if (ObjectHelper.equal(providerName, in.readLine())) {
- 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);
- }
- }
- };
- }
- }
- }
- }
-
- return null;
- }
-}
diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepository.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepository.java
index b8ef868..4bb33b1 100644
--- a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepository.java
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepository.java
@@ -24,6 +24,7 @@ import java.util.Set;
import javax.cache.Cache;
import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
import org.apache.camel.Exchange;
import org.apache.camel.component.jcache.JCacheConfiguration;
import org.apache.camel.component.jcache.JCacheHelper;
@@ -36,10 +37,12 @@ import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class JCacheAggregationRepository extends ServiceSupport implements OptimisticLockingAggregationRepository {
+public class JCacheAggregationRepository extends ServiceSupport
+ implements CamelContextAware, OptimisticLockingAggregationRepository {
private static final Logger LOG = LoggerFactory.getLogger(JCacheAggregationRepository.class);
+ private CamelContext camelContext;
private JCacheConfiguration configuration;
private Cache<String, DefaultExchangeHolder> cache;
private boolean optimistic;
@@ -50,6 +53,16 @@ public class JCacheAggregationRepository extends ServiceSupport implements Optim
this.configuration = new JCacheConfiguration();
}
+ @Override
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+
+ @Override
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
public JCacheConfiguration getConfiguration() {
return configuration;
}
@@ -176,12 +189,13 @@ public class JCacheAggregationRepository extends ServiceSupport implements Optim
@Override
protected void doStart() throws Exception {
+ ObjectHelper.notNull(camelContext, "camelContext");
+ ObjectHelper.notNull(configuration, "configuration");
+
if (cache != null) {
cacheManager = new JCacheManager<>(cache);
} else {
- cacheManager = JCacheHelper.createManager(
- ObjectHelper.notNull(configuration, "configuration"));
-
+ cacheManager = JCacheHelper.createManager(getCamelContext(), configuration);
cache = cacheManager.getCache();
}
}
diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/idempotent/JCacheIdempotentRepository.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/idempotent/JCacheIdempotentRepository.java
index 4fdd87d..8e9c8d5 100644
--- a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/idempotent/JCacheIdempotentRepository.java
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/idempotent/JCacheIdempotentRepository.java
@@ -18,6 +18,8 @@ package org.apache.camel.component.jcache.processor.idempotent;
import javax.cache.Cache;
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
import org.apache.camel.api.management.ManagedAttribute;
import org.apache.camel.api.management.ManagedOperation;
import org.apache.camel.api.management.ManagedResource;
@@ -29,7 +31,8 @@ import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.ObjectHelper;
@ManagedResource(description = "JCache based message id repository")
-public class JCacheIdempotentRepository extends ServiceSupport implements IdempotentRepository {
+public class JCacheIdempotentRepository extends ServiceSupport implements CamelContextAware, IdempotentRepository {
+ private CamelContext camelContext;
private JCacheConfiguration configuration;
private Cache<String, Boolean> cache;
private JCacheManager<String, Boolean> cacheManager;
@@ -38,6 +41,16 @@ public class JCacheIdempotentRepository extends ServiceSupport implements Idempo
this.configuration = new JCacheConfiguration();
}
+ @Override
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+
+ @Override
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
public JCacheConfiguration getConfiguration() {
return configuration;
}
@@ -94,12 +107,13 @@ public class JCacheIdempotentRepository extends ServiceSupport implements Idempo
@Override
protected void doStart() throws Exception {
+ ObjectHelper.notNull(camelContext, "camelContext");
+ ObjectHelper.notNull(configuration, "configuration");
+
if (cache != null) {
cacheManager = new JCacheManager<>(cache);
} else {
- cacheManager = JCacheHelper.createManager(
- ObjectHelper.notNull(configuration, "configuration"));
-
+ cacheManager = JCacheHelper.createManager(getCamelContext(), configuration);
cache = cacheManager.getCache();
}
}
diff --git a/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTestSupport.java b/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTestSupport.java
index fa655ca..bdf415d 100644
--- a/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTestSupport.java
+++ b/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTestSupport.java
@@ -25,6 +25,7 @@ class JCacheAggregationRepositoryTestSupport extends CamelTestSupport {
protected JCacheAggregationRepository createRepository(boolean optimistic) throws Exception {
JCacheAggregationRepository repository = new JCacheAggregationRepository();
+ repository.setCamelContext(context);
repository.setConfiguration(new JCacheConfiguration("aggregation-repository"));
repository.setOptimistic(optimistic);
diff --git a/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/idempotent/JCacheIdempotentRepositoryTest.java b/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/idempotent/JCacheIdempotentRepositoryTest.java
index dd7b42b..1ca2f70 100644
--- a/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/idempotent/JCacheIdempotentRepositoryTest.java
+++ b/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/idempotent/JCacheIdempotentRepositoryTest.java
@@ -43,10 +43,12 @@ public class JCacheIdempotentRepositoryTest extends CamelTestSupport {
@Override
@BeforeEach
public void setUp() throws Exception {
- cacheManager = JCacheHelper.createManager(new JCacheConfiguration("idempotent-repository"));
+ super.setUp();
+ cacheManager = JCacheHelper.createManager(context, new JCacheConfiguration("idempotent-repository"));
cache = cacheManager.getCache();
repository = new JCacheIdempotentRepository();
+ repository.setCamelContext(context);
repository.setCache(cache);
repository.start();
}
@@ -54,6 +56,7 @@ public class JCacheIdempotentRepositoryTest extends CamelTestSupport {
@Override
@AfterEach
public void tearDown() throws Exception {
+ super.tearDown();
repository.stop();
cacheManager.close();
}