Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / C#

NetMX - a JMX port to the .NET world

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
10 Mar 2008LGPL34 min read 39.5K   268   11  
An introduction to NetMX - a lightweight .NET management solution.
using System;
using System.Collections.Generic;
using System.Text;

namespace NetMX.OpenMBean.Mapper
{
   public class OpenMBeanMapperService : OpenMBeanMapperServiceMBean, IMBeanRegistration, INotificationListener
   {
      #region Members
      private IMBeanServer _server;
      private ObjectName _ownName;
      private ObjectName[] _beansToMapPatterns;
		private string _proxyIndicatorProperty = "OpenMBeanProxy";


      private readonly OpenTypeCache _typeCache = new OpenTypeCache();
      private readonly Dictionary<ObjectName, int> _externalMappers = new Dictionary<ObjectName, int>();
      private readonly Dictionary<ObjectName, ObjectName> _mappedBeans = new Dictionary<ObjectName, ObjectName>();
      #endregion

      #region Constructors
      public OpenMBeanMapperService()
      {
         _typeCache.AddTypeMapper(new PlainNetTypeMapper(), null, int.MaxValue);
         _typeCache.AddTypeMapper(new SimpleTypeMapper(), null, int.MaxValue - 1);
         _typeCache.AddTypeMapper(new CollectionTypeMapper(), null, int.MaxValue - 2);         
      }
      public OpenMBeanMapperService(ObjectName[] beansToMapPatterns)
         : this()
      {
         _beansToMapPatterns = beansToMapPatterns;
      }
      #endregion

      #region Utility
      private void UnregisterAllProxies()
      {
         foreach (ObjectName name in _mappedBeans.Values)
         {
            _server.UnregisterMBean(name);
         }
      }
      private void MapBean(ObjectName originalBeanName)
      {
         Dictionary<string, string> props = new Dictionary<string, string>(originalBeanName.KeyPropertyList);
         props.Add(_proxyIndicatorProperty, "true");
         ObjectName proxyName = new ObjectName(originalBeanName.Domain, props);

         MBeanInfo originalInfo = _server.GetMBeanInfo(originalBeanName);
         ProxyBean proxyBean = new ProxyBean(originalInfo, originalBeanName, _typeCache);
         _server.RegisterMBean(proxyBean, proxyName);
      }
      private bool ShouldMapBean(ObjectName newBeanName)
      {
         if (_beansToMapPatterns != null)
         {
            foreach (ObjectName name in _beansToMapPatterns)
            {
               if (name.Apply(newBeanName))
               {
                  return true;
               }
            }
         }
         return false;
      }
      #endregion
      
      #region IMBeanRegistration Members
      public void PostDeregister()
      {         
      }
      public void PostRegister(bool registrationDone)
      {
         _server.AddNotificationListener(MBeanServerDelegate.ObjectName, _ownName, null, null);
      }
      public void PreDeregister()
      {
         _server.RemoveNotificationListener(MBeanServerDelegate.ObjectName, _ownName);
      }
      public ObjectName PreRegister(IMBeanServer server, ObjectName name)
      {
         _ownName = name;
         _server = server;
         return name;
      }
      #endregion      

      #region INotificationListener Members
      public void HandleNotification(Notification notification, object handback)
      {
         MBeanServerNotification serverNotification = notification as MBeanServerNotification;
         if (serverNotification != null)            
         {
            if (serverNotification.Type == MBeanServerNotification.RegistrationNotification)
            {
               if (ShouldMapBean(serverNotification.ObjectName))
               {
						MapBean(serverNotification.ObjectName);
               }
            }
            else if (serverNotification.Type == MBeanServerNotification.UnregistrationNotification)
            {
               if (_externalMappers.ContainsKey(serverNotification.ObjectName))
               {
                  _typeCache.RemoveTypeMapper(_externalMappers[serverNotification.ObjectName]);
                  _externalMappers.Remove(serverNotification.ObjectName);
               }
               else if (_mappedBeans.ContainsKey(serverNotification.ObjectName))
               {
                  _server.UnregisterMBean(_mappedBeans[serverNotification.ObjectName]);
                  _mappedBeans.Remove(serverNotification.ObjectName);
               }
            }
         }
      }		
      #endregion

      #region OpenMBeanMapperServiceMBean Members
      public ObjectName[] BeansToMapPatterns
      {
         get
         {
            return _beansToMapPatterns;
         }
         set
         {            
            _beansToMapPatterns = value;
         }
      }
      public void RefreshMappings()
      {
         UnregisterAllProxies();
         if (_beansToMapPatterns != null)
         {
            foreach (ObjectName pattern in _beansToMapPatterns)
            {
               foreach (ObjectName name in _server.QueryNames(pattern, null))
               {
                  MapBean(name);
               }
            }
         }
      }
      public void FlushMappedTypeCache()
      {
         _typeCache.FlushCache();
      }
      #endregion
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) VSoft
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions