Skip to content

Commit

Permalink
AS7-1532 ClassLoaderAwareCache decoration will fail for multiple web …
Browse files Browse the repository at this point in the history
…applications.
  • Loading branch information
pferraro committed Aug 13, 2011
1 parent 46ec865 commit 6bfbcc1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 66 deletions.
Expand Up @@ -35,6 +35,7 @@
import org.infinispan.AbstractDelegatingAdvancedCache;
import org.infinispan.AdvancedCache;
import org.infinispan.commands.VisitableCommand;
import org.infinispan.config.ConfigurationException;
import org.infinispan.context.InvocationContext;
import org.infinispan.interceptors.base.CommandInterceptor;
import org.infinispan.notifications.Listener;
Expand All @@ -60,10 +61,14 @@ public class ClassLoaderAwareCache<K, V> extends AbstractDelegatingAdvancedCache

final WeakReference<ClassLoader> classLoaderRef;

public ClassLoaderAwareCache(AdvancedCache<K, V> cache, ClassLoader loader) {
public ClassLoaderAwareCache(AdvancedCache<K, V> cache, ClassLoader classLoader) {
super(cache);
this.classLoaderRef = new WeakReference<ClassLoader>(loader);
cache.addInterceptor(new ClassLoaderAwareCommandInterceptor(this), 0);
this.classLoaderRef = new WeakReference<ClassLoader>(classLoader);
try {
cache.addInterceptor(new ClassLoaderAwareCommandInterceptor(this), 0);
} catch (ConfigurationException e) {
// This means the ClassLoaderAwareCommandInterceptor is already in the interceptor chain
}
}

@Override
Expand All @@ -88,12 +93,6 @@ public AdvancedCache<K, V> getAdvancedCache() {
return this;
}

@Override
public void stop() {
super.stop();
this.classLoaderRef.clear();
}

@Override
public void addListener(Object listener) {
super.addListener(new ClassLoaderAwareListener(listener, this));
Expand All @@ -106,11 +105,14 @@ class ClassLoaderAwareCommandInterceptor extends CommandInterceptor {
}
@Override
protected Object handleDefault(InvocationContext ctx, VisitableCommand command) throws Throwable {
ContextClassLoaderSwitcher.SwitchContext context = switcher.getSwitchContext(this.cache.getClassLoader());
ClassLoader classLoader = this.cache.getClassLoader();
ContextClassLoaderSwitcher.SwitchContext context = (classLoader != null) ? switcher.getSwitchContext(classLoader) : null;
try {
return super.handleDefault(ctx, command);
} finally {
context.reset();
if (context != null) {
context.reset();
}
}
}
}
Expand Down Expand Up @@ -163,7 +165,8 @@ public ClassLoaderAwareListener(Object listener, AdvancedCache<?, ?> cache) {
public <K, V> void event(Event<K, V> event) throws Throwable {
List<Method> methods = this.methods.get(event.getType());
if (methods != null) {
ContextClassLoaderSwitcher.SwitchContext context = switcher.getSwitchContext(this.cache.getClassLoader());
ClassLoader classLoader = this.cache.getClassLoader();
ContextClassLoaderSwitcher.SwitchContext context = (classLoader != null) ? switcher.getSwitchContext(classLoader) : null;
try {
for (Method method : this.methods.get(event.getType())) {
try {
Expand All @@ -173,7 +176,9 @@ public <K, V> void event(Event<K, V> event) throws Throwable {
}
}
} finally {
context.reset();
if (context != null) {
context.reset();
}
}
}
}
Expand Down
Expand Up @@ -32,7 +32,6 @@
import org.infinispan.Cache;
import org.infinispan.config.Configuration;
import org.infinispan.config.GlobalConfiguration;
import org.infinispan.interceptors.base.CommandInterceptor;
import org.infinispan.lifecycle.ComponentStatus;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.EmbeddedCacheManager;
Expand Down Expand Up @@ -317,44 +316,8 @@ public AdvancedCache<K, V> getAdvancedCache() {
}

@Override
public AdvancedCache<K, V> with(ClassLoader loader) {
return new ClassLoaderAwareCache<K, V>(this, loader);
}

// ISPN-1333 Workaround
@Override
public synchronized void addInterceptor(CommandInterceptor i, int position) {
this.cache.addInterceptor(i, position);
}

// ISPN-1333 Workaround
@Override
public synchronized void addInterceptorAfter(CommandInterceptor i, Class<? extends CommandInterceptor> afterInterceptor) {
this.cache.addInterceptorAfter(i, afterInterceptor);
}

// ISPN-1333 Workaround
@Override
public synchronized void addInterceptorBefore(CommandInterceptor i, Class<? extends CommandInterceptor> beforeInterceptor) {
this.cache.addInterceptorBefore(i, beforeInterceptor);
}

// ISPN-1333 Workaround
@Override
public synchronized void removeInterceptor(Class<? extends CommandInterceptor> interceptorType) {
this.cache.removeInterceptor(interceptorType);
}

// ISPN-1333 Workaround
@Override
public synchronized void removeInterceptor(int position) {
this.cache.removeInterceptor(position);
}

// ISPN-1333 Workaround
@Override
public synchronized List<CommandInterceptor> getInterceptorChain() {
return this.cache.getInterceptorChain();
public AdvancedCache<K, V> with(ClassLoader classLoader) {
return new ClassLoaderAwareCache<K, V>(this, classLoader);
}

@Override
Expand Down
Expand Up @@ -39,7 +39,6 @@
import org.infinispan.notifications.cachelistener.event.CacheEntryActivatedEvent;
import org.infinispan.notifications.cachelistener.event.Event;
import org.jboss.as.clustering.infinispan.ClassLoaderAwareCache.ClassLoaderAwareListener;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -92,15 +91,6 @@ protected Object handleDefault(InvocationContext ctx, VisitableCommand command)
}
}

@After
public void after() {
this.cache.stop();

verify(this.mockCache).stop();

assertNull(this.cache.getClassLoader());
}

@Test
public void getClassLoader() {
assertSame(this.loader, this.cache.getClassLoader());
Expand All @@ -111,6 +101,11 @@ public void getAdvancedCache() {
assertSame(this.cache, this.cache.getAdvancedCache());
}

@Test
public void with() {
assertSame(this.cache, this.cache.with(Thread.currentThread().getContextClassLoader()));
}

@Test
public void addListener() throws Throwable {
ArgumentCaptor<ClassLoaderAwareListener> capturedListener = ArgumentCaptor.forClass(ClassLoaderAwareListener.class);
Expand Down
Expand Up @@ -22,7 +22,18 @@

package org.jboss.as.clustering.infinispan;

import static org.mockito.Mockito.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.List;
Expand All @@ -31,16 +42,14 @@
import org.infinispan.AdvancedCache;
import org.infinispan.Cache;
import org.infinispan.config.Configuration;
import org.infinispan.config.ConfigurationException;
import org.infinispan.config.GlobalConfiguration;
import org.infinispan.lifecycle.ComponentStatus;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.remoting.transport.Address;
import org.jboss.as.clustering.infinispan.DefaultEmbeddedCacheManager;
import org.junit.After;
import org.junit.Test;

import static org.junit.Assert.*;
/**
* @author Paul Ferraro
*/
Expand Down Expand Up @@ -103,6 +112,18 @@ public void getCache() {
assertNotSame(defaultCache, result);
assertEquals(result, defaultCache);
assertSame(this.subject, result.getCacheManager());

Cache<Object, Object> cache = result;
AdvancedCache<Object, Object> advancedCache = cache.getAdvancedCache();
assertSame(cache, advancedCache);
AdvancedCache<Object, Object> classLoaderCache = advancedCache.with(Thread.currentThread().getContextClassLoader());
verify(defaultCache).addInterceptor(isA(ClassLoaderAwareCache.ClassLoaderAwareCommandInterceptor.class), eq(0));
assertNotSame(advancedCache, classLoaderCache);

// Subsequent calls to with(...) cause addInterceptor() to fail
doThrow(new ConfigurationException("")).when(defaultCache).addInterceptor(isA(ClassLoaderAwareCache.ClassLoaderAwareCommandInterceptor.class), eq(0));
AdvancedCache<Object, Object> classLoaderCache2 = advancedCache.with(Thread.currentThread().getContextClassLoader());
assertNotSame(advancedCache, classLoaderCache2);
}

@Test
Expand Down

0 comments on commit 6bfbcc1

Please sign in to comment.