com.alachisoft.integrations.spring.configuration.SpringConfigurationManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ncache.spring Show documentation
Show all versions of ncache.spring Show documentation
NCache Spring integration.
/*
* 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 Alachisoft.NCache.Common.Configuration.ConfigurationBuilder;
import com.alachisoft.ncache.runtime.exceptions.ConfigurationException;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SpringConfigurationManager {
private String configFile = null;
private com.alachisoft.integrations.spring.configuration.ApplicationConfiguration appConfig;
private com.alachisoft.integrations.spring.configuration.SpringCacheConfigurationManager cacheConfigManager = null;
private boolean initialized = false;
public SpringConfigurationManager() {
}
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.integrations.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.");
}
cacheConfigManager = new SpringCacheConfigurationManager(appConfig.getCacheList());
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 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;
}
}