KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > remoting > tracker > MBeanTrackerFilter


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9 package org.jboss.mx.remoting.tracker;
10
11 import java.io.Serializable JavaDoc;
12 import java.util.List JavaDoc;
13 import javax.management.AttributeChangeNotification JavaDoc;
14 import javax.management.InstanceNotFoundException JavaDoc;
15 import javax.management.MBeanServer JavaDoc;
16 import javax.management.MBeanServerFactory JavaDoc;
17 import javax.management.MBeanServerNotification JavaDoc;
18 import javax.management.Notification JavaDoc;
19 import javax.management.NotificationFilter JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21
22 /**
23  * MBeanTrackerFilter
24  *
25  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
26  * @version $Revision: 30251 $
27  */

28 public class MBeanTrackerFilter implements NotificationFilter JavaDoc, Serializable JavaDoc
29 {
30    static final long serialVersionUID = -4239778153506655691L;
31    protected final String JavaDoc classNames[];
32    protected final String JavaDoc serverId;
33    protected final boolean wantNotifications;
34
35    public MBeanTrackerFilter(String JavaDoc serverId, String JavaDoc cn[], boolean wantNotifications)
36    {
37       this.serverId = serverId;
38       this.classNames = cn;
39       this.wantNotifications = wantNotifications;
40    }
41
42    public boolean isNotificationEnabled(Notification JavaDoc notification)
43    {
44       if(notification instanceof MBeanServerNotification JavaDoc)
45       {
46          MBeanServerNotification JavaDoc n = (MBeanServerNotification JavaDoc) notification;
47          ObjectName JavaDoc mbean = n.getMBeanName();
48          // find the server using the server id
49
List JavaDoc list = MBeanServerFactory.findMBeanServer(serverId);
50          if(list.isEmpty() == false)
51          {
52             MBeanServer JavaDoc server = (MBeanServer JavaDoc) list.get(0);
53             if(notification.getType().equals(MBeanServerNotification.REGISTRATION_NOTIFICATION))
54             {
55                if(classNames == null)
56                {
57                   return true;
58                }
59                for(int c = 0; c < classNames.length; c++)
60                {
61                   try
62                   {
63                      if(server.isInstanceOf(mbean, classNames[c]))
64                      {
65                         // add an interest, since we can't call this same method later when
66
// the mbean is unregistered
67
return true;
68                      }
69                   }
70                   catch(Exception JavaDoc ex)
71                   {
72 // ex.printStackTrace();
73
}
74                }
75             }
76             else if(notification.getType().equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION))
77             {
78                return false == (mbean.getDomain().equals("mx.remoting") ||
79                                 mbean.getDomain().equals("JMImplementation"));
80             }
81          }
82
83       }
84       else if(notification instanceof AttributeChangeNotification JavaDoc)
85       {
86          // we want state changes directly
87
AttributeChangeNotification JavaDoc ch = (AttributeChangeNotification JavaDoc) notification;
88          if(ch.getAttributeName().equals("State") &&
89             (ch.getAttributeType().equals(Integer.TYPE.getName()) || ch.getAttributeType().equals(Integer JavaDoc.class.getName())))
90          {
91             return true;
92          }
93       }
94       if(wantNotifications)
95       {
96          Object JavaDoc src = notification.getSource();
97          if(src instanceof ObjectName JavaDoc)
98          {
99             ObjectName JavaDoc obj = (ObjectName JavaDoc) src;
100             // find the server using the server id
101
List JavaDoc list = MBeanServerFactory.findMBeanServer(serverId);
102 // log.debug("list of servers=="+list);
103
if(list.isEmpty() == false)
104             {
105                MBeanServer JavaDoc server = (MBeanServer JavaDoc) list.get(0);
106                if(classNames == null)
107                {
108                   return true;
109                }
110                for(int c = 0; c < classNames.length; c++)
111                {
112                   try
113                   {
114                      if(server.isInstanceOf(obj, classNames[c]))
115                      {
116                         // add an interest, since we can't call this same method later when
117
// the mbean is unregistered
118
return true;
119                      }
120                   }
121                   catch(InstanceNotFoundException JavaDoc inf)
122                   {
123                      return false;
124                   }
125                   catch(Exception JavaDoc ex)
126                   {
127                      ex.printStackTrace();
128                   }
129                }
130             }
131          }
132       }
133       return false;
134    }
135
136 }
137
138
Popular Tags