Search in sources :

Example 1 with ConfigurationManager

use of org.infinispan.configuration.ConfigurationManager in project indy by Commonjava.

the class CacheProducer method mergedCachesFromConfig.

/**
 * For the ISPN merging, we should involve at least two different xml config scopes here,
 * one is from indy self default resource xml, another one is from customer's config xml.
 *
 * To prevent the error of EmbeddedCacheManager instances configured with same JMX domain,
 * ISPN should enable 'allowDuplicateDomains' attribute for per GlobalConfigurationBuilder build,
 * that will cost more price there for DefaultCacheManager construct and ConfigurationBuilder build.
 *
 * Since what we need here is simply parsing xml inputStreams to the defined configurations that ISPN
 * could accept, then merging the two stream branches into a entire one.
 * What classes this method uses from ISPN are:
 * {@link ConfigurationBuilderHolder}
 * {@link ParserRegistry}
 * {@link ConfigurationManager}
 *
 * @param cacheMgr
 * @param config
 * @param path
 */
private EmbeddedCacheManager mergedCachesFromConfig(EmbeddedCacheManager cacheMgr, String config, String path) {
    logger.debug("[ISPN xml merge] cache config xml to merge:\n {}", config);
    // FIXME: here may cause ISPN000343 problem if your cache config has enabled distributed cache. Because distributed
    // cache needs transport support, so if the cache manager does not enable it and then add this type of cache
    // by defineConfiguration, it will report ISPN000343. So we should ensure the transport has been added by initialization.
    EmbeddedCacheManager mgr = cacheMgr;
    if (mgr == null) {
        try {
            logger.info("Using {} resource Infinispan configuration to construct mergable cache configuration:\n\n{}\n\n", path, config);
            mgr = new DefaultCacheManager(new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8)));
        } catch (IOException e) {
            throw new RuntimeException(String.format("Failed to construct ISPN cacheManger due to %s xml stream read error.", path), e);
        }
    }
    final ConfigurationBuilderHolder holder = (new ParserRegistry()).parse(IOUtils.toInputStream(config));
    final ConfigurationManager manager = new ConfigurationManager(holder);
    final Set<String> definedCaches = mgr.getCacheNames();
    for (String name : manager.getDefinedCaches()) {
        if (definedCaches.isEmpty() || !definedCaches.contains(name)) {
            logger.info("[ISPN xml merge] Define cache: {} from {} config.", name, path);
            mgr.defineConfiguration(name, manager.getConfiguration(name, false));
        }
    }
    return mgr;
}
Also used : DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) ConfigurationBuilderHolder(org.infinispan.configuration.parsing.ConfigurationBuilderHolder) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserRegistry(org.infinispan.configuration.parsing.ParserRegistry) IOException(java.io.IOException) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) ConfigurationManager(org.infinispan.configuration.ConfigurationManager)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ConfigurationManager (org.infinispan.configuration.ConfigurationManager)1 ConfigurationBuilderHolder (org.infinispan.configuration.parsing.ConfigurationBuilderHolder)1 ParserRegistry (org.infinispan.configuration.parsing.ParserRegistry)1 DefaultCacheManager (org.infinispan.manager.DefaultCacheManager)1 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)1