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

com.alachisoft.ncache.jsr107.NCacheConfiguration Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
package com.alachisoft.ncache.jsr107;

import javax.cache.CacheException;
import javax.cache.configuration.CacheEntryListenerConfiguration;
import javax.cache.configuration.CompleteConfiguration;
import javax.cache.configuration.Configuration;
import javax.cache.configuration.Factory;
import javax.cache.expiry.EternalExpiryPolicy;
import javax.cache.expiry.ExpiryPolicy;
import javax.cache.integration.CacheLoader;
import javax.cache.integration.CacheWriter;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;


public class NCacheConfiguration implements javax.cache.configuration.CompleteConfiguration {

    private final Set> initialCacheEntryListenerConfigurations;
    private final ConcurrentMap, CacheEventListener> cacheEntryListenerConfigurations = new ConcurrentHashMap, CacheEventListener>();
    private final ExpiryPolicy expiryPolicy;
    private final Class keyType;
    private final Class valueType;
    private final Factory expiryPolicyFactory;
    boolean readThru;
    boolean writeThru;
    boolean statisticsEnabled;
    boolean managementEnabled;
    boolean storeByValue;
    com.alachisoft.ncache.client.internal.util.ClientConfiguration clientConfiguration;
    private Factory> cacheLoaderFactory;
    private Factory> cacheWriterFactory;


    /**
     * @param cacheName
     * @param configuration
     * @param keyType
     */
    public NCacheConfiguration(String cacheName, final Configuration configuration, final Class keyType, final Class valueType) {
        this.keyType = keyType;
        this.valueType = valueType;

        if (configuration instanceof CompleteConfiguration) {
            CompleteConfiguration cConfiguration = (CompleteConfiguration) configuration;
            this.expiryPolicyFactory = cConfiguration.getExpiryPolicyFactory();

            expiryPolicy = expiryPolicyFactory.create();

            try {
                //clientConfiguration = new ClientConfiguration(cacheName);
                //readThru = clientConfiguration.getDefaultReadThru()!=null && clientConfiguration.getDefaultReadThru().trim().length() > 0 ? true: false;
                //writeThru = clientConfiguration.getDefaultWriteThru()!=null && clientConfiguration.getDefaultWriteThru().trim().length() > 0 ? true:false;
                readThru = cConfiguration.isReadThrough();
                writeThru = cConfiguration.isWriteThrough();
                storeByValue = cConfiguration.isStoreByValue();
                cacheLoaderFactory = cConfiguration.getCacheLoaderFactory();

            } catch (Exception ex) {
                throw new CacheException(ex);
            }

            final HashSet> set = new HashSet>();
            for (CacheEntryListenerConfiguration kvCacheEntryListenerConfiguration : cConfiguration.getCacheEntryListenerConfigurations()) {
                set.add(kvCacheEntryListenerConfiguration);
            }
            initialCacheEntryListenerConfigurations = Collections.unmodifiableSet(set);
        } else {
            expiryPolicyFactory = EternalExpiryPolicy.factoryOf();
            expiryPolicy = expiryPolicyFactory.create();
            storeByValue = true;
            readThru = false;
            writeThru = false;

            initialCacheEntryListenerConfigurations = new HashSet>();
        }
    }

    /**
     * @param configuration
     */
    public NCacheConfiguration(final Configuration configuration) {
        this(null, configuration, configuration.getKeyType(), configuration.getValueType());
    }

    /**
     * Determines if a Cache should operate in read-through mode.
     *
     * @return
     */

    @Override
    public boolean isReadThrough() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * Determines if a Cache should operate in write-through mode.
     *
     * @return
     */

    @Override
    public boolean isWriteThrough() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * Checks whether statistics collection is enabled in this cache.
     *
     * @return
     */
    @Override
    public boolean isStatisticsEnabled() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return statisticsEnabled;
    }

    void setStatisticsEnabled(final boolean statisticsEnabled) {
        this.statisticsEnabled = statisticsEnabled;
    }

    /**
     * Checks whether management is enabled on this cache.
     *
     * @return
     */

    @Override
    public boolean isManagementEnabled() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return managementEnabled;
    }

    void setManagementEnabled(final boolean managementEnabled) {
        this.managementEnabled = managementEnabled;
    }

    /**
     * Obtains the CacheEntryListenerConfigurations for CacheEntryListeners to be configured on a Cache.
     *
     * @return
     */
    @Override
    public Iterable getCacheEntryListenerConfigurations() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        //return initialCacheEntryListenerConfigurations;
        return cacheEntryListenerConfigurations.keySet();
    }

    /**
     * @return
     */
    @Override
    public Factory getCacheLoaderFactory() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * @return
     */

    @Override
    public Factory getCacheWriterFactory() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * Gets the Factory for the ExpiryPolicy to be used for caches.
     *
     * @return
     */

    @Override
    public Factory getExpiryPolicyFactory() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return expiryPolicyFactory;
    }

    /**
     * get key type
     *
     * @return
     */

    @Override
    public Class getKeyType() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return keyType;
    }

    /**
     * get value type
     *
     * @return
     */

    @Override
    public Class getValueType() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return valueType;
    }

    /**
     * Whether storeByValue (true) or storeByReference (false). When true, both keys and values are stored by value.
     *
     * @return
     */

    @Override
    public boolean isStoreByValue() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return storeByValue;
    }

    public ExpiryPolicy getExpiryPolicy() {
        return expiryPolicy;
    }

    public Iterable> getInitialCacheEntryListenerConfigurations() {
        return initialCacheEntryListenerConfigurations;
    }

    public boolean addCacheEntryListenerConfiguration(final CacheEntryListenerConfiguration cacheEntryListenerConfiguration, final CacheEventListener cacheEventListener) {
        return cacheEntryListenerConfigurations.putIfAbsent(cacheEntryListenerConfiguration, cacheEventListener) == null;
    }

    public CacheEventListener removeCacheEntryListenerConfiguration(final CacheEntryListenerConfiguration cacheEntryListenerConfiguration) {
        return cacheEntryListenerConfigurations.remove(cacheEntryListenerConfiguration);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy