KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.UndeclaredThrowableException JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21 import javax.management.AttributeChangeNotification JavaDoc;
22 import javax.management.InstanceNotFoundException JavaDoc;
23 import javax.management.MBeanException JavaDoc;
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.MBeanServerNotification JavaDoc;
26 import javax.management.Notification JavaDoc;
27 import javax.management.NotificationBroadcaster JavaDoc;
28 import javax.management.NotificationFilter JavaDoc;
29 import javax.management.NotificationListener JavaDoc;
30 import javax.management.ObjectInstance JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.QueryExp JavaDoc;
33 import javax.management.ReflectionException JavaDoc;
34 import org.jboss.logging.Logger;
35 import org.jboss.mx.remoting.JMXUtil;
36 import org.jboss.mx.remoting.MBeanLocator;
37 import org.jboss.mx.remoting.MBeanServerLocator;
38 import org.jboss.mx.remoting.event.ClassQueryExp;
39 import org.jboss.mx.remoting.event.CompositeEventFilter;
40 import org.jboss.mx.remoting.event.CompositeQueryExp;
41 import org.jboss.remoting.ConnectionFailedException;
42 import org.jboss.remoting.ident.Identity;
43 import org.jboss.remoting.network.NetworkInstance;
44 import org.jboss.remoting.network.NetworkNotification;
45 import org.jboss.remoting.network.NetworkRegistryFinder;
46 import org.jboss.remoting.network.NetworkRegistryMBean;
47
48 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
49
50 /**
51  * MBeanTracker is a utility class that will track MBeans on behalf of a user object.
52  *
53  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
54  * @version $Revision: 30251 $
55  */

56 public class MBeanTracker implements NotificationListener JavaDoc
57 {
58    private static final boolean logEvents = Boolean.getBoolean("jboss.mx.tracker.debug");
59    private static final transient Logger log = Logger.getLogger(MBeanTracker.class.getName());
60    private final QueryExp JavaDoc query;
61    private final boolean localOnly;
62    private final boolean wantNotifications;
63    private final NotificationFilter JavaDoc filter;
64    private final SynchronizedInt count = new SynchronizedInt(0);
65    private final Map JavaDoc mbeans = new HashMap JavaDoc();
66    private final String JavaDoc classes[];
67    private final List JavaDoc actions = new ArrayList JavaDoc(1);
68    private final ObjectName JavaDoc networkRegistry;
69    private final MBeanServer JavaDoc myserver;
70
71    public MBeanTracker(MBeanServer JavaDoc myserver, Class JavaDoc cl[], QueryExp JavaDoc query, boolean localOnly, MBeanTrackerAction action)
72          throws Exception JavaDoc
73    {
74       this(myserver, cl, query, localOnly, null, false, new MBeanTrackerAction[]{action});
75    }
76
77    public MBeanTracker(MBeanServer JavaDoc myserver, Class JavaDoc cl[], QueryExp JavaDoc query, boolean localOnly, MBeanTrackerAction actions[])
78          throws Exception JavaDoc
79    {
80       this(myserver, cl, query, localOnly, null, false, actions);
81    }
82
83    public MBeanTracker(MBeanServer JavaDoc myserver, Class JavaDoc cl[], boolean localOnly, MBeanTrackerAction action)
84          throws Exception JavaDoc
85    {
86       this(myserver, cl, null, localOnly, null, false, new MBeanTrackerAction[]{action});
87    }
88
89    public MBeanTracker(MBeanServer JavaDoc myserver, Class JavaDoc cl[], boolean localOnly, MBeanTrackerAction actions[])
90          throws Exception JavaDoc
91    {
92       this(myserver, cl, null, localOnly, null, false, actions);
93    }
94
95    public MBeanTracker(MBeanServer JavaDoc myserver, Class JavaDoc cl[], QueryExp JavaDoc query, boolean localOnly, NotificationFilter JavaDoc filter, boolean wantNotifications, MBeanTrackerAction action)
96          throws Exception JavaDoc
97    {
98       this(myserver, cl, query, localOnly, filter, wantNotifications, new MBeanTrackerAction[]{action});
99    }
100
101    public MBeanTracker(MBeanServer JavaDoc myserver, Class JavaDoc cl[], QueryExp JavaDoc query, boolean localOnly, NotificationFilter JavaDoc filter, boolean wantNotifications)
102          throws Exception JavaDoc
103    {
104       this(myserver, cl, query, localOnly, filter, wantNotifications, (MBeanTrackerAction[]) null);
105    }
106
107    /**
108     * create a tracker
109     *
110     * @param myserver local mbean server
111     * @param cl array of classes that mbeans implement that you want to track, or null to not look at class interfaces
112     * @param query query expression to apply when selecting mbeans or null to not use a query expression
113     * @param localOnly true to only search the local mbeanserver, false to search the entire network of mbeans servers
114     * @param filter filter to apply for receiving notifications or null to apply no filter
115     * @param wantNotifications if true, will also track notifications by the mbeans being tracked
116     * @param actions array of actions to automatically register as listeners, or null if none
117     * @throws Exception raised on exception
118     */

119    public MBeanTracker(MBeanServer JavaDoc myserver, Class JavaDoc cl[], QueryExp JavaDoc query, boolean localOnly, NotificationFilter JavaDoc filter, boolean wantNotifications, MBeanTrackerAction actions[])
120          throws Exception JavaDoc
121    {
122       this.localOnly = localOnly;
123       this.wantNotifications = wantNotifications;
124       this.filter = filter;
125       this.myserver = myserver;
126
127       if(log.isTraceEnabled())
128       {
129          StringBuffer JavaDoc buf = new StringBuffer JavaDoc("creating an MBeanTracker with the following parameters:\n");
130          buf.append("==========================================\n");
131          buf.append("MBeanServer: " + myserver + "\n");
132          if(cl == null)
133          {
134             buf.append("classes: none\n");
135          }
136          else
137          {
138             for(int c = 0; c < cl.length; c++)
139             {
140                buf.append("classes[" + c + "] " + cl[c].getName() + "\n");
141             }
142          }
143          log.debug("QueryExp: " + query + "\n");
144          log.debug("localOnly: " + localOnly + "\n");
145          log.debug("filter: " + filter + "\n");
146          log.debug("notifications: " + wantNotifications + "\n");
147
148          if(actions == null)
149          {
150             log.debug("actions: none\n");
151          }
152          else
153          {
154             for(int c = 0; c < actions.length; c++)
155             {
156                log.debug("actions[" + c + "]: " + actions[c] + "\n");
157             }
158          }
159          buf.append("==========================================\n");
160          log.debug(buf.toString());
161       }
162
163       // add actions
164
if(actions != null)
165       {
166          for(int c = 0; c < actions.length; c++)
167          {
168             if(actions[c] != null)
169             {
170                addActionListener(actions[c]);
171             }
172          }
173       }
174       if(cl != null)
175       {
176          this.classes = new String JavaDoc[cl.length];
177          for(int c = 0; c < cl.length; c++)
178          {
179             classes[c] = cl[c].getName();
180          }
181       }
182       else
183       {
184          this.classes = null;
185       }
186       if(query == null && cl != null)
187       {
188          this.query = new ClassQueryExp(cl);
189       }
190       else
191       {
192          if(cl != null)
193          {
194             this.query = new CompositeQueryExp(new QueryExp JavaDoc[]{new ClassQueryExp(cl, ClassQueryExp.OR), query});
195          }
196          else
197          {
198             this.query = query;
199          }
200       }
201       // add ourself as a listener to the NetworkRegistry
202
networkRegistry = NetworkRegistryFinder.find(myserver);
203       if(networkRegistry == null)
204       {
205          throw new Exception JavaDoc("NetworkRegistryMBean not found - MBeanTracker has a dependency on this MBean");
206       }
207
208       foundMBeanServer(new MBeanServerLocator(Identity.get(myserver)));
209
210       if(this.localOnly == false)
211       {
212          // add ourself as a listener for network changes
213
myserver.addNotificationListener(networkRegistry, this, null, null);
214
215          // find any instances we already have registered
216
NetworkInstance instances[] = (NetworkInstance[]) myserver.getAttribute(networkRegistry, "Servers");
217
218          if(instances != null)
219          {
220             for(int c = 0; c < instances.length; c++)
221             {
222                foundMBeanServer(new MBeanServerLocator(instances[c].getIdentity()));
223             }
224          }
225       }
226    }
227
228    /**
229     * add a action listener. this method will automatically call register to your action on
230     * all the mbeans that are contained within it before this method returns.
231     *
232     * @param action
233     */

234    public void addActionListener(MBeanTrackerAction action)
235    {
236       addActionListener(action, true);
237    }
238
239    /**
240     * add a action listener. this method will automatically call register to your action on
241     * all the mbeans that are contained within it before this method returns.
242     *
243     * @param action
244     */

245    public void addActionListener(MBeanTrackerAction action, boolean autoinitialregister)
246    {
247       if(log.isTraceEnabled())
248       {
249          log.debug("adding action: " + action + ", autoinitialregister:" + autoinitialregister);
250       }
251
252       synchronized(actions)
253       {
254          actions.add(action);
255       }
256       if(autoinitialregister)
257       {
258          Set JavaDoc set = getMBeans();
259          Iterator JavaDoc iter = set.iterator();
260          while(iter.hasNext())
261          {
262             MBeanLocator locator = (MBeanLocator) iter.next();
263             fireRegister(locator);
264          }
265       }
266    }
267
268    /**
269     * remove a action listener
270     *
271     * @param action
272     */

273    public void removeActionListener(MBeanTrackerAction action)
274    {
275       if(log.isTraceEnabled())
276       {
277          log.debug("removing action: " + action);
278       }
279
280       Iterator JavaDoc iter = actions();
281       while(iter.hasNext())
282       {
283          MBeanTrackerAction _action = (MBeanTrackerAction) iter.next();
284          if(_action.equals(action))
285          {
286             iter.remove();
287          }
288       }
289    }
290
291    private NotificationFilter JavaDoc createFilterForServer(String JavaDoc id)
292    {
293       NotificationFilter JavaDoc serverfilter = null;
294       NotificationFilter JavaDoc nfilter = new MBeanTrackerFilter(id, classes, wantNotifications);
295       if(filter == null)
296       {
297          serverfilter = nfilter;
298       }
299       else
300       {
301          serverfilter = new CompositeEventFilter(new NotificationFilter JavaDoc[]{nfilter, filter});
302       }
303       return serverfilter;
304    }
305
306    protected void finalize() throws Throwable JavaDoc
307    {
308       destroy();
309       super.finalize();
310    }
311
312    /**
313     * called to stop tracking and clean up internally held resources
314     */

315    public void destroy()
316    {
317       if(log.isTraceEnabled())
318       {
319          log.debug("destroy");
320       }
321       try
322       {
323          myserver.removeNotificationListener(networkRegistry, this);
324       }
325       catch(Throwable JavaDoc ex)
326       {
327       }
328    }
329
330    /**
331     * returns true if no mbeans are found that are being tracked
332     *
333     * @return
334     */

335    public final boolean isEmpty()
336    {
337       return count() <= 0;
338    }
339
340    /**
341     * return the number of mbeans being tracked
342     *
343     * @return
344     */

345    public final int count()
346    {
347       return count.get();
348    }
349
350    /**
351     * return a copy of the internal mbeans being tracked
352     *
353     * @return
354     */

355    public final Set JavaDoc getMBeans()
356    {
357       Set JavaDoc set = new HashSet JavaDoc();
358       synchronized(mbeans)
359       {
360          Iterator JavaDoc iter = mbeans.values().iterator();
361          while(iter.hasNext())
362          {
363             Set JavaDoc beans = (Set JavaDoc) iter.next();
364             set.addAll(beans);
365          }
366       }
367       return set;
368    }
369
370    /**
371     * return an iterator to a copy of the internal mbeans being tracked
372     *
373     * @return
374     */

375    public final Iterator JavaDoc iterator()
376    {
377       return getMBeans().iterator();
378    }
379
380
381    private void tryAddListener(MBeanServerLocator server, ObjectName JavaDoc mbean)
382    {
383       try
384       {
385          if(server.getMBeanServer().isInstanceOf(mbean, NotificationBroadcaster JavaDoc.class.getName()) &&
386             server.getMBeanServer().isInstanceOf(mbean, NetworkRegistryMBean.class.getName()) == false)
387          {
388             server.getMBeanServer().addNotificationListener(mbean, this, createFilterForServer(server.getServerId()), server);
389             if(log.isTraceEnabled())
390             {
391                log.debug("added notification listener to: " + mbean + " on server: " + server);
392             }
393          }
394       }
395       catch(Throwable JavaDoc e)
396       {
397          log.error("Error registering listener for server:" + server + " and mbean:" + mbean, e);
398       }
399    }
400
401    /**
402     * try and remove a listener
403     *
404     * @param server
405     * @param mbean
406     */

407    private void tryRemoveListener(MBeanServerLocator server, ObjectName JavaDoc mbean)
408    {
409       try
410       {
411          if(server.getMBeanServer() == null)
412          {
413             return;
414          }
415          if(server.getMBeanServer().isInstanceOf(mbean, NotificationBroadcaster JavaDoc.class.getName()) &&
416             server.getMBeanServer().isInstanceOf(mbean, NetworkRegistryMBean.class.getName()) == false)
417          {
418             server.getMBeanServer().removeNotificationListener(mbean, this);
419             if(log.isTraceEnabled())
420             {
421                log.debug("removed notification listener to: " + mbean + " on server: " + server);
422             }
423          }
424       }
425       catch(javax.management.InstanceNotFoundException JavaDoc nf)
426       {
427          //this is OK, since it means we're trying to remove a listener from an
428
// unregsitered mbean - which in most cases it is
429
}
430       catch(ConnectionFailedException cnf)
431       {
432          // this is OK
433
}
434       catch(Exception JavaDoc e)
435       {
436          if(e instanceof UndeclaredThrowableException JavaDoc)
437          {
438             UndeclaredThrowableException JavaDoc ut = (UndeclaredThrowableException JavaDoc) e;
439             if(ut.getUndeclaredThrowable() instanceof ReflectionException JavaDoc)
440             {
441                ReflectionException JavaDoc re = (ReflectionException JavaDoc) ut.getUndeclaredThrowable();
442                if(re.getTargetException() instanceof InstanceNotFoundException JavaDoc ||
443                   re.getTargetException() instanceof ConnectionFailedException)
444                {
445                   // these are OK
446
return;
447                }
448             }
449             else if(ut.getUndeclaredThrowable() instanceof MBeanException JavaDoc)
450             {
451                MBeanException JavaDoc mbe = (MBeanException JavaDoc) ut.getUndeclaredThrowable();
452                if(mbe.getTargetException() instanceof ConnectionFailedException)
453                {
454                   // this is OK
455
return;
456                }
457             }
458          }
459          if(e instanceof MBeanException JavaDoc)
460          {
461             MBeanException JavaDoc mbe = (MBeanException JavaDoc) e;
462             if(mbe.getTargetException() instanceof ConnectionFailedException)
463             {
464                // this is OK
465
return;
466             }
467          }
468          log.warn("Error removing listener for server:" + server + " and mbean:" + mbean, e);
469       }
470    }
471
472    /**
473     * called for each notification
474     *
475     * @param notification
476     * @param o
477     */

478    public void handleNotification(Notification JavaDoc notification, Object JavaDoc o)
479    {
480       if(log.isTraceEnabled())
481       {
482          log.debug("tracker received notification=" + notification + " with handback=" + o);
483       }
484       try
485       {
486          if(notification instanceof MBeanServerNotification JavaDoc && JMXUtil.getMBeanServerObjectName().equals(notification.getSource()))
487          {
488             MBeanServerNotification JavaDoc n = (MBeanServerNotification JavaDoc) notification;
489             String JavaDoc type = n.getType();
490             ObjectName JavaDoc mbean = n.getMBeanName();
491             if(type.equals(MBeanServerNotification.REGISTRATION_NOTIFICATION))
492             {
493                addMBean((MBeanServerLocator) o, mbean);
494             }
495             else
496             {
497                // unreg a specific MBean
498
removeMBean((MBeanServerLocator) o, mbean);
499             }
500             return;
501          }
502          else if(notification instanceof NetworkNotification)
503          {
504             NetworkNotification nn = (NetworkNotification) notification;
505             String JavaDoc type = nn.getType();
506             if(type.equals(NetworkNotification.SERVER_ADDED))
507             {
508                // found a server
509
Identity ident = nn.getIdentity();
510                MBeanServerLocator l = new MBeanServerLocator(ident);
511                foundMBeanServer(l);
512             }
513             else if(type.equals(NetworkNotification.SERVER_REMOVED))
514             {
515                // lost a server
516
Identity ident = nn.getIdentity();
517                MBeanServerLocator l = new MBeanServerLocator(ident);
518                lostMBeanServer(l);
519             }
520             return;
521          }
522          else if(notification instanceof AttributeChangeNotification JavaDoc)
523          {
524             AttributeChangeNotification JavaDoc ch = (AttributeChangeNotification JavaDoc) notification;
525             if(ch.getAttributeName().equals("State") && hasActions())
526             {
527                MBeanServerLocator server = (MBeanServerLocator) o;
528                Object JavaDoc src = ch.getSource();
529                if(src instanceof ObjectName JavaDoc)
530                {
531                   ObjectName JavaDoc obj = (ObjectName JavaDoc) src;
532                   // indicate the state changed
533
fireStateChange(new MBeanLocator(server, obj), ((Integer JavaDoc) ch.getOldValue()).intValue(), ((Integer JavaDoc) ch.getNewValue()).intValue());
534                   return;
535                }
536                else if(src instanceof MBeanLocator)
537                {
538                   fireNotification((MBeanLocator) src, notification, o);
539                   return;
540                }
541             }
542          }
543          if(wantNotifications && hasActions())
544          {
545             // fire notification to listener
546
MBeanServerLocator server = (MBeanServerLocator) o;
547             if(server != null)
548             {
549                Object JavaDoc src = notification.getSource();
550                if(src instanceof ObjectName JavaDoc)
551                {
552                   ObjectName JavaDoc obj = (ObjectName JavaDoc) src;
553                   MBeanLocator locator = new MBeanLocator(server, obj);
554                   fireNotification(locator, notification, o);
555                   return;
556                }
557                else if(src instanceof MBeanLocator)
558                {
559                   fireNotification((MBeanLocator) src, notification, o);
560                   return;
561                }
562                else
563                {
564                   log.debug("Unknown source type for notification: " + src);
565                }
566             }
567          }
568       }
569       catch(Exception JavaDoc e)
570       {
571          log.warn("Error encountered receiving notification: " + notification, e);
572       }
573    }
574
575    /**
576     * returns true if we have any actions
577     *
578     * @return
579     */

580    private boolean hasActions()
581    {
582       synchronized(actions)
583       {
584          return actions.isEmpty() == false;
585       }
586    }
587
588    /**
589     * fire a notification to actions
590     *
591     * @param locator
592     * @param n
593     * @param o
594     */

595    protected void fireNotification(MBeanLocator locator, Notification JavaDoc n, Object JavaDoc o)
596    {
597       Iterator JavaDoc iter = actions();
598       while(iter.hasNext())
599       {
600          MBeanTrackerAction action = (MBeanTrackerAction) iter.next();
601          if(wantNotifications && log.isTraceEnabled())
602          {
603             log.debug("forwarding tracker notification: " + n + " to action: " + action + " for tracker: " + this);
604          }
605          action.mbeanNotification(locator, n, o);
606       }
607    }
608
609    /**
610     * fire a state changed event to actions
611     *
612     * @param locator
613     * @param ov
614     * @param nv
615     */

616    protected void fireStateChange(MBeanLocator locator, int ov, int nv)
617    {
618       Iterator JavaDoc iter = actions();
619       while(iter.hasNext())
620       {
621          MBeanTrackerAction action = (MBeanTrackerAction) iter.next();
622          if(wantNotifications && log.isTraceEnabled())
623          {
624             log.debug("forwarding tracker state change: " + nv + " [" + ov + "] to action: " + action + " for tracker: " + this);
625          }
626          action.mbeanStateChanged(locator, ov, nv);
627       }
628    }
629
630    /**
631     * return an Iterator to a unmodifiable Iterator so that you can avoid having to synchronize
632     * on traversing the action list
633     *
634     * @return
635     */

636    private final Iterator JavaDoc actions()
637    {
638       synchronized(actions)
639       {
640          if(actions.isEmpty())
641          {
642             return Collections.EMPTY_LIST.iterator();
643          }
644          return new ArrayList JavaDoc(actions).iterator();
645       }
646    }
647
648    /**
649     * fire unregister event to listeners
650     *
651     * @param locator
652     */

653    protected void fireUnregister(MBeanLocator locator)
654    {
655       int c = 0;
656       Iterator JavaDoc iter = actions();
657       while(iter.hasNext())
658       {
659          MBeanTrackerAction action = (MBeanTrackerAction) iter.next();
660          if(logEvents && log.isTraceEnabled())
661          {
662             log.debug("firing unregister to action [" + (++c) + "] => " + action + " for locator => " + locator);
663          }
664          action.mbeanUnregistered(locator);
665       }
666    }
667
668    /**
669     * fire register event to listeners
670     *
671     * @param locator
672     */

673    protected void fireRegister(MBeanLocator locator)
674    {
675       int c = 0;
676       Iterator JavaDoc iter = actions();
677       while(iter.hasNext())
678       {
679          MBeanTrackerAction action = (MBeanTrackerAction) iter.next();
680          if(logEvents && log.isTraceEnabled())
681          {
682             log.debug("firing register to action [" + (++c) + "] => " + action + " for locator => " + locator);
683          }
684          action.mbeanRegistered(locator);
685       }
686    }
687
688    /**
689     * fired when an MBeanServer is found
690     *
691     * @param theserver
692     */

693    public void foundMBeanServer(MBeanServerLocator theserver)
694    {
695       synchronized(mbeans)
696       {
697          // already found him
698
if(mbeans.containsKey(theserver))
699          {
700             return;
701          }
702          mbeans.put(theserver, new HashSet JavaDoc());
703       }
704
705       for(int c = 0; c < 3; c++)
706       {
707          try
708          {
709             theserver.getMBeanServer().addNotificationListener(JMXUtil.getMBeanServerObjectName(), this, createFilterForServer(theserver.getServerId()), theserver);
710             Set JavaDoc beans = theserver.getMBeanServer().queryMBeans(new ObjectName JavaDoc("*:*"), query);
711             if(beans.isEmpty() == false)
712             {
713                Iterator JavaDoc iter = beans.iterator();
714                while(iter.hasNext())
715                {
716                   addMBean(theserver, ((ObjectInstance JavaDoc) iter.next()).getObjectName());
717                }
718             }
719             else
720             {
721                if(log.isTraceEnabled())
722                {
723                   log.debug("Queried server: " + theserver + ", but found 0 mbeans matching query");
724                }
725             }
726
727             break;
728          }
729          catch(ConnectionFailedException ce)
730          {
731             if(log.isTraceEnabled())
732             {
733                log.debug("while trying to add a listener and get info for: " + theserver + ", i lost it", ce);
734             }
735             if(c >= 3)
736             {
737                if(log.isTraceEnabled())
738                {
739                   log.debug("giving up on connection failed after " + c + " attempts... " + theserver);
740                }
741                // lost mbean server
742
lostMBeanServer(theserver);
743             }
744          }
745          catch(Exception JavaDoc ex)
746          {
747             log.warn("Exception adding mbeans from server: " + theserver, ex);
748          }
749       }
750    }
751
752    /**
753     * add an mbean
754     *
755     * @param server
756     * @param mbean
757     */

758    private void addMBean(MBeanServerLocator server, ObjectName JavaDoc mbean)
759    {
760       if(log.isTraceEnabled())
761       {
762          log.debug("addMBean called: " + server + ", mbean: " + mbean);
763       }
764
765       MBeanLocator locator = new MBeanLocator(server, mbean);
766
767       boolean found = false;
768
769       synchronized(mbeans)
770       {
771          Set JavaDoc set = (Set JavaDoc) mbeans.get(server);
772          if(set != null)
773          {
774             if(set.add(locator))
775             {
776                count.increment();
777                found = true;
778             }
779          }
780
781       }
782
783       if(!found)
784       {
785          return;
786       }
787
788       tryAddListener(server, mbean);
789
790       if(hasActions())
791       {
792          fireRegister(locator);
793       }
794    }
795
796    /**
797     * called to remove an mbean
798     *
799     * @param server
800     * @param mbean
801     */

802    private void removeMBean(MBeanServerLocator server, ObjectName JavaDoc mbean)
803    {
804       if(log.isTraceEnabled())
805       {
806          log.debug("removeMBean called: " + server + ", mbean: " + mbean);
807       }
808
809       MBeanLocator locator = new MBeanLocator(server, mbean);
810
811       synchronized(mbeans)
812       {
813          Set JavaDoc set = (Set JavaDoc) mbeans.get(server);
814          if(set != null)
815          {
816             if(set.remove(locator))
817             {
818                // only if we found the dude
819
count.decrement();
820             }
821             else
822             {
823                // we didn't find him, just return
824
return;
825             }
826          }
827       }
828
829       tryRemoveListener(server, mbean);
830
831       if(hasActions())
832       {
833          fireUnregister(locator);
834       }
835    }
836
837    /**
838     * fired when we lose an MBeanServer
839     *
840     * @param server
841     */

842    public void lostMBeanServer(MBeanServerLocator server)
843    {
844       if(wantNotifications && log.isTraceEnabled())
845       {
846          log.debug("lostMBeanServer: " + server + " for tracker: " + this);
847       }
848
849       Collection JavaDoc list = null;
850
851       synchronized(mbeans)
852       {
853          list = (Set JavaDoc) mbeans.remove(server);
854       }
855       if(list != null)
856       {
857          if(log.isTraceEnabled())
858          {
859             log.debug("lost mbean server = " + server + ", list = " + list);
860          }
861          Iterator JavaDoc iter = list.iterator();
862          while(iter.hasNext())
863          {
864             MBeanLocator locator = (MBeanLocator) iter.next();
865             removeMBean(server, locator.getObjectName());
866          }
867          list.clear();
868          list = null;
869       }
870    }
871
872 }
Popular Tags