use of javax.cache.annotation.CachePut in project Payara by payara.
the class CachePutInterceptor method cachePut.
@AroundInvoke
public Object cachePut(InvocationContext ctx) throws Throwable {
if (!isEnabled()) {
return ctx.proceed();
}
CachePut annotation = ctx.getMethod().getAnnotation(CachePut.class);
PayaraCacheKeyInvocationContext<CachePut> pctx = new PayaraCacheKeyInvocationContext<>(ctx, annotation);
if (!annotation.afterInvocation()) {
doPut(pctx);
}
Object result = null;
try {
result = ctx.proceed();
} catch (Throwable e) {
if (annotation.afterInvocation()) {
if (shouldICache(annotation.cacheFor(), annotation.noCacheFor(), e, false)) {
doPut(pctx);
}
}
throw e;
}
if (annotation.afterInvocation()) {
doPut(pctx);
}
return result;
}
use of javax.cache.annotation.CachePut in project Payara by payara.
the class PayaraCacheKeyInvocationContext method getFactory.
public final CacheResolverFactory getFactory() {
if (factory != null) {
return factory;
}
factory = new PayaraCacheResolverFactory();
Class defaultClazz = javax.cache.annotation.CacheResolverFactory.class;
Class suggestedClazz = null;
if (annotation instanceof CachePut) {
CachePut put = CachePut.class.cast(annotation);
suggestedClazz = put.cacheResolverFactory();
} else if (annotation instanceof CacheRemove) {
CacheRemove put = CacheRemove.class.cast(annotation);
suggestedClazz = put.cacheResolverFactory();
} else if (annotation instanceof CacheResult) {
CacheResult put = CacheResult.class.cast(annotation);
suggestedClazz = put.cacheResolverFactory();
} else if (annotation instanceof CacheRemoveAll) {
CacheRemoveAll put = CacheRemoveAll.class.cast(annotation);
suggestedClazz = put.cacheResolverFactory();
}
if (suggestedClazz.equals(defaultClazz) && defaults != null) {
suggestedClazz = defaults.cacheResolverFactory();
}
if (!defaultClazz.equals(suggestedClazz)) {
try {
factory = (CacheResolverFactory) suggestedClazz.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(PayaraCacheKeyInvocationContext.class.getName()).log(Level.SEVERE, null, ex);
}
}
return factory;
}
use of javax.cache.annotation.CachePut in project Payara by payara.
the class PayaraCacheKeyInvocationContext method getGenerator.
public final CacheKeyGenerator getGenerator() {
if (generator != null) {
return generator;
}
generator = new PayaraCacheKeyGenerator();
Class defaultClazz = javax.cache.annotation.CacheKeyGenerator.class;
Class suggestedClazz = null;
if (annotation instanceof CachePut) {
CachePut put = CachePut.class.cast(annotation);
suggestedClazz = put.cacheKeyGenerator();
} else if (annotation instanceof CacheRemove) {
CacheRemove put = CacheRemove.class.cast(annotation);
suggestedClazz = put.cacheKeyGenerator();
} else if (annotation instanceof CacheResult) {
CacheResult put = CacheResult.class.cast(annotation);
suggestedClazz = put.cacheKeyGenerator();
}
if (suggestedClazz.equals(defaultClazz) && defaults != null) {
suggestedClazz = defaults.cacheKeyGenerator();
}
if (!defaultClazz.equals(suggestedClazz)) {
try {
generator = (CacheKeyGenerator) suggestedClazz.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(PayaraCacheKeyInvocationContext.class.getName()).log(Level.SEVERE, null, ex);
}
}
return generator;
}
use of javax.cache.annotation.CachePut in project Payara by payara.
the class PayaraCacheKeyInvocationContext method getCacheName.
@Override
public String getCacheName() {
String result = null;
if (annotation instanceof CachePut) {
CachePut put = CachePut.class.cast(annotation);
result = put.cacheName();
} else if (annotation instanceof CacheRemove) {
CacheRemove put = CacheRemove.class.cast(annotation);
result = put.cacheName();
} else if (annotation instanceof CacheRemoveAll) {
CacheRemoveAll put = CacheRemoveAll.class.cast(annotation);
result = put.cacheName();
} else if (annotation instanceof CacheResult) {
CacheResult put = CacheResult.class.cast(annotation);
result = put.cacheName();
}
if ((result == null || result.isEmpty()) && (defaults != null)) {
result = defaults.cacheName();
}
if ((result == null) || (result.isEmpty())) {
String targetClassName = ctx.getTarget().getClass().getName();
String methodName = ctx.getMethod().getName();
Object[] params = ctx.getParameters();
StringBuilder cacheName = new StringBuilder(targetClassName);
cacheName.append('.').append(methodName).append('(');
if (params != null) {
for (int i = 0; i < params.length; i++) {
cacheName.append(params[i].getClass().getName());
if (i != params.length - 1) {
cacheName.append(',');
}
}
}
cacheName.append(')');
result = cacheName.toString();
}
return result;
}
use of javax.cache.annotation.CachePut in project spring-framework by spring-projects.
the class AnnotationJCacheOperationSource method createCachePutOperation.
protected CachePutOperation createCachePutOperation(Method method, @Nullable CacheDefaults defaults, CachePut ann) {
String cacheName = determineCacheName(method, defaults, ann.cacheName());
CacheResolverFactory cacheResolverFactory = determineCacheResolverFactory(defaults, ann.cacheResolverFactory());
KeyGenerator keyGenerator = determineKeyGenerator(defaults, ann.cacheKeyGenerator());
CacheMethodDetails<CachePut> methodDetails = createMethodDetails(method, ann, cacheName);
CacheResolver cacheResolver = getCacheResolver(cacheResolverFactory, methodDetails);
return new CachePutOperation(methodDetails, cacheResolver, keyGenerator);
}
Aggregations