All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.evcache.util.EVCacheConfig Maven / Gradle / Ivy

The newest version!
package com.netflix.evcache.util;


import java.lang.reflect.Type;
import java.util.function.Consumer;
import java.util.function.Function;

import javax.inject.Inject;

import com.netflix.archaius.DefaultPropertyFactory;
import com.netflix.archaius.api.Property;
import com.netflix.archaius.api.PropertyListener;
import com.netflix.archaius.api.PropertyRepository;
import com.netflix.archaius.api.config.CompositeConfig;
import com.netflix.archaius.config.DefaultCompositeConfig;
import com.netflix.archaius.config.DefaultSettableConfig;
import com.netflix.archaius.config.EnvironmentConfig;
import com.netflix.archaius.config.SystemConfig;
import com.netflix.evcache.config.EVCachePersistedProperties;
public class EVCacheConfig {

    private static EVCacheConfig INSTANCE;

    /**
     * This is an hack, should find a better way to do this 
     **/
    private static PropertyRepository propertyRepository;

	@Inject
    public EVCacheConfig(PropertyRepository repository) {
	    PropertyRepository _propertyRepository = null;
	    if(repository == null) {
	        try {
    	        final CompositeConfig applicationConfig = new DefaultCompositeConfig(true);
    	        CompositeConfig remoteLayer = new DefaultCompositeConfig(true);
    	        applicationConfig.addConfig("RUNTIME", new DefaultSettableConfig());
                applicationConfig.addConfig("REMOTE", remoteLayer);
    	        applicationConfig.addConfig("SYSTEM", SystemConfig.INSTANCE);
    	        applicationConfig.addConfig("ENVIRONMENT",  EnvironmentConfig.INSTANCE);
    	        final EVCachePersistedProperties remote = new EVCachePersistedProperties();
                remoteLayer.addConfig("remote-1", remote.getPollingDynamicConfig());
    	        _propertyRepository = new DefaultPropertyFactory(applicationConfig);
    	        
	        } catch (Exception e) {
	            e.printStackTrace();
	            _propertyRepository = new DefaultPropertyFactory(new DefaultCompositeConfig());
            }
	    } else {
	        _propertyRepository = repository;
	    }
	    propertyRepository =  new EVCachePropertyRepository(_propertyRepository);
	    //propertyRepository = _propertyRepository;

        INSTANCE = this;
    }

    private EVCacheConfig() {
        this(null);
    }


    public static EVCacheConfig getInstance() {
        if(INSTANCE == null) new EVCacheConfig();
        return INSTANCE;
    }

    public PropertyRepository getPropertyRepository() {
    	return propertyRepository;
    }

    public static void setPropertyRepository(PropertyRepository repository) {
        propertyRepository = repository;
    }

    class EVCachePropertyRepository implements PropertyRepository {
        
        private final PropertyRepository delegate;

        EVCachePropertyRepository(PropertyRepository delegate) {
            this.delegate = delegate;
        }

        @Override
        public  Property get(String key, Class type) {
            return new EVCacheProperty(delegate.get(key, type));
        }

        @Override
        public  Property get(String key, Type type) {
            return new EVCacheProperty(delegate.get(key, type));
        }
    }

    class EVCacheProperty implements Property {

        private final Property property;
        EVCacheProperty(Property prop) {
            property = prop;
        }

        @Override
        public T get() {
            return property.get();
        }

        @Override
        public String getKey() {
            return property.getKey();
        }

        @Override
        public void addListener(PropertyListener listener) {
            // TODO Auto-generated method stub
            property.addListener(listener);
        }

        @Override
        public void removeListener(PropertyListener listener) {
            // TODO Auto-generated method stub
            property.removeListener(listener);
        }

        @Override
        public Subscription onChange(Consumer consumer) {
            // TODO Auto-generated method stub
            return property.onChange(consumer);
        }

        @Override
        public Subscription subscribe(Consumer consumer) {
            // TODO Auto-generated method stub
            return property.subscribe(consumer);
        }

        @Override
        public Property orElse(T defaultValue) {
            // TODO Auto-generated method stub
            return new EVCacheProperty(property.orElse(defaultValue));
        }

        @Override
        public Property orElseGet(String key) {
            // TODO Auto-generated method stub
            return new EVCacheProperty(property.orElseGet(key));
        }

        @Override
        public  Property map(Function mapper) {
            // TODO Auto-generated method stub
            return property.map(mapper);
        }

        @Override
        public String toString() {
            return "EVCacheProperty [Key=" + getKey() + ",value="+get() + "]";
        }
        
    }
}