Author: davsclaus
Date: Mon Apr 11 07:52:41 2011
New Revision: 1090960

URL: http://svn.apache.org/viewvc?rev=1090960&view=rev
Log:
CAMEL-3849: Fixed camel-cache to load ehcache.xml in OSGi. Thanks to Piotr 
Klimczak for the patch.

Added:
    
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java
    
camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheManagerFactoryRefTest.java
Modified:
    
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
    
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheManagerFactory.java
    
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedMessageBodyReplacer.java
    
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedTokenReplacer.java
    
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java
    
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheValidate.java

Modified: 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java?rev=1090960&r1=1090959&r2=1090960&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
 (original)
+++ 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
 Mon Apr 11 07:52:41 2011
@@ -27,7 +27,7 @@ import org.apache.camel.util.ServiceHelp
 
 public class CacheComponent extends DefaultComponent {
     private CacheConfiguration configuration;
-    private CacheManagerFactory cacheManagerFactory = new 
CacheManagerFactory();
+    private CacheManagerFactory cacheManagerFactory = new 
DefaultCacheManagerFactory();
     
     public CacheComponent() {
         configuration = new CacheConfiguration();
@@ -46,9 +46,11 @@ public class CacheComponent extends Defa
         CacheConfiguration config = configuration.copy();
 
         config.parseURI(new URI(uri));
-        
+
+        setProperties(this, parameters);
+        setProperties(config, parameters);
+
         CacheEndpoint cacheEndpoint = new CacheEndpoint(uri, this, config, 
cacheManagerFactory);
-        setProperties(cacheEndpoint.getConfig(), parameters);
         return cacheEndpoint;
     }
 

Modified: 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheManagerFactory.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheManagerFactory.java?rev=1090960&r1=1090959&r2=1090960&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheManagerFactory.java
 (original)
+++ 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheManagerFactory.java
 Mon Apr 11 07:52:41 2011
@@ -19,17 +19,26 @@ package org.apache.camel.component.cache
 import net.sf.ehcache.CacheManager;
 import org.apache.camel.impl.ServiceSupport;
 
-public class CacheManagerFactory extends ServiceSupport {
+public abstract class CacheManagerFactory extends ServiceSupport {
     private CacheManager cacheManager;
 
     public synchronized CacheManager instantiateCacheManager() {
         if (cacheManager == null) {
-            cacheManager = CacheManager.getInstance();
+            cacheManager = createCacheManagerInstance();
         }
         
         return cacheManager;
     }
 
+    /**
+     * Creates {@link CacheManager}.
+     * <p/>
+     * The default implementation is {@link DefaultCacheManagerFactory}.
+     *
+     * @return {@link CacheManager}
+     */
+    protected abstract CacheManager createCacheManagerInstance();
+
     @Override
     protected void doStart() throws Exception {
     }

Added: 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java?rev=1090960&view=auto
==============================================================================
--- 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java
 (added)
+++ 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java
 Mon Apr 11 07:52:41 2011
@@ -0,0 +1,28 @@
+/**
+ * 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.cache;
+
+import net.sf.ehcache.CacheManager;
+
+public class DefaultCacheManagerFactory extends CacheManagerFactory {
+
+    @Override
+    protected CacheManager createCacheManagerInstance() {
+        return 
CacheManager.create(getClass().getResourceAsStream("/ehcache.xml"));
+    }
+
+}

Modified: 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedMessageBodyReplacer.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedMessageBodyReplacer.java?rev=1090960&r1=1090959&r2=1090960&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedMessageBodyReplacer.java
 (original)
+++ 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedMessageBodyReplacer.java
 Mon Apr 11 07:52:41 2011
@@ -21,7 +21,7 @@ import net.sf.ehcache.Ehcache;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
-import org.apache.camel.component.cache.CacheManagerFactory;
+import org.apache.camel.component.cache.DefaultCacheManagerFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,7 +44,7 @@ public class CacheBasedMessageBodyReplac
 
     public void process(Exchange exchange) throws Exception {
         // Cache the buffer to the specified Cache against the specified key
-        cacheManager = new CacheManagerFactory().instantiateCacheManager();
+        cacheManager = new 
DefaultCacheManagerFactory().instantiateCacheManager();
 
         if (isValid(cacheManager, cacheName, key)) {
             cache = cacheManager.getCache(cacheName);

Modified: 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedTokenReplacer.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedTokenReplacer.java?rev=1090960&r1=1090959&r2=1090960&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedTokenReplacer.java
 (original)
+++ 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedTokenReplacer.java
 Mon Apr 11 07:52:41 2011
@@ -20,9 +20,10 @@ import java.io.InputStream;
 
 import net.sf.ehcache.CacheManager;
 import net.sf.ehcache.Ehcache;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
-import org.apache.camel.component.cache.CacheManagerFactory;
+import org.apache.camel.component.cache.DefaultCacheManagerFactory;
 import org.apache.camel.converter.IOConverter;
 import org.apache.camel.util.IOHelper;
 import org.slf4j.Logger;
@@ -49,7 +50,7 @@ public class CacheBasedTokenReplacer ext
 
     public void process(Exchange exchange) throws Exception {
         // Cache the buffer to the specified Cache against the specified key
-        cacheManager = new CacheManagerFactory().instantiateCacheManager();
+        cacheManager = new 
DefaultCacheManagerFactory().instantiateCacheManager();
 
         if (isValid(cacheManager, cacheName, key)) {
             cache = cacheManager.getCache(cacheName);

Modified: 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java?rev=1090960&r1=1090959&r2=1090960&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java
 (original)
+++ 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java
 Mon Apr 11 07:52:41 2011
@@ -32,7 +32,7 @@ import net.sf.ehcache.CacheManager;
 import net.sf.ehcache.Ehcache;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
-import org.apache.camel.component.cache.CacheManagerFactory;
+import org.apache.camel.component.cache.DefaultCacheManagerFactory;
 import org.apache.camel.converter.IOConverter;
 import org.apache.camel.converter.jaxp.XmlConverter;
 import org.apache.camel.util.IOHelper;
@@ -63,7 +63,7 @@ public class CacheBasedXPathReplacer ext
 
     public void process(Exchange exchange) throws Exception {
         // Cache the buffer to the specified Cache against the specified key
-        cacheManager = new CacheManagerFactory().instantiateCacheManager();
+        cacheManager = new 
DefaultCacheManagerFactory().instantiateCacheManager();
 
         if (isValid(cacheManager, cacheName, key)) {
             cache = cacheManager.getCache(cacheName);

Modified: 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheValidate.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheValidate.java?rev=1090960&r1=1090959&r2=1090960&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheValidate.java
 (original)
+++ 
camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheValidate.java
 Mon Apr 11 07:52:41 2011
@@ -29,17 +29,12 @@ public class CacheValidate {
         LOG.trace("Cache Name: {}", cacheName);
 
         if (!cacheManager.cacheExists(cacheName)) {
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("No existing Cache found with name: " + cacheName
-                        + ". Please ensure a cache is first instantiated using 
a Cache Consumer or Cache Producer."
-                        + " Replacement will not be performed since the cache 
" + cacheName + "does not presently exist");
-            }
+            LOG.debug("No existing Cache found with name: {}"
+                    + ". Please ensure a cache is first instantiated using a 
Cache Consumer or Cache Producer."
+                    + " Replacement will not be performed since the cache {} 
does not presently exist", cacheName, cacheName);
             return false;
         }
-
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Found an existing cache: " + cacheName);
-        }
+        LOG.debug("Found an existing cache: {}", cacheName);
 
         if (LOG.isTraceEnabled()) {
             LOG.trace("Cache {} currently contains {} elements", cacheName, 
cacheManager.getCache(cacheName).getSize());
@@ -47,11 +42,8 @@ public class CacheValidate {
         Ehcache cache = cacheManager.getCache(cacheName);
 
         if (!cache.isKeyInCache(key)) {
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("No Key with name: " + key
-                        + "presently exists in the cache. It is also possible 
that the key may have expired in the cache."
-                        + " Replacement will not be performed until an 
appropriate key/value pair is added to (or) found in the cache.");
-            }
+            LOG.debug("No Key with name: {} presently exists in the cache. It 
is also possible that the key may have expired in the cache."
+                    + " Replacement will not be performed until an appropriate 
key/value pair is added to (or) found in the cache.", key);
             return false;
         }
 

Added: 
camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheManagerFactoryRefTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheManagerFactoryRefTest.java?rev=1090960&view=auto
==============================================================================
--- 
camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheManagerFactoryRefTest.java
 (added)
+++ 
camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheManagerFactoryRefTest.java
 Mon Apr 11 07:52:41 2011
@@ -0,0 +1,82 @@
+/**
+ * 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.cache;
+
+import net.sf.ehcache.CacheManager;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.CamelTestSupport;
+import org.junit.Test;
+
+public class CacheManagerFactoryRefTest extends CamelTestSupport {
+    private static final String CACHE_ENDPOINT_URI =
+            "cache://myname1?cacheManagerFactory=#testCacheManagerFactory";
+
+    @EndpointInject(uri = CACHE_ENDPOINT_URI)
+    protected CacheEndpoint cacheEndpoint;
+
+    @Produce(uri = "direct:start")
+    protected ProducerTemplate producerTemplate;
+
+    private CacheManagerFactory testingCacheManagerFactory = new 
TestingCacheManagerFactory();
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("testCacheManagerFactory", testingCacheManagerFactory);
+        return jndi;
+    }
+
+    @Override
+    public RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to(CACHE_ENDPOINT_URI);
+            }
+        };
+    }
+
+    @Test
+    public void testConfig() throws Exception {
+        producerTemplate.send(new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
+                Message in = exchange.getIn();
+                in.setHeader(CacheConstants.CACHE_OPERATION, 
CacheConstants.CACHE_OPERATION_ADD);
+                in.setHeader(CacheConstants.CACHE_KEY, "greeting");
+                in.setBody("Hello World");
+            }
+        });
+
+        assertEquals("Is CacheManagerFactory set", testingCacheManagerFactory,
+                cacheEndpoint.getCacheManagerFactory());
+    }
+
+    public static class TestingCacheManagerFactory extends CacheManagerFactory 
{
+        @Override
+        protected CacheManager createCacheManagerInstance() {
+            return 
CacheManager.create(getClass().getResourceAsStream("/ehcache.xml"));
+        }
+    }
+}


Reply via email to