[jbosscache-commits] JBoss Cache SVN: r5105 - in core/trunk/src: main/java/org/jboss/cache/marshall and 1 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Wed Jan 9 11:16:25 EST 2008


Author: manik.surtani at jboss.com
Date: 2008-01-09 11:16:24 -0500 (Wed, 09 Jan 2008)
New Revision: 5105

Added:
   core/trunk/src/test/java/org/jboss/cache/marshall/AbstractVersionAwareMarshallerTest.java
Modified:
   core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java
   core/trunk/src/main/java/org/jboss/cache/marshall/AbstractMarshaller.java
   core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java
   core/trunk/src/main/java/org/jboss/cache/marshall/VersionAwareMarshaller.java
   core/trunk/src/test/java/org/jboss/cache/marshall/ActiveInactiveTest.java
   core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java
   core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshallerTestBase.java
   core/trunk/src/test/java/org/jboss/cache/marshall/VersionAwareMarshallerTest.java
Log:
JBCACHE-1232 - default class loader used by marshallers

Modified: core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java	2008-01-09 15:02:25 UTC (rev 5104)
+++ core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java	2008-01-09 16:16:24 UTC (rev 5105)
@@ -85,6 +85,7 @@
    public ComponentRegistry(Configuration configuration)
    {
       // bootstrap.
+      registerComponent("deployerClassLoader", getClass().getClassLoader(), ClassLoader.class);
       registerComponent(this, ComponentRegistry.class);
       registerComponent(configuration, Configuration.class);
    }
@@ -554,6 +555,7 @@
    public void reset()
    {
       // the bootstrap classes
+      Component deployerClassLoader = componentLookup.get("deployerClassLoader");
       Component spi = componentLookup.get(CacheSPI.class.getName());
       Component impl = componentLookup.get(CacheImpl.class.getName());
       Component conf = componentLookup.get(Configuration.class.getName());
@@ -561,12 +563,13 @@
 
       componentLookup.clear();
 
+      deployerClassLoader.changeState(CONSTRUCTED);
       spi.changeState(CONSTRUCTED);
       impl.changeState(CONSTRUCTED);
       conf.changeState(CONSTRUCTED);
       cr.changeState(CONSTRUCTED);
 
-      bootstrap = new Bootstrap((CacheImpl) impl.instance, (CacheSPI) spi.instance, (ComponentRegistry) cr.instance, (Configuration) conf.instance);
+      bootstrap = new Bootstrap((ClassLoader) deployerClassLoader.instance, (CacheImpl) impl.instance, (CacheSPI) spi.instance, (ComponentRegistry) cr.instance, (Configuration) conf.instance);
 
       overallState = null;
    }
@@ -904,9 +907,11 @@
       CacheSPI cacheSPI;
       ComponentRegistry componentRegistry;
       Configuration configuration;
+      private ClassLoader deployerClassLoader;
 
-      Bootstrap(CacheImpl cacheImpl, CacheSPI cacheSPI, ComponentRegistry componentRegistry, Configuration configuration)
+      Bootstrap(ClassLoader deployerClassLoader, CacheImpl cacheImpl, CacheSPI cacheSPI, ComponentRegistry componentRegistry, Configuration configuration)
       {
+         this.deployerClassLoader = deployerClassLoader;
          this.cacheImpl = cacheImpl;
          this.cacheSPI = cacheSPI;
          this.componentRegistry = componentRegistry;
@@ -918,12 +923,14 @@
          return componentLookup.containsKey(Configuration.class.getName()) &&
                componentLookup.containsKey(CacheImpl.class.getName()) &&
                componentLookup.containsKey(CacheSPI.class.getName()) &&
-               componentLookup.containsKey(ComponentRegistry.class.getName());
+               componentLookup.containsKey(ComponentRegistry.class.getName()) &&
+               componentLookup.containsKey("deployerClassLoader");
       }
 
       void bootstrap()
       {
          overallState = CONSTRUCTED;
+         registerComponent("deployerClassLoader", deployerClassLoader, ClassLoader.class);
          registerComponent(Configuration.class.getName(), configuration, Configuration.class);
          registerComponent(ComponentRegistry.class.getName(), componentRegistry, ComponentRegistry.class);
          registerComponent(CacheImpl.class.getName(), cacheImpl, CacheImpl.class);

Modified: core/trunk/src/main/java/org/jboss/cache/marshall/AbstractMarshaller.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/marshall/AbstractMarshaller.java	2008-01-09 15:02:25 UTC (rev 5104)
+++ core/trunk/src/main/java/org/jboss/cache/marshall/AbstractMarshaller.java	2008-01-09 16:16:24 UTC (rev 5105)
@@ -13,6 +13,7 @@
 import org.jboss.cache.RegionManager;
 import org.jboss.cache.buddyreplication.BuddyManager;
 import org.jboss.cache.config.Configuration;
+import org.jboss.cache.factories.annotations.ComponentName;
 import org.jboss.cache.factories.annotations.Inject;
 import org.jboss.cache.factories.annotations.Start;
 import org.jboss.cache.transaction.GlobalTransaction;
@@ -43,10 +44,12 @@
     */
    private Map<GlobalTransaction, Fqn> transactions = new ConcurrentHashMap<GlobalTransaction, Fqn>(16);
    protected Configuration configuration;
+   protected ClassLoader defaultClassLoader;
 
    @Inject
-   void injectDependencies(RegionManager regionManager, Configuration configuration)
+   void injectDependencies(RegionManager regionManager, Configuration configuration, @ComponentName("deployerClassLoader")ClassLoader defaultClassLoader)
    {
+      this.defaultClassLoader = defaultClassLoader;
       this.regionManager = regionManager;
       this.configuration = configuration;
    }

Modified: core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java	2008-01-09 15:02:25 UTC (rev 5104)
+++ core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java	2008-01-09 16:16:24 UTC (rev 5105)
@@ -117,8 +117,7 @@
       {
          // not region based!
          if (trace) log.trace("Marshalling object " + o);
-         Map<Object, Integer> refMap = new HashMap<Object, Integer>();
-         marshallObject(o, out, refMap);
+         objectToObjectStream(o, out, null);
       }
    }
 
@@ -131,7 +130,7 @@
       else
       {
          Map<Integer, Object> refMap = new HashMap<Integer, Object>();
-         Object retValue = unmarshallObject(in, refMap);
+         Object retValue = unmarshallObject(in, defaultClassLoader, refMap);
          if (trace) log.trace("Unmarshalled object " + retValue);
          return retValue;
       }
@@ -141,12 +140,31 @@
    {
       if (trace) log.trace("Marshalling object " + o);
       Map<Object, Integer> refMap = new HashMap<Object, Integer>();
-      if (useRegionBasedMarshalling) // got to check again in case this meth is called directly
+      ClassLoader toUse = defaultClassLoader;
+      Thread current = Thread.currentThread();
+      ClassLoader old = current.getContextClassLoader();
+
+      try
       {
-         log.trace("Writing region " + region + " to stream");
-         marshallObject(region, out, refMap);
+         if (useRegionBasedMarshalling) // got to check again in case this meth is called directly
+         {
+            log.trace("Writing region " + region + " to stream");
+            Region r = null;
+            if (region != null) r = regionManager.getRegion(region, false);
+            if (r != null && r.getClassLoader() != null) toUse = r.getClassLoader();
+            current.setContextClassLoader(toUse);
+            marshallObject(region, out, refMap);
+         }
+         else
+         {
+            current.setContextClassLoader(toUse);
+         }
+         marshallObject(o, out, refMap);
       }
-      marshallObject(o, out, refMap);
+      finally
+      {
+         current.setContextClassLoader(old);
+      }
    }
 
    protected Object objectFromObjectStreamRegionBased(ObjectInputStream in) throws Exception
@@ -177,7 +195,7 @@
       {
          if (log.isDebugEnabled())
             log.debug("Region does not exist for Fqn " + regionFqn + " - not using a context classloader.");
-         retValue = unmarshallObject(in, refMap);
+         retValue = unmarshallObject(in, defaultClassLoader, refMap);
       }
       else
       {

Modified: core/trunk/src/main/java/org/jboss/cache/marshall/VersionAwareMarshaller.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/marshall/VersionAwareMarshaller.java	2008-01-09 15:02:25 UTC (rev 5104)
+++ core/trunk/src/main/java/org/jboss/cache/marshall/VersionAwareMarshaller.java	2008-01-09 16:16:24 UTC (rev 5105)
@@ -9,6 +9,8 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.jboss.cache.Fqn;
+import org.jboss.cache.factories.ComponentRegistry;
+import org.jboss.cache.factories.annotations.Inject;
 import org.jboss.cache.factories.annotations.Start;
 import org.jboss.cache.util.Util;
 
@@ -31,16 +33,23 @@
  */
 public class VersionAwareMarshaller extends AbstractMarshaller
 {
-
    private static final Log log = LogFactory.getLog(VersionAwareMarshaller.class);
    private static final int VERSION_200 = 20;
    private static final int VERSION_210 = 21;
    private static final int CUSTOM_MARSHALLER = 999;
 
+   private ComponentRegistry componentRegistry;
+
    Marshaller defaultMarshaller;
    Map<Integer, Marshaller> marshallers = new HashMap<Integer, Marshaller>();
    private int versionInt;
 
+   @Inject
+   void injectComponentRegistry(ComponentRegistry componentRegistry)
+   {
+      this.componentRegistry = componentRegistry;
+   }
+
    @Start
    public void initReplicationVersions()
    {
@@ -225,7 +234,7 @@
             {
                am = new CacheMarshaller200();
                marshaller = am;
-               am.injectDependencies(regionManager, configuration);
+               componentRegistry.wireDependencies(am);
                am.init();
                marshallers.put(VERSION_200, marshaller);
             }
@@ -240,7 +249,7 @@
             {
                am = new CacheMarshaller210();
                marshaller = am;
-               am.injectDependencies(regionManager, configuration);
+               componentRegistry.wireDependencies(am);
                am.init();
                marshallers.put(VERSION_210, marshaller);
             }

Added: core/trunk/src/test/java/org/jboss/cache/marshall/AbstractVersionAwareMarshallerTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/marshall/AbstractVersionAwareMarshallerTest.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/marshall/AbstractVersionAwareMarshallerTest.java	2008-01-09 16:16:24 UTC (rev 5105)
@@ -0,0 +1,32 @@
+package org.jboss.cache.marshall;
+
+import org.jboss.cache.RegionManager;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.factories.ComponentRegistry;
+
+/**
+ * @author Manik Surtani (<a href="mailto:manik at jboss.org">manik at jboss.org</a>)
+ * @since 2.1.0
+ */
+public abstract class AbstractVersionAwareMarshallerTest
+{
+   protected VersionAwareMarshaller createVAM(String replVersion)
+   {
+      Configuration c = new Configuration();
+      c.setReplVersionString(replVersion);
+      return createVAM(c, new RegionManager());
+   }
+
+   protected VersionAwareMarshaller createVAM(Configuration c, RegionManager rm)
+   {
+      ComponentRegistry cr = new ComponentRegistry(c);
+      cr.registerComponent(rm, RegionManager.class);
+      c.setReplVersionString(c.getReplVersionString());
+      VersionAwareMarshaller vam = new VersionAwareMarshaller();
+
+      cr.wireDependencies(vam);
+      vam.init();
+      vam.initReplicationVersions();
+      return vam;
+   }
+}

Modified: core/trunk/src/test/java/org/jboss/cache/marshall/ActiveInactiveTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/marshall/ActiveInactiveTest.java	2008-01-09 15:02:25 UTC (rev 5104)
+++ core/trunk/src/test/java/org/jboss/cache/marshall/ActiveInactiveTest.java	2008-01-09 16:16:24 UTC (rev 5105)
@@ -27,8 +27,8 @@
  * @author <a href="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
  * @version $Revision$
  */
- at Test(groups = {"functional"})
-public class ActiveInactiveTest
+ at Test(groups = "functional")
+public class ActiveInactiveTest extends AbstractVersionAwareMarshallerTest
 {
    RegionManager rman;
    CacheSPI c;
@@ -166,10 +166,7 @@
       Configuration c = new Configuration();
       c.setUseRegionBasedMarshalling(true);
       c.setInactiveOnStartup(true);
-      VersionAwareMarshaller testee = new VersionAwareMarshaller();
-      testee.injectDependencies(rman, c);
-      testee.init();
-      testee.initReplicationVersions();
+      VersionAwareMarshaller testee = createVAM(c, rman);
 
       byte[] callBytes = testee.objectToByteBuffer(replicate);
 

Modified: core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java	2008-01-09 15:02:25 UTC (rev 5104)
+++ core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java	2008-01-09 16:16:24 UTC (rev 5105)
@@ -37,7 +37,7 @@
       // need to test what's going on with 
       CacheMarshaller200 cm200 = new CacheMarshaller200();
       c.setUseRegionBasedMarshalling(true);
-      cm200.injectDependencies(new RegionManager(), c);
+      cm200.injectDependencies(new RegionManager(), c, getClass().getClassLoader());
       cm200.init();
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       ObjectOutputStream oos = new ObjectOutputStream(baos);
@@ -85,7 +85,7 @@
       RegionManager rm = new RegionManager();
       final CacheMarshaller200 cm200 = new CacheMarshaller200();
       c.setUseRegionBasedMarshalling(true);
-      cm200.injectDependencies(new RegionManager(), c);
+      cm200.injectDependencies(new RegionManager(), c, getClass().getClassLoader());
       cm200.init();
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       ObjectOutputStream oos = new ObjectOutputStream(baos);

Modified: core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshallerTestBase.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshallerTestBase.java	2008-01-09 15:02:25 UTC (rev 5104)
+++ core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshallerTestBase.java	2008-01-09 16:16:24 UTC (rev 5105)
@@ -17,8 +17,8 @@
 import java.util.ArrayList;
 import java.util.List;
 
- at Test(groups = {"functional"})
-public abstract class CacheMarshallerTestBase
+ at Test(groups = "functional")
+public abstract class CacheMarshallerTestBase extends AbstractVersionAwareMarshallerTest
 {
    protected String currentVersion;
    protected int currentVersionShort;
@@ -35,10 +35,7 @@
       c.setUseRegionBasedMarshalling(false);
       c.setInactiveOnStartup(false);
       c.setReplVersionString(currentVersion);
-      marshaller = new VersionAwareMarshaller();
-      marshaller.injectDependencies(regionManager, c);
-      marshaller.init();
-      marshaller.initReplicationVersions();
+      marshaller = createVAM(c, regionManager);
    }
 
    @AfterMethod(alwaysRun = true)

Modified: core/trunk/src/test/java/org/jboss/cache/marshall/VersionAwareMarshallerTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/marshall/VersionAwareMarshallerTest.java	2008-01-09 15:02:25 UTC (rev 5104)
+++ core/trunk/src/test/java/org/jboss/cache/marshall/VersionAwareMarshallerTest.java	2008-01-09 16:16:24 UTC (rev 5105)
@@ -6,9 +6,7 @@
  */
 package org.jboss.cache.marshall;
 
-import org.jboss.cache.RegionManager;
 import org.jboss.cache.Version;
-import org.jboss.cache.config.Configuration;
 import static org.testng.AssertJUnit.assertEquals;
 import org.testng.annotations.Test;
 
@@ -20,52 +18,41 @@
  * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
  */
 @Test(groups = {"functional"})
-public class VersionAwareMarshallerTest
+public class VersionAwareMarshallerTest extends AbstractVersionAwareMarshallerTest
 {
    public void testMarshallerSelection()
    {
-      VersionAwareMarshaller marshaller = createAndConfigure("2.1.0.GA");
+      VersionAwareMarshaller marshaller = createVAM("2.1.0.GA");
       assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
-      marshaller = createAndConfigure("2.0.0.GA");
+      marshaller = createVAM("2.0.0.GA");
       assertEquals(CacheMarshaller200.class, marshaller.defaultMarshaller.getClass());
 
-      marshaller = createAndConfigure("1.4.0.GA");
+      marshaller = createVAM("1.4.0.GA");
       assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
-      marshaller = createAndConfigure("1.5.0.GA");
+      marshaller = createVAM("1.5.0.GA");
       assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
-      marshaller = createAndConfigure("1.3.0.GA");
+      marshaller = createVAM("1.3.0.GA");
       assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
-      marshaller = createAndConfigure("1.3.0.SP2");
+      marshaller = createVAM("1.3.0.SP2");
       assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
-      marshaller = createAndConfigure("1.3.1.GA");
+      marshaller = createVAM("1.3.1.GA");
       assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
-      marshaller = createAndConfigure("1.2.4.SP2");
+      marshaller = createVAM("1.2.4.SP2");
       assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
-      marshaller = createAndConfigure("1.2.3");
+      marshaller = createVAM("1.2.3");
       assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
    }
 
-   private VersionAwareMarshaller createAndConfigure(String replVersion)
-   {
-      Configuration c = new Configuration();
-      c.setReplVersionString(replVersion);
-      VersionAwareMarshaller vam = new VersionAwareMarshaller();
-      vam.injectDependencies(new RegionManager(), c);
-      vam.init();
-      vam.initReplicationVersions();
-      return vam;
-   }
-
    public void testVersionHeaderDefaultCurrent() throws Exception
    {
-      VersionAwareMarshaller marshaller = createAndConfigure(Version.getVersionString(Version.getVersionShort()));
+      VersionAwareMarshaller marshaller = createVAM(Version.getVersionString(Version.getVersionShort()));
       byte[] bytes = marshaller.objectToByteBuffer("Hello");
 
       // expect that this has been serialized using JBoss Serialization so use this to get an OIS.
@@ -75,7 +62,7 @@
 
    public void testVersionHeader200() throws Exception
    {
-      VersionAwareMarshaller marshaller = createAndConfigure("2.0.0.GA");
+      VersionAwareMarshaller marshaller = createVAM("2.0.0.GA");
       byte[] bytes = marshaller.objectToByteBuffer("Hello");
 
       // expect that this has been serialized using JBoss Serialization so use this to get an OIS.




More information about the jbosscache-commits mailing list