com.alachisoft.ncache.spring.configuration.SpringConfigurationManager Maven / Gradle / Ivy
/*
* 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.ncache.spring.configuration;
import Alachisoft.NCache.Common.Configuration.ConfigurationBuilder;
import com.alachisoft.ncache.runtime.exceptions.ConfigurationException;
import java.io.FileNotFoundException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SpringConfigurationManager {
private String configFile = null;
private com.alachisoft.ncache.spring.configuration.ApplicationConfiguration appConfig;
private com.alachisoft.ncache.spring.configuration.SpringCacheConfigurationManager cacheConfigManager = null;
private boolean initialized = false;
private Properties properties;
public SpringConfigurationManager() {
properties = new Properties();
}
public void initialize() throws ConfigurationException, FileNotFoundException, Exception {
if (configFile == null) {
throw new ConfigurationException("Configuration file not specified. Please make sure correct configuration file path is specified.");
}
ConfigurationBuilder configBuilder = new ConfigurationBuilder(configFile);
configBuilder.RegisterRootConfigurationObject(com.alachisoft.ncache.spring.configuration.ApplicationConfiguration.class);
configBuilder.ReadConfiguration();
Object[] configuraion = configBuilder.getConfiguration();
boolean appConfigFound = false;
if (configuraion != null && configuraion.length > 0) {
appConfig = (ApplicationConfiguration) configuraion[0];
appConfigFound = true;
}
if (!appConfigFound) {
throw new ConfigurationException("Cache's configuration not found. Please make sure correct configuration file is used.");
}
if (appConfig.getDefaultCacheName()
== null || appConfig.getDefaultCacheName().isEmpty()) {
throw new ConfigurationException("default-cache-name cannot be null.");
}
String cacheInstanceID = getProperty("ncacheid-instance");
cacheConfigManager = new SpringCacheConfigurationManager(appConfig.getCacheList(), cacheInstanceID);
if (!cacheConfigManager.contains(appConfig.getDefaultCacheName())) {
throw new ConfigurationException("Cache's configuration not specified for default-cache : " + appConfig.getDefaultCacheName());
}
initialized = true;
}
public void setConfigFile(String path) {
configFile = path;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Properties getProperties() {
return properties;
}
public String getProperty(String propertyName) {
Object o = properties.get( propertyName );
return o instanceof String ? (String) o : null;
}
public SpringCacheConfiguration getCacheConfiguration(String cacheName) throws ConfigurationException, FileNotFoundException, Exception {
if (!initialized) {
this.initialize();
}
SpringCacheConfiguration config = cacheConfigManager.getCacheConfig(cacheName);
if (config == null) {
config = cacheConfigManager.getCacheConfig(appConfig.getDefaultCacheName());
Logger.getLogger(SpringConfigurationManager.class.getName()).log(Level.WARNING, "Configuration not found for cache : {0}, using default cache configurations.", cacheName);
}
return config;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy