Search in sources :

Example 1 with CachePut

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;
}
Also used : CachePut(javax.cache.annotation.CachePut) PayaraCacheKeyInvocationContext(fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext) AroundInvoke(javax.interceptor.AroundInvoke)

Example 2 with CachePut

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;
}
Also used : CacheRemove(javax.cache.annotation.CacheRemove) CacheResult(javax.cache.annotation.CacheResult) CacheRemoveAll(javax.cache.annotation.CacheRemoveAll) CachePut(javax.cache.annotation.CachePut) CacheResolverFactory(javax.cache.annotation.CacheResolverFactory)

Example 3 with CachePut

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;
}
Also used : CacheRemove(javax.cache.annotation.CacheRemove) CacheResult(javax.cache.annotation.CacheResult) CacheKeyGenerator(javax.cache.annotation.CacheKeyGenerator) CachePut(javax.cache.annotation.CachePut)

Example 4 with CachePut

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;
}
Also used : CacheRemove(javax.cache.annotation.CacheRemove) CacheResult(javax.cache.annotation.CacheResult) CacheRemoveAll(javax.cache.annotation.CacheRemoveAll) CachePut(javax.cache.annotation.CachePut)

Example 5 with CachePut

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);
}
Also used : CacheResolver(org.springframework.cache.interceptor.CacheResolver) KeyGenerator(org.springframework.cache.interceptor.KeyGenerator) CacheKeyGenerator(javax.cache.annotation.CacheKeyGenerator) CachePut(javax.cache.annotation.CachePut) CacheResolverFactory(javax.cache.annotation.CacheResolverFactory)

Aggregations

CachePut (javax.cache.annotation.CachePut)6 CacheRemove (javax.cache.annotation.CacheRemove)4 CacheResult (javax.cache.annotation.CacheResult)4 CacheRemoveAll (javax.cache.annotation.CacheRemoveAll)3 CacheKeyGenerator (javax.cache.annotation.CacheKeyGenerator)2 CacheResolverFactory (javax.cache.annotation.CacheResolverFactory)2 PayaraCacheKeyInvocationContext (fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext)1 CacheDefaults (javax.cache.annotation.CacheDefaults)1 AroundInvoke (javax.interceptor.AroundInvoke)1 CacheResolver (org.springframework.cache.interceptor.CacheResolver)1 KeyGenerator (org.springframework.cache.interceptor.KeyGenerator)1