public void start(Settings settings,
Properties properties) throws CacheException {
try {
// We need our tm, so get it now and avoid doing other work
// if there is a problem
TransactionManagerLookup tml = settings.getTransactionManagerLookup();
TransactionManager tm = (tml == null ? null : tml.getTransactionManager(properties));
// We only build caches if *none* were passed in. Passing in
// caches counts as a clear statement of exactly what is wanted
boolean buildCaches = jbcEntityCache == null
&& jbcCollectionCache == null
&& jbcTsCache == null
&& jbcQueryCache == null;
// Set up the cache factory
if (buildCaches && jbcFactory == null) {
// See if the user configured a multiplexer stack
if (channelFactory == null) {
String muxStacks = PropertiesHelper.getString(CHANNEL_FACTORY_RESOURCE_PROP, properties, DEF_JGROUPS_RESOURCE);
if (muxStacks != null) {
channelFactory = new JChannelFactory();
channelFactory.setMultiplexerConfig(muxStacks);
}
}
String factoryRes = PropertiesHelper.getString(CACHE_FACTORY_RESOURCE_PROP, properties, DEF_CACHE_FACTORY_RESOURCE);
jbcFactory = new CacheManagerImpl(factoryRes, channelFactory);
((CacheManagerImpl) jbcFactory).start();
selfCreatedFactory = true;
}
if (settings.isSecondLevelCacheEnabled()) {
if (buildCaches) {
entityConfig = PropertiesHelper
.getString(ENTITY_CACHE_RESOURCE_PROP, properties, DEF_ENTITY_RESOURCE);
jbcEntityCache = jbcFactory.getCache(entityConfig, true);
// Default to collections sharing entity cache if there is one
collectionConfig = PropertiesHelper.getString(COLLECTION_CACHE_RESOURCE_PROP, properties, entityConfig);
if (entityConfig.equals(collectionConfig)) {
jbcCollectionCache = jbcEntityCache;
}
else {
jbcCollectionCache = jbcFactory.getCache(collectionConfig, true);
}
}
if (jbcEntityCache != null) {
configureTransactionManager(jbcEntityCache, tm, false);
jbcEntityCache.start();
}
if (jbcCollectionCache != null) {
configureTransactionManager(jbcCollectionCache, tm, false);
jbcCollectionCache.start();
}
}
else {
jbcEntityCache = null;
jbcCollectionCache = null;
}
if (settings.isQueryCacheEnabled()) {
if (buildCaches) {
// Default to sharing the entity cache if there is one
String dfltQueryResource = (entityConfig == null ? DEF_QUERY_RESOURCE : entityConfig);
queryConfig = PropertiesHelper.getString(QUERY_CACHE_RESOURCE_PROP, properties, dfltQueryResource);
if (queryConfig.equals(entityConfig)) {
jbcQueryCache = jbcEntityCache;
} else if (queryConfig.equals(collectionConfig)) {
jbcQueryCache = jbcCollectionCache;
} else {
jbcQueryCache = jbcFactory.getCache(queryConfig, true);
}
// For Timestamps, we default to a separate config
tsConfig = PropertiesHelper.getString(TIMESTAMP_CACHE_RESOURCE_PROP, properties, DEF_TS_RESOURCE);
if (tsConfig.equals(queryConfig)) {
jbcTsCache = jbcQueryCache;
}
else if (tsConfig.equals(entityConfig)) {
jbcTsCache = jbcEntityCache;
}
else if (tsConfig.equals(collectionConfig)) {
jbcTsCache = jbcCollectionCache;
}
else {
jbcTsCache = jbcFactory.getCache(tsConfig, true);
}
}
if (jbcQueryCache != null) {
configureTransactionManager(jbcQueryCache, tm, false);
jbcQueryCache.start();
// TODO: I considered validating the presence of the TS cache here,
// but decided to defer unti getQueryCacheInstance() in case the
// cache is never actually used
}
if (jbcTsCache != null) {
configureTransactionManager(jbcTsCache, tm, true);
jbcTsCache.start();
// TODO: I considered validating TS cache config here,
// but decided to defer unti getTimestampsCacheInstance() in case the
// cache is never actually used
}
}
else {
jbcTsCache = null;
jbcQueryCache = null;
}
}
catch (CacheException ce) {
throw ce;
}
catch (Throwable t) {
throw new CacheException("Unable to start region factory", t);
}
}
|