Author: olamy Date: Sun Apr 27 23:48:32 2014 New Revision: 1590531 URL: http://svn.apache.org/r1590531 Log: first version passing 465 test - not cdi ones. stats to review (they are not yet correct) and code to check
Added: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/proxy/ExceptionWrapperHandler.java (with props) commons/proper/jcs/trunk/src/tck-resources/ commons/proper/jcs/trunk/src/tck-resources/ExcludeList commons/proper/jcs/trunk/src/tck-resources/log4j.properties (with props) Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/JCSCache.java commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/JCSCachingManager.java commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/TempStateCacheView.java commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/proxy/ClassLoaderAwareHandler.java commons/proper/jcs/trunk/tck.xml Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/JCSCache.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/JCSCache.java?rev=1590531&r1=1590530&r2=1590531&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/JCSCache.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/JCSCache.java Sun Apr 27 23:48:32 2014 @@ -8,6 +8,7 @@ import org.apache.commons.jcs.engine.con import org.apache.commons.jcs.jcache.jmx.JCSCacheMXBean; import org.apache.commons.jcs.jcache.jmx.JCSCacheStatisticsMXBean; import org.apache.commons.jcs.jcache.jmx.JMXs; +import org.apache.commons.jcs.jcache.proxy.ExceptionWrapperHandler; import org.apache.commons.jcs.utils.serialization.StandardSerializer; import javax.cache.Cache; @@ -24,6 +25,7 @@ import javax.cache.expiry.ExpiryPolicy; import javax.cache.integration.CacheLoader; import javax.cache.integration.CacheLoaderException; import javax.cache.integration.CacheWriter; +import javax.cache.integration.CacheWriterException; import javax.cache.integration.CompletionListener; import javax.cache.processor.EntryProcessor; import javax.cache.processor.EntryProcessorException; @@ -54,7 +56,8 @@ public class JCSCache<K extends Serializ private final Statistics statistics = new Statistics(); private final IElementSerializer serializer = new StandardSerializer(); - public JCSCache(final CacheManager mgr, final JCSConfiguration<K, V> configuration, final CompositeCache<K, JCSElement<V>> cache) { + public JCSCache(final ClassLoader classLoader, final CacheManager mgr, final JCSConfiguration<K, V> configuration, + final CompositeCache<K, JCSElement<V>> cache) { manager = mgr; delegate = new CacheAccess<K, JCSElement<V>>(cache); config = configuration; @@ -63,14 +66,14 @@ public class JCSCache<K extends Serializ if (cacheLoaderFactory == null) { loader = NoLoader.INSTANCE; } else { - loader = cacheLoaderFactory.create(); + loader = ExceptionWrapperHandler.newProxy(classLoader, cacheLoaderFactory.create(), CacheLoaderException.class, CacheLoader.class); } final Factory<CacheWriter<? super K, ? super V>> cacheWriterFactory = configuration.getCacheWriterFactory(); if (cacheWriterFactory == null) { writer = NoWriter.INSTANCE; } else { - writer = cacheWriterFactory.create(); + writer = ExceptionWrapperHandler.newProxy(classLoader, cacheWriterFactory.create(), CacheWriterException.class, CacheWriter.class); } final Factory<ExpiryPolicy> expiryPolicyFactory = configuration.getExpiryPolicyFactory(); @@ -122,25 +125,24 @@ public class JCSCache<K extends Serializ return doGetControllingExpiry(key, true, false, false, true); } - private V doLoad(final K key, final boolean forceDoLoad, final boolean propagateLoadException) { + private V doLoad(final K key, final boolean update, final boolean propagateLoadException) { V v = null; try { v = loader.load(key); - } catch (final RuntimeException e) { + } catch (final CacheLoaderException e) { if (propagateLoadException) { - throw new CacheLoaderException(e); + throw e; } } if (v != null) { try { - final Duration duration = forceDoLoad ? expiryPolicy.getExpiryForUpdate() : expiryPolicy.getExpiryForCreation(); + final Duration duration = update ? expiryPolicy.getExpiryForUpdate() : expiryPolicy.getExpiryForCreation(); if (duration == null || !duration.isZero()) { delegate.put(key, new JCSElement<V>(v, duration)); } } catch (final CacheException e) { throw new IllegalStateException(e); } - writer.write(new JCSEntry<K, V>(key, v)); } return v; } @@ -179,11 +181,13 @@ public class JCSCache<K extends Serializ } result.put(key, val); } - for (final K k : keys) { - if (!result.containsKey(k)) { - final V v = doLoad(k, false, true); - if (v != null) { - result.put(k, v); + if (config.isReadThrough() && result.size() != keys.size()) { + for (final K k : keys) { + if (!result.containsKey(k)) { + final V v = doLoad(k, false, false); + if (v != null) { + result.put(k, v); + } } } } @@ -219,8 +223,8 @@ public class JCSCache<K extends Serializ delegate.remove(key); } } else { - delegate.put(storeByValue ? copy(key) : key, element); writer.write(new JCSEntry<K, V>(key, value)); + delegate.put(storeByValue ? copy(key) : key, element); for (final JCSListener<K, V> listener : listeners.values()) { if (created) { listener.onCreated(Arrays.<CacheEntryEvent<? extends K, ? extends V>>asList( @@ -280,10 +284,12 @@ public class JCSCache<K extends Serializ assertNotClosed(); assertNotNull(key, "key"); - final long start = config.isStatisticsEnabled() ? Times.now() : 0; + final boolean statisticsEnabled = config.isStatisticsEnabled(); + final long start = statisticsEnabled ? Times.now() : 0; final JCSElement<V> v = delegate.get(key); final V value = v != null && v.getElement() != null ? v.getElement() : null; + writer.delete(key); boolean remove = delegate.getCacheControl().remove(key); if (v != null && v.isExpired()) { remove = false; @@ -292,7 +298,7 @@ public class JCSCache<K extends Serializ listener.onRemoved(Arrays.<CacheEntryEvent<? extends K, ? extends V>>asList( new JCSCacheEntryEvent<K, V>(this, EventType.REMOVED, null, key, value))); } - if (remove && config.isStatisticsEnabled()) { + if (remove && statisticsEnabled) { statistics.increaseRemovals(1); statistics.addRemoveTime(Times.now() - start); } @@ -326,21 +332,22 @@ public class JCSCache<K extends Serializ private V doGetControllingExpiry(final K key, final boolean updateAcess, final boolean forceDoLoad, final boolean skipLoad, final boolean propagateLoadException) { + final boolean statisticsEnabled = config.isStatisticsEnabled(); final long getStart = Times.now(); final JCSElement<V> elt = delegate.get(key); V v = elt != null? elt.getElement() : null; - if (v == null && config.isReadThrough()) { + if (v == null && (config.isReadThrough() || forceDoLoad)) { if (!skipLoad) { - v = doLoad(key, forceDoLoad, propagateLoadException); + v = doLoad(key, false, propagateLoadException); } - } else if (config.isStatisticsEnabled()) { - if (elt != null) { + } else if (statisticsEnabled) { + if (v != null) { statistics.increaseHits(1); } else { statistics.increaseMisses(1); - statistics.addGetTime(Times.now() - getStart); } } + if (updateAcess && elt != null) { elt.update(expiryPolicy.getExpiryForAccess()); if (elt.isExpired()) { @@ -440,17 +447,7 @@ public class JCSCache<K extends Serializ @Override public void removeAll() { - if (config.isStatisticsEnabled()) { - int i = 0; - final CompositeCache<K, JCSElement<V>> underlying = delegate.getCacheControl(); - for (final K elt : underlying.getKeySet()) { - if (!underlying.get(elt).getVal().isExpired()) { - i++; - } - } - statistics.increaseRemovals(i); - } - clear(); + removeAll(delegate.getCacheControl().getKeySet()); } @Override @@ -480,15 +477,13 @@ public class JCSCache<K extends Serializ // TODO: async try { for (final K k : keys) { - final boolean removed; if (replaceExistingValues) { - removed = delegate.getCacheControl().remove(k); + final V v = doLoad(k, containsKey(k), completionListener != null); + continue; } else if (containsKey(k)) { continue; - } else { - removed = false; } - doGetControllingExpiry(k, true, removed, false, completionListener != null); // will trigger cacheloader and init + doGetControllingExpiry(k, true, true, false, completionListener != null); // will trigger cacheloader and init } } catch (final RuntimeException e) { if (completionListener != null) { Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/JCSCachingManager.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/JCSCachingManager.java?rev=1590531&r1=1590530&r2=1590531&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/JCSCachingManager.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/JCSCachingManager.java Sun Apr 27 23:48:32 2014 @@ -79,6 +79,7 @@ public class JCSCachingManager implement final Cache<K, V> cache = ClassLoaderAwareHandler.newProxy( loader, new JCSCache/*<K, V, C>*/( + loader, this, new JCSConfiguration(configuration, configuration.getKeyType(), configuration.getValueType()), instance.getCache(cacheName)), Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/TempStateCacheView.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/TempStateCacheView.java?rev=1590531&r1=1590530&r2=1590531&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/TempStateCacheView.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/TempStateCacheView.java Sun Apr 27 23:48:32 2014 @@ -20,6 +20,7 @@ import java.util.Set; import static org.apache.commons.jcs.jcache.Asserts.assertNotNull; +// kind of transactional view for a Cache<K, V>, to use with EntryProcessor public class TempStateCacheView<K extends Serializable, V extends Serializable> implements Cache<K, V> { private final JCSCache<K, V, ?> cache; private final Map<K, V> put = new HashMap<K, V>(); @@ -41,7 +42,7 @@ public class TempStateCacheView<K extend return v; } - // for an EntryProcessor we already incremented stats + // for an EntryProcessor we already incremented stats - to enhance surely if (cache.getConfiguration(CompleteConfiguration.class).isStatisticsEnabled()) { final Statistics statistics = cache.getStatistics(); if (cache.containsKey(key)) { @@ -112,9 +113,12 @@ public class TempStateCacheView<K extend } public boolean remove(final K key) { + final boolean noop = put.containsKey(key); put.remove(key); - if (!ignoreKey(key) && cache.containsKey(key)) { - remove.add(key); + if (!ignoreKey(key)) { + if (!noop) { + remove.add(key); + } return true; } return false; Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/proxy/ClassLoaderAwareHandler.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/proxy/ClassLoaderAwareHandler.java?rev=1590531&r1=1590530&r2=1590531&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/proxy/ClassLoaderAwareHandler.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/proxy/ClassLoaderAwareHandler.java Sun Apr 27 23:48:32 2014 @@ -46,7 +46,7 @@ public class ClassLoaderAwareHandler imp return method.invoke(delegate, args); } - private boolean isEquals(Method method, Object[] args) { + private boolean isEquals(final Method method, final Object[] args) { return "equals".equals(method.getName()) && args != null && args.length == 1; } Added: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/proxy/ExceptionWrapperHandler.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/proxy/ExceptionWrapperHandler.java?rev=1590531&view=auto ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/proxy/ExceptionWrapperHandler.java (added) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/proxy/ExceptionWrapperHandler.java Sun Apr 27 23:48:32 2014 @@ -0,0 +1,44 @@ +package org.apache.commons.jcs.jcache.proxy; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +public class ExceptionWrapperHandler<T> implements InvocationHandler { + private final T delegate; + private final Constructor<? extends RuntimeException> wrapper; + + public ExceptionWrapperHandler(final T delegate, final Class<? extends RuntimeException> exceptionType) { + this.delegate = delegate; + try { + this.wrapper = exceptionType.getConstructor(Throwable.class); + } catch (final NoSuchMethodException e) { + throw new IllegalStateException(e); + } + } + + @Override + public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { + try { + return method.invoke(delegate, args); + } catch (final InvocationTargetException ite) { + final Throwable e = ite.getCause(); + if (RuntimeException.class.isInstance(e)) { + final RuntimeException re; + try { + re = wrapper.newInstance(e); + } catch (final Exception e1) { + throw new IllegalArgumentException(e1); + } + throw re; + } + throw e; + } + } + + public static <T> T newProxy(final ClassLoader loader, final T delegate, final Class<? extends RuntimeException> exceptionType, final Class<T> apis) { + return (T) Proxy.newProxyInstance(loader, new Class<?>[] { apis }, new ExceptionWrapperHandler<T>(delegate, exceptionType)); + } +} Propchange: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/proxy/ExceptionWrapperHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/jcache/proxy/ExceptionWrapperHandler.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: commons/proper/jcs/trunk/src/tck-resources/ExcludeList URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/tck-resources/ExcludeList?rev=1590531&view=auto ============================================================================== --- commons/proper/jcs/trunk/src/tck-resources/ExcludeList (added) +++ commons/proper/jcs/trunk/src/tck-resources/ExcludeList Sun Apr 27 23:48:32 2014 @@ -0,0 +1,2 @@ +# for tck this test needs to be excluded +org.jsr107.tck.CachingTest#dummyTest Added: commons/proper/jcs/trunk/src/tck-resources/log4j.properties URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/tck-resources/log4j.properties?rev=1590531&view=auto ============================================================================== --- commons/proper/jcs/trunk/src/tck-resources/log4j.properties (added) +++ commons/proper/jcs/trunk/src/tck-resources/log4j.properties Sun Apr 27 23:48:32 2014 @@ -0,0 +1,19 @@ +# 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. +log4j.rootCategory=INFO, stdout +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout Propchange: commons/proper/jcs/trunk/src/tck-resources/log4j.properties ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/jcs/trunk/src/tck-resources/log4j.properties ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: commons/proper/jcs/trunk/tck.xml URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/tck.xml?rev=1590531&r1=1590530&r2=1590531&view=diff ============================================================================== --- commons/proper/jcs/trunk/tck.xml (original) +++ commons/proper/jcs/trunk/tck.xml Sun Apr 27 23:48:32 2014 @@ -3,6 +3,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- + NOTE: ENSURE TO hAVE BUILD commons-jcs BEFORE. + TO RUN TCKs CALL: $ mvn -f tck.xml test @@ -100,7 +102,7 @@ <build> <testResources> <testResource> - <directory>src/test/resources</directory> + <directory>src/tck-resources</directory> <filtering>true</filtering> </testResource> </testResources> @@ -154,7 +156,7 @@ <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> - <version>2.9</version> + <version>2.17</version> <configuration> <systemPropertyVariables> <domainJar>${domain-lib-dir}/${domain-jar}</domainJar>