/*
 * Copyright (c) 2015,  nwillc@gmail.com
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package com.github.nwillc.mysnipserver.dao.orchestrate;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.annotation.CacheInvocationContext;
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 java.lang.annotation.Annotation;

public class ResolverFactory implements CacheResolverFactory {
	private CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();

	public CacheResolver getCacheResolver(CacheMethodDetails<? extends Annotation> cacheMethodDetails) {
		return new CacheResolver() {
			@Override
			public <K, V> Cache<K, V> resolveCache(CacheInvocationContext<? extends Annotation> cacheInvocationContext) {
				Object target = cacheInvocationContext.getTarget();
				Cache<K,V> cache = null;
				if (target instanceof CollectionDao) {
					String collection = ((CollectionDao) target).getCollection();
					cache = cacheManager.getCache(collection);
					if (cache == null) {
						cache = cacheManager.createCache(collection, new MutableConfiguration<>());
					}
				}
				return cache;
			}
		};
	}

	@Override
	public CacheResolver getExceptionCacheResolver(CacheMethodDetails<CacheResult> cacheMethodDetails) {
		return null;
	}
}

@@ -1931,24 +1931,182 @@
  == null) %7B%0A
+                        MutableConfiguration%3CK,V%3E configuration = new MutableConfiguration%3C%3E();%0A                        configuration.setStoreByValue(false);%0A
 %09%09%09%09%09%09cache 
@@ -2144,28 +2144,17 @@
 ection, 
-new MutableC
+c
 onfigura
@@ -2157,20 +2157,16 @@
 guration
-%3C%3E()
 );%0A%09%09%09%09%09

Make cache store by reference