You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by st...@apache.org on 2014/05/03 23:10:46 UTC
svn commit: r1592277 - in /commons/proper/jcs/trunk: commons-jcs-jcache/
commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/
commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/
commons-jcs-jcache/src/main/resources/META-INF/...
Author: struberg
Date: Sat May 3 21:10:45 2014
New Revision: 1592277
URL: http://svn.apache.org/r1592277
Log:
JCS-120 add CDI support and fix TCK
I Just applied it. Patch provided by rmannibucau.
Thanks for your work, Romain!
Added:
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java (with props)
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationContextImpl.java (with props)
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationParameterImpl.java (with props)
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyGeneratorImpl.java (with props)
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyInvocationContextImpl.java (with props)
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheMethodDetailsImpl.java (with props)
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResolverFactoryImpl.java (with props)
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResolverImpl.java (with props)
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/GeneratedCacheKeyImpl.java (with props)
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/resources/META-INF/beans.xml (with props)
commons/proper/jcs/trunk/commons-jcs-tck-tests/run-tck.sh (with props)
commons/proper/jcs/trunk/commons-jcs-tck-tests/src/test/java/org/apache/commons/jcs/jcache/EnsureCDIIsTestedWhenTCKsRunTest.java (with props)
Modified:
commons/proper/jcs/trunk/commons-jcs-jcache/pom.xml
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CachePutInterceptor.java
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveAllInterceptor.java
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveInterceptor.java
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResultInterceptor.java
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
commons/proper/jcs/trunk/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/CacheTest.java
commons/proper/jcs/trunk/commons-jcs-tck-tests/src/test/java/org/apache/commons/jcs/jcache/OWBBeanProvider.java
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/pom.xml?rev=1592277&r1=1592276&r2=1592277&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/pom.xml (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/pom.xml Sat May 3 21:10:45 2014
@@ -47,6 +47,12 @@
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-atinject_1.0_spec</artifactId>
+ <version>1.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.0_spec</artifactId>
<scope>provided</scope>
</dependency>
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java?rev=1592277&r1=1592276&r2=1592277&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java Sat May 3 21:10:45 2014
@@ -108,19 +108,12 @@ public class JCSCachingManager implement
{
assertNotClosed();
assertNotNull(cacheName, "cacheName");
+ assertNotNull(configuration, "configuration");
final Class<?> keyType = configuration == null ? Object.class : configuration.getKeyType();
final Class<?> valueType = configuration == null ? Object.class : configuration.getValueType();
if (!caches.containsKey(cacheName))
{
- final Cache<K, V> cache = ClassLoaderAwareHandler.newProxy(loader, new JCSCache/*
- * <
- * K
- * ,
- * V
- * ,
- * C
- * >
- */(loader, this, new JCSConfiguration(
+ final Cache<K, V> cache = ClassLoaderAwareHandler.newProxy(loader, new JCSCache(loader, this, new JCSConfiguration(
configuration, keyType, valueType), instance.getCache(cacheName), instance.getConfigurationProperties()), Cache.class);
caches.putIfAbsent(cacheName, cache);
}
@@ -144,6 +137,7 @@ public class JCSCachingManager implement
cache.close();
instance.freeCache(cacheName, true);
}
+ instance.shutDown();
}
@Override
@@ -247,6 +241,8 @@ public class JCSCachingManager implement
{
assertNotClosed();
assertNotNull(cacheName, "cacheName");
+ assertNotNull(keyType, "keyType");
+ assertNotNull(valueType, "valueType");
try
{
return doGetCache(cacheName, keyType, valueType);
Added: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java?rev=1592277&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java (added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java Sat May 3 21:10:45 2014
@@ -0,0 +1,190 @@
+/*
+ * 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.commons.jcs.jcache.cdi;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.cache.annotation.CacheDefaults;
+import javax.cache.annotation.CacheKeyGenerator;
+import javax.cache.annotation.CacheResolverFactory;
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.Dependent;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
+import javax.interceptor.InvocationContext;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+@ApplicationScoped
+public class CDIJCacheHelper
+{
+ private static final Log LOGGER = LogFactory.getLog(CDIJCacheHelper.class);
+
+ private final CacheResolverFactory defaultCacheResolverFactory = new CacheResolverFactoryImpl();
+ private final CacheKeyGeneratorImpl defaultCacheKeyGenerator = new CacheKeyGeneratorImpl();
+ private final ConcurrentMap<Method, String> generatedNames = new ConcurrentHashMap<Method, String>();
+
+ @Inject
+ private BeanManager beanManager;
+
+ public String defaultName(final Method method, final CacheDefaults defaults, final String cacheName)
+ {
+ if (!cacheName.isEmpty())
+ {
+ return cacheName;
+ }
+ if (defaults != null)
+ {
+ final String name = defaults.cacheName();
+ if (!name.isEmpty())
+ {
+ return name;
+ }
+ }
+
+ String computedName = generatedNames.get(method);
+ if (computedName == null)
+ {
+ final StringBuilder name = new StringBuilder(method.getDeclaringClass().getName());
+ name.append(".");
+ name.append(method.getName());
+ name.append("(");
+ final Class<?>[] parameterTypes = method.getParameterTypes();
+ for (int pIdx = 0; pIdx < parameterTypes.length; pIdx++)
+ {
+ name.append(parameterTypes[pIdx].getName());
+ if ((pIdx + 1) < parameterTypes.length)
+ {
+ name.append(",");
+ }
+ }
+ name.append(")");
+ computedName = name.toString();
+ generatedNames.putIfAbsent(method, computedName);
+ }
+ return computedName;
+ }
+
+ public CacheDefaults findDefaults(final InvocationContext ic)
+ {
+ Class<?> clazz = ic.getTarget().getClass();
+ CacheDefaults annotation = null;
+ while (clazz != null && clazz != Object.class)
+ {
+ annotation = clazz.getAnnotation(CacheDefaults.class);
+ if (annotation != null)
+ {
+ break;
+ }
+ clazz = clazz.getSuperclass();
+ }
+ return annotation;
+ }
+
+ public boolean isIncluded(final Class<?> aClass, final Class<?>[] in, final Class<?>[] out)
+ {
+ if (in.length == 0 && out.length == 0)
+ {
+ return false;
+ }
+ for (final Class<?> potentialIn : in)
+ {
+ if (potentialIn.isAssignableFrom(aClass))
+ {
+ for (final Class<?> potentialOut : out)
+ {
+ if (potentialOut.isAssignableFrom(aClass))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public CacheKeyGenerator cacheKeyGeneratorFor(final CacheDefaults defaults, final Class<? extends CacheKeyGenerator> cacheKeyGenerator)
+ {
+ if (!CacheKeyGenerator.class.equals(cacheKeyGenerator))
+ {
+ return instance(cacheKeyGenerator);
+ }
+ if (defaults != null)
+ {
+ final Class<? extends CacheKeyGenerator> defaultCacheKeyGenerator = defaults.cacheKeyGenerator();
+ if (!CacheResolverFactory.class.equals(defaultCacheKeyGenerator))
+ {
+ return instance(defaultCacheKeyGenerator);
+ }
+ }
+ return defaultCacheKeyGenerator;
+ }
+
+ public CacheResolverFactory cacheResolverFactoryFor(final CacheDefaults defaults, final Class<? extends CacheResolverFactory> cacheResolverFactory)
+ {
+ if (!CacheResolverFactory.class.equals(cacheResolverFactory))
+ {
+ return instance(cacheResolverFactory);
+ }
+ if (defaults != null)
+ {
+ final Class<? extends CacheResolverFactory> defaultCacheResolverFactory = defaults.cacheResolverFactory();
+ if (!CacheResolverFactory.class.equals(defaultCacheResolverFactory))
+ {
+ return instance(defaultCacheResolverFactory);
+ }
+ }
+ return defaultCacheResolverFactory;
+ }
+
+ public <T> T instance(final Class<T> type)
+ {
+ final Set<Bean<?>> beans = beanManager.getBeans(type);
+ if (beans.isEmpty())
+ {
+ return null;
+ }
+ final Bean<?> bean = beanManager.resolve(beans);
+ final CreationalContext<?> context = beanManager.createCreationalContext(bean);
+ final Class<? extends Annotation> scope = bean.getScope();
+ final boolean dependent = Dependent.class.equals(scope);
+ if (!dependent && !beanManager.isNormalScope(scope))
+ {
+ LOGGER.warn("Not normal scope beans (" + type.getName() + ") can leak");
+ }
+ try
+ {
+ return (T) beanManager.getReference(bean, bean.getBeanClass(), context);
+ }
+ finally
+ {
+ if (dependent)
+ { // TODO: depent or pseudo scope?
+ context.release();
+ }
+ }
+ }
+}
Propchange: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationContextImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationContextImpl.java?rev=1592277&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationContextImpl.java (added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationContextImpl.java Sat May 3 21:10:45 2014
@@ -0,0 +1,70 @@
+/*
+ * 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.commons.jcs.jcache.cdi;
+
+import javax.cache.annotation.CacheInvocationContext;
+import javax.cache.annotation.CacheInvocationParameter;
+import javax.interceptor.InvocationContext;
+import java.lang.annotation.Annotation;
+import java.util.HashSet;
+
+import static java.util.Arrays.asList;
+
+public class CacheInvocationContextImpl<A extends Annotation> extends CacheMethodDetailsImpl<A> implements CacheInvocationContext<A>
+{
+ private CacheInvocationParameter[] parameters = null;
+
+ public CacheInvocationContextImpl(final InvocationContext delegate, final A cacheAnnotation, final String cacheName)
+ {
+ super(delegate, cacheAnnotation, cacheName);
+ }
+
+ @Override
+ public Object getTarget()
+ {
+ return delegate.getTarget();
+ }
+
+ @Override
+ public CacheInvocationParameter[] getAllParameters()
+ {
+ if (parameters == null)
+ {
+ final Object[] args = delegate.getParameters();
+ final Class<?>[] parameterTypes = getMethod().getParameterTypes();
+ final Annotation[][] parameterAnnotations = getMethod().getParameterAnnotations();
+ parameters = new CacheInvocationParameter[args.length];
+ for (int i = 0; i < args.length; i++)
+ {
+ parameters[i] = new CacheInvocationParameterImpl(parameterTypes[i], args[i], new HashSet<Annotation>(asList(parameterAnnotations[i])), i);
+ }
+ }
+ return parameters;
+ }
+
+ @Override
+ public <T> T unwrap(final Class<T> cls)
+ {
+ if (cls.isAssignableFrom(getClass()))
+ {
+ return cls.cast(this);
+ }
+ throw new IllegalArgumentException(cls.getName());
+ }
+}
Propchange: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationContextImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationParameterImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationParameterImpl.java?rev=1592277&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationParameterImpl.java (added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationParameterImpl.java Sat May 3 21:10:45 2014
@@ -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.commons.jcs.jcache.cdi;
+
+import javax.cache.annotation.CacheInvocationParameter;
+import java.lang.annotation.Annotation;
+import java.util.Set;
+
+public class CacheInvocationParameterImpl implements CacheInvocationParameter
+{
+ private final Class<?> type;
+ private final Object value;
+ private final Set<Annotation> annotations;
+ private final int position;
+
+ public CacheInvocationParameterImpl(final Class<?> type, final Object value, final Set<Annotation> annotations, final int position)
+ {
+ this.type = type;
+ this.value = value;
+ this.annotations = annotations;
+ this.position = position;
+ }
+
+ @Override
+ public Class<?> getRawType()
+ {
+ return type;
+ }
+
+ @Override
+ public Object getValue()
+ {
+ return value;
+ }
+
+ @Override
+ public Set<Annotation> getAnnotations()
+ {
+ return annotations;
+ }
+
+ @Override
+ public int getParameterPosition()
+ {
+ return position;
+ }
+}
Propchange: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationParameterImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyGeneratorImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyGeneratorImpl.java?rev=1592277&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyGeneratorImpl.java (added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyGeneratorImpl.java Sat May 3 21:10:45 2014
@@ -0,0 +1,40 @@
+/*
+ * 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.commons.jcs.jcache.cdi;
+
+import javax.cache.annotation.CacheInvocationParameter;
+import javax.cache.annotation.CacheKeyGenerator;
+import javax.cache.annotation.CacheKeyInvocationContext;
+import javax.cache.annotation.GeneratedCacheKey;
+import java.lang.annotation.Annotation;
+
+public class CacheKeyGeneratorImpl implements CacheKeyGenerator
+{
+ @Override
+ public GeneratedCacheKey generateCacheKey(final CacheKeyInvocationContext<? extends Annotation> cacheKeyInvocationContext)
+ {
+ final CacheInvocationParameter[] keyParameters = cacheKeyInvocationContext.getKeyParameters();
+ final Object[] parameters = new Object[keyParameters.length];
+ for (int index = 0; index < keyParameters.length; index++)
+ {
+ parameters[index] = keyParameters[index].getValue();
+ }
+ return new GeneratedCacheKeyImpl(parameters);
+ }
+}
Propchange: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyGeneratorImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyInvocationContextImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyInvocationContextImpl.java?rev=1592277&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyInvocationContextImpl.java (added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyInvocationContextImpl.java Sat May 3 21:10:45 2014
@@ -0,0 +1,87 @@
+/*
+ * 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.commons.jcs.jcache.cdi;
+
+import javax.cache.annotation.CacheInvocationParameter;
+import javax.cache.annotation.CacheKey;
+import javax.cache.annotation.CacheKeyInvocationContext;
+import javax.cache.annotation.CacheValue;
+import javax.interceptor.InvocationContext;
+import java.lang.annotation.Annotation;
+import java.util.Collection;
+import java.util.LinkedList;
+
+public class CacheKeyInvocationContextImpl<A extends Annotation> extends CacheInvocationContextImpl<A> implements CacheKeyInvocationContext<A>
+{
+ private CacheInvocationParameter[] keyParams = null;
+ private CacheInvocationParameter valueParam = null;
+
+ public CacheKeyInvocationContextImpl(final InvocationContext delegate, final A annotation, final String name)
+ {
+ super(delegate, annotation, name);
+ }
+
+ @Override
+ public CacheInvocationParameter[] getKeyParameters()
+ {
+ if (keyParams == null)
+ {
+ final Collection<CacheInvocationParameter> keys = new LinkedList<CacheInvocationParameter>();
+ for (final CacheInvocationParameter param : getAllParameters())
+ {
+ for (final Annotation a : param.getAnnotations())
+ {
+ if (a.annotationType().equals(CacheKey.class))
+ {
+ keys.add(param);
+ }
+ }
+ }
+ if (keys.isEmpty())
+ {
+ keyParams = getAllParameters();
+ }
+ else
+ {
+ keyParams = keys.toArray(new CacheInvocationParameter[keys.size()]);
+ }
+ }
+ return keyParams;
+ }
+
+ @Override
+ public CacheInvocationParameter getValueParameter()
+ {
+ if (valueParam == null)
+ {
+ for (final CacheInvocationParameter param : getAllParameters())
+ {
+ for (final Annotation a : param.getAnnotations())
+ {
+ if (a.annotationType().equals(CacheValue.class))
+ {
+ valueParam = param;
+ return valueParam;
+ }
+ }
+ }
+ }
+ return valueParam;
+ }
+}
Propchange: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyInvocationContextImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheMethodDetailsImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheMethodDetailsImpl.java?rev=1592277&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheMethodDetailsImpl.java (added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheMethodDetailsImpl.java Sat May 3 21:10:45 2014
@@ -0,0 +1,68 @@
+/*
+ * 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.commons.jcs.jcache.cdi;
+
+import javax.cache.annotation.CacheMethodDetails;
+import javax.interceptor.InvocationContext;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.HashSet;
+import java.util.Set;
+
+import static java.util.Arrays.asList;
+
+public class CacheMethodDetailsImpl<A extends Annotation> implements CacheMethodDetails<A>
+{
+ protected final InvocationContext delegate;
+ private final Set<Annotation> annotations;
+ private final A cacheAnnotation;
+ private final String cacheName;
+
+ public CacheMethodDetailsImpl(final InvocationContext delegate, final A cacheAnnotation, final String cacheName)
+ {
+ this.delegate = delegate;
+ this.annotations = new HashSet<Annotation>(asList(delegate.getMethod().getAnnotations()));
+ this.cacheAnnotation = cacheAnnotation;
+ this.cacheName = cacheName;
+ }
+
+ @Override
+ public Method getMethod()
+ {
+ return delegate.getMethod();
+ }
+
+ @Override
+ public Set<Annotation> getAnnotations()
+ {
+ return annotations;
+ }
+
+ @Override
+ public A getCacheAnnotation()
+ {
+ return cacheAnnotation;
+ }
+
+ @Override
+ public String getCacheName()
+ {
+ return cacheName;
+ }
+}
Propchange: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheMethodDetailsImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CachePutInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CachePutInterceptor.java?rev=1592277&r1=1592276&r2=1592277&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CachePutInterceptor.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CachePutInterceptor.java Sat May 3 21:10:45 2014
@@ -18,7 +18,12 @@
*/
package org.apache.commons.jcs.jcache.cdi;
+import javax.cache.Cache;
+import javax.cache.annotation.CacheDefaults;
+import javax.cache.annotation.CacheKeyInvocationContext;
import javax.cache.annotation.CachePut;
+import javax.cache.annotation.GeneratedCacheKey;
+import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
@@ -27,9 +32,54 @@ import javax.interceptor.InvocationConte
@Interceptor
public class CachePutInterceptor
{
+ @Inject
+ private CDIJCacheHelper helper;
+
@AroundInvoke
- public Object cache(final InvocationContext ic) throws Exception
+ public Object cache(final InvocationContext ic) throws Throwable
+ {
+ final CacheDefaults defaults = helper.findDefaults(ic);
+
+ final CachePut cachePut = ic.getMethod().getAnnotation(CachePut.class);
+ final String cacheName = helper.defaultName(ic.getMethod(), defaults, cachePut.cacheName());
+ final boolean afterInvocation = cachePut.afterInvocation();
+
+ final CacheKeyInvocationContext<CachePut> context = new CacheKeyInvocationContextImpl<CachePut>(ic, cachePut, cacheName);
+ if (!afterInvocation)
+ {
+ doCache(context, defaults, cachePut);
+ }
+
+ final Object result;
+ try
+ {
+ result = ic.proceed();
+ }
+ catch (final Throwable t)
+ {
+ if (afterInvocation)
+ {
+ if (helper.isIncluded(t.getClass(), cachePut.cacheFor(), cachePut.noCacheFor()))
+ {
+ doCache(context, defaults, cachePut);
+ }
+ }
+
+ throw t;
+ }
+
+ if (afterInvocation)
+ {
+ doCache(context, defaults, cachePut);
+ }
+
+ return result;
+ }
+
+ private void doCache(final CacheKeyInvocationContext<CachePut> context, final CacheDefaults defaults, final CachePut cachePut)
{
- throw new UnsupportedOperationException("TODO");
+ final Cache<Object, Object> cache = helper.cacheResolverFactoryFor(defaults, cachePut.cacheResolverFactory()).getCacheResolver(context).resolveCache(context);
+ final GeneratedCacheKey key = helper.cacheKeyGeneratorFor(defaults, cachePut.cacheKeyGenerator()).generateCacheKey(context);
+ cache.put(key, context.getValueParameter().getValue());
}
}
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveAllInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveAllInterceptor.java?rev=1592277&r1=1592276&r2=1592277&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveAllInterceptor.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveAllInterceptor.java Sat May 3 21:10:45 2014
@@ -18,7 +18,11 @@
*/
package org.apache.commons.jcs.jcache.cdi;
+import javax.cache.Cache;
+import javax.cache.annotation.CacheDefaults;
+import javax.cache.annotation.CacheKeyInvocationContext;
import javax.cache.annotation.CacheRemoveAll;
+import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
@@ -27,9 +31,52 @@ import javax.interceptor.InvocationConte
@Interceptor
public class CacheRemoveAllInterceptor
{
+ @Inject
+ private CDIJCacheHelper helper;
+
@AroundInvoke
- public Object cache(final InvocationContext ic) throws Exception
+ public Object cache(final InvocationContext ic) throws Throwable
+ {
+ final CacheDefaults defaults = helper.findDefaults(ic);
+
+ final CacheRemoveAll cacheRemoveAll = ic.getMethod().getAnnotation(CacheRemoveAll.class);
+ final String cacheName = helper.defaultName(ic.getMethod(), defaults, cacheRemoveAll.cacheName());
+ final boolean afterInvocation = cacheRemoveAll.afterInvocation();
+
+ final CacheKeyInvocationContext<CacheRemoveAll> context = new CacheKeyInvocationContextImpl<CacheRemoveAll>(ic, cacheRemoveAll, cacheName);
+ if (!afterInvocation)
+ {
+ removeAll(context, defaults, cacheRemoveAll);
+ }
+
+ final Object result;
+ try
+ {
+ result = ic.proceed();
+ }
+ catch (final Throwable t)
+ {
+ if (afterInvocation)
+ {
+ if (helper.isIncluded(t.getClass(), cacheRemoveAll.evictFor(), cacheRemoveAll.noEvictFor()))
+ {
+ removeAll(context, defaults, cacheRemoveAll);
+ }
+ }
+ throw t;
+ }
+
+ if (afterInvocation)
+ {
+ removeAll(context, defaults, cacheRemoveAll);
+ }
+
+ return result;
+ }
+
+ private void removeAll(final CacheKeyInvocationContext<CacheRemoveAll> context, final CacheDefaults defaults, final CacheRemoveAll cacheRemoveAll)
{
- throw new UnsupportedOperationException("TODO");
+ final Cache<Object, Object> cache = helper.cacheResolverFactoryFor(defaults, cacheRemoveAll.cacheResolverFactory()).getCacheResolver(context).resolveCache(context);
+ cache.removeAll();
}
}
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveInterceptor.java?rev=1592277&r1=1592276&r2=1592277&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveInterceptor.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveInterceptor.java Sat May 3 21:10:45 2014
@@ -18,7 +18,12 @@
*/
package org.apache.commons.jcs.jcache.cdi;
+import javax.cache.Cache;
+import javax.cache.annotation.CacheDefaults;
+import javax.cache.annotation.CacheKeyInvocationContext;
import javax.cache.annotation.CacheRemove;
+import javax.cache.annotation.GeneratedCacheKey;
+import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
@@ -27,9 +32,55 @@ import javax.interceptor.InvocationConte
@Interceptor
public class CacheRemoveInterceptor
{
+ @Inject
+ private CDIJCacheHelper helper;
+
@AroundInvoke
- public Object cache(final InvocationContext ic) throws Exception
+ public Object cache(final InvocationContext ic) throws Throwable
+ {
+ final CacheDefaults defaults = helper.findDefaults(ic);
+
+ final CacheRemove cacheRemove = ic.getMethod().getAnnotation(CacheRemove.class);
+ final String cacheName = helper.defaultName(ic.getMethod(), defaults, cacheRemove.cacheName());
+ final boolean afterInvocation = cacheRemove.afterInvocation();
+
+ final CacheKeyInvocationContext<CacheRemove> context = new CacheKeyInvocationContextImpl<CacheRemove>(ic, cacheRemove, cacheName);
+
+ if (!afterInvocation)
+ {
+ doRemove(context, defaults, cacheRemove);
+ }
+
+ final Object result;
+ try
+ {
+ result = ic.proceed();
+ }
+ catch (final Throwable t)
+ {
+ if (afterInvocation)
+ {
+ if (helper.isIncluded(t.getClass(), cacheRemove.evictFor(), cacheRemove.noEvictFor()))
+ {
+ doRemove(context, defaults, cacheRemove);
+ }
+ }
+
+ throw t;
+ }
+
+ if (afterInvocation)
+ {
+ doRemove(context, defaults, cacheRemove);
+ }
+
+ return result;
+ }
+
+ private void doRemove(final CacheKeyInvocationContext<CacheRemove> context, final CacheDefaults defaults, final CacheRemove cacheRemove)
{
- throw new UnsupportedOperationException("TODO");
+ final Cache<Object, Object> cache = helper.cacheResolverFactoryFor(defaults, cacheRemove.cacheResolverFactory()).getCacheResolver(context).resolveCache(context);
+ final GeneratedCacheKey key = helper.cacheKeyGeneratorFor(defaults, cacheRemove.cacheKeyGenerator()).generateCacheKey(context);
+ cache.remove(key);
}
}
Added: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResolverFactoryImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResolverFactoryImpl.java?rev=1592277&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResolverFactoryImpl.java (added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResolverFactoryImpl.java Sat May 3 21:10:45 2014
@@ -0,0 +1,74 @@
+/*
+ * 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.commons.jcs.jcache.cdi;
+
+import javax.cache.Cache;
+import javax.cache.CacheManager;
+import javax.cache.Caching;
+import javax.cache.annotation.CacheMethodDetails;
+import javax.cache.annotation.CacheResolver;
+import javax.cache.annotation.CacheResolverFactory;
+import javax.cache.annotation.CacheResult;
+import javax.cache.configuration.MutableConfiguration;
+import javax.cache.spi.CachingProvider;
+import java.lang.annotation.Annotation;
+
+public class CacheResolverFactoryImpl implements CacheResolverFactory
+{
+ private final CacheManager cacheManager;
+
+ public CacheResolverFactoryImpl()
+ {
+ final CachingProvider provider = Caching.getCachingProvider();
+ cacheManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader());
+ }
+
+ @Override
+ public CacheResolver getCacheResolver(CacheMethodDetails<? extends Annotation> cacheMethodDetails)
+ {
+ return findCacheResolver(cacheMethodDetails.getCacheName());
+ }
+
+ @Override
+ public CacheResolver getExceptionCacheResolver(final CacheMethodDetails<CacheResult> cacheMethodDetails)
+ {
+ final String exceptionCacheName = cacheMethodDetails.getCacheAnnotation().exceptionCacheName();
+ if (exceptionCacheName == null || exceptionCacheName.isEmpty())
+ {
+ throw new IllegalArgumentException("CacheResult.exceptionCacheName() not specified");
+ }
+ return findCacheResolver(exceptionCacheName);
+ }
+
+ private CacheResolver findCacheResolver(String exceptionCacheName)
+ {
+ Cache<?, ?> cache = cacheManager.getCache(exceptionCacheName);
+ if (cache == null)
+ {
+ cache = createCache(exceptionCacheName);
+ }
+ return new CacheResolverImpl(cache);
+ }
+
+ private Cache<?, ?> createCache(final String exceptionCacheName)
+ {
+ cacheManager.createCache(exceptionCacheName, new MutableConfiguration<Object, Object>());
+ return cacheManager.getCache(exceptionCacheName);
+ }
+}
Propchange: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResolverFactoryImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResolverImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResolverImpl.java?rev=1592277&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResolverImpl.java (added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResolverImpl.java Sat May 3 21:10:45 2014
@@ -0,0 +1,40 @@
+/*
+ * 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.commons.jcs.jcache.cdi;
+
+import javax.cache.Cache;
+import javax.cache.annotation.CacheInvocationContext;
+import javax.cache.annotation.CacheResolver;
+import java.lang.annotation.Annotation;
+
+public class CacheResolverImpl implements CacheResolver
+{
+ private final Cache<?, ?> delegate;
+
+ public CacheResolverImpl(final Cache<?, ?> cache)
+ {
+ delegate = cache;
+ }
+
+ @Override
+ public <K, V> Cache<K, V> resolveCache(final CacheInvocationContext<? extends Annotation> cacheInvocationContext)
+ {
+ return (Cache<K, V>) delegate;
+ }
+}
Propchange: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResolverImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResultInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResultInterceptor.java?rev=1592277&r1=1592276&r2=1592277&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResultInterceptor.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResultInterceptor.java Sat May 3 21:10:45 2014
@@ -18,7 +18,14 @@
*/
package org.apache.commons.jcs.jcache.cdi;
+import javax.cache.Cache;
+import javax.cache.annotation.CacheDefaults;
+import javax.cache.annotation.CacheKeyInvocationContext;
+import javax.cache.annotation.CacheResolver;
+import javax.cache.annotation.CacheResolverFactory;
import javax.cache.annotation.CacheResult;
+import javax.cache.annotation.GeneratedCacheKey;
+import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
@@ -27,9 +34,69 @@ import javax.interceptor.InvocationConte
@Interceptor
public class CacheResultInterceptor
{
+ @Inject
+ private CDIJCacheHelper helper;
+
@AroundInvoke
- public Object cache(final InvocationContext ic) throws Exception
+ public Object cache(final InvocationContext ic) throws Throwable
{
- throw new UnsupportedOperationException("TODO");
+ final CacheDefaults defaults = helper.findDefaults(ic);
+
+ final CacheResult cacheResult = ic.getMethod().getAnnotation(CacheResult.class);
+ final String cacheName = helper.defaultName(ic.getMethod(), defaults, cacheResult.cacheName());
+
+ final CacheKeyInvocationContext<CacheResult> context = new CacheKeyInvocationContextImpl<CacheResult>(ic, cacheResult, cacheName);
+
+ final CacheResolverFactory cacheResolverFactory = helper.cacheResolverFactoryFor(defaults, cacheResult.cacheResolverFactory());
+ final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context);
+ final Cache<Object, Object> cache = cacheResolver.resolveCache(context);
+
+ final GeneratedCacheKey cacheKey = helper.cacheKeyGeneratorFor(defaults, cacheResult.cacheKeyGenerator()).generateCacheKey(context);
+
+ Cache<Object, Object> exceptionCache = null; // lazily created
+
+ Object result;
+ if (!cacheResult.skipGet())
+ {
+ result = cache.get(cacheKey);
+ if (result != null)
+ {
+ return result;
+ }
+
+
+ if (!cacheResult.exceptionCacheName().isEmpty())
+ {
+ exceptionCache = cacheResolverFactory.getExceptionCacheResolver(context).resolveCache(context);
+ final Object exception = exceptionCache.get(cacheKey);
+ if (exception != null)
+ {
+ throw Throwable.class.cast(exception);
+ }
+ }
+ }
+
+ try
+ {
+ result = ic.proceed();
+ if (result != null)
+ {
+ cache.put(cacheKey, result);
+ }
+
+ return result;
+ }
+ catch (final Throwable t)
+ {
+ if (helper.isIncluded(t.getClass(), cacheResult.cachedExceptions(), cacheResult.nonCachedExceptions()))
+ {
+ if (exceptionCache == null)
+ {
+ exceptionCache = cacheResolverFactory.getExceptionCacheResolver(context).resolveCache(context);
+ }
+ exceptionCache.put(cacheKey, t);
+ }
+ throw t;
+ }
}
}
Added: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/GeneratedCacheKeyImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/GeneratedCacheKeyImpl.java?rev=1592277&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/GeneratedCacheKeyImpl.java (added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/GeneratedCacheKeyImpl.java Sat May 3 21:10:45 2014
@@ -0,0 +1,56 @@
+/*
+ * 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.commons.jcs.jcache.cdi;
+
+import javax.cache.annotation.GeneratedCacheKey;
+import java.util.Arrays;
+
+public class GeneratedCacheKeyImpl implements GeneratedCacheKey
+{
+ private final Object[] params;
+ private final int hash;
+
+ public GeneratedCacheKeyImpl(final Object[] parameters)
+ {
+ this.params = parameters;
+ this.hash = Arrays.deepHashCode(parameters);
+ }
+
+ @Override
+ public boolean equals(final Object o)
+ {
+ if (this == o)
+ {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass())
+ {
+ return false;
+ }
+ final GeneratedCacheKeyImpl that = GeneratedCacheKeyImpl.class.cast(o);
+ return Arrays.deepEquals(params, that.params);
+
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return hash;
+ }
+}
Propchange: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/GeneratedCacheKeyImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/resources/META-INF/beans.xml
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/resources/META-INF/beans.xml?rev=1592277&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/resources/META-INF/beans.xml (added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/resources/META-INF/beans.xml Sat May 3 21:10:45 2014
@@ -0,0 +1,31 @@
+<?xml version="1.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.
+
+-->
+<beans xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://java.sun.com/xml/ns/javaee
+ http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
+ <interceptors>
+ <class>org.apache.commons.jcs.jcache.cdi.CachePutInterceptor</class>
+ <class>org.apache.commons.jcs.jcache.cdi.CacheRemoveAllInterceptor</class>
+ <class>org.apache.commons.jcs.jcache.cdi.CacheRemoveInterceptor</class>
+ <class>org.apache.commons.jcs.jcache.cdi.CacheResultInterceptor</class>
+ </interceptors>
+</beans>
Propchange: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/resources/META-INF/beans.xml
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension?rev=1592277&r1=1592276&r2=1592277&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension Sat May 3 21:10:45 2014
@@ -14,5 +14,4 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-
-org.apache.commons.jcs.jcache.cdi.MakeJCacheCDIIntercetporFriendly
+org.apache.commons.jcs.jcache.cdi.MakeJCacheCDIInterceptorFriendly
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/CacheTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/CacheTest.java?rev=1592277&r1=1592276&r2=1592277&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/CacheTest.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/CacheTest.java Sat May 3 21:10:45 2014
@@ -34,6 +34,7 @@ import javax.cache.configuration.CacheEn
import javax.cache.configuration.CompleteConfiguration;
import javax.cache.configuration.Configuration;
import javax.cache.configuration.Factory;
+import javax.cache.configuration.MutableConfiguration;
import javax.cache.event.CacheEntryCreatedListener;
import javax.cache.event.CacheEntryEvent;
import javax.cache.event.CacheEntryEventFilter;
@@ -56,7 +57,7 @@ public class CacheTest
{
final CachingProvider cachingProvider = Caching.getCachingProvider();
final CacheManager cacheManager = cachingProvider.getCacheManager();
- cacheManager.createCache("default", (Configuration<String, String>)null);
+ cacheManager.createCache("default", new MutableConfiguration<Object, Object>());
final Cache<String, String> cache = cacheManager.getCache("default");
assertFalse(cache.containsKey("foo"));
cache.put("foo", "bar");
@@ -72,7 +73,7 @@ public class CacheTest
{
final CachingProvider cachingProvider = Caching.getCachingProvider();
final CacheManager cacheManager = cachingProvider.getCacheManager();
- cacheManager.createCache("default", (Configuration<String, String>)null);
+ cacheManager.createCache("default", new MutableConfiguration<Object, Object>());
final Cache<String, String> cache = cacheManager.getCache("default");
final Set<String> event = new HashSet<String>();
cache.registerCacheEntryListener(new CacheEntryListenerConfiguration<String, String>()
@@ -292,13 +293,13 @@ public class CacheTest
@Override
public Class<Object> getKeyType()
{
- return null;
+ return Object.class;
}
@Override
public Class<Object> getValueType()
{
- return null;
+ return Object.class;
}
@Override
Added: commons/proper/jcs/trunk/commons-jcs-tck-tests/run-tck.sh
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-tck-tests/run-tck.sh?rev=1592277&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-tck-tests/run-tck.sh (added)
+++ commons/proper/jcs/trunk/commons-jcs-tck-tests/run-tck.sh Sat May 3 21:10:45 2014
@@ -0,0 +1,3 @@
+#! /bin/bash
+mvn clean install -PjcacheTck
+
Propchange: commons/proper/jcs/trunk/commons-jcs-tck-tests/run-tck.sh
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/jcs/trunk/commons-jcs-tck-tests/src/test/java/org/apache/commons/jcs/jcache/EnsureCDIIsTestedWhenTCKsRunTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-tck-tests/src/test/java/org/apache/commons/jcs/jcache/EnsureCDIIsTestedWhenTCKsRunTest.java?rev=1592277&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-tck-tests/src/test/java/org/apache/commons/jcs/jcache/EnsureCDIIsTestedWhenTCKsRunTest.java (added)
+++ commons/proper/jcs/trunk/commons-jcs-tck-tests/src/test/java/org/apache/commons/jcs/jcache/EnsureCDIIsTestedWhenTCKsRunTest.java Sat May 3 21:10:45 2014
@@ -0,0 +1,42 @@
+/*
+ * 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.commons.jcs.jcache;
+
+import org.junit.Test;
+
+import javax.cache.annotation.BeanProvider;
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+// useless test but without it we are not sure
+// CDI TCKs passed
+public class EnsureCDIIsTestedWhenTCKsRunTest
+{
+ @Test
+ public void checkOWBProvider()
+ {
+ final Iterator<BeanProvider> iterator = ServiceLoader.load(BeanProvider.class).iterator();
+ assertTrue(iterator.hasNext());
+ assertThat(iterator.next(), instanceOf(OWBBeanProvider.class));
+ }
+}
Propchange: commons/proper/jcs/trunk/commons-jcs-tck-tests/src/test/java/org/apache/commons/jcs/jcache/EnsureCDIIsTestedWhenTCKsRunTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/proper/jcs/trunk/commons-jcs-tck-tests/src/test/java/org/apache/commons/jcs/jcache/OWBBeanProvider.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-tck-tests/src/test/java/org/apache/commons/jcs/jcache/OWBBeanProvider.java?rev=1592277&r1=1592276&r2=1592277&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-tck-tests/src/test/java/org/apache/commons/jcs/jcache/OWBBeanProvider.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-tck-tests/src/test/java/org/apache/commons/jcs/jcache/OWBBeanProvider.java Sat May 3 21:10:45 2014
@@ -26,41 +26,40 @@ import javax.cache.annotation.BeanProvid
import javax.enterprise.inject.spi.Bean;
import java.util.Set;
-public class OWBBeanProvider
- implements BeanProvider
+public class OWBBeanProvider implements BeanProvider
{
private final BeanManagerImpl bm;
public OWBBeanProvider()
{
final WebBeansContext webBeansContext = WebBeansContext.currentInstance();
- final ContainerLifecycle lifecycle = webBeansContext.getService( ContainerLifecycle.class );
- lifecycle.startApplication( null );
- Runtime.getRuntime().addShutdownHook( new Thread()
+ final ContainerLifecycle lifecycle = webBeansContext.getService(ContainerLifecycle.class);
+ lifecycle.startApplication(null);
+ Runtime.getRuntime().addShutdownHook(new Thread()
{
@Override
public void run()
{
- lifecycle.stopApplication( null );
+ lifecycle.stopApplication(null);
}
- } );
+ });
bm = webBeansContext.getBeanManagerImpl();
}
@Override
- public <T> T getBeanByType( final Class<T> tClass )
+ public <T> T getBeanByType(final Class<T> tClass)
{
- if ( tClass == null )
+ if (tClass == null)
{
- throw new IllegalArgumentException( "no bean class specified" );
+ throw new IllegalArgumentException("no bean class specified");
}
- final Set<Bean<?>> beans = bm.getBeans( tClass );
- if ( beans.isEmpty() )
+ final Set<Bean<?>> beans = bm.getBeans(tClass);
+ if (beans.isEmpty())
{
- throw new IllegalStateException( "no bean of type " + tClass.getName() );
+ throw new IllegalStateException("no bean of type " + tClass.getName());
}
- final Bean<?> bean = bm.resolve( beans );
- return (T) bm.getReference( bean, bean.getBeanClass(), bm.createCreationalContext( bean ) );
+ final Bean<?> bean = bm.resolve(beans);
+ return (T) bm.getReference(bean, bean.getBeanClass(), bm.createCreationalContext(bean));
}
}