Search in sources :

Example 1 with CacheCommandInitializer

use of org.hibernate.cache.infinispan.util.CacheCommandInitializer in project hibernate-orm by hibernate.

the class PutFromLoadValidator method addToCache.

/**
	 * Besides the call from constructor, this should be called only from tests when mocking the validator.
	 */
public static void addToCache(AdvancedCache cache, PutFromLoadValidator validator) {
    List<CommandInterceptor> interceptorChain = cache.getInterceptorChain();
    log.debug("Interceptor chain was: " + interceptorChain);
    int position = 0;
    // add interceptor before uses exact match, not instanceof match
    int invalidationPosition = 0;
    int entryWrappingPosition = 0;
    for (CommandInterceptor ci : interceptorChain) {
        if (ci instanceof InvalidationInterceptor) {
            invalidationPosition = position;
        }
        if (ci instanceof EntryWrappingInterceptor) {
            entryWrappingPosition = position;
        }
        position++;
    }
    boolean transactional = cache.getCacheConfiguration().transaction().transactionMode().isTransactional();
    if (transactional) {
        cache.removeInterceptor(invalidationPosition);
        TxInvalidationInterceptor txInvalidationInterceptor = new TxInvalidationInterceptor();
        cache.getComponentRegistry().registerComponent(txInvalidationInterceptor, TxInvalidationInterceptor.class);
        cache.addInterceptor(txInvalidationInterceptor, invalidationPosition);
        // Note that invalidation does *NOT* acquire locks; therefore, we have to start invalidating before
        // wrapping the entry, since if putFromLoad was invoked between wrap and beginInvalidatingKey, the invalidation
        // would not commit the entry removal (as during wrap the entry was not in cache)
        TxPutFromLoadInterceptor txPutFromLoadInterceptor = new TxPutFromLoadInterceptor(validator, cache.getName());
        cache.getComponentRegistry().registerComponent(txPutFromLoadInterceptor, TxPutFromLoadInterceptor.class);
        cache.addInterceptor(txPutFromLoadInterceptor, entryWrappingPosition);
    } else {
        cache.removeInterceptor(invalidationPosition);
        NonTxInvalidationInterceptor nonTxInvalidationInterceptor = new NonTxInvalidationInterceptor(validator);
        cache.getComponentRegistry().registerComponent(nonTxInvalidationInterceptor, NonTxInvalidationInterceptor.class);
        cache.addInterceptor(nonTxInvalidationInterceptor, invalidationPosition);
        NonTxPutFromLoadInterceptor nonTxPutFromLoadInterceptor = new NonTxPutFromLoadInterceptor(validator, cache.getName());
        cache.getComponentRegistry().registerComponent(nonTxPutFromLoadInterceptor, NonTxPutFromLoadInterceptor.class);
        cache.addInterceptor(nonTxPutFromLoadInterceptor, entryWrappingPosition);
        validator.nonTxPutFromLoadInterceptor = nonTxPutFromLoadInterceptor;
    }
    log.debug("New interceptor chain is: " + cache.getInterceptorChain());
    CacheCommandInitializer cacheCommandInitializer = cache.getComponentRegistry().getComponent(CacheCommandInitializer.class);
    cacheCommandInitializer.addPutFromLoadValidator(cache.getName(), validator);
}
Also used : InvalidationInterceptor(org.infinispan.interceptors.InvalidationInterceptor) EntryWrappingInterceptor(org.infinispan.interceptors.EntryWrappingInterceptor) CommandInterceptor(org.infinispan.interceptors.base.CommandInterceptor) CacheCommandInitializer(org.hibernate.cache.infinispan.util.CacheCommandInitializer)

Example 2 with CacheCommandInitializer

use of org.hibernate.cache.infinispan.util.CacheCommandInitializer in project hibernate-orm by hibernate.

the class PutFromLoadValidator method removeFromCache.

/**
	 * This methods should be called only from tests; it removes existing validator from the cache structures
	 * in order to replace it with new one.
	 *
	 * @param cache
	 */
public static PutFromLoadValidator removeFromCache(AdvancedCache cache) {
    cache.removeInterceptor(TxPutFromLoadInterceptor.class);
    cache.removeInterceptor(NonTxPutFromLoadInterceptor.class);
    for (Object i : cache.getInterceptorChain()) {
        if (i instanceof NonTxInvalidationInterceptor) {
            InvalidationInterceptor invalidationInterceptor = new InvalidationInterceptor();
            cache.getComponentRegistry().registerComponent(invalidationInterceptor, InvalidationInterceptor.class);
            cache.addInterceptorBefore(invalidationInterceptor, NonTxInvalidationInterceptor.class);
            cache.removeInterceptor(NonTxInvalidationInterceptor.class);
            break;
        } else if (i instanceof TxInvalidationInterceptor) {
            InvalidationInterceptor invalidationInterceptor = new InvalidationInterceptor();
            cache.getComponentRegistry().registerComponent(invalidationInterceptor, InvalidationInterceptor.class);
            cache.addInterceptorBefore(invalidationInterceptor, TxInvalidationInterceptor.class);
            cache.removeInterceptor(TxInvalidationInterceptor.class);
            break;
        }
    }
    CacheCommandInitializer cci = cache.getComponentRegistry().getComponent(CacheCommandInitializer.class);
    return cci.removePutFromLoadValidator(cache.getName());
}
Also used : InvalidationInterceptor(org.infinispan.interceptors.InvalidationInterceptor) CacheCommandInitializer(org.hibernate.cache.infinispan.util.CacheCommandInitializer)

Aggregations

CacheCommandInitializer (org.hibernate.cache.infinispan.util.CacheCommandInitializer)2 InvalidationInterceptor (org.infinispan.interceptors.InvalidationInterceptor)2 EntryWrappingInterceptor (org.infinispan.interceptors.EntryWrappingInterceptor)1 CommandInterceptor (org.infinispan.interceptors.base.CommandInterceptor)1