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

com.alachisoft.integrations.spring.configuration.SpringCacheConfigurationManager Maven / Gradle / Ivy

There is a newer version: 5.2-alpha-23
Show newest version
/*
 * Alachisoft (R) NCache Integrations
 * NCache Provider for Spring
 * ===============================================================================
 * Copyright © Alachisoft.  All rights reserved.
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.
 * ===============================================================================
 */
package com.alachisoft.integrations.spring.configuration;

import com.alachisoft.ncache.runtime.exceptions.ConfigurationException;

public class SpringCacheConfigurationManager {

    SpringCacheConfiguration[] _caches = null;

    public SpringCacheConfigurationManager(CacheList cacheList) throws ConfigurationException {
        if (cacheList == null || cacheList.getCaches() == null) {
            throw new ConfigurationException("No cache specified in configuration file.");
        }

        _caches = cacheList.getCaches();
        for (int i = 0; i < _caches.length; i++) {
            validateCacheConfig(_caches[i]);
        }
    }

    public SpringCacheConfiguration getCacheConfig(String cacheName) {
        for (int i = 0; i < _caches.length; i++) {
            if (_caches[i].getSpringCacheName().equals(cacheName)) {
                return _caches[i];
            }
        }
        return null;
    }

    public boolean contains(String cacheName) {
        return this.getCacheConfig(cacheName) != null;
    }

    private void validateCacheConfig(SpringCacheConfiguration config) throws ConfigurationException {

        if (config.getSpringCacheName() == null || config.getSpringCacheName().isEmpty()) {
            throw new ConfigurationException("cache name cannot be null or empty.");
        }
        if (config.getCacheidInstanceName() == null || config.getCacheidInstanceName().isEmpty()) {
            throw new ConfigurationException("instance cannot be null for cache: " + config.getSpringCacheName());
        }
        if ((!config.getExpirationType().equalsIgnoreCase("absolute")) && (!config.getExpirationType().equalsIgnoreCase("sliding")) && (!config.getExpirationType().equalsIgnoreCase("none"))) {
            throw new ConfigurationException("Invalid value for expiraion-type for cache: " + config.getSpringCacheName());
        }
        if (!"none".equals(config.getExpirationType().toLowerCase())) {
            if (config.getExpirationPeriod() <= 0) {
                throw new ConfigurationException("Invalid value for expiraion-period in cache: " + config.getSpringCacheName() + ". Expiraion period must be greater than zero.");
            }
        }

        if (config.getPriority().equalsIgnoreCase("abovenormal")) {
            config.setCacheItemPriority(com.alachisoft.ncache.runtime.CacheItemPriority.AboveNormal);
        } else if (config.getPriority().equalsIgnoreCase("belownormal")) {
            config.setCacheItemPriority(com.alachisoft.ncache.runtime.CacheItemPriority.BelowNormal);
        } else if (config.getPriority().equalsIgnoreCase("default")) {
            config.setCacheItemPriority(com.alachisoft.ncache.runtime.CacheItemPriority.Default);
        } else if (config.getPriority().equalsIgnoreCase("high")) {
            config.setCacheItemPriority(com.alachisoft.ncache.runtime.CacheItemPriority.High);
        } else if (config.getPriority().equalsIgnoreCase("low")) {
            config.setCacheItemPriority(com.alachisoft.ncache.runtime.CacheItemPriority.Low);
        } else if (config.getPriority().equalsIgnoreCase("normal")) {
            config.setCacheItemPriority(com.alachisoft.ncache.runtime.CacheItemPriority.Normal);
        } else if (config.getPriority().equalsIgnoreCase("notremovable")) {
            config.setCacheItemPriority(com.alachisoft.ncache.runtime.CacheItemPriority.NotRemovable);
        } else {
            throw new ConfigurationException("Invalid value for priority for cache: " + config.getSpringCacheName());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy