Author: manik.surtani(a)jboss.com
Date: 2007-12-19 08:17:43 -0500 (Wed, 19 Dec 2007)
New Revision: 4878
Removed:
core/trunk/src/main/java/org/jboss/cache/factories/annotations/ClasspathScanner.java
core/trunk/src/test/java/org/jboss/cache/factories/ComponentRegistryTest.java
Modified:
core/trunk/src/main/java/org/jboss/cache/invocation/CacheInvocationDelegate.java
core/trunk/src/test/java/org/jboss/cache/api/pfer/PutForExternalReadTestBase.java
Log:
Added fix for JBCACHE-1246
Deleted:
core/trunk/src/main/java/org/jboss/cache/factories/annotations/ClasspathScanner.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/factories/annotations/ClasspathScanner.java 2007-12-19
12:57:49 UTC (rev 4877)
+++
core/trunk/src/main/java/org/jboss/cache/factories/annotations/ClasspathScanner.java 2007-12-19
13:17:43 UTC (rev 4878)
@@ -1,171 +0,0 @@
-package org.jboss.cache.factories.annotations;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import java.io.File;
-import java.io.IOException;
-import java.lang.annotation.Annotation;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
-
-/**
- * Class for scanning archives and classpaths in the current JBoss Cache classpath for
classes annotated with a given annotation. Inspired by a similar class in
- * JBoss SEAM.
- *
- * @author Manik Surtani
- */
-public class ClasspathScanner
-{
- private Log log = LogFactory.getLog(ClasspathScanner.class);
- private URLClassLoader classLoader;
-
- /**
- * Constructor with the type of annotation to scan for.
- */
- public ClasspathScanner()
- {
- classLoader = (URLClassLoader) ClasspathScanner.class.getClassLoader();
- }
-
- /**
- * Scans the class path element that contains JBoss Cache for all classes that contain
the annotation type this class is
- * initialised with. Note that this only scans CLASSES for the annotation; not
methods, etc.
- *
- * @param annotationType the type of annotation to scan for.
- * @param classType the type of class to scan for. Subclasses will be scanned,
others will not.
- * @return a set of Classes that contain the specified annotation on the class.
- */
- public <T> Set<Class<? extends T>> scan(Class<? extends
Annotation> annotationType, Class<T> classType)
- {
- Set<Class<? extends T>> classes = Collections.emptySet();
-
- try
- {
- // only scan the current ClassPath location that contains this file. Could be a
directory or a JAR file.
- URL url = getURLPathFromClassLoader();
- String urlPath = url.getFile();
- if (urlPath.endsWith("/"))
- {
- urlPath = urlPath.substring(0, urlPath.length() - 1);
- }
-
- if (log.isDebugEnabled()) log.debug("scanning: " + urlPath);
- File file = new File(urlPath);
- if (file.isDirectory())
- {
- classes = handleDirectory(file, null, classType, annotationType);
- }
- else
- {
- classes = handleArchive(file, classType, annotationType);
- }
- }
- catch (IOException ioe)
- {
- log.warn("could not read entries", ioe);
- }
- catch (ClassNotFoundException e)
- {
- log.warn("Unable to load class", e);
- }
-
- return classes;
- }
-
- private URL getURLPathFromClassLoader() throws MalformedURLException
- {
- URL u2 = classLoader.findResource("org/jboss/cache/Version.class");
-
- for (URL u : classLoader.getURLs())
- {
- String urlString = u.toString().replaceAll("\\/\\.\\/",
"/");
- if (u2.toString().startsWith(urlString))
- {
- return new URL(urlString);
- }
- }
-
- return null;
- }
-
-
- private <T> Set<Class<? extends T>> handleArchive(File file,
Class<T> classType, Class<? extends Annotation> annotationType) throws
IOException, ClassNotFoundException
- {
- Set<Class<? extends T>> classesWithAnnotations = new
HashSet<Class<? extends T>>();
- ZipFile zip = new ZipFile(file);
- Enumeration<? extends ZipEntry> entries = zip.entries();
- while (entries.hasMoreElements())
- {
- ZipEntry entry = entries.nextElement();
- String name = entry.getName();
- Class<? extends T> c = handleItem(name, classType, annotationType);
- if (c != null) classesWithAnnotations.add(c);
- }
-
- return classesWithAnnotations;
- }
-
- private <T> Set<Class<? extends T>> handleDirectory(File file,
String path, Class<T> classType, Class<? extends Annotation> annotationType)
throws IOException, ClassNotFoundException
- {
- Set<Class<? extends T>> classesWithAnnotations = new
HashSet<Class<? extends T>>();
- for (File child : file.listFiles())
- {
- String newPath = path == null ? child.getName() : path + '/' +
child.getName();
- if (child.isDirectory())
- {
- classesWithAnnotations.addAll(handleDirectory(child, newPath, classType,
annotationType));
- }
- else
- {
- Class<? extends T> c = handleItem(newPath, classType, annotationType);
- if (c != null)
- {
- classesWithAnnotations.add(c);
- }
- }
- }
-
- return classesWithAnnotations;
- }
-
- private <T> Class<? extends T> handleItem(String name, Class<T>
classType, Class<? extends Annotation> annotationType) throws IOException,
ClassNotFoundException
- {
- if (!name.endsWith(".class")) return null;
-
- Class<? extends T> c = getClassFile(filenameToClassname(name), classType);
- if (c != null && hasAnnotation(c, annotationType))
- {
- return c;
- }
- else
- {
- return null;
- }
- }
-
- private <T> Class<? extends T> getClassFile(String name, Class<T>
classType) throws IOException, ClassNotFoundException
- {
- Class c = classLoader.loadClass(name);
- if (c != null && classType.isAssignableFrom(c)) return c;
- else return null;
- }
-
- private boolean hasAnnotation(Class clazz, Class<? extends Annotation>
annotationType)
- {
- return (clazz.isAnnotationPresent(annotationType));
- }
-
- private static String filenameToClassname(String filename)
- {
- return filename.substring(0,
filename.lastIndexOf(".class")).replace('/',
'.').replace('\\', '.');
- }
-
-}
Modified:
core/trunk/src/main/java/org/jboss/cache/invocation/CacheInvocationDelegate.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/invocation/CacheInvocationDelegate.java 2007-12-19
12:57:49 UTC (rev 4877)
+++
core/trunk/src/main/java/org/jboss/cache/invocation/CacheInvocationDelegate.java 2007-12-19
13:17:43 UTC (rev 4878)
@@ -450,11 +450,11 @@
public void putForExternalRead(Fqn<?> fqn, K key, V value)
{
// if the node exists then this should be a no-op.
- if (peek(fqn, false, false) != null)
+ if (peek(fqn, false, false) == null)
{
getInvocationContext().getOptionOverrides().setFailSilently(true);
- GlobalTransaction tx = cache.getCurrentTransaction();
- MethodCall m =
MethodCallFactory.create(MethodDeclarations.putForExternalReadMethodLocal, tx, fqn, key,
value);
+ //GlobalTransaction tx = cache.getCurrentTransaction();
+ MethodCall m =
MethodCallFactory.create(MethodDeclarations.putForExternalReadMethodLocal, null, fqn, key,
value);
invoke(m);
}
else
Modified:
core/trunk/src/test/java/org/jboss/cache/api/pfer/PutForExternalReadTestBase.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/api/pfer/PutForExternalReadTestBase.java 2007-12-19
12:57:49 UTC (rev 4877)
+++
core/trunk/src/test/java/org/jboss/cache/api/pfer/PutForExternalReadTestBase.java 2007-12-19
13:17:43 UTC (rev 4878)
@@ -74,41 +74,7 @@
@AfterMethod(alwaysRun = true)
public void tearDown()
{
- if (cache1 != null)
- {
- if (tm1 != null)
- {
- try
- {
- tm1.rollback();
- }
- catch (Exception e)
- {
- // do nothing
- }
- }
- cache1.stop();
- tm1 = null;
- cache1 = null;
- }
-
- if (cache2 != null)
- {
- if (tm2 != null)
- {
- try
- {
- tm2.rollback();
- }
- catch (Exception e)
- {
- // do nothing
- }
- }
- cache2.stop();
- tm2 = null;
- cache2 = null;
- }
+ TestingUtil.killCaches(cache1, cache2);
}
/**
@@ -314,6 +280,8 @@
public void testBasicPropagation() throws Exception
{
+ assert !cache1.exists(fqn);
+ assert !cache2.exists(fqn);
cache1.putForExternalRead(fqn, key, value);
Deleted: core/trunk/src/test/java/org/jboss/cache/factories/ComponentRegistryTest.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/factories/ComponentRegistryTest.java 2007-12-19
12:57:49 UTC (rev 4877)
+++
core/trunk/src/test/java/org/jboss/cache/factories/ComponentRegistryTest.java 2007-12-19
13:17:43 UTC (rev 4878)
@@ -1,233 +0,0 @@
-package org.jboss.cache.factories;
-
-import org.jboss.cache.CacheImpl;
-import org.jboss.cache.CacheSPI;
-import org.jboss.cache.DefaultCacheFactory;
-import org.jboss.cache.RPCManager;
-import org.jboss.cache.RegionManager;
-import org.jboss.cache.buddyreplication.BuddyManager;
-import org.jboss.cache.buddyreplication.NextMemberBuddyLocator;
-import org.jboss.cache.config.BuddyReplicationConfig;
-import org.jboss.cache.config.Configuration;
-import org.jboss.cache.factories.annotations.Inject;
-import org.jboss.cache.invocation.CacheInvocationDelegate;
-import org.jboss.cache.marshall.CacheMarshaller200;
-import org.jboss.cache.marshall.CacheMarshaller210;
-import org.jboss.cache.marshall.Marshaller;
-import org.jboss.cache.marshall.VersionAwareMarshaller;
-import org.jboss.cache.misc.TestingUtil;
-import org.jboss.cache.statetransfer.StateTransferManager;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-
-import java.lang.reflect.Constructor;
-
-/**
- * @author Manik Surtani (<a
href="mailto:manik@jboss.org">manik@jboss.org</a>)
- * @since 2.1.0
- */
-@Test(groups = {"functional"})
-public class ComponentRegistryTest
-{
- private ComponentFactory cf;
- private ComponentRegistry cr;
- private Configuration configuration;
-
- @BeforeMethod
- public void setUp() throws Exception
- {
- cf = (ComponentFactory) DefaultCacheFactory.getInstance();
-
- CacheSPI spi = new CacheInvocationDelegate();
- Constructor ctor = CacheImpl.class.getDeclaredConstructor();
- ctor.setAccessible(true);
- CacheImpl ci = (CacheImpl) ctor.newInstance();
-
- configuration = ci.getConfiguration();
- cr = TestingUtil.extractComponentRegistry(ci);
-
- cr.registerComponent(ci);
- cr.registerComponent(spi);
- cr.registerComponent(cr); // register self
- cr.registerComponent(configuration);
-
- cr.wireDependencies(cf);
- cr.wireDependencies(ci);
- cr.wireDependencies(spi);
- }
-
- public void testWiringDependenciesInFactory()
- {
- assert cf.componentRegistry == cr;
- assert cf.configuration == configuration;
-
- System.out.println(cr.registry);
- }
-
- public void testDefaultFactoryScanning()
- {
- cr.scanDefaultFactories();
-
- assert cr.defaultFactories != null : "Should be populated";
-
- // at very least, expecting a Marshaller factory and a DefaultCacheFactory.
-
- assert cr.defaultFactories.containsKey(CacheSPI.class);
- assert cr.defaultFactories.get(CacheSPI.class).equals(DefaultCacheFactory.class);
- assert cr.defaultFactories.containsKey(Marshaller.class);
- assert
cr.defaultFactories.get(Marshaller.class).equals(CacheMarshallerFactory.class);
- }
-
- public void testNamedComponents()
- {
- cr.registerComponent("blah", new Object());
- Object namedComponent1 = cr.getOrCreateComponent("blah", Object.class);
- Object namedComponent2 = cr.getOrCreateComponent("blah", Object.class);
-
- assert namedComponent1 == namedComponent2;
- }
-
- /**
- * Case 1:
- * nothing injected, nothing specified in Configuration. Should use default factory.
- */
- public void testConstructionOrder1()
- {
- Class<Marshaller> componentToTest = Marshaller.class;
- Marshaller m = cr.getOrCreateComponent(null, componentToTest);
- assert m instanceof VersionAwareMarshaller;
- VersionAwareMarshaller vam = (VersionAwareMarshaller) m;
- m = (Marshaller) TestingUtil.extractField(vam, "defaultMarshaller");
- assert m instanceof CacheMarshaller210;
- }
-
- /**
- * Case 2:
- * instance injected, class specified in Configuration. Should use injected.
- */
- public void testConstructionOrder2()
- {
- Class<Marshaller> componentToTest = Marshaller.class;
- configuration.setMarshallerClass(CacheMarshaller200.class.getName());
- Marshaller instance = new CacheMarshaller210(null, false, false);
- configuration.setCacheMarshaller(instance);
-
- // the setup() would have wired the default marshaller. Need to update deps.
- cr.unregisterComponent(Marshaller.class);
- cr.updateDependencies();
-
- Marshaller m = cr.getOrCreateComponent(null, componentToTest);
- assert m == instance : "m is " + m + " but expected " +
instance;
- }
-
- /**
- * Case 3:
- * instance injected, no class specified in Configuration. Should use injected.
- */
- public void testConstructionOrder3()
- {
- Class<Marshaller> componentToTest = Marshaller.class;
- Marshaller instance = new CacheMarshaller210(null, false, false);
- configuration.setCacheMarshaller(instance);
-
- // the setup() would have wired the default marshaller. Need to update deps.
- cr.unregisterComponent(Marshaller.class);
- cr.updateDependencies();
-
- Marshaller m = cr.getOrCreateComponent(null, componentToTest);
- assert m == instance : "m is " + m + " but expected " +
instance;
- }
-
- /**
- * Case 4:
- * nothing injected, class specified in Configuration. Should use class specified.
- */
- public void testConstructionOrder4()
- {
- Class<Marshaller> componentToTest = Marshaller.class;
- configuration.setMarshallerClass(CacheMarshaller200.class.getName());
- Marshaller m = cr.getOrCreateComponent(null, componentToTest);
- assert m instanceof VersionAwareMarshaller;
- VersionAwareMarshaller vam = (VersionAwareMarshaller) m;
- m = (Marshaller) TestingUtil.extractField(vam, "defaultMarshaller");
- assert m instanceof CacheMarshaller200;
- }
-
- public void testTransitiveDependencies()
- {
- Class<BuddyManager> componentToTest = BuddyManager.class;
-
- // configure the cfg to use BR
- BuddyReplicationConfig brc = new BuddyReplicationConfig();
- brc.setEnabled(true);
- BuddyReplicationConfig.BuddyLocatorConfig blc = new
BuddyReplicationConfig.BuddyLocatorConfig();
- blc.setBuddyLocatorClass(NextMemberBuddyLocator.class.getName());
- brc.setBuddyLocatorConfig(blc);
- configuration.setBuddyReplicationConfig(brc);
-
- BuddyManager bm = cr.getOrCreateComponent(null, componentToTest);
- assert bm != null;
-
- StateTransferManager stm = (StateTransferManager) TestingUtil.extractField(bm,
"stateTransferManager");
- assert stm != null;
-
- RPCManager rpcm = (RPCManager) TestingUtil.extractField(bm,
"rpcManager");
- assert rpcm != null;
-
- RegionManager rm = (RegionManager) TestingUtil.extractField(bm,
"regionManager");
- assert rm != null;
-
- Configuration cfg = (Configuration) TestingUtil.extractField(bm,
"configuration");
- assert cfg == configuration;
- }
-
- public void testInjectionOrder()
- {
- // injection should only occur after dependent components have been fully wired.
-
- // E.g. Test1 depends on Test2 and Test2 depends on Test3.
- //cr.reset();
-
- // DefaultFactoryFor annotation won't work since tests are compiled into a
separate classpath
- cr.defaultFactories.put(Test1.class, EmptyConstructorFactory.class);
- cr.defaultFactories.put(Test2.class, EmptyConstructorFactory.class);
- cr.defaultFactories.put(Test3.class, EmptyConstructorFactory.class);
-
- Test1 t1 = cr.getOrCreateComponent(null, Test1.class);
-
- assert t1 != null;
- assert t1.test2 != null;
- assert t1.test2.test3 != null;
- assert t1.someValue == t1.test2.test3.someValue;
- }
-
- public static class Test1
- {
- private Test2 test2;
- private boolean someValue = false;
-
- @Inject
- public void setTest2(Test2 test2)
- {
- this.test2 = test2;
- someValue = test2.test3.someValue;
- }
- }
-
- public static class Test2
- {
- private Test3 test3;
-
- @Inject
- public void setTest3(Test3 test3)
- {
- this.test3 = test3;
- }
- }
-
- public static class Test3
- {
- private boolean someValue = true;
- }
-}
-