public Object cache()

in src/main/java/org/apache/geronimo/jcache/simple/cdi/CacheRemoveAllInterceptor.java [44:93]


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

        final String cacheName = methodMeta.getCacheRemoveAllCacheName();

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

        final boolean afterInvocation = methodMeta.isCachePutAfter();
        if (!afterInvocation) {
            cache.removeAll();
        }

        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(), methodMeta.getCacheRemoveAll().evictFor(),
                                methodMeta.getCacheRemoveAll().noEvictFor())) {
                            cache.removeAll();
                        }
                    }
                    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(), methodMeta.getCacheRemoveAll().evictFor(),
                        methodMeta.getCacheRemoveAll().noEvictFor())) {
                    cache.removeAll();
                }
            }
            throw t;
        }

        if (afterInvocation) {
            cache.removeAll();
        }

        return result;
    }