package org.gluu.service.cache;

import org.slf4j.Logger;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * @author yuriyz on 02/21/2017.
 */
@ApplicationScoped
@Named
public class CacheProviderFactory {

    @Inject
    private Logger log;

    @Inject
    private CacheConfiguration cacheConfiguration;

    @Inject
    @Any
    private Instance<CacheProvider> instance;

    @Produces
    @ApplicationScoped
    public CacheProvider getCacheProvider() {
        log.debug("Started to create cache provider");

        AbstractCacheProvider<?> cacheProvider = null;

        return getCacheProvider(cacheProvider);
    }

    public CacheProvider getCacheProvider(AbstractCacheProvider<?> cacheProvider) {
		CacheProviderType cacheProviderType = cacheConfiguration.getCacheProviderType();

        if (cacheProviderType == null) {
            log.error("Failed to initialize cacheProvider, cacheProviderType is null. Fallback to IN_MEMORY type.");
            cacheProviderType = CacheProviderType.IN_MEMORY;
        }

        // Create proxied bean
        switch (cacheProviderType) {
            case IN_MEMORY:
            	cacheProvider = instance.select(InMemoryCacheProvider.class).get();
                break;
            case MEMCACHED:
            	cacheProvider = instance.select(MemcachedProvider.class).get();
                break;
            case REDIS:
            	cacheProvider = instance.select(RedisProvider.class).get();
                break;
            case NATIVE_PERSISTENCE:
                cacheProvider = instance.select(NativePersistenceCacheProvider.class).get();
                break;
        }

        if (cacheProvider == null) {
            throw new RuntimeException("Failed to initialize cacheProvider, cacheProviderType is unsupported: " + cacheProviderType);
        }

        cacheProvider.create();

        return cacheProvider;
	}

    @Produces
    @ApplicationScoped
    @LocalCache
    public CacheProvider getLocalCacheProvider() {
        log.debug("Started to create local cache provider");

        CacheProviderType cacheProviderType = CacheProviderType.IN_MEMORY;
        AbstractCacheProvider<?> cacheProvider = instance.select(InMemoryCacheProvider.class).get();

        if (cacheProvider == null) {
            throw new RuntimeException("Failed to initialize cacheProvider, cacheProviderType is unsupported: " + cacheProviderType);
        }

        cacheProvider.create();

        return cacheProvider;
    }

}

@@ -693,63 +693,8 @@
 );%0A%0A
-        AbstractCacheProvider%3C?%3E cacheProvider = null;%0A
 %0A   
@@ -727,24 +727,29 @@
 er(cache
-Provider
+Configuration
 );%0A    %7D
@@ -796,46 +796,45 @@
 der(
-AbstractCacheProvider%3C?%3E cacheProvider
+CacheConfiguration cacheConfiguration
 ) %7B%0A
@@ -1178,16 +1178,71 @@
 ed bean%0A
+        AbstractCacheProvider%3C?%3E cacheProvider = null;%0A
         

Add method to get cache provider by configuration