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

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Thu Jun 19 01:09:22 EDT 2008


Author: jason.greene at jboss.com
Date: 2008-06-19 01:09:21 -0400 (Thu, 19 Jun 2008)
New Revision: 6003

Modified:
   pojo/trunk/src/main/java/org/jboss/cache/pojo/collection/CachedObjectArray.java
   pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/CacheListenerAdaptor.java
   pojo/trunk/src/test/java/org/jboss/cache/pojo/ArrayTest.java
Log:
Add notification support for arrays


Modified: pojo/trunk/src/main/java/org/jboss/cache/pojo/collection/CachedObjectArray.java
===================================================================
--- pojo/trunk/src/main/java/org/jboss/cache/pojo/collection/CachedObjectArray.java	2008-06-17 23:59:38 UTC (rev 6002)
+++ pojo/trunk/src/main/java/org/jboss/cache/pojo/collection/CachedObjectArray.java	2008-06-19 05:09:21 UTC (rev 6003)
@@ -23,6 +23,7 @@
 
 import org.jboss.cache.Fqn;
 import org.jboss.cache.pojo.impl.ArrayInterceptable;
+import org.jboss.cache.pojo.impl.InternalConstant;
 import org.jboss.cache.pojo.impl.PojoCacheImpl;
 import org.jboss.cache.pojo.util.AopUtil;
 import org.jboss.cache.pojo.util.Null;
@@ -49,6 +50,7 @@
       Fqn<?> fqn = AopUtil.constructFqn(this.fqn, IntegerCache.toString(index));
 
       cache.attach(fqn, Null.toNullObject(element), null, arraySource);
+      cache.getCache().put(fqn, InternalConstant.POJOCACHE_OPERATION, "SET");
    }
 
    public Object get(int index)

Modified: pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/CacheListenerAdaptor.java
===================================================================
--- pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/CacheListenerAdaptor.java	2008-06-17 23:59:38 UTC (rev 6002)
+++ pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/CacheListenerAdaptor.java	2008-06-19 05:09:21 UTC (rev 6003)
@@ -22,10 +22,10 @@
 
 package org.jboss.cache.pojo.impl;
 
+import static org.jboss.cache.notifications.event.Event.Type.TRANSACTION_COMPLETED;
+import static org.jboss.cache.notifications.event.Event.Type.TRANSACTION_REGISTERED;
 import static org.jboss.cache.pojo.impl.InternalConstant.POJOCACHE_OPERATION;
 import static org.jboss.cache.pojo.impl.InternalConstant.POJOCACHE_STATUS;
-import static org.jboss.cache.notifications.event.Event.Type.TRANSACTION_COMPLETED;
-import static org.jboss.cache.notifications.event.Event.Type.TRANSACTION_REGISTERED;
 
 import java.lang.reflect.Field;
 import java.util.Collection;
@@ -50,6 +50,7 @@
 import org.jboss.cache.pojo.PojoCache;
 import org.jboss.cache.pojo.interceptors.PojoTxLockInterceptor;
 import org.jboss.cache.pojo.notification.NotificationContext;
+import org.jboss.cache.pojo.notification.event.ArrayModifiedEvent;
 import org.jboss.cache.pojo.notification.event.AttachedEvent;
 import org.jboss.cache.pojo.notification.event.DetachedEvent;
 import org.jboss.cache.pojo.notification.event.Event;
@@ -220,6 +221,11 @@
                Object value = cache.find(fqn.toString());
                sendNotification(new MapModifiedEvent(createContext(event), (Map) collection, operation, fqn.getLastElement(), value, isLocal), matched);
             }
+            else if (collection instanceof Object[]) {
+               int i = Integer.parseInt(fqn.getLastElementAsString());
+               Object value = cache.find(fqn.toString());
+               sendNotification(new ArrayModifiedEvent(createContext(event), collection, i, value, isLocal), matched);
+            }
          }
          else if ("ATTACHED".equals(cache.getCache().get(fqn, POJOCACHE_STATUS)))
          {

Modified: pojo/trunk/src/test/java/org/jboss/cache/pojo/ArrayTest.java
===================================================================
--- pojo/trunk/src/test/java/org/jboss/cache/pojo/ArrayTest.java	2008-06-17 23:59:38 UTC (rev 6002)
+++ pojo/trunk/src/test/java/org/jboss/cache/pojo/ArrayTest.java	2008-06-19 05:09:21 UTC (rev 6003)
@@ -9,6 +9,8 @@
 
 
 import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Queue;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -24,6 +26,9 @@
 import org.jboss.cache.notifications.event.NodeEvent;
 import org.jboss.cache.notifications.event.NodeModifiedEvent;
 import org.jboss.cache.notifications.event.NodeVisitedEvent;
+import org.jboss.cache.pojo.notification.annotation.ArrayModified;
+import org.jboss.cache.pojo.notification.annotation.PojoCacheListener;
+import org.jboss.cache.pojo.notification.event.ArrayModifiedEvent;
 import org.jboss.cache.pojo.test.ArrayObject;
 import org.jboss.cache.pojo.test.ArrayObject.Person;
 import org.testng.AssertJUnit;
@@ -47,11 +52,11 @@
    {
       log.info("setUp() ....");
       cache1 = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), false);
-      cache1.getCache().addCacheListener(new MyCacheListener(false));
+      //cache1.getCache().addCacheListener(new MyCacheListener(false));
       cache1.start();
 
       cache2 = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), false);
-      cache2.getCache().addCacheListener(new MyCacheListener(true));
+      //cache2.getCache().addCacheListener(new MyCacheListener(false));
       cache2.start();
 
    }
@@ -99,6 +104,36 @@
       AssertJUnit.assertEquals(4, obj.getNum(5));
    }
 
+   public void testArrayNotifications() throws Exception
+   {
+
+      ArrayObject ao = new ArrayObject();
+      Person joe = new Person();
+      joe.setName("Joe");
+      joe.setAge(19);
+
+      Person ben = new Person();
+      ben.setName("Ben");
+      ben.setAge(19);
+      cache1.attach("/ao", ao);
+
+      ArrayListener listener = new ArrayListener();
+      cache1.addListener(listener);
+      ao.setPerson(0, joe);
+      ao.setPerson(1, ben);
+
+      ArrayModifiedEvent event = listener.take();
+      AssertJUnit.assertSame(ao.getTeam(), event.getSource());
+      AssertJUnit.assertSame(joe, event.getValue());
+      AssertJUnit.assertEquals(0, event.getIndex());
+
+      event = listener.take();
+      AssertJUnit.assertSame(ao.getTeam(), event.getSource());
+      AssertJUnit.assertSame(ben, event.getValue());
+      AssertJUnit.assertEquals(1, event.getIndex());
+      cache1.removeListener(listener);
+   }
+
    public void testMulti() throws Exception
    {
       log.info("testMulti() ....");
@@ -133,8 +168,25 @@
       AssertJUnit.assertSame(ao.getMultiPerson(1, 1, 5), ao.getMultiPerson(2, 1, 4));
    }
 
+   @PojoCacheListener
+   public static class ArrayListener
+   {
+      private Queue<ArrayModifiedEvent> events = new LinkedList<ArrayModifiedEvent>();
+
+      @ArrayModified
+      public void record(ArrayModifiedEvent event)
+      {
+         events.offer(event);
+      }
+
+      public ArrayModifiedEvent take()
+      {
+         return events.poll();
+      }
+   }
+
    @CacheListener
-   public class MyCacheListener
+   public static class MyCacheListener
    {
       private boolean visits;
 




More information about the jbosscache-commits mailing list