[jbosscache-commits] JBoss Cache SVN: r8178 - core/trunk/src/main/java/org/jboss/cache/transaction.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Aug 11 07:24:10 EDT 2009


Author: manik.surtani at jboss.com
Date: 2009-08-11 07:24:10 -0400 (Tue, 11 Aug 2009)
New Revision: 8178

Modified:
   core/trunk/src/main/java/org/jboss/cache/transaction/DummyContext.java
Log:
Ability to serialize

Modified: core/trunk/src/main/java/org/jboss/cache/transaction/DummyContext.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/transaction/DummyContext.java	2009-08-10 12:42:28 UTC (rev 8177)
+++ core/trunk/src/main/java/org/jboss/cache/transaction/DummyContext.java	2009-08-11 11:24:10 UTC (rev 8178)
@@ -30,6 +30,10 @@
 import javax.naming.NamingException;
 import java.util.HashMap;
 import java.util.Hashtable;
+import java.io.ObjectInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ByteArrayOutputStream;
 
 /**
  * @author bela
@@ -40,7 +44,18 @@
 {
 
    HashMap<String, Object> bindings = new HashMap<String, Object>();
+   boolean serializing;
 
+   public DummyContext()
+   {
+      this.serializing = false;
+   }
+
+   public DummyContext(boolean serializing)
+   {
+      this.serializing = serializing;
+   }
+
    /**
     * Retrieves the named object.
     * If <tt>name</tt> is empty, returns a new instance of this context
@@ -69,7 +84,15 @@
     */
    public Object lookup(String name) throws NamingException
    {
-      return bindings.get(name);
+      try
+      {
+         deserialize();
+         return bindings.get(name);
+      }
+      finally
+      {
+         serialize();
+      }
    }
 
    /**
@@ -89,6 +112,7 @@
     */
    public void bind(Name name, Object obj) throws NamingException
    {
+      bind("NAME: " + name.toString(), obj);
    }
 
    /**
@@ -103,7 +127,15 @@
     */
    public void bind(String name, Object obj) throws NamingException
    {
-      bindings.put(name, obj);
+      try
+      {
+         deserialize();
+         bindings.put(name, obj);
+      }
+      finally
+      {
+         serialize();
+      }
    }
 
    /**
@@ -128,6 +160,7 @@
     */
    public void rebind(Name name, Object obj) throws NamingException
    {
+      bind(name, obj);
    }
 
    /**
@@ -141,7 +174,7 @@
     */
    public void rebind(String name, Object obj) throws NamingException
    {
-      bindings.put(name, obj);
+      bind(name, obj);
    }
 
    /**
@@ -166,6 +199,7 @@
     */
    public void unbind(Name name) throws NamingException
    {
+      unbind("NAME: " + name.toString());
    }
 
    /**
@@ -178,7 +212,15 @@
     */
    public void unbind(String name) throws NamingException
    {
-      bindings.remove(name);
+      try
+      {
+         deserialize();
+         bindings.remove(name);
+      }
+      finally
+      {
+         serialize();
+      }
    }
 
    /**
@@ -590,4 +632,50 @@
    {
       return null;
    }
+
+   byte[] bytes = null;
+   private void serialize()
+   {
+      if (serializing)
+      {
+         try
+         {
+            ByteArrayOutputStream bstream = new ByteArrayOutputStream();
+            ObjectOutputStream oos = new ObjectOutputStream(bstream);
+            oos.writeObject(bindings);
+            oos.close();
+            bstream.close();
+            bytes = bstream.toByteArray();
+            bindings = null;
+         }
+         catch (Exception e)
+         {
+            throw new RuntimeException(e);
+         }
+      }
+   }
+
+   @SuppressWarnings("unchecked")
+   private void deserialize()
+   {
+      if (serializing)
+      {
+         if (bytes == null)
+            bindings = new HashMap<String, Object>();
+         else
+         {
+            try
+            {
+               ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
+               bindings = (HashMap<String, Object>) ois.readObject();
+               ois.close();
+               bytes = null;
+            }
+            catch (Exception e)
+            {
+               throw new RuntimeException(e);
+            }
+         }
+      }
+   }
 }



More information about the jbosscache-commits mailing list