Search in sources :

Example 6 with GlobalConfigurationBuilder

use of org.infinispan.configuration.global.GlobalConfigurationBuilder in project kernel by exoplatform.

the class ExoCacheFactoryImpl method createCache.

/**
 * To create a new cache instance according to the given configuration, we follow the steps below:
 *
 * We first try to find if a specific location of the cache configuration has been defined thanks
 * to an external component plugin of type ExoCacheFactoryConfigPlugin. If so we use the default cache
 * configuration defined in this file otherwise we use the default cache configuration defined in
 * "${CACHE_CONFIG_TEMPLATE_KEY}"
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public ExoCache<Serializable, Object> createCache(final ExoCacheConfig config) throws ExoCacheInitException {
    final String region = config.getName();
    String CacheConfig = mappingCacheNameConfig.get(region);
    if (CacheConfig == null && config.isAsync() && asyncCacheTemplate != null && asyncCacheManager == null) {
        // use async template if cache use async mode
        this.asyncCacheManager = initCacheManager(asyncCacheTemplate);
    }
    final String customConfig = CacheConfig;
    final ExoCache<Serializable, Object> eXoCache;
    final DefaultCacheManager cacheManager;
    try {
        final ConfigurationBuilder confBuilder = new ConfigurationBuilder();
        if (customConfig != null) {
            try {
                cacheManager = SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<DefaultCacheManager>() {

                    public DefaultCacheManager run() throws Exception {
                        // A custom configuration has been set
                        if (LOG.isInfoEnabled())
                            LOG.info("A custom configuration has been set for the cache '" + region + "'.");
                        ParserRegistry parser = new ParserRegistry(Thread.currentThread().getContextClassLoader());
                        // Load the configuration
                        ConfigurationBuilderHolder holder = parser.parse(configManager.getInputStream(customConfig));
                        GlobalConfigurationBuilder configBuilder = holder.getGlobalConfigurationBuilder();
                        // Configure JGroups and JMX since it could affect the state of the Global Config
                        configureCacheManager(configBuilder);
                        GlobalConfiguration gc = configBuilder.build();
                        // Check if a CacheManager with the same GlobalConfiguration exists
                        DefaultCacheManager currentCacheManager = mappingGlobalConfigCacheManager.get(gc.transport().clusterName());
                        if (currentCacheManager == null) {
                            // Use a different cache manager name to prevent naming conflict
                            configBuilder.globalJmxStatistics().cacheManagerName(gc.globalJmxStatistics().cacheManagerName() + "_" + region + "_" + ctx.getName());
                            // No cache manager has been defined so far for this Cache Configuration
                            currentCacheManager = new DefaultCacheManager(configBuilder.build(), holder.getDefaultConfigurationBuilder().build(), false);
                            for (Entry<String, ConfigurationBuilder> entry : holder.getNamedConfigurationBuilders().entrySet()) {
                                currentCacheManager.defineConfiguration(entry.getKey(), entry.getValue().build());
                            }
                            currentCacheManager.start();
                            // We register this new cache manager
                            mappingGlobalConfigCacheManager.put(gc.transport().clusterName(), currentCacheManager);
                        }
                        return currentCacheManager;
                    }
                });
            } catch (PrivilegedActionException e) {
                Throwable cause = e.getCause();
                if (cause instanceof Exception) {
                    // NOSONAR
                    throw (Exception) cause;
                } else {
                    throw new Exception(e);
                }
            }
            confBuilder.read(cacheManager.getDefaultCacheConfiguration());
        } else if (config.isDistributed()) {
            // We expect a distributed cache
            if (distributedCacheManager == null) {
                throw new IllegalArgumentException("The DistributedCacheManager has not been defined in the configuration," + " please configure it at root container level if you want to use a distributed cache.");
            }
            return new DistributedExoCache(ctx, config, distributedCacheManager.getCache(DistributedExoCache.CACHE_NAME));
        } else {
            cacheManager = (config.isAsync() && asyncCacheManager != null) ? this.asyncCacheManager : this.cacheManager;
            // No custom configuration has been found, a configuration template will be used
            if (LOG.isInfoEnabled())
                LOG.info("The configuration template will be used for the cache '" + region + "'.");
            confBuilder.read(cacheManager.getDefaultCacheConfiguration());
            if (!config.isRepicated()) {
                // The cache is local
                confBuilder.clustering().cacheMode(CacheMode.LOCAL);
            }
        }
        // Reset the configuration to avoid conflicts
        resetConfiguration(confBuilder);
        final ExoCacheCreator creator = getExoCacheCreator(config);
        // Create the cache
        eXoCache = creator.create(config, confBuilder, new Callable<Cache<Serializable, Object>>() {

            public Cache<Serializable, Object> call() throws Exception {
                try {
                    return SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<Cache<Serializable, Object>>() {

                        public Cache<Serializable, Object> run() throws Exception {
                            // Define the configuration
                            cacheManager.defineConfiguration(region, confBuilder.build());
                            // create and start the cache
                            return cacheManager.getCache(region);
                        }
                    });
                } catch (PrivilegedActionException e) {
                    Throwable cause = e.getCause();
                    if (cause instanceof Exception) {
                        // NOSONAR
                        throw (Exception) cause;
                    } else {
                        throw new Exception(e);
                    }
                }
            }
        });
    } catch (// NOSONAR
    Exception e) {
        throw new ExoCacheInitException("The cache '" + region + "' could not be initialized", e);
    }
    return eXoCache;
}
Also used : GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) Serializable(java.io.Serializable) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) PrivilegedActionException(java.security.PrivilegedActionException) GenericExoCacheCreator(org.exoplatform.services.cache.impl.infinispan.generic.GenericExoCacheCreator) ParserRegistry(org.infinispan.configuration.parsing.ParserRegistry) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) PrivilegedActionException(java.security.PrivilegedActionException) ExoCacheInitException(org.exoplatform.services.cache.ExoCacheInitException) Callable(java.util.concurrent.Callable) ExoCacheInitException(org.exoplatform.services.cache.ExoCacheInitException) ConfigurationBuilderHolder(org.infinispan.configuration.parsing.ConfigurationBuilderHolder) DistributedExoCache(org.exoplatform.services.cache.impl.infinispan.distributed.DistributedExoCache) ExoCache(org.exoplatform.services.cache.ExoCache) Cache(org.infinispan.Cache) DistributedExoCache(org.exoplatform.services.cache.impl.infinispan.distributed.DistributedExoCache)

Example 7 with GlobalConfigurationBuilder

use of org.infinispan.configuration.global.GlobalConfigurationBuilder in project jnosql-diana-driver by eclipse.

the class InfinispanKeyValueConfiguration method get.

/**
 * Creates a {@link InfinispanBucketManagerFactory} from configuration map
 * @param configurations the configuration map
 * @return the InfinispanBucketManagerFactory instance
 * @throws NullPointerException when configurations is null
 */
public InfinispanBucketManagerFactory get(Map<String, String> configurations) {
    requireNonNull(configurations, "configurations is required");
    List<String> servers = configurations.keySet().stream().filter(s -> s.startsWith("infinispan-server-")).collect(Collectors.toList());
    if (!servers.isEmpty()) {
        org.infinispan.client.hotrod.configuration.ConfigurationBuilder builder = new org.infinispan.client.hotrod.configuration.ConfigurationBuilder();
        for (String server : servers) {
            builder.addServer().host(server);
        }
        return new InfinispanBucketManagerFactory(new RemoteCacheManager(builder.build()));
    } else if (configurations.containsKey("infinispan-config")) {
        try {
            return new InfinispanBucketManagerFactory(new DefaultCacheManager(configurations.get("infinispan-config")));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        GlobalConfigurationBuilder builder = new GlobalConfigurationBuilder();
        builder.globalJmxStatistics().allowDuplicateDomains(true);
        return new InfinispanBucketManagerFactory(new DefaultCacheManager(builder.build()));
    }
}
Also used : List(java.util.List) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) ConfigurationReader(org.jnosql.diana.driver.ConfigurationReader) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) IOException(java.io.IOException) HashMap(java.util.HashMap) KeyValueConfiguration(org.jnosql.diana.api.key.KeyValueConfiguration) Settings(org.jnosql.diana.api.Settings) Collectors(java.util.stream.Collectors) RemoteCacheManager(org.infinispan.client.hotrod.RemoteCacheManager) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) IOException(java.io.IOException) RemoteCacheManager(org.infinispan.client.hotrod.RemoteCacheManager)

Example 8 with GlobalConfigurationBuilder

use of org.infinispan.configuration.global.GlobalConfigurationBuilder in project indy by Commonjava.

the class CacheProducerMergeXmlTest method setup.

@Before
public void setup() {
    manager = new DefaultCacheManager(new GlobalConfigurationBuilder().globalJmxStatistics().allowDuplicateDomains(true).build());
    producer = new CacheProducer(new DefaultIndyConfiguration(), manager, null);
    producer.start();
    manager = producer.getCacheManager();
}
Also used : GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) CacheProducer(org.commonjava.indy.subsys.infinispan.CacheProducer) DefaultIndyConfiguration(org.commonjava.indy.conf.DefaultIndyConfiguration) Before(org.junit.Before)

Example 9 with GlobalConfigurationBuilder

use of org.infinispan.configuration.global.GlobalConfigurationBuilder in project wildfly by wildfly.

the class DefaultCacheContainerTestCase method getCacheManagerConfiguration.

@Test
public void getCacheManagerConfiguration() {
    GlobalConfiguration global = new GlobalConfigurationBuilder().build();
    when(this.manager.getCacheManagerConfiguration()).thenReturn(global);
    GlobalConfiguration result = this.subject.getCacheManagerConfiguration();
    assertSame(global, result);
}
Also used : GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) Test(org.junit.Test)

Example 10 with GlobalConfigurationBuilder

use of org.infinispan.configuration.global.GlobalConfigurationBuilder in project wildfly by wildfly.

the class CacheContainerServiceConfigurator method get.

@Override
public EmbeddedCacheManager get() {
    GlobalConfiguration config = this.configuration.get();
    String defaultCacheName = config.defaultCacheName().orElse(null);
    ConfigurationBuilderHolder holder = new ConfigurationBuilderHolder(config.classLoader(), new GlobalConfigurationBuilder().read(config));
    // We need to create a dummy default configuration if cache has a default cache
    if (defaultCacheName != null) {
        holder.newConfigurationBuilder(defaultCacheName);
    }
    EmbeddedCacheManager manager = new DefaultCacheManager(holder, false);
    // Undefine the default cache, if we defined one
    if (defaultCacheName != null) {
        manager.undefineConfiguration(defaultCacheName);
    }
    manager.start();
    // Must create executor before registering cache listener
    this.executor = Executors.newSingleThreadExecutor(new DefaultThreadFactory(this.getClass()));
    manager.addListener(this);
    InfinispanLogger.ROOT_LOGGER.debugf("%s cache container started", this.name);
    return manager;
}
Also used : GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) DefaultThreadFactory(org.jboss.as.clustering.context.DefaultThreadFactory) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) ConfigurationBuilderHolder(org.infinispan.configuration.parsing.ConfigurationBuilderHolder) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager)

Aggregations

GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)11 DefaultCacheManager (org.infinispan.manager.DefaultCacheManager)6 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)4 GlobalConfiguration (org.infinispan.configuration.global.GlobalConfiguration)4 TransportConfiguration (org.infinispan.configuration.global.TransportConfiguration)3 Configuration (org.infinispan.configuration.cache.Configuration)2 TransportConfigurationBuilder (org.infinispan.configuration.global.TransportConfigurationBuilder)2 ConfigurationBuilderHolder (org.infinispan.configuration.parsing.ConfigurationBuilderHolder)2 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)2 ChannelFactory (org.wildfly.clustering.jgroups.spi.ChannelFactory)2 ProtocolStackConfiguration (org.wildfly.clustering.jgroups.spi.ProtocolStackConfiguration)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Properties (java.util.Properties)1