This patch makes our MemoryMXBean implementation implement
the NotificationEmitter interface.  Now it just needs methods
to create the necessary notifications, so they can be called by
VMs when appropriate.

Changelog:

2006-07-18  Andrew John Hughes  <[EMAIL PROTECTED]>

        * gnu/java/lang/management/MemoryMXBeanImpl.java:
        (MemoryMXBeanImpl()): Implemented.
        (ListenerData): New private class.
        (addNotificationListener(NotificationListener,
        NotificationFilter, Object)): Implemented.
        (getNotificationInfo()): Likewise.
        (removeNotificationListener(NotificationListener)):
        Likewise.
        (removeNotificationListener(NotificationListener,
        NotificationFilter, Object)): Likewise.

-- 
Andrew :-)

Escape the Java Trap with GNU Classpath!
http://www.gnu.org/philosophy/java-trap.html
public class gcj extends Freedom implements Java { ... }
Index: gnu/java/lang/management/MemoryMXBeanImpl.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/gnu/java/lang/management/MemoryMXBeanImpl.java,v
retrieving revision 1.1
diff -u -3 -p -u -r1.1 MemoryMXBeanImpl.java
--- gnu/java/lang/management/MemoryMXBeanImpl.java      2 Jul 2006 17:29:10 
-0000       1.1
+++ gnu/java/lang/management/MemoryMXBeanImpl.java      18 Jul 2006 20:39:25 
-0000
@@ -38,8 +38,20 @@ exception statement from your version. *
 package gnu.java.lang.management;
 
 import java.lang.management.MemoryMXBean;
+import java.lang.management.MemoryNotificationInfo;
 import java.lang.management.MemoryUsage;
 
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.management.ListenerNotFoundException;
+import javax.management.MBeanNotificationInfo;
+import javax.management.Notification;
+import javax.management.NotificationEmitter;
+import javax.management.NotificationFilter;
+import javax.management.NotificationListener;
+
 /**
  * Provides access to information about the memory 
  * management of the current invocation of the virtual
@@ -51,9 +63,16 @@ import java.lang.management.MemoryUsage;
  */
 public final class MemoryMXBeanImpl
   extends BeanImpl
-  implements MemoryMXBean
+  implements MemoryMXBean, NotificationEmitter
 {
 
+  private List listeners;
+
+  public MemoryMXBeanImpl()
+  {
+    listeners = new ArrayList();
+  }
+
   public void gc()
   {
     System.gc();
@@ -85,4 +104,103 @@ public final class MemoryMXBeanImpl
     VMMemoryMXBeanImpl.setVerbose(verbose);
   }
 
+  private class ListenerData
+  {
+    private NotificationListener listener;
+    private NotificationFilter filter;
+    private Object passback;
+
+    public ListenerData(NotificationListener listener,
+                       NotificationFilter filter, Object passback)
+    {
+      this.listener = listener;
+      this.filter = filter;
+      this.passback = passback;
+    }
+    
+    public NotificationListener getListener()
+    {
+      return listener;
+    }
+
+    public NotificationFilter getFilter()
+    {
+      return filter;
+    }
+
+    public Object getPassback()
+    {
+      return passback;
+    }
+
+    public boolean equals(Object obj)
+    {
+      if (obj instanceof ListenerData)
+       {
+         ListenerData data = (ListenerData) obj;
+         return (data.getListener() == listener &&
+                 data.getFilter() == filter &&
+                 data.getPassback() == passback);
+       }
+      return false;
+    }
+
+  }
+
+  public void addNotificationListener(NotificationListener listener,
+                                     NotificationFilter filter,
+                                     Object passback)
+  {
+    if (listener == null)
+      throw new IllegalArgumentException("Null listener added to bean.");
+    listeners.add(new ListenerData(listener, filter, passback));
+  }
+
+  public MBeanNotificationInfo[] getNotificationInfo()
+  {
+    return new MBeanNotificationInfo[]
+      {
+       new MBeanNotificationInfo(new String[]
+         {
+           MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED,
+           MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED
+         },
+                                 Notification.class.getName(), 
+                                 "Memory Usage Notifications")
+      };
+  }
+
+  public void removeNotificationListener(NotificationListener listener)
+    throws ListenerNotFoundException
+  {
+    Iterator it = listeners.iterator();
+    boolean foundOne = false;
+    while (it.hasNext())
+      {
+       ListenerData data = (ListenerData) it.next();
+       if (data.getListener() == listener)
+         {
+           it.remove();
+           foundOne = true;
+         }
+      }
+    if (!foundOne)
+      throw new ListenerNotFoundException("The specified listener, " + 
listener +
+                                         "is not registered with this bean.");
+  }
+
+  public void removeNotificationListener(NotificationListener listener,
+                                        NotificationFilter filter,
+                                        Object passback)
+    throws ListenerNotFoundException
+  {
+    if (!(listeners.remove(new ListenerData(listener, filter, passback))))
+      {
+       throw new ListenerNotFoundException("The specified listener, " + 
listener +
+                                           " with filter " + filter + 
+                                           "and passback " + passback + 
+                                           ", is not registered with this 
bean.");
+      }
+  }
+
 }

Attachment: signature.asc
Description: Digital signature

Reply via email to