You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ha...@apache.org on 2011/08/31 00:07:15 UTC
svn commit: r1163431 - in /camel/trunk:
camel-core/src/main/java/org/apache/camel/impl/
camel-core/src/main/java/org/apache/camel/util/
camel-core/src/test/java/org/apache/camel/util/
components/camel-cxf/src/main/java/org/apache/camel/component/cxf/ja...
Author: hadrian
Date: Tue Aug 30 22:07:14 2011
New Revision: 1163431
URL: http://svn.apache.org/viewvc?rev=1163431&view=rev
Log:
CAMEL-4392. LRUCache is no longer a Service. Other minor fixes.
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointRegistry.java
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUCache.java
camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUSoftCache.java
camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUCacheTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUSoftCacheTest.java
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java?rev=1163431&r1=1163430&r2=1163431&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java Tue Aug 30 22:07:14 2011
@@ -151,7 +151,7 @@ public class ConsumerCache extends Servi
public int getCapacity() {
int capacity = -1;
if (consumers instanceof LRUCache) {
- LRUCache cache = (LRUCache) consumers;
+ LRUCache<String, PollingConsumer> cache = (LRUCache<String, PollingConsumer>)consumers;
capacity = cache.getMaxCacheSize();
}
return capacity;
@@ -167,7 +167,7 @@ public class ConsumerCache extends Servi
public long getHits() {
long hits = -1;
if (consumers instanceof LRUCache) {
- LRUCache cache = (LRUCache) consumers;
+ LRUCache<String, PollingConsumer> cache = (LRUCache<String, PollingConsumer>)consumers;
hits = cache.getHits();
}
return hits;
@@ -183,7 +183,7 @@ public class ConsumerCache extends Servi
public long getMisses() {
long misses = -1;
if (consumers instanceof LRUCache) {
- LRUCache cache = (LRUCache) consumers;
+ LRUCache<String, PollingConsumer> cache = (LRUCache<String, PollingConsumer>)consumers;
misses = cache.getMisses();
}
return misses;
@@ -194,7 +194,7 @@ public class ConsumerCache extends Servi
*/
public void resetCacheStatistics() {
if (consumers instanceof LRUCache) {
- LRUCache cache = (LRUCache) consumers;
+ LRUCache<String, PollingConsumer> cache = (LRUCache<String, PollingConsumer>)consumers;
cache.resetStatistics();
}
}
@@ -212,11 +212,11 @@ public class ConsumerCache extends Servi
}
protected void doStart() throws Exception {
- ServiceHelper.startServices(consumers);
+ ServiceHelper.startServices(consumers.values());
}
protected void doStop() throws Exception {
- ServiceHelper.stopServices(consumers);
+ ServiceHelper.stopServices(consumers.values());
consumers.clear();
}
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointRegistry.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointRegistry.java?rev=1163431&r1=1163430&r2=1163431&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointRegistry.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointRegistry.java Tue Aug 30 22:07:14 2011
@@ -20,8 +20,10 @@ import java.util.Map;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
+import org.apache.camel.Service;
import org.apache.camel.util.CamelContextHelper;
import org.apache.camel.util.LRUSoftCache;
+import org.apache.camel.util.ServiceHelper;
/**
* Endpoint registry which is a based on a {@link org.apache.camel.util.LRUSoftCache}.
@@ -30,7 +32,7 @@ import org.apache.camel.util.LRUSoftCach
*
* @version
*/
-public class EndpointRegistry extends LRUSoftCache<EndpointKey, Endpoint> {
+public class EndpointRegistry extends LRUSoftCache<EndpointKey, Endpoint> implements Service {
private final CamelContext context;
@@ -44,7 +46,20 @@ public class EndpointRegistry extends LR
putAll(endpoints);
}
- /**
+ @Override
+ public void start() throws Exception {
+ resetStatistics();
+ }
+
+ @Override
+ public void stop() throws Exception {
+ if (!isEmpty()) {
+ ServiceHelper.stopServices(values());
+ }
+ purge();
+ }
+
+ /**
* Purges the cache
*/
public void purge() {
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java?rev=1163431&r1=1163430&r2=1163431&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java Tue Aug 30 22:07:14 2011
@@ -395,12 +395,14 @@ public class ProducerCache extends Servi
}
protected void doStop() throws Exception {
- ServiceHelper.stopServices(producers, pool);
+ ServiceHelper.stopServices(pool);
+ ServiceHelper.stopServices(producers.values());
producers.clear();
}
protected void doStart() throws Exception {
- ServiceHelper.startServices(pool, producers);
+ ServiceHelper.startServices(producers.values());
+ ServiceHelper.startServices(pool);
}
/**
@@ -426,7 +428,7 @@ public class ProducerCache extends Servi
public int getCapacity() {
int capacity = -1;
if (producers instanceof LRUCache) {
- LRUCache cache = (LRUCache) producers;
+ LRUCache<String, Producer> cache = (LRUCache<String, Producer>)producers;
capacity = cache.getMaxCacheSize();
}
return capacity;
@@ -442,7 +444,7 @@ public class ProducerCache extends Servi
public long getHits() {
long hits = -1;
if (producers instanceof LRUCache) {
- LRUCache cache = (LRUCache) producers;
+ LRUCache<String, Producer> cache = (LRUCache<String, Producer>)producers;
hits = cache.getHits();
}
return hits;
@@ -458,7 +460,7 @@ public class ProducerCache extends Servi
public long getMisses() {
long misses = -1;
if (producers instanceof LRUCache) {
- LRUCache cache = (LRUCache) producers;
+ LRUCache<String, Producer> cache = (LRUCache<String, Producer>)producers;
misses = cache.getMisses();
}
return misses;
@@ -469,7 +471,7 @@ public class ProducerCache extends Servi
*/
public void resetCacheStatistics() {
if (producers instanceof LRUCache) {
- LRUCache cache = (LRUCache) producers;
+ LRUCache<String, Producer> cache = (LRUCache<String, Producer>)producers;
cache.resetStatistics();
}
}
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUCache.java?rev=1163431&r1=1163430&r2=1163431&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUCache.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUCache.java Tue Aug 30 22:07:14 2011
@@ -16,21 +16,19 @@
*/
package org.apache.camel.util;
-import java.util.AbstractMap;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
-import org.apache.camel.Service;
/**
* A Least Recently Used Cache
*
* @version
*/
-public class LRUCache<K, V> implements Service, Map<K, V> {
+public class LRUCache<K, V> implements Map<K, V> {
private static final long serialVersionUID = -342098639681884414L;
private int maxCacheSize = 10000;
private final AtomicLong hits = new AtomicLong();
@@ -51,10 +49,9 @@ public class LRUCache<K, V> implements S
* or the load factor is non positive.
*/
public LRUCache(int initialCapacity, int maximumCacheSize) {
- map = new ConcurrentLinkedHashMap
- .Builder<K, V>()
- .initialCapacity(initialCapacity)
- .maximumWeightedCapacity(maximumCacheSize).build();
+ map = new ConcurrentLinkedHashMap.Builder<K, V>()
+ .initialCapacity(initialCapacity)
+ .maximumWeightedCapacity(maximumCacheSize).build();
this.maxCacheSize = maximumCacheSize;
}
@@ -100,14 +97,14 @@ public class LRUCache<K, V> implements S
}
@Override
- @SuppressWarnings("unchecked")
public void putAll(Map<? extends K, ? extends V> map) {
- ((AbstractMap)map).putAll(map);
+ this.map.putAll(map);
}
@Override
public void clear() {
map.clear();
+ resetStatistics();
}
@Override
@@ -154,24 +151,6 @@ public class LRUCache<K, V> implements S
misses.set(0);
}
- protected boolean removeEldestEntry(Map.Entry<K, V> entry) {
- return map.size() > maxCacheSize;
- }
-
- public void start() throws Exception {
- // noop
- }
-
- public void stop() throws Exception {
- // stop the value and clear the cache
- if (!isEmpty()) {
- ServiceHelper.stopServices(values());
- map.clear();
- hits.set(0);
- misses.set(0);
- }
- }
-
@Override
public String toString() {
return "LRUCache@" + ObjectHelper.getIdentityHashCode(this);
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUSoftCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUSoftCache.java?rev=1163431&r1=1163430&r2=1163431&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUSoftCache.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/LRUSoftCache.java Tue Aug 30 22:07:14 2011
@@ -104,13 +104,12 @@ public class LRUSoftCache<K, V> extends
}
@Override
- @SuppressWarnings("unchecked")
public int size() {
// only count as a size if there is a value
int size = 0;
- Collection<SoftReference<V>> col = (Collection<SoftReference<V>>) super.values();
- for (SoftReference<V> ref : col) {
- if (ref.get() != null) {
+ for (V value : super.values()) {
+ SoftReference<?> ref = (SoftReference<?>)value;
+ if (ref != null && ref.get() != null) {
size++;
}
}
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUCacheTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUCacheTest.java?rev=1163431&r1=1163430&r2=1163431&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUCacheTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUCacheTest.java Tue Aug 30 22:07:14 2011
@@ -81,35 +81,8 @@ public class LRUCacheTest extends TestCa
cache.get("B");
assertEquals(1, cache.getHits());
assertEquals(0, cache.getMisses());
- }
-
- public void testLRUCacheHitsAndMissesStop() throws Exception {
- MyService service1 = new MyService();
- MyService service2 = new MyService();
-
- cache.put("A", service1);
- cache.put("B", service2);
-
- assertEquals(0, cache.getHits());
- assertEquals(0, cache.getMisses());
-
- cache.get("A");
- assertEquals(1, cache.getHits());
- assertEquals(0, cache.getMisses());
-
- cache.get("A");
- assertEquals(2, cache.getHits());
- assertEquals(0, cache.getMisses());
-
- cache.get("B");
- assertEquals(3, cache.getHits());
- assertEquals(0, cache.getMisses());
- cache.stop();
- assertEquals(0, cache.getHits());
- assertEquals(0, cache.getMisses());
-
- cache.start();
+ cache.clear();
assertEquals(0, cache.getHits());
assertEquals(0, cache.getMisses());
@@ -118,37 +91,11 @@ public class LRUCacheTest extends TestCa
assertEquals(1, cache.getMisses());
}
- public void testLRUCacheStop() throws Exception {
- MyService service1 = new MyService();
- MyService service2 = new MyService();
-
- cache.put("A", service1);
- cache.put("B", service2);
-
- assertEquals(false, service1.isStopped());
- assertEquals(false, service2.isStopped());
-
- cache.stop();
-
- assertEquals(0, cache.size());
-
- assertEquals(true, service1.isStopped());
- assertEquals(true, service2.isStopped());
- }
-
private static final class MyService implements Service {
-
- private boolean stopped;
-
public void start() throws Exception {
}
public void stop() throws Exception {
- stopped = true;
- }
-
- public boolean isStopped() {
- return stopped;
}
}
}
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUSoftCacheTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUSoftCacheTest.java?rev=1163431&r1=1163430&r2=1163431&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUSoftCacheTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/LRUSoftCacheTest.java Tue Aug 30 22:07:14 2011
@@ -31,7 +31,6 @@ public class LRUSoftCacheTest extends Te
public void testLRUSoftCacheGetAndPut() throws Exception {
LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
- cache.start();
cache.put(1, "foo");
cache.put(2, "bar");
@@ -39,15 +38,11 @@ public class LRUSoftCacheTest extends Te
assertEquals("foo", cache.get(1));
assertEquals("bar", cache.get(2));
assertEquals(null, cache.get(3));
-
assertEquals(2, cache.size());
-
- cache.stop();
}
public void testLRUSoftCacheHitsAndMisses() throws Exception {
LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
- cache.start();
cache.put(1, "foo");
cache.put(2, "bar");
@@ -66,13 +61,10 @@ public class LRUSoftCacheTest extends Te
cache.get(2);
assertEquals(2, cache.getHits());
assertEquals(1, cache.getMisses());
-
- cache.stop();
}
public void testLRUSoftCachePutOverride() throws Exception {
LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
- cache.start();
Object old = cache.put(1, "foo");
assertNull(old);
@@ -85,15 +77,11 @@ public class LRUSoftCacheTest extends Te
old = cache.put(1, "changed");
assertEquals("foo", old);
assertEquals("changed", cache.get(1));
-
assertEquals(2, cache.size());
-
- cache.stop();
}
public void testLRUSoftCachePutAll() throws Exception {
LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
- cache.start();
Map<Integer, Object> map = new HashMap<Integer, Object>();
map.put(1, "foo");
@@ -105,16 +93,12 @@ public class LRUSoftCacheTest extends Te
assertEquals("bar", cache.get(2));
assertEquals(null, cache.get(3));
assertEquals(2, cache.size());
-
- cache.stop();
}
public void testLRUSoftCachePutAllAnotherLRUSoftCache() throws Exception {
LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
- cache.start();
LRUSoftCache<Integer, Object> cache2 = new LRUSoftCache<Integer, Object>(1000);
- cache2.start();
cache2.put(1, "foo");
cache2.put(2, "bar");
@@ -124,14 +108,10 @@ public class LRUSoftCacheTest extends Te
assertEquals("bar", cache.get(2));
assertEquals(null, cache.get(3));
assertEquals(2, cache.size());
-
- cache.stop();
- cache2.stop();
}
public void testLRUSoftCacheRemove() throws Exception {
LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
- cache.start();
cache.put(1, "foo");
cache.put(2, "bar");
@@ -139,13 +119,10 @@ public class LRUSoftCacheTest extends Te
assertEquals("bar", cache.get(2));
cache.remove(2);
assertEquals(null, cache.get(2));
-
- cache.stop();
}
public void testLRUSoftCacheValues() throws Exception {
LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
- cache.start();
cache.put(1, "foo");
cache.put(2, "bar");
@@ -156,13 +133,10 @@ public class LRUSoftCacheTest extends Te
Iterator<Object> it = col.iterator();
assertEquals("foo", it.next());
assertEquals("bar", it.next());
-
- cache.stop();
}
public void testLRUSoftCacheEmpty() throws Exception {
LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
- cache.start();
assertTrue(cache.isEmpty());
@@ -177,28 +151,10 @@ public class LRUSoftCacheTest extends Te
cache.clear();
assertTrue(cache.isEmpty());
-
- cache.stop();
- assertTrue(cache.isEmpty());
- }
-
- public void testLRUSoftCacheStopEmpty() throws Exception {
- LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
- cache.start();
-
- assertTrue(cache.isEmpty());
-
- cache.put(1, "foo");
- cache.put(2, "bar");
- assertFalse(cache.isEmpty());
-
- cache.stop();
- assertTrue(cache.isEmpty());
}
public void testLRUSoftCacheContainsKey() throws Exception {
LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
- cache.start();
assertFalse(cache.containsKey(1));
cache.put(1, "foo");
@@ -208,14 +164,13 @@ public class LRUSoftCacheTest extends Te
cache.put(2, "foo");
assertTrue(cache.containsKey(2));
- cache.stop();
+ cache.clear();
assertFalse(cache.containsKey(1));
assertFalse(cache.containsKey(2));
}
public void testLRUSoftCacheKeySet() throws Exception {
LRUSoftCache<Integer, Object> cache = new LRUSoftCache<Integer, Object>(1000);
- cache.start();
cache.put(1, "foo");
cache.put(2, "foo");
@@ -226,8 +181,5 @@ public class LRUSoftCacheTest extends Te
Iterator<Integer> it = keys.iterator();
assertEquals(1, it.next().intValue());
assertEquals(2, it.next().intValue());
-
- cache.stop();
}
-
}
Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java?rev=1163431&r1=1163430&r2=1163431&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java (original)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java Tue Aug 30 22:07:14 2011
@@ -340,11 +340,11 @@ public class CxfRsProducer extends Defau
}
public void start() throws Exception {
- this.cache.start();
+ // noop
}
public void stop() throws Exception {
- this.cache.stop();
+ cache.clear();
}
public JAXRSClientFactoryBean get(String address) throws Exception {