View Javadoc

1   package org.apache.helix.monitoring.mbeans;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.lang.management.ManagementFactory;
24  
25  import javax.management.AttributeNotFoundException;
26  import javax.management.InstanceNotFoundException;
27  import javax.management.IntrospectionException;
28  import javax.management.ListenerNotFoundException;
29  import javax.management.MBeanAttributeInfo;
30  import javax.management.MBeanException;
31  import javax.management.MBeanInfo;
32  import javax.management.MBeanServerConnection;
33  import javax.management.MBeanServerDelegate;
34  import javax.management.MBeanServerNotification;
35  import javax.management.MalformedObjectNameException;
36  import javax.management.Notification;
37  import javax.management.NotificationListener;
38  import javax.management.ReflectionException;
39  import javax.management.relation.MBeanServerNotificationFilter;
40  
41  import org.apache.log4j.Logger;
42  
43  /*
44   * TODO: this class should be in espresso common, as the only usage of it is
45   * to create ingraph adaptors
46   * **/
47  public abstract class ClusterMBeanObserver implements NotificationListener
48  {
49    protected final String _domain;
50    protected MBeanServerConnection _server;
51    private static final Logger _logger = Logger.getLogger(ClusterMBeanObserver.class);
52        
53    public ClusterMBeanObserver(String domain) 
54        throws InstanceNotFoundException, IOException, MalformedObjectNameException, NullPointerException
55    {
56      // Get a reference to the target MBeanServer
57      _domain = domain;
58      _server = ManagementFactory.getPlatformMBeanServer();
59      MBeanServerNotificationFilter filter = new MBeanServerNotificationFilter();
60      filter.enableAllObjectNames();
61      _server.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, this, filter, null);
62    }
63    
64    public void handleNotification(Notification notification, Object handback)
65    {
66      MBeanServerNotification mbs = (MBeanServerNotification) notification;
67      if(MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(mbs.getType())) 
68      {
69        if(mbs.getMBeanName().getDomain().equalsIgnoreCase(_domain))
70        {
71          _logger.info("MBean Registered, name :" + mbs.getMBeanName());
72          onMBeanRegistered(_server, mbs);
73        } 
74      }
75      else if(MBeanServerNotification.UNREGISTRATION_NOTIFICATION.equals(mbs.getType())) 
76      {
77        if(mbs.getMBeanName().getDomain().equalsIgnoreCase(_domain))
78        {
79          _logger.info("MBean Unregistered, name :" + mbs.getMBeanName());
80          onMBeanUnRegistered(_server, mbs);
81        }
82      }
83    }
84    
85    public void disconnect()
86    {
87      MBeanServerNotificationFilter filter = new MBeanServerNotificationFilter();
88      try
89      {
90        _server.removeNotificationListener(MBeanServerDelegate.DELEGATE_NAME, this);
91      }
92      catch (Exception e)
93      {
94        _logger.error("", e);
95      }
96    }
97    
98    public abstract void onMBeanRegistered(MBeanServerConnection server, MBeanServerNotification mbsNotification);
99    
100   public abstract void onMBeanUnRegistered(MBeanServerConnection server, MBeanServerNotification mbsNotification);  
101   
102 }