[jbosscache-commits] JBoss Cache SVN: r5986 - in pojo/trunk/src: main/java/org/jboss/cache/pojo/interceptors/dynamic and 1 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Thu Jun 12 22:36:30 EDT 2008


Author: jason.greene at jboss.com
Date: 2008-06-12 22:36:30 -0400 (Thu, 12 Jun 2008)
New Revision: 5986

Modified:
   pojo/trunk/src/main/java/org/jboss/cache/pojo/collection/CollectionInterceptorUtil.java
   pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/AbstractCollectionInterceptor.java
   pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/CachedListInterceptor.java
   pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/CachedMapInterceptor.java
   pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/CachedSetInterceptor.java
   pojo/trunk/src/test/java/org/jboss/cache/pojo/collection/CachedListTest.java
   pojo/trunk/src/test/java/org/jboss/cache/pojo/collection/CachedMapTest.java
   pojo/trunk/src/test/java/org/jboss/cache/pojo/collection/CachedSetTest.java
Log:
PCACHE-66 - Add support for serializable proxies


Modified: pojo/trunk/src/main/java/org/jboss/cache/pojo/collection/CollectionInterceptorUtil.java
===================================================================
--- pojo/trunk/src/main/java/org/jboss/cache/pojo/collection/CollectionInterceptorUtil.java	2008-06-12 16:31:07 UTC (rev 5985)
+++ pojo/trunk/src/main/java/org/jboss/cache/pojo/collection/CollectionInterceptorUtil.java	2008-06-13 02:36:30 UTC (rev 5986)
@@ -47,7 +47,7 @@
    private static ClassProxy createProxy(Class clazz, AbstractCollectionInterceptor interceptor)
            throws Exception
    {
-      ClassProxy result = ClassProxyFactory.newInstance(clazz);
+      ClassProxy result = ClassProxyFactory.newInstance(clazz, null, true);
       InstanceAdvisor advisor = result._getInstanceAdvisor();
       advisor.appendInterceptor(interceptor);
 
@@ -153,6 +153,14 @@
             {
                log.trace("invoke(): method intercepted " + method.getName());
             }
+
+            if (writeReplaceInvocation(methodInvocation)) {
+               if (!skipVerify(methodInvocation.getMethod()))
+                  interceptor.verifyAttached(impl);
+
+               return interceptor.getSerializationCopy();
+            }
+
             Object[] args = methodInvocation.getArguments();
             if (method != null)
             {
@@ -193,4 +201,10 @@
       return invocation.invokeNext();
    }
 
+   private static boolean writeReplaceInvocation(MethodInvocation methodInvocation)
+   {
+      Method method = methodInvocation.getMethod();
+      return "writeReplace".equals(method.getName()) && method.getParameterTypes().length == 0;
+   }
+
 }

Modified: pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/AbstractCollectionInterceptor.java
===================================================================
--- pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/AbstractCollectionInterceptor.java	2008-06-12 16:31:07 UTC (rev 5985)
+++ pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/AbstractCollectionInterceptor.java	2008-06-13 02:36:30 UTC (rev 5986)
@@ -97,8 +97,32 @@
       throw new PojoCacheAlreadyDetachedException(identity + " has possibly been detached remotely. Internal id: " + fqn);
    }
 
+   Object copyOrConstruct(Object mem)
+   {
+      if (mem instanceof Cloneable) {
+         try
+         {
+            return mem.getClass().getMethod("clone").invoke(mem);
+         }
+         catch (Exception e)
+         {
+         }
+      }
+
+      try
+      {
+         return mem.getClass().newInstance();
+      }
+      catch (Exception e)
+      {
+      }
+
+      return null;
+   }
+
    abstract void setInMemoryCopy(Object obj);
    abstract Object getInMemoryCopy();
+   public abstract Object getSerializationCopy();
    abstract void setCacheCopy(Object obj);
    abstract Object getCacheCopy();
    abstract void setCurrentCopy(Object obj);

Modified: pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/CachedListInterceptor.java
===================================================================
--- pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/CachedListInterceptor.java	2008-06-12 16:31:07 UTC (rev 5985)
+++ pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/CachedListInterceptor.java	2008-06-13 02:36:30 UTC (rev 5986)
@@ -8,6 +8,7 @@
 
 import org.jboss.aop.advice.Interceptor;
 import org.jboss.cache.Fqn;
+import org.jboss.cache.pojo.PojoCacheException;
 import org.jboss.cache.pojo.collection.CachedListImpl;
 import org.jboss.cache.pojo.collection.CollectionInterceptorUtil;
 import org.jboss.cache.pojo.impl.PojoCacheImpl;
@@ -182,6 +183,32 @@
       }
    }
 
+   public Object getSerializationCopy()
+   {
+      if (current_ == inMemImpl_)
+         return inMemImpl_;
+
+      List list;
+      Object mem = inMemImpl_;
+      if (mem == null)
+      {
+         list = new ArrayList();
+      }
+      else
+      {
+         list = (List) copyOrConstruct(mem);
+         if (list == null)
+            throw new PojoCacheException("Could not serialize class, since it can not be copied: " + mem.getClass().getName());
+      }
+
+      list.clear();
+      int size = cacheImpl_.size();
+      for (int i = 0; i < size; i++)
+         list.add(cacheImpl_.get(i));
+
+      return list;
+   }
+
    public String getName()
    {
       return "CachedListInterceptor";

Modified: pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/CachedMapInterceptor.java
===================================================================
--- pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/CachedMapInterceptor.java	2008-06-12 16:31:07 UTC (rev 5985)
+++ pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/CachedMapInterceptor.java	2008-06-13 02:36:30 UTC (rev 5986)
@@ -8,12 +8,15 @@
 
 import org.jboss.aop.advice.Interceptor;
 import org.jboss.cache.Fqn;
+import org.jboss.cache.pojo.PojoCacheException;
 import org.jboss.cache.pojo.collection.CachedMapImpl;
 import org.jboss.cache.pojo.collection.CollectionInterceptorUtil;
 import org.jboss.cache.pojo.impl.PojoCacheImpl;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -165,6 +168,35 @@
       }
    }
 
+   public Object getSerializationCopy()
+   {
+      if (current_ == inMemImpl_)
+         return inMemImpl_;
+
+      Map map;
+      Object mem = inMemImpl_;
+      if (mem == null)
+      {
+         map = new HashMap();
+      }
+      else
+      {
+         map = (Map) copyOrConstruct(mem);
+         if (map == null)
+            throw new PojoCacheException("Could not serialize class, since it can not be copied: " + mem.getClass().getName());
+      }
+
+      map.clear();
+      Iterator it = cacheImpl_.keySet().iterator();
+      while (it.hasNext())
+      {
+         Object key = it.next();
+         Object val = cacheImpl_.get(key);
+         map.put(key, val);
+      }
+      return map;
+   }
+
    public String getName()
    {
       return "CachedMapInterceptor";

Modified: pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/CachedSetInterceptor.java
===================================================================
--- pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/CachedSetInterceptor.java	2008-06-12 16:31:07 UTC (rev 5985)
+++ pojo/trunk/src/main/java/org/jboss/cache/pojo/interceptors/dynamic/CachedSetInterceptor.java	2008-06-13 02:36:30 UTC (rev 5986)
@@ -8,10 +8,12 @@
 
 import org.jboss.aop.advice.Interceptor;
 import org.jboss.cache.Fqn;
+import org.jboss.cache.pojo.PojoCacheException;
 import org.jboss.cache.pojo.collection.CachedSetImpl;
 import org.jboss.cache.pojo.collection.CollectionInterceptorUtil;
 import org.jboss.cache.pojo.impl.PojoCacheImpl;
 
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
@@ -159,7 +161,34 @@
       }
    }
 
+   public Object getSerializationCopy()
+   {
+      if (current_ == inMemImpl_)
+         return inMemImpl_;
 
+      Set set;
+      Object mem = inMemImpl_;
+      if (mem == null)
+      {
+         set = new HashSet();
+      }
+      else
+      {
+         set = (Set) copyOrConstruct(mem);
+         if (set == null)
+            throw new PojoCacheException("Could not serialize class, since it can not be copied: " + mem.getClass().getName());
+      }
+
+      set.clear();
+      Iterator it = cacheImpl_.iterator();
+      while (it.hasNext())
+      {
+         Object obj = it.next();
+         set.add(obj);
+      }
+      return set;
+   }
+
    public String getName()
    {
       return "CachedSetInterceptor";

Modified: pojo/trunk/src/test/java/org/jboss/cache/pojo/collection/CachedListTest.java
===================================================================
--- pojo/trunk/src/test/java/org/jboss/cache/pojo/collection/CachedListTest.java	2008-06-12 16:31:07 UTC (rev 5985)
+++ pojo/trunk/src/test/java/org/jboss/cache/pojo/collection/CachedListTest.java	2008-06-13 02:36:30 UTC (rev 5986)
@@ -5,6 +5,10 @@
 import static org.testng.AssertJUnit.assertTrue;
 import static org.testng.AssertJUnit.fail;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -321,6 +325,27 @@
    }
 
    @SuppressWarnings("unchecked")
+   public void testSerialize() throws Exception
+   {
+      List<String> list = new ArrayList<String>();
+      list.add("English");
+      list.add("French");
+      list.add("Taiwanese");
+
+      cache_.attach("/test", list); // attach
+      list = (List<String>) cache_.find("/test");
+      ByteArrayOutputStream bout = new ByteArrayOutputStream();
+      ObjectOutputStream out = new ObjectOutputStream(bout);
+      out.writeObject(list);
+      List<String> newList = (List<String>)
+         (new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()))).readObject();
+
+      assertTrue(newList instanceof ArrayList);
+      assertEquals(list, newList);
+   }
+
+
+   @SuppressWarnings("unchecked")
    public void testPojoAttachAndDetach() throws Exception
    {
       Address add1 = new Address();

Modified: pojo/trunk/src/test/java/org/jboss/cache/pojo/collection/CachedMapTest.java
===================================================================
--- pojo/trunk/src/test/java/org/jboss/cache/pojo/collection/CachedMapTest.java	2008-06-12 16:31:07 UTC (rev 5985)
+++ pojo/trunk/src/test/java/org/jboss/cache/pojo/collection/CachedMapTest.java	2008-06-13 02:36:30 UTC (rev 5986)
@@ -5,11 +5,17 @@
 import static org.testng.AssertJUnit.assertTrue;
 import static org.testng.AssertJUnit.fail;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -271,6 +277,26 @@
    }
 
    @SuppressWarnings("unchecked")
+   public void testSerialize() throws Exception
+   {
+      Map<Integer, String> map = new HashMap<Integer,String>();
+      map.put(1, "English");
+      map.put(2, "French");
+      map.put(3, "Taiwanese");
+
+      cache_.attach("/test", map); // attach
+      map = (Map<Integer, String>) cache_.find("/test");
+      ByteArrayOutputStream bout = new ByteArrayOutputStream();
+      ObjectOutputStream out = new ObjectOutputStream(bout);
+      out.writeObject(map);
+      Map<Integer, String> newMap = (Map<Integer, String>)
+         (new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()))).readObject();
+
+      assertTrue(newMap instanceof HashMap);
+      assertEquals(map, newMap);
+   }
+
+   @SuppressWarnings("unchecked")
    public void testPojoAttachAndDetach() throws Exception
    {
       Address add1 = new Address();

Modified: pojo/trunk/src/test/java/org/jboss/cache/pojo/collection/CachedSetTest.java
===================================================================
--- pojo/trunk/src/test/java/org/jboss/cache/pojo/collection/CachedSetTest.java	2008-06-12 16:31:07 UTC (rev 5985)
+++ pojo/trunk/src/test/java/org/jboss/cache/pojo/collection/CachedSetTest.java	2008-06-13 02:36:30 UTC (rev 5986)
@@ -5,6 +5,10 @@
 import static org.testng.AssertJUnit.assertTrue;
 import static org.testng.AssertJUnit.fail;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.io.PrintWriter;
 import java.io.Serializable;
 import java.io.StringWriter;
@@ -35,7 +39,7 @@
  */
 
 @Test(groups = {"functional"})
-public class CachedSetTest 
+public class CachedSetTest
 {
    Log log = LogFactory.getLog(CachedSetTest.class);
    PojoCache cache_;
@@ -126,7 +130,7 @@
          return "" + i;
       }
    }
-   
+
    @SuppressWarnings("unchecked")
    public void testConflictingHash() throws Exception
    {
@@ -299,6 +303,26 @@
    }
 
    @SuppressWarnings("unchecked")
+   public void testSerialize() throws Exception
+   {
+      Set<String> set = new HashSet<String>();
+      set.add("English");
+      set.add("French");
+      set.add("Taiwanese");
+
+      cache_.attach("/test", set); // attach
+      set = (Set<String>) cache_.find("/test");
+      ByteArrayOutputStream bout = new ByteArrayOutputStream();
+      ObjectOutputStream out = new ObjectOutputStream(bout);
+      out.writeObject(set);
+      Set<String> newSet = (Set<String>)
+         (new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()))).readObject();
+
+      assertTrue(newSet instanceof HashSet);
+      assertEquals(set, newSet);
+   }
+
+   @SuppressWarnings("unchecked")
    public void testPojoAttachAndDetach() throws Exception
    {
       Address add1 = new Address();




More information about the jbosscache-commits mailing list