public Object cache()

in src/main/java/org/apache/geronimo/jcache/simple/cdi/CachePutInterceptor.java [45:96]


    public Object cache(final InvocationContext ic) throws Throwable {
        final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic);

        final String cacheName = methodMeta.getCachePutCacheName();

        final CacheResolverFactory cacheResolverFactory = methodMeta.getCachePutResolverFactory();
        final CacheKeyInvocationContext<CachePut> context = new CacheKeyInvocationContextImpl<CachePut>(ic,
                methodMeta.getCachePut(), cacheName, methodMeta);
        final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context);
        final Cache<Object, Object> cache = cacheResolver.resolveCache(context);

        final GeneratedCacheKey cacheKey = methodMeta.getCachePutKeyGenerator().generateCacheKey(context);
        final CachePut cachePut = methodMeta.getCachePut();
        final boolean afterInvocation = methodMeta.isCachePutAfter();

        if (!afterInvocation) {
            cache.put(cacheKey, context.getValueParameter());
        }

        final Object result;
        try {
            result = ic.proceed();
            if (CompletionStage.class.isInstance(result)) {
                final CompletionStage<?> completionStage = CompletionStage.class.cast(result);
                completionStage.exceptionally(t -> {
                    if (afterInvocation) {
                        if (helper.isIncluded(t.getClass(), cachePut.cacheFor(), cachePut.noCacheFor())) {
                            cache.put(cacheKey, context.getValueParameter());
                        }
                    }
                    if (RuntimeException.class.isInstance(t)) {
                        throw RuntimeException.class.cast(t);
                    }
                    throw new IllegalStateException(t);
                });
            }
        } catch (final Throwable t) {
            if (afterInvocation) {
                if (helper.isIncluded(t.getClass(), cachePut.cacheFor(), cachePut.noCacheFor())) {
                    cache.put(cacheKey, context.getValueParameter());
                }
            }

            throw t;
        }

        if (afterInvocation) {
            cache.put(cacheKey, context.getValueParameter());
        }

        return result;
    }