You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2016/08/12 13:54:06 UTC
camel git commit: CAMEL-10234: Improve OSGi camel-jcache support
Repository: camel
Updated Branches:
refs/heads/master b9f91f4d3 -> e3c9f1b01
CAMEL-10234: Improve OSGi camel-jcache support
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e3c9f1b0
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e3c9f1b0
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e3c9f1b0
Branch: refs/heads/master
Commit: e3c9f1b01f74d8105f81fa2a9fcf8d1cd902c690
Parents: b9f91f4
Author: lburgazzoli <lb...@gmail.com>
Authored: Wed Aug 10 17:22:09 2016 +0200
Committer: lburgazzoli <lb...@gmail.com>
Committed: Fri Aug 12 15:48:52 2016 +0200
----------------------------------------------------------------------
components/camel-jcache/pom.xml | 7 ++
.../camel-jcache/src/main/docs/jcache.adoc | 7 +-
.../camel/component/jcache/JCacheComponent.java | 4 +-
.../component/jcache/JCacheConfiguration.java | 50 +++++++-
.../camel/component/jcache/JCacheEndpoint.java | 10 +-
.../camel/component/jcache/JCacheHelper.java | 63 +++++++++++
.../camel/component/jcache/JCacheManager.java | 102 ++++++++---------
.../camel/component/jcache/JCacheProvider.java | 22 ++++
.../camel/component/jcache/JCacheProviders.java | 71 ++++++++++++
.../component/jcache/osgi/OSGiCacheManager.java | 113 +++++++++++++++++++
.../aggregate/JCacheAggregationRepository.java | 37 +++---
.../idempotent/JCacheIdempotentRepository.java | 39 +++----
.../component/jcache/JCacheManagerTest.java | 6 +-
.../JCacheAggregationRepositoryTestSupport.java | 3 +-
.../JCacheIdempotentRepositoryTest.java | 3 +-
15 files changed, 418 insertions(+), 119 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-jcache/pom.xml b/components/camel-jcache/pom.xml
index cacc9ba..7584eb1 100644
--- a/components/camel-jcache/pom.xml
+++ b/components/camel-jcache/pom.xml
@@ -52,6 +52,13 @@
<version>${jcache-bundle-version}</version>
</dependency>
+ <!-- osgi -->
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/main/docs/jcache.adoc
----------------------------------------------------------------------
diff --git a/components/camel-jcache/src/main/docs/jcache.adoc b/components/camel-jcache/src/main/docs/jcache.adoc
index cc1cdfe..4c09c8d 100644
--- a/components/camel-jcache/src/main/docs/jcache.adoc
+++ b/components/camel-jcache/src/main/docs/jcache.adoc
@@ -5,8 +5,10 @@ JCache
+
+
// endpoint options: START
-The JCache component supports 22 endpoint options which are listed below:
+The JCache component supports 23 endpoint options which are listed below:
{% raw %}
[width="100%",cols="2s,1,1m,1m,5",options="header"]
@@ -34,6 +36,7 @@ The JCache component supports 22 endpoint options which are listed below:
| createCacheIfNotExists | advanced | true | boolean | Configure if a cache need to be created if it does exist or can't be pre-configured.
| exchangePattern | advanced | InOnly | ExchangePattern | Sets the default exchange pattern when creating an exchange
| expiryPolicyFactory | advanced | | ExpiryPolicy> | The ExpiryPolicy factory
+| lookupProviders | advanced | false | boolean | Configure if a camel-cache should try to find implementations of jcache api in runtimes like OSGi.
|=======================================================================
{% endraw %}
// endpoint options: END
@@ -43,6 +46,8 @@ The JCache component supports 22 endpoint options which are listed below:
+
+
// component options: START
The JCache component has no options.
// component options: END
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheComponent.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheComponent.java
index c94af86..8ee5052 100644
--- a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheComponent.java
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheComponent.java
@@ -36,9 +36,9 @@ public class JCacheComponent extends UriEndpointComponent {
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
- JCacheConfiguration configuration = new JCacheConfiguration(getCamelContext());
+ JCacheConfiguration configuration = new JCacheConfiguration(getCamelContext(), remaining);
setProperties(configuration, parameters);
- return new JCacheEndpoint(uri, this, configuration, remaining);
+ return new JCacheEndpoint(uri, this, configuration);
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheConfiguration.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheConfiguration.java
index ceda31a..05dd5a6 100644
--- a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheConfiguration.java
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheConfiguration.java
@@ -93,14 +93,46 @@ public class JCacheConfiguration {
@UriParam(label = "advanced", defaultValue = "true")
private boolean createCacheIfNotExists = true;
- private final CamelContext camelContext;
+ @UriParam(label = "advanced")
+ private boolean lookupProviders;
+
+ private CamelContext camelContext;
+ private String cacheName;
+
public JCacheConfiguration() {
- this(null);
+ this(null, null);
}
- public JCacheConfiguration(CamelContext camelContext) {
+ public JCacheConfiguration(String cacheName) {
+ this(null, cacheName);
+ }
+
+ public JCacheConfiguration(CamelContext camelContext, String cacheName) {
this.camelContext = camelContext;
+ this.cacheName = cacheName;
+ }
+
+ public CamelContext getCamelContext() {
+ return this.camelContext;
+ }
+
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
+ public String getCacheName() {
+ return this.cacheName;
+ }
+
+ public void setCacheName(String cacheName) {
+ this.cacheName = cacheName;
+ }
+
+ public ClassLoader getApplicationContextClassLoader() {
+ return this.camelContext != null
+ ? this.camelContext.getApplicationContextClassLoader()
+ : null;
}
/**
@@ -315,4 +347,16 @@ public class JCacheConfiguration {
public void setCreateCacheIfNotExists(boolean createCacheIfNotExists) {
this.createCacheIfNotExists = createCacheIfNotExists;
}
+
+ public boolean isLookupProviders() {
+ return lookupProviders;
+ }
+
+ /**
+ * Configure if a camel-cache should try to find implementations of jcache
+ * api in runtimes like OSGi.
+ */
+ public void setLookupProviders(boolean lookupProviders) {
+ this.lookupProviders = lookupProviders;
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheEndpoint.java
----------------------------------------------------------------------
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 ffa8c2d..079ab12 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
@@ -38,16 +38,12 @@ public class JCacheEndpoint extends DefaultEndpoint {
private final JCacheConfiguration cacheConfiguration;
private final JCacheManager<Object, Object> cacheManager;
- public JCacheEndpoint(String uri, JCacheComponent component, JCacheConfiguration configuration, String cacheName) {
+ public JCacheEndpoint(String uri, JCacheComponent component, JCacheConfiguration configuration) {
super(uri, component);
- this.cacheName = cacheName;
+ this.cacheName = configuration.getCacheName();
this.cacheConfiguration = configuration;
- this.cacheManager = new JCacheManager<>(
- configuration,
- cacheName,
- component.getCamelContext().getApplicationContextClassLoader(),
- component.getCamelContext());
+ this.cacheManager = JCacheHelper.createManager(configuration);
}
@Override
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheHelper.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..bf591a7
--- /dev/null
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheHelper.java
@@ -0,0 +1,63 @@
+/**
+ * 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;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
+import org.apache.camel.RuntimeCamelException;
+
+public final class JCacheHelper {
+ private JCacheHelper() {
+ }
+
+ public static <K, V> JCacheManager<K, V> createManager(JCacheConfiguration configuration) {
+ try {
+ // Check if we are in an osgi container
+ Class.forName("org.osgi.framework.FrameworkUtil");
+ 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 (ClassNotFoundException e) {
+ return new JCacheManager<>(configuration);
+ } catch (Exception e) {
+ throw new RuntimeCamelException(e);
+ }
+ }
+
+ @SuppressWarnings("uncheked")
+ public static <T> T tcclProxy(final T instance, Class<T> type, final ClassLoader classLoader) {
+ return (T) Proxy.newProxyInstance(
+ JCacheHelper.class.getClassLoader(),
+ new Class<?>[] {
+ type
+ },
+ (Object proxy, Method method, Object[] args) -> {
+ final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+ try {
+ Thread.currentThread().setContextClassLoader(classLoader);
+ return method.invoke(instance, args);
+ } finally {
+ Thread.currentThread().setContextClassLoader(tccl);
+ }
+ }
+ );
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheManager.java
----------------------------------------------------------------------
diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheManager.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheManager.java
index b7eb90c..fd7c70c 100644
--- a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheManager.java
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheManager.java
@@ -31,43 +31,32 @@ import org.apache.camel.CamelContext;
import org.apache.camel.util.ObjectHelper;
public class JCacheManager<K, V> implements Closeable {
- private JCacheConfiguration configuration;
+ private final JCacheConfiguration configuration;
+ private final ClassLoader classLoader;
+ private final String cacheName;
+ private final CamelContext camelContext;
private CachingProvider provider;
private CacheManager manager;
- private ClassLoader classLoader;
private Cache<K, V> cache;
- private String cacheName;
- private CamelContext camelContext;
- public JCacheManager(JCacheConfiguration configuration, String cacheName) {
- this(configuration, cacheName, null, null);
- }
-
- public JCacheManager(JCacheConfiguration configuration, String cacheName, CamelContext camelContext) {
- this(configuration, cacheName, null, camelContext);
- }
-
- public JCacheManager(JCacheConfiguration configuration, String cacheName, ClassLoader classLoader) {
- this(configuration, cacheName, classLoader, null);
- }
-
- public JCacheManager(JCacheConfiguration configuration, String cacheName, ClassLoader classLoader, CamelContext camelContext) {
+ public JCacheManager(JCacheConfiguration configuration) {
this.configuration = configuration;
+ this.camelContext = configuration.getCamelContext();
+ this.classLoader = camelContext != null ? camelContext.getApplicationContextClassLoader() : null;
+ this.cacheName = configuration.getCacheName();
this.provider = null;
this.manager = null;
- this.classLoader = classLoader;
this.cache = null;
- this.cacheName = cacheName;
- this.camelContext = camelContext;
}
public JCacheManager(Cache<K, V> cache) {
- this.cache = cache;
this.configuration = null;
+ this.camelContext = null;
+ this.classLoader = null;
this.cacheName = cache.getName();
this.provider = null;
this.manager = null;
- this.camelContext = null;
+ this.cache = cache;
}
public String getCacheName() {
@@ -80,18 +69,53 @@ public class JCacheManager<K, V> implements Closeable {
public synchronized Cache<K, V> getCache() throws Exception {
if (cache == null) {
+ JCacheProvider provider = JCacheProviders.lookup(configuration.getCachingProvider());
+ cache = doGetCache(provider);
+ }
+
+ return cache;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public synchronized void close() throws IOException {
+ if (configuration != null) {
+ if (cache != null) {
+ cache.close();
+ }
+
+ if (manager != null) {
+ manager.close();
+ }
+
+ if (provider != null) {
+ provider.close();
+ }
+ }
+ }
+
+ protected CacheEntryEventFilter getEventFilter() {
+ if (configuration.getEventFilters() != null) {
+ return new JCacheEntryEventFilters.Chained(configuration.getEventFilters());
+ }
+
+ return new JCacheEntryEventFilters.Named(configuration.getFilteredEvents());
+ }
+
+ protected Cache<K, V> doGetCache(JCacheProvider jcacheProvider) throws Exception {
+ if (cache == null) {
String uri = configuration.getConfigurationUri();
if (uri != null && camelContext != null) {
uri = camelContext.resolvePropertyPlaceholders(uri);
}
- provider = configuration.getCachingProvider() != null
- ? Caching.getCachingProvider(configuration.getCachingProvider())
+ provider = ObjectHelper.isNotEmpty(jcacheProvider.className())
+ ? Caching.getCachingProvider(jcacheProvider.className())
: Caching.getCachingProvider();
manager = provider.getCacheManager(
ObjectHelper.isNotEmpty(uri) ? URI.create(uri) : null,
- classLoader,
+ null,
configuration.getCacheConfigurationProperties());
cache = manager.getCache(cacheName);
@@ -110,25 +134,7 @@ public class JCacheManager<K, V> implements Closeable {
return cache;
}
- @Override
- public synchronized void close() throws IOException {
- if (configuration != null) {
- if (cache != null) {
- cache.close();
- }
-
- if (manager != null) {
- manager.close();
- }
-
- if (provider != null) {
- provider.close();
- }
- }
- }
-
-
- Configuration getOrCreateCacheConfiguration() {
+ private Configuration getOrCreateCacheConfiguration() {
if (configuration.getCacheConfiguration() != null) {
return configuration.getCacheConfiguration();
}
@@ -152,12 +158,4 @@ public class JCacheManager<K, V> implements Closeable {
return mutableConfiguration;
}
-
- CacheEntryEventFilter getEventFilter() {
- if (configuration.getEventFilters() != null) {
- return new JCacheEntryEventFilters.Chained(configuration.getEventFilters());
- }
-
- return new JCacheEntryEventFilters.Named(configuration.getFilteredEvents());
- }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheProvider.java
----------------------------------------------------------------------
diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheProvider.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheProvider.java
new file mode 100644
index 0000000..254ee91
--- /dev/null
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheProvider.java
@@ -0,0 +1,22 @@
+/**
+ * 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;
+
+public interface JCacheProvider {
+ String shortName();
+ String className();
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheProviders.java
----------------------------------------------------------------------
diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheProviders.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheProviders.java
new file mode 100644
index 0000000..dc08832
--- /dev/null
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheProviders.java
@@ -0,0 +1,71 @@
+/**
+ * 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;
+
+public enum JCacheProviders implements JCacheProvider {
+ hazelcast {{
+ shortName = "hazelcast";
+ className = "com.hazelcast.cache.HazelcastCachingProvider";
+ }},
+ ehcache {{
+ shortName = "ehcache";
+ className = "org.ehcache.jsr107.EhcacheCachingProvider";
+ }},
+ caffeine {{
+ shortName = "caffeine";
+ className = "com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider";
+ }},
+ ispnEmbedded {{
+ shortName = "infinispan-embedded";
+ className = "org.infinispan.jcache.embedded.JCachingProvider";
+ }};
+
+ protected String shortName;
+ protected String className;
+
+ @Override
+ public String shortName() {
+ return shortName;
+ }
+
+ @Override
+ public String className() {
+ return className;
+ }
+
+ public static JCacheProvider lookup(String providerName) {
+ if (providerName != null) {
+ for (JCacheProvider provider : values()) {
+ if (provider.shortName().equals(providerName) || provider.className().equals(providerName)) {
+ return provider;
+ }
+ }
+ }
+
+ return new JCacheProvider() {
+ @Override
+ public String shortName() {
+ return providerName;
+ }
+
+ @Override
+ public String className() {
+ return providerName;
+ }
+ };
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManager.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..9d55e87
--- /dev/null
+++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManager.java
@@ -0,0 +1,113 @@
+/**
+ * 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 {
+ if (jcl != null) {
+ 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;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepository.java
----------------------------------------------------------------------
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 341c9ca..482dd26 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
@@ -26,26 +26,28 @@ import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.component.jcache.JCacheConfiguration;
import org.apache.camel.component.jcache.JCacheManager;
+import org.apache.camel.component.jcache.JCacheHelper;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.impl.DefaultExchangeHolder;
import org.apache.camel.spi.OptimisticLockingAggregationRepository;
import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JCacheAggregationRepository extends ServiceSupport implements OptimisticLockingAggregationRepository {
private static final Logger LOG = LoggerFactory.getLogger(JCacheAggregationRepository.class);
- private JCacheConfiguration configuration = new JCacheConfiguration();
- private String cacheName;
- private ClassLoader classLoader;
- private CamelContext camelContext;
+ private JCacheConfiguration configuration;
private Cache<String, DefaultExchangeHolder> cache;
private boolean optimistic;
private boolean allowSerializedHeaders;
-
private JCacheManager<String, DefaultExchangeHolder> cacheManager;
+ public JCacheAggregationRepository() {
+ this.configuration = new JCacheConfiguration();
+ }
+
public JCacheConfiguration getConfiguration() {
return configuration;
}
@@ -55,27 +57,11 @@ public class JCacheAggregationRepository extends ServiceSupport implements Opti
}
public String getCacheName() {
- return cacheName;
+ return configuration.getCacheName();
}
public void setCacheName(String cacheName) {
- this.cacheName = cacheName;
- }
-
- public ClassLoader getClassLoader() {
- return classLoader;
- }
-
- public void setClassLoader(ClassLoader classLoader) {
- this.classLoader = classLoader;
- }
-
- public CamelContext getCamelContext() {
- return camelContext;
- }
-
- public void setCamelContext(CamelContext camelContext) {
- this.camelContext = camelContext;
+ configuration.setCacheName(cacheName);
}
public Cache<String, DefaultExchangeHolder> getCache() {
@@ -185,7 +171,10 @@ public class JCacheAggregationRepository extends ServiceSupport implements Opti
if (cache != null) {
cacheManager = new JCacheManager<>(cache);
} else {
- cacheManager = new JCacheManager(configuration, cacheName, classLoader, camelContext);
+ cacheManager = JCacheHelper.createManager(
+ ObjectHelper.notNull(configuration, "configuration")
+ );
+
cache = cacheManager.getCache();
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/idempotent/JCacheIdempotentRepository.java
----------------------------------------------------------------------
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 7564ce5..b41c32c 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,25 +18,27 @@ package org.apache.camel.component.jcache.processor.idempotent;
import javax.cache.Cache;
-import org.apache.camel.CamelContext;
import org.apache.camel.api.management.ManagedAttribute;
import org.apache.camel.api.management.ManagedOperation;
import org.apache.camel.api.management.ManagedResource;
import org.apache.camel.component.jcache.JCacheConfiguration;
import org.apache.camel.component.jcache.JCacheManager;
+import org.apache.camel.component.jcache.JCacheHelper;
import org.apache.camel.spi.IdempotentRepository;
import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.util.ObjectHelper;
@ManagedResource(description = "JCache based message id repository")
public class JCacheIdempotentRepository extends ServiceSupport implements IdempotentRepository<Object> {
- private JCacheConfiguration configuration = new JCacheConfiguration();
- private String cacheName;
- private ClassLoader classLoader;
- private CamelContext camelContext;
+ private JCacheConfiguration configuration;
private Cache<Object, Boolean> cache;
-
private JCacheManager<Object, Boolean> cacheManager;
+ public JCacheIdempotentRepository() {
+ this.configuration = new JCacheConfiguration();
+ }
+
+
public JCacheConfiguration getConfiguration() {
return configuration;
}
@@ -45,22 +47,6 @@ public class JCacheIdempotentRepository extends ServiceSupport implements Idempo
this.configuration = configuration;
}
- public ClassLoader getClassLoader() {
- return classLoader;
- }
-
- public void setClassLoader(ClassLoader classLoader) {
- this.classLoader = classLoader;
- }
-
- public CamelContext getCamelContext() {
- return camelContext;
- }
-
- public void setCamelContext(CamelContext camelContext) {
- this.camelContext = camelContext;
- }
-
public Cache<Object, Boolean> getCache() {
return cache;
}
@@ -94,12 +80,12 @@ public class JCacheIdempotentRepository extends ServiceSupport implements Idempo
}
public void setCacheName(String cacheName) {
- this.cacheName = cacheName;
+ configuration.setCacheName(cacheName);
}
@ManagedAttribute(description = "The processor name")
public String getCacheName() {
- return cacheName;
+ return configuration.getCacheName();
}
@Override
@@ -112,7 +98,10 @@ public class JCacheIdempotentRepository extends ServiceSupport implements Idempo
if (cache != null) {
cacheManager = new JCacheManager<>(cache);
} else {
- cacheManager = new JCacheManager(configuration, cacheName, classLoader, camelContext);
+ cacheManager = JCacheHelper.createManager(
+ ObjectHelper.notNull(configuration, "configuration")
+ );
+
cache = cacheManager.getCache();
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/JCacheManagerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/JCacheManagerTest.java b/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/JCacheManagerTest.java
index 3eb98b1..b5b3278 100644
--- a/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/JCacheManagerTest.java
+++ b/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/JCacheManagerTest.java
@@ -23,8 +23,9 @@ public class JCacheManagerTest extends JCacheComponentTestSupport {
@Test
public void testCacheCreation() throws Exception {
JCacheConfiguration conf = new JCacheConfiguration();
+ conf.setCacheName(randomString());
- JCacheManager<Object, Object> manager = new JCacheManager<>(conf, randomString());
+ JCacheManager<Object, Object> manager = new JCacheManager<>(conf);
assertNotNull(manager.getCache());
manager.close();
@@ -33,9 +34,10 @@ public class JCacheManagerTest extends JCacheComponentTestSupport {
@Test(expected = IllegalStateException.class)
public void testCacheCreationFailure() throws Exception {
JCacheConfiguration conf = new JCacheConfiguration();
+ conf.setCacheName(randomString());
conf.setCreateCacheIfNotExists(false);
- new JCacheManager<>(conf, randomString()).getCache();
+ new JCacheManager<>(conf).getCache();
fail("Should have raised IllegalStateException");
}
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTestSupport.java
----------------------------------------------------------------------
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 c4bc2a4..73eea13 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
@@ -23,8 +23,7 @@ class JCacheAggregationRepositoryTestSupport extends CamelTestSupport {
protected JCacheAggregationRepository createRepository(boolean optimistic) throws Exception {
JCacheAggregationRepository repository = new JCacheAggregationRepository();
- repository.setConfiguration(new JCacheConfiguration());
- repository.setCacheName("aggregation-repository");
+ repository.setConfiguration(new JCacheConfiguration("aggregation-repository"));
repository.setOptimistic(optimistic);
return repository;
http://git-wip-us.apache.org/repos/asf/camel/blob/e3c9f1b0/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/idempotent/JCacheIdempotentRepositoryTest.java
----------------------------------------------------------------------
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 0235654..3923a5d 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
@@ -20,6 +20,7 @@ import javax.cache.Cache;
import org.apache.camel.component.jcache.JCacheConfiguration;
import org.apache.camel.component.jcache.JCacheManager;
+import org.apache.camel.component.jcache.JCacheHelper;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.After;
import org.junit.Before;
@@ -37,7 +38,7 @@ public class JCacheIdempotentRepositoryTest extends CamelTestSupport {
@Before
public void setUp() throws Exception {
- cacheManager = new JCacheManager<>(new JCacheConfiguration(), "idempotent-repository");
+ cacheManager = JCacheHelper.createManager(new JCacheConfiguration("idempotent-repository"));
cache = cacheManager.getCache();
repository = new JCacheIdempotentRepository();