Search in sources :

Example 1 with InternalCacheFactory

use of org.infinispan.factories.InternalCacheFactory in project infinispan by infinispan.

the class DefaultCacheManager method wireAndStartCache.

/**
 * @return a null return value means the cache was created by someone else before we got the lock
 */
private <K, V> Cache<K, V> wireAndStartCache(String cacheName) {
    Configuration c = configurationManager.getConfiguration(cacheName);
    if (c == null) {
        throw CONFIG.noSuchCacheConfiguration(cacheName);
    }
    if (c.security().authorization().enabled()) {
        // Don't even attempt to wire anything if we don't have LIFECYCLE privileges
        authorizer.checkPermission(c.security().authorization(), getSubject(), AuthorizationPermission.LIFECYCLE, null);
    }
    if (c.isTemplate()) {
        throw CONFIG.templateConfigurationStartAttempt(cacheName);
    }
    CompletableFuture<Cache<?, ?>> cacheFuture = new CompletableFuture<>();
    CompletableFuture<Cache<?, ?>> oldFuture = caches.computeIfAbsent(cacheName, name -> {
        assertIsNotTerminated();
        return cacheFuture;
    });
    Cache<K, V> cache = null;
    try {
        if (oldFuture != cacheFuture) {
            cache = (Cache<K, V>) oldFuture.join();
            if (!cache.getStatus().isTerminated()) {
                return cache;
            }
        }
    } catch (CompletionException ce) {
        throw ((CacheException) ce.getCause());
    }
    try {
        log.debugf("Creating cache %s on %s", cacheName, identifierString());
        if (cache == null) {
            cache = new InternalCacheFactory<K, V>().createCache(c, globalComponentRegistry, cacheName);
            if (cache.getAdvancedCache().getAuthorizationManager() != null) {
                cache = new SecureCacheImpl<>(cache.getAdvancedCache());
            }
        }
        ComponentRegistry cr = SecurityActions.getUnwrappedCache(cache).getAdvancedCache().getComponentRegistry();
        boolean notStartedYet = cr.getStatus() != ComponentStatus.RUNNING && cr.getStatus() != ComponentStatus.INITIALIZING;
        // start the cache-level components
        cache.start();
        cacheFuture.complete(cache);
        boolean needToNotifyCacheStarted = notStartedYet && cr.getStatus() == ComponentStatus.RUNNING;
        if (needToNotifyCacheStarted) {
            globalComponentRegistry.notifyCacheStarted(cacheName);
        }
        log.tracef("Cache %s is ready", cacheName);
        return cache;
    } catch (CacheException e) {
        cacheFuture.completeExceptionally(e);
        throw e;
    } catch (Throwable t) {
        cacheFuture.completeExceptionally(new CacheException(t));
        throw t;
    }
}
Also used : GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) GlobalAuthorizationConfiguration(org.infinispan.configuration.global.GlobalAuthorizationConfiguration) TransportConfiguration(org.infinispan.configuration.global.TransportConfiguration) Configuration(org.infinispan.configuration.cache.Configuration) CacheException(org.infinispan.commons.CacheException) CompletableFuture(java.util.concurrent.CompletableFuture) BasicComponentRegistry(org.infinispan.factories.impl.BasicComponentRegistry) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry) ComponentRegistry(org.infinispan.factories.ComponentRegistry) CompletionException(java.util.concurrent.CompletionException) InternalCacheFactory(org.infinispan.factories.InternalCacheFactory) Cache(org.infinispan.Cache)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 Cache (org.infinispan.Cache)1 CacheException (org.infinispan.commons.CacheException)1 Configuration (org.infinispan.configuration.cache.Configuration)1 GlobalAuthorizationConfiguration (org.infinispan.configuration.global.GlobalAuthorizationConfiguration)1 GlobalConfiguration (org.infinispan.configuration.global.GlobalConfiguration)1 TransportConfiguration (org.infinispan.configuration.global.TransportConfiguration)1 ComponentRegistry (org.infinispan.factories.ComponentRegistry)1 GlobalComponentRegistry (org.infinispan.factories.GlobalComponentRegistry)1 InternalCacheFactory (org.infinispan.factories.InternalCacheFactory)1 BasicComponentRegistry (org.infinispan.factories.impl.BasicComponentRegistry)1