org.infinispan.configuration.global.GlobalConfigurationBuilder Maven / Gradle / Ivy
package org.infinispan.configuration.global;
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import org.infinispan.commons.CacheConfigurationException;
import org.infinispan.commons.configuration.Builder;
import org.infinispan.commons.configuration.BuiltBy;
import org.infinispan.commons.util.Util;
public class GlobalConfigurationBuilder implements GlobalConfigurationChildBuilder {
private WeakReference cl;
private final TransportConfigurationBuilder transport;
private final GlobalJmxStatisticsConfigurationBuilder globalJmxStatistics;
private final SerializationConfigurationBuilder serialization;
private final GlobalSecurityConfigurationBuilder security;
private final ThreadPoolConfigurationBuilder expirationThreadPool;
private final ThreadPoolConfigurationBuilder listenerThreadPool;
private final ThreadPoolConfigurationBuilder replicationQueueThreadPool;
private final ThreadPoolConfigurationBuilder persistenceThreadPool;
private final ThreadPoolConfigurationBuilder stateTransferThreadPool;
private final ThreadPoolConfigurationBuilder asyncThreadPool;
private final ShutdownConfigurationBuilder shutdown;
private final GlobalStateConfigurationBuilder globalState;
private final List> modules = new ArrayList<>();
private final SiteConfigurationBuilder site;
private Optional defaultCacheName;
public GlobalConfigurationBuilder() {
// In OSGi contexts the TCCL should not be used. Use the infinispan-core bundle as default instead.
ClassLoader defaultCL = null;
if (!Util.isOSGiContext()) defaultCL = Thread.currentThread().getContextClassLoader();
if (defaultCL == null) defaultCL = GlobalConfigurationBuilder.class.getClassLoader();
this.cl = new WeakReference<>(defaultCL);
this.transport = new TransportConfigurationBuilder(this);
this.globalJmxStatistics = new GlobalJmxStatisticsConfigurationBuilder(this);
this.serialization = new SerializationConfigurationBuilder(this);
this.security = new GlobalSecurityConfigurationBuilder(this);
this.shutdown = new ShutdownConfigurationBuilder(this);
this.globalState = new GlobalStateConfigurationBuilder(this);
this.site = new SiteConfigurationBuilder(this);
this.expirationThreadPool = new ThreadPoolConfigurationBuilder(this);
this.listenerThreadPool = new ThreadPoolConfigurationBuilder(this);
this.replicationQueueThreadPool = new ThreadPoolConfigurationBuilder(this);
this.persistenceThreadPool = new ThreadPoolConfigurationBuilder(this);
this.stateTransferThreadPool = new ThreadPoolConfigurationBuilder(this);
this.asyncThreadPool = new ThreadPoolConfigurationBuilder(this);
this.defaultCacheName = Optional.empty();
}
/**
* Helper method that gets you a default constructed GlobalConfiguration, preconfigured to use the default clustering
* stack.
*
* @return a new global configuration
*/
public GlobalConfigurationBuilder clusteredDefault() {
transport().
defaultTransport()
.clearProperties();
return this;
}
/**
* Helper method that gets you a default constructed GlobalConfiguration, preconfigured for use in LOCAL mode
*
* @return a new global configuration
*/
public GlobalConfigurationBuilder nonClusteredDefault() {
transport()
.transport(null)
.clearProperties();
return this;
}
protected ClassLoader getClassLoader() {
return cl.get();
}
public GlobalConfigurationBuilder classLoader(ClassLoader cl) {
this.cl = new WeakReference<>(cl);
return this;
}
@Override
public TransportConfigurationBuilder transport() {
return transport;
}
@Override
public GlobalJmxStatisticsConfigurationBuilder globalJmxStatistics() {
return globalJmxStatistics;
}
@Override
public SerializationConfigurationBuilder serialization() {
return serialization;
}
@Deprecated
@Override
public ThreadPoolConfigurationBuilder evictionThreadPool() {
return expirationThreadPool;
}
@Override
public ThreadPoolConfigurationBuilder expirationThreadPool() {
return expirationThreadPool;
}
@Override
public ThreadPoolConfigurationBuilder listenerThreadPool() {
return listenerThreadPool;
}
@Deprecated
@Override
public ThreadPoolConfigurationBuilder replicationQueueThreadPool() {
return replicationQueueThreadPool;
}
@Override
public ThreadPoolConfigurationBuilder persistenceThreadPool() {
return persistenceThreadPool;
}
@Override
public ThreadPoolConfigurationBuilder stateTransferThreadPool() {
return stateTransferThreadPool;
}
@Override
public ThreadPoolConfigurationBuilder asyncThreadPool() {
return asyncThreadPool;
}
@Override
public GlobalSecurityConfigurationBuilder security() {
return security;
}
@Override
public ShutdownConfigurationBuilder shutdown() {
return shutdown;
}
@Override
public List> modules() {
return modules;
}
public GlobalConfigurationBuilder clearModules() {
modules.clear();
return this;
}
@Override
public SiteConfigurationBuilder site() {
return site;
}
public > T addModule(Class klass) {
try {
Constructor constructor = klass.getDeclaredConstructor(GlobalConfigurationBuilder.class);
T builder = constructor.newInstance(this);
this.modules.add(builder);
return builder;
} catch (Exception e) {
throw new CacheConfigurationException("Could not instantiate module configuration builder '" + klass.getName() + "'", e);
}
}
@Override
public GlobalStateConfigurationBuilder globalState() {
return globalState;
}
@Override
public GlobalConfigurationBuilder defaultCacheName(String defaultCacheName) {
this.defaultCacheName = Optional.of(defaultCacheName);
return this;
}
public Optional defaultCacheName() {
return defaultCacheName;
}
@SuppressWarnings("unchecked")
public void validate() {
List validationExceptions = new ArrayList<>();
Arrays.asList(
expirationThreadPool,
listenerThreadPool,
replicationQueueThreadPool,
persistenceThreadPool,
stateTransferThreadPool,
asyncThreadPool,
globalJmxStatistics,
transport,
security,
serialization,
shutdown,
globalState,
site
).forEach(c -> {
try {
c.validate();
} catch (RuntimeException e) {
validationExceptions.add(e);
}
});
modules.forEach(c -> {
try {
c.validate();
} catch (RuntimeException e) {
validationExceptions.add(e);
}
});
CacheConfigurationException.fromMultipleRuntimeExceptions(validationExceptions).ifPresent(e -> { throw e; });
}
@Override
public GlobalConfiguration build() {
validate();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy