com.alachisoft.ncache.client.CacheManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ncache-professional-client Show documentation
Show all versions of ncache-professional-client Show documentation
NCache Professional client for java.
package com.alachisoft.ncache.client;
import Alachisoft.NCache.Caching.CacheFactory;
import Alachisoft.NCache.Common.AppUtil;
import Alachisoft.NCache.Common.Monitoring.CategoriesConstants;
import Alachisoft.NCache.Common.Monitoring.MetricsPublisher;
import Alachisoft.NCache.Common.Monitoring.MonitoringConfigManager;
import Alachisoft.NCache.Config.Dom.CacheServerConfig;
import Alachisoft.NCache.Management.CacheConfig;
import Alachisoft.NCache.Management.MetricsTransporterFactoryImpl;
import Alachisoft.NCache.Management.Statistics.StatisticsCounter;
import com.alachisoft.ncache.client.internal.caching.*;
import com.alachisoft.ncache.client.internal.statistics.PerfStatsCollector;
import com.alachisoft.ncache.client.internal.util.ClientConfiguration;
import com.alachisoft.ncache.client.internal.util.DirectoryUtil;
import Alachisoft.NCache.Common.ErrorHandling.ErrorCodes;
import Alachisoft.NCache.Common.ErrorHandling.ErrorMessages;
import com.alachisoft.ncache.common.monitoring.MetricsTransporterFactory;
import com.alachisoft.ncache.runtime.exceptions.OperationFailedException;
import static tangible.DotNetToJavaStringHelper.isNullOrEmpty;
public final class CacheManager {
//region Private Fields
private static CacheImpl _cache = new CacheImpl();
private static boolean s_exceptions = true;
private static CacheCollection _cacheCollection = new CacheCollection();
private static String _configPath = null;
//endregion
//region Public Properties
public static CacheCollection getCaches() {
synchronized (_cacheCollection) {
return _cacheCollection;
}
}
public static boolean getExceptionsEnabled() {
if (_cache != null) {
s_exceptions = _cache.getExceptionEnabled();
}
return s_exceptions;
}
public static void setExceptionsEnabled(boolean value) {
s_exceptions = value;
if (_cache != null) {
_cache.setExceptionsEnabled(value);
}
}
public static String getConfigPath() {
return _configPath;
}
public static void setConfigPath(String path)
{
_configPath = path;
}
//endregion
//region Public Methods
public static Cache getCache(String cacheName) throws Exception {
if (isNullOrEmpty(cacheName))
throw new IllegalArgumentException("Value cannot be null or empty."+System.lineSeparator()+"Parameter name: cacheName");
return getCachePrivate(cacheName, null);
}
public static Cache getCache(String cacheName, CacheConnectionOptions cacheConnectionOptions) throws Exception {
if (isNullOrEmpty(cacheName))
throw new IllegalArgumentException("Value cannot be null or empty."+System.lineSeparator()+"Parameter name: cacheName");
if (cacheConnectionOptions == null)
throw new IllegalArgumentException("Connection Options");
return getCachePrivate(cacheName, cacheConnectionOptions);
}
//endregion
//region Internal Methods
private static CacheImpl getCachePrivate(String cacheName, CacheConnectionOptions cacheConnectionOptions) throws Exception {
if (cacheConnectionOptions == null) {
cacheConnectionOptions = new CacheConnectionOptions();
}
cacheConnectionOptions.initialize(cacheName);
CacheImpl cache = getCacheInternal(cacheName, cacheConnectionOptions);
cache.setMessagingServiceCacheImpl(cache.getCacheImpl());
return cache;
}
private static CacheImpl getCacheInternal(String cacheName, CacheConnectionOptions cacheConnectionOptions) throws Exception {
if (cacheName == null) {
throw new IllegalArgumentException("Value cannot be null or empty."+System.lineSeparator()+"Parameter name: cacheName");
}
if (cacheName.isEmpty()) {
throw new IllegalArgumentException("cacheId cannot be an empty string");
}
IsolationLevel mode = cacheConnectionOptions.getIsolationMode();
String cacheIdWithAlias = cacheName;
int maxTries = 2;
try {
CacheServerConfig config = null;
if (mode != IsolationLevel.OutProc) {
do {
try {
config = DirectoryUtil.getCacheDom(cacheName, null, null, mode == IsolationLevel.InProc);
} catch (SecurityException se) {
maxTries--;
if (maxTries == 0) {
throw se;
}
continue;
} catch (Exception ex) {
if (mode == IsolationLevel.Default) {
mode = IsolationLevel.OutProc;
} else {
throw ex;
}
}
if (config != null) {
if (config.getCacheType().toLowerCase().equals("clustered-cache")) {
throw new OperationFailedException(ErrorCodes.CacheInit.CLUSTER_INIT_IN_INPROC, ErrorMessages.getErrorMessage(ErrorCodes.CacheInit.CLUSTER_INIT_IN_INPROC));
}
switch (mode) {
case InProc:
config.setInProc(true);
break;
case OutProc:
config.setInProc(false);
break;
}
}
break;
} while (maxTries > 0);
}
synchronized (CacheManager.class) {
CacheImpl primaryCache = null;
synchronized (_cacheCollection) {
if (!_cacheCollection.contains(cacheIdWithAlias)) {
CacheImplBase cacheImpl = null;
PerfStatsCollector perfStatsCollector = null;
if (config != null && config.getInProc()) {
Alachisoft.NCache.Caching.Cache ncache = null;
CacheImpl cache = null;
maxTries = 2;
do {
try {
CacheConfig cacheConfig = CacheConfig.FromDom(config);
cache = new CacheImpl(null, cacheConfig);
ncache = CacheFactory.CreateFromPropertyString(cacheConfig.getPropertyString(), config, null, null, false, false);
cacheImpl = new InprocCache(ncache, cacheConfig, cache, null, null);
cache.setCacheImpl(cacheImpl);
if (primaryCache == null) {
primaryCache = cache;
} else {
primaryCache.addSecondaryInprocInstance(cache);
}
break;
} catch (SecurityException se) {
maxTries--;
if (maxTries == 0) {
throw se;
}
}
} while (maxTries > 0);
}
else {
maxTries = 2;
do {
perfStatsCollector = new PerfStatsCollector(cacheName, false);
perfStatsCollector.initializePerfCounters(false);
primaryCache = new CacheImpl(null, cacheName, perfStatsCollector);
cacheImpl = new RemoteCache(cacheName, primaryCache, cacheConnectionOptions, perfStatsCollector);
primaryCache.setCacheImpl(cacheImpl);
break;
} while (maxTries > 0);
}
if (primaryCache != null) {
if (!AppUtil.isMavenOnlyInstallation()) {
MetricsTransporterFactory transporterFactory = new MetricsTransporterFactoryImpl();
MonitoringConfigManager monitoringConfigManager = new MonitoringConfigManager();
ClientConfiguration clientConfig = new ClientConfiguration(null);
//dont load the cache related information from client.conf
clientConfig.setLoadCacheConfiguration(false);
clientConfig.loadLocalServerIP();
if (monitoringConfigManager.getCongigFile()) {
MetricsPublisher metricsPublisher = new MetricsPublisher(transporterFactory, cacheImpl);
metricsPublisher.setNCacheLog(cacheImpl.getNCacheLog());
metricsPublisher.setSessionId(cacheImpl.getMonitoringSessionId());
metricsPublisher.setCacheConfigID(cacheImpl.getCacheConfigId());
perfStatsCollector.setCategory(monitoringConfigManager.GetCategory(CategoriesConstants.NCacheClient));
perfStatsCollector.setStatsPublisher(metricsPublisher);
perfStatsCollector.RegisterOnMetricsPublisher();
metricsPublisher.Initialize();
}
perfStatsCollector.startPublishingCounters(clientConfig.getLocalServerIP());
}
primaryCache.initializeCompactFramework();
_cacheCollection.addCache(cacheIdWithAlias, primaryCache);
}
} else {
synchronized (_cacheCollection.getCache(cacheIdWithAlias, false)) {
Object tempVar = _cacheCollection.getCache(cacheIdWithAlias, false);
primaryCache = (CacheImpl) ((tempVar instanceof CacheImpl) ? tempVar : null);
primaryCache.addRef();
}
}
}
synchronized (_cache) {
// it is first cache instance.
if (_cache.getCacheImpl() == null) {
primaryCache.setExceptionEnabled(getExceptionsEnabled());
_cache = primaryCache;
}
}
return primaryCache;
}
} catch (Exception e2) {
throw e2;
}
}
//endregion
}