Author: manik.surtani(a)jboss.com
Date: 2008-04-09 11:59:33 -0400 (Wed, 09 Apr 2008)
New Revision: 5524
Modified:
core/branches/2.1.X/src/main/java/org/jboss/cache/factories/EmptyConstructorFactory.java
core/branches/2.1.X/src/main/java/org/jboss/cache/marshall/VersionAwareMarshaller.java
core/branches/2.1.X/src/test/java/org/jboss/cache/marshall/VersionAwareMarshallerTest.java
Log:
JBCACHE-1325: ClassLoader and probable memory leak in ReusableObjectInputStream
Modified:
core/branches/2.1.X/src/main/java/org/jboss/cache/factories/EmptyConstructorFactory.java
===================================================================
---
core/branches/2.1.X/src/main/java/org/jboss/cache/factories/EmptyConstructorFactory.java 2008-04-09
14:33:24 UTC (rev 5523)
+++
core/branches/2.1.X/src/main/java/org/jboss/cache/factories/EmptyConstructorFactory.java 2008-04-09
15:59:33 UTC (rev 5524)
@@ -8,7 +8,6 @@
import org.jboss.cache.loader.CacheLoaderManager;
import org.jboss.cache.marshall.Marshaller;
import org.jboss.cache.marshall.VersionAwareMarshaller;
-import org.jboss.cache.marshall.io.ObjectStreamPool;
import org.jboss.cache.notifications.Notifier;
import org.jboss.cache.remoting.jgroups.CacheMessageListener;
import org.jboss.cache.statetransfer.StateTransferManager;
@@ -22,7 +21,7 @@
*/
@DefaultFactoryFor(classes = {StateTransferManager.class, TransactionTable.class,
RegionManager.class, Notifier.class,
CacheMessageListener.class, CacheLoaderManager.class,
RemoteCacheInvocationDelegate.class, Marshaller.class,
- InvocationContextContainer.class, ObjectStreamPool.class})
+ InvocationContextContainer.class})
public class EmptyConstructorFactory extends ComponentFactory
{
@Override
Modified:
core/branches/2.1.X/src/main/java/org/jboss/cache/marshall/VersionAwareMarshaller.java
===================================================================
---
core/branches/2.1.X/src/main/java/org/jboss/cache/marshall/VersionAwareMarshaller.java 2008-04-09
14:33:24 UTC (rev 5523)
+++
core/branches/2.1.X/src/main/java/org/jboss/cache/marshall/VersionAwareMarshaller.java 2008-04-09
15:59:33 UTC (rev 5524)
@@ -12,8 +12,6 @@
import org.jboss.cache.factories.ComponentRegistry;
import org.jboss.cache.factories.annotations.Inject;
import org.jboss.cache.factories.annotations.Start;
-import org.jboss.cache.marshall.io.ObjectStreamPool;
-import org.jboss.cache.marshall.io.ReusableObjectOutputStream;
import org.jboss.cache.util.Util;
import org.jboss.util.stream.MarshalledValueInputStream;
@@ -47,13 +45,11 @@
Marshaller defaultMarshaller;
Map<Integer, Marshaller> marshallers = new HashMap<Integer,
Marshaller>();
private int versionInt;
- ObjectStreamPool pool;
@Inject
- void injectComponents(ComponentRegistry componentRegistry, ObjectStreamPool pool)
+ void injectComponents(ComponentRegistry componentRegistry)
{
this.componentRegistry = componentRegistry;
- this.pool = pool;
}
@Start
@@ -152,7 +148,8 @@
}
}
- private byte[] useNonPooledStream(Object obj) throws Exception
+ @Override
+ public byte[] objectToByteBuffer(Object obj, boolean writeHeader) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
@@ -169,57 +166,26 @@
}
@Override
- public byte[] objectToByteBuffer(Object obj, boolean writeHeader) throws Exception
- {
- if (writeHeader) return useNonPooledStream(obj);
-
- ReusableObjectOutputStream out = pool.getOutputStream();
-
- try
- {
- out.writeShort(versionInt);
- if (trace) log.trace("Wrote version " + versionInt);
-
- //now marshall the contents of the object
- defaultMarshaller.objectToObjectStream(obj, out);
-
- // and return bytes.
- return out.getBytes();
- }
- finally
- {
- pool.returnStreamToPool(out);
- }
- }
-
- @Override
public Object objectFromByteBuffer(byte[] buf) throws Exception
{
Marshaller marshaller;
int versionId;
- ObjectInputStream in = pool.getInputStream(buf);
+ ObjectInputStream in = new MarshalledValueInputStream(new
ByteArrayInputStream(buf));
try
{
- try
- {
- versionId = in.readShort();
- if (trace) log.trace("Read version " + versionId);
- }
- catch (Exception e)
- {
- log.error("Unable to read version id from first two bytes of stream,
barfing.");
- throw e;
- }
-
- marshaller = getMarshaller(versionId);
-
- return marshaller.objectFromObjectStream(in);
+ versionId = in.readShort();
+ if (trace) log.trace("Read version " + versionId);
}
- finally
+ catch (Exception e)
{
- pool.returnStreamToPool(in);
+ log.error("Unable to read version id from first two bytes of stream,
barfing.");
+ throw e;
}
+
+ marshaller = getMarshaller(versionId);
+
+ return marshaller.objectFromObjectStream(in);
}
@Override
@@ -227,29 +193,22 @@
{
Marshaller marshaller;
int versionId;
- ObjectInputStream in = pool.getInputStream(buf);
+ ObjectInputStream in = new MarshalledValueInputStream(new
ByteArrayInputStream(buf));
try
{
- try
- {
- versionId = in.readShort();
- if (trace) log.trace("Read version " + versionId);
- }
- catch (Exception e)
- {
- log.error("Unable to read version id from first two bytes of stream,
barfing.");
- throw e;
- }
-
- marshaller = getMarshaller(versionId);
-
- return marshaller.regionalizedMethodCallFromObjectStream(in);
+ versionId = in.readShort();
+ if (trace) log.trace("Read version " + versionId);
}
- finally
+ catch (Exception e)
{
- pool.returnStreamToPool(in);
+ log.error("Unable to read version id from first two bytes of stream,
barfing.");
+ throw e;
}
+
+ marshaller = getMarshaller(versionId);
+
+ return marshaller.regionalizedMethodCallFromObjectStream(in);
}
@Override
Modified:
core/branches/2.1.X/src/test/java/org/jboss/cache/marshall/VersionAwareMarshallerTest.java
===================================================================
---
core/branches/2.1.X/src/test/java/org/jboss/cache/marshall/VersionAwareMarshallerTest.java 2008-04-09
14:33:24 UTC (rev 5523)
+++
core/branches/2.1.X/src/test/java/org/jboss/cache/marshall/VersionAwareMarshallerTest.java 2008-04-09
15:59:33 UTC (rev 5524)
@@ -9,13 +9,13 @@
import org.jboss.cache.Version;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.factories.ComponentRegistry;
-import org.jboss.cache.marshall.io.ObjectStreamPool;
-import org.jboss.cache.misc.TestingUtil;
+import org.jboss.util.stream.MarshalledValueInputStream;
import static org.testng.AssertJUnit.assertEquals;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
/**
@@ -73,8 +73,7 @@
VersionAwareMarshaller marshaller =
createVAMandRestartCache(Version.getVersionString(Version.getVersionShort()));
byte[] bytes = marshaller.objectToByteBuffer("Hello");
- ObjectStreamPool pool = (ObjectStreamPool) TestingUtil.extractField(marshaller,
"pool");
- ObjectInputStream in = pool.getInputStream(bytes);
+ ObjectInputStream in = new MarshalledValueInputStream(new
ByteArrayInputStream(bytes));
assertEquals("Version header short should be '21'", 21,
in.readShort());
}
@@ -83,8 +82,7 @@
VersionAwareMarshaller marshaller =
createVAMandRestartCache("2.0.0.GA");
byte[] bytes = marshaller.objectToByteBuffer("Hello");
- ObjectStreamPool pool = (ObjectStreamPool) TestingUtil.extractField(marshaller,
"pool");
- ObjectInputStream in = pool.getInputStream(bytes);
+ ObjectInputStream in = new MarshalledValueInputStream(new
ByteArrayInputStream(bytes));
assertEquals("Version header short should be '20'", 20,
in.readShort());
}