[jboss-cvs] JBossCache/src/org/jboss/cache/pojo/jmx ...
Brian Stansberry
brian.stansberry at jboss.com
Mon Jun 4 18:56:02 EDT 2007
User: bstansberry
Date: 07/06/04 18:56:02
Modified: src/org/jboss/cache/pojo/jmx PojoCacheJmxWrapper.java
Log:
[JBCACHE-1089] PojoCacheJmxWrapper should implement NotificationEmitter
Revision Changes Path
1.13 +159 -4 JBossCache/src/org/jboss/cache/pojo/jmx/PojoCacheJmxWrapper.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: PojoCacheJmxWrapper.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/pojo/jmx/PojoCacheJmxWrapper.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -b -r1.12 -r1.13
--- PojoCacheJmxWrapper.java 24 May 2007 03:56:33 -0000 1.12
+++ PojoCacheJmxWrapper.java 4 Jun 2007 22:56:02 -0000 1.13
@@ -21,6 +21,10 @@
*/
package org.jboss.cache.pojo.jmx;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.CacheException;
@@ -33,6 +37,7 @@
import org.jboss.cache.config.RuntimeConfig;
import org.jboss.cache.factories.XmlConfigurationParser;
import org.jboss.cache.jmx.CacheJmxWrapper;
+import org.jboss.cache.jmx.CacheNotificationListener;
import org.jboss.cache.pojo.PojoCache;
import org.jboss.cache.pojo.PojoCacheAlreadyDetachedException;
import org.jboss.cache.pojo.PojoCacheException;
@@ -44,13 +49,18 @@
import org.w3c.dom.Element;
import javax.management.JMException;
+import javax.management.ListenerNotFoundException;
+import javax.management.MBeanNotificationInfo;
import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
+import javax.management.NotificationEmitter;
+import javax.management.NotificationFilter;
+import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.transaction.TransactionManager;
public class PojoCacheJmxWrapper
- implements PojoCacheJmxWrapperMBean, MBeanRegistration
+ implements PojoCacheJmxWrapperMBean, MBeanRegistration, NotificationEmitter
{
private Log log = LogFactory.getLog(getClass().getName());
@@ -63,6 +73,8 @@
private boolean registerPlainCache = true;
private boolean plainCacheRegistered;
private CacheStatus cacheStatus;
+ private final Set<NotificationListenerArgs> pendingListeners =
+ new HashSet<NotificationListenerArgs>();
// Legacy config support
@@ -87,7 +99,7 @@
*/
public PojoCacheJmxWrapper(PojoCache toWrap)
{
- cacheStatus = CacheStatus.INSTANTIATED;
+ this();
setPojoCache(toWrap);
}
@@ -612,6 +624,9 @@
cacheObjectName = objName.getCanonicalName();
}
+ if (plainCacheWrapper != null)
+ plainCacheWrapper.setNotificationServiceName(cacheObjectName);
+
return new ObjectName(cacheObjectName);
}
@@ -658,7 +673,83 @@
}
}
- // -------------------------------------------------------------- Public methods
+ // ---------------------------------------------------- NotificationEmitter
+
+ public void removeNotificationListener(NotificationListener listener,
+ NotificationFilter filter,
+ Object handback)
+ throws ListenerNotFoundException
+ {
+ synchronized (pendingListeners)
+ {
+ boolean found = pendingListeners.remove(new NotificationListenerArgs(listener, filter, handback));
+
+ if (plainCacheWrapper != null)
+ {
+ plainCacheWrapper.removeNotificationListener(listener, filter, handback);
+ }
+ else if (!found)
+ {
+ throw new ListenerNotFoundException();
+ }
+ }
+ }
+
+ public void addNotificationListener(NotificationListener listener,
+ NotificationFilter filter,
+ Object handback)
+ throws IllegalArgumentException
+ {
+ synchronized (pendingListeners)
+ {
+ if (plainCacheWrapper != null)
+ {
+ plainCacheWrapper.addNotificationListener(listener, filter, handback);
+ }
+ else
+ {
+ // Add it for addition to the plainCacheWrapper when it's created
+ pendingListeners.add(new NotificationListenerArgs(listener, filter, handback));
+ }
+ }
+
+ }
+
+ public MBeanNotificationInfo[] getNotificationInfo()
+ {
+ return CacheNotificationListener.getNotificationInfo();
+ }
+
+ public void removeNotificationListener(NotificationListener listener)
+ throws ListenerNotFoundException
+ {
+ synchronized (pendingListeners)
+ {
+ boolean found = false;
+ for (Iterator<NotificationListenerArgs> iter = pendingListeners.iterator();
+ iter.hasNext(); )
+ {
+ NotificationListenerArgs args = iter.next();
+ if (safeEquals(listener, args.listener))
+ {
+ found = true;
+ iter.remove();
+ }
+ }
+
+ if (plainCacheWrapper != null)
+ {
+ plainCacheWrapper.removeNotificationListener(listener);
+ }
+ else if (!found)
+ {
+ throw new ListenerNotFoundException();
+ }
+
+ }
+ }
+
+ // --------------------------------------------------------- Public methods
public MBeanServer getMBeanServer()
{
@@ -713,6 +804,20 @@
plainCache.setRegisterInterceptors(getRegisterInterceptors());
plainCache.setCache(pojoCache.getCache());
+ if (server != null)
+ {
+ plainCache.setNotificationServiceName(cacheObjectName);
+ }
+
+ // Add any NotificationListeners we registered before creating
+ // the CacheJmxWrapper
+ synchronized(pendingListeners)
+ {
+ for (NotificationListenerArgs args : pendingListeners)
+ {
+ plainCache.addNotificationListener(args.listener, args.filter, args.handback);
+ }
+ }
return plainCache;
}
@@ -800,4 +905,54 @@
else
throw new PojoCacheException(t);
}
+
+ private static boolean safeEquals(Object us, Object them)
+ {
+ return (us == null ? them == null : us.equals(them));
+ }
+
+ private static class NotificationListenerArgs
+ {
+ NotificationListener listener;
+ NotificationFilter filter;
+ Object handback;
+
+ NotificationListenerArgs(NotificationListener listener,
+ NotificationFilter filter,
+ Object handback)
+ {
+ this.listener = listener;
+ this.filter = filter;
+ this.handback = handback;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (this == obj) return true;
+
+ if (obj instanceof NotificationListenerArgs)
+ {
+ NotificationListenerArgs other = (NotificationListenerArgs) obj;
+ if (safeEquals(listener, other.listener)
+ && safeEquals(filter, other.filter)
+ && safeEquals(handback, other.handback))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ int result = 17;
+ result = 29 * result + (listener != null ? listener.hashCode() : 0);
+ result = 29 * result + (filter != null ? filter.hashCode() : 0);
+ result = 29 * result + (handback != null ? handback.hashCode() : 0);
+ return result;
+ }
+ }
+
}
More information about the jboss-cvs-commits
mailing list