FileDocCategorySizeDatePackage
AlertConfigurator.javaAPI DocGlassfish v2 API16335Fri May 04 22:33:16 BST 2007com.sun.enterprise.admin.alert

AlertConfigurator

public class AlertConfigurator extends Object
AlertConfigurator reads the domain.xml entries and configures the NotificationListeners and NotificationFilters and keeps it ready to subscribe (add) to the Target MBeans(Monitors) to recieve alerts.
AUTHOR:
Hemanth Puttaswamy

Fields Summary
private static final String
MBEAN_SERVER_DELEGATE_OBJECT_NAME
private static final String
DEFAULT_FILTER_CLASS_NAME
private static final String
LOG_MBEAN_NAME
private static final AlertConfigurator
instance
Constructors Summary
private AlertConfigurator()

 
Methods Summary
public voidconfigure()
Configure will be called only once to read the domain.xml entries and configure the Notification Listener and Filter. It will also create a list of AlertSubscription objects and creates MBeanServerRegistrationEventListener to listen to RegisterMBean events and add the Notification Listener if required.

        Logger logger = LogDomains.getAlertLogger( );
        if( logger.isLoggable( Level.FINE ) ) {
            logger.log( Level.FINE, "AlertConfigurator.configure called.." );
        }
        AlertService alertService = getAlertService( );
        if( alertService != null ) {
            int count = alertService.sizeAlertSubscription( );
            if( count == 0 ) return;

            List alertSubscriptionList = new ArrayList( );
            for( int i = 0; i < count; i++ ) {
                AlertSubscription subscription = 
                    alertService.getAlertSubscription( i );
                NotificationListener listener = configureNotificationListener( 
                    subscription.getListenerConfig( ) );
                NotificationFilter filter = configureNotificationFilter( 
                    subscription.getFilterConfig( ) );
                alertSubscriptionList.add( new AlertSubscriptionInfo(
                    subscription.getListenerConfig().getSubscribeListenerWith( ),
                        listener, filter )); 
                String monitorNames = subscription.getListenerConfig(
                    ).getSubscribeListenerWith( );
                if( logger.isLoggable( Level.FINE ) ) {
                    logger.log( Level.FINE, 
                        "AlertConfigurator.configure monitorNames.." + 
                             monitorNames );
                }
                StringTokenizer tokenizer = new StringTokenizer( monitorNames,
                    "," );
                // If we are interested in listening to LogMBean event
                // event as well. Subscribe it with an explicit call
                // as LogMBean would've been registered already by now and
                // will never recieve the LogMBean registered notification.
                while( tokenizer.hasMoreTokens( ) ) {
                    String token = tokenizer.nextToken().trim();
                    if( token.equals( LOG_MBEAN_NAME ) ) {
                        LogMBean.getInstance( ).addNotificationListener(
                            listener, filter, null );
                    }
                }
            }
            // Now, we have read all the domain.xml alert subscriptions.
            // We can register a Notification Listener to listen to
            // MBean registered event and then we can introspect the name
            // to see if it matches to add the Listeners capable of
            // emitting alerts.
            MBeanRegistrationEventListener registrationListener =
                 new MBeanRegistrationEventListener( alertSubscriptionList );
            readyForMBeanRegistrationEvent( registrationListener );
        }
    
private javax.management.NotificationFilterconfigureNotificationFilter(com.sun.enterprise.config.serverbeans.FilterConfig filterConfig)
Initializes the NotificationFilter based on FilterConfig in domain.xml .

        String filterClassName = DEFAULT_FILTER_CLASS_NAME;
        ElementProperty[] properties = null;
        if( filterConfig != null ) {
            filterClassName = filterConfig.getFilterClassName();
            properties = filterConfig.getElementProperty();
        }
        NotificationFilter filter = null;
        try {
            filter = (NotificationFilter) instantiateAndConfigure( 
                filterClassName, properties );
        } catch( Exception e ) {
            new ErrorManager().error(
                "Error In Notification Filter Config ", e,
                          ErrorManager.GENERIC_FAILURE );
        }
        return filter;
    
private javax.management.NotificationListenerconfigureNotificationListener(com.sun.enterprise.config.serverbeans.ListenerConfig listenerConfig)
Initializes the NotificationListener based on ListenerConfig in domain.xml .

        Logger alertLogger = LogDomains.getAlertLogger( );
        if( alertLogger.isLoggable( Level.FINE ) ) {
            alertLogger.log( Level.FINE, 
             "ConfigureNotificationListener called with className ..." +
             listenerConfig.getListenerClassName( ) ); 
        }
        NotificationListener listener = null;
        try {
            listener = (NotificationListener) instantiateAndConfigure( 
                listenerConfig.getListenerClassName( ), 
                    listenerConfig.getElementProperty( ) );
        } catch( Exception e ) {    
            new ErrorManager().error(
                "Error In Notification Listener Config ", e,
                          ErrorManager.GENERIC_FAILURE );
        }
        return listener;
    
public static com.sun.enterprise.admin.alert.AlertConfiguratorgetAlertConfigurator()
A Singleton Accessor method.


             
         
        return instance;
    
private com.sun.enterprise.config.serverbeans.AlertServicegetAlertService()
Get AlertService config from domain.xml

        try {
            ServerContext sc = ApplicationServer.getServerContext();
            if( sc == null ) {
                return null;
            }
            return ServerBeansFactory.getConfigBean(
                sc.getConfigContext()).getAlertService( );
        } catch( Exception e ) {
            new ErrorManager().error( "Error In getAlertService  ", e,
                ErrorManager.GENERIC_FAILURE );
        }
        return null;
    
private final java.lang.reflect.Method[]getDeclaredMethods(java.lang.Class clz)
Utility method to get the declared fields.

        return (Method[]) AccessController.doPrivileged(new PrivilegedAction() {            public Object run() {
                return clz.getDeclaredMethods();
            }
        });
    
private java.lang.ObjectgetInstance(java.lang.String className)
A Utility method to get an instance of the class. _REVISIT_: Check to see if there is a utility method to do this. If yes, this method can be taken out.

        if( className == null ) return null;
        return  AccessController.doPrivileged( new PrivilegedAction() {
                public Object run() {
                    try {
                        ClassLoader cl =
                            Thread.currentThread().getContextClassLoader();
                        if (cl == null)
                            cl = ClassLoader.getSystemClassLoader();
                        return Class.forName( className, true, cl).newInstance();                    } catch( Exception e ) {
                        new ErrorManager().error(
                            "Error In Instantiating Class " + className, e,
                            ErrorManager.GENERIC_FAILURE );
                    }
                    return null;
               }
           }
       );
    
private java.lang.ObjectinstantiateAndConfigure(java.lang.String className, com.sun.enterprise.config.serverbeans.ElementProperty[] properties)
A Utility method to instantiate a class and set the properties.

         Logger alertLogger = LogDomains.getAlertLogger();
         if( alertLogger.isLoggable( Level.FINE ) ) {
             alertLogger.log( Level.FINE, 
                 "instantiateAndConfigure called with className.." + 
                 className ); 
         }
         Object o = getInstance( className );
         if( ( o != null )
           &&( properties != null ) )
         {
             if( alertLogger.isLoggable( Level.FINE ) ) {
                 alertLogger.log( Level.FINE, 
                     "instantiateAndConfigure setting Properties.." ); 
             }
             setProperties( o, properties );
         }
         return o;
    
private voidreadyForMBeanRegistrationEvent(javax.management.NotificationListener registrationListener)
Registers the MBeanRegistrationEventListener with MBean Server Delegate.

        try {
            MBeanServer mbeanServer = 
                AdminService.getAdminService().getAdminContext(
                    ).getMBeanServer();
            mbeanServer.addNotificationListener( 
                new ObjectName( MBEAN_SERVER_DELEGATE_OBJECT_NAME ),
                registrationListener, (NotificationFilter) null,(Object) null );
        } catch( Exception e ) {
            new ErrorManager().error(
                "Error In registerning MBeanServerNotificationListener ", e,
                ErrorManager.GENERIC_FAILURE );
        }
    
private voidsetProperties(java.lang.Object o, com.sun.enterprise.config.serverbeans.ElementProperty[] properties)
Utility method to set properties to the instantiated Object.

        if( properties == null ) return;
        Method[] methods = null;
        try { 
            methods = getDeclaredMethods( o.getClass( ) );
            for( int i = 0; i < properties.length; i++ ) {
                ElementProperty property = properties[i];
                String propertyName = property.getName( ).toLowerCase( );
                String propertyValue = property.getValue( );
                for( int j = 0; j < methods.length; j++ ) {
                    String methodName = methods[j].getName().toLowerCase();
                    if ( ( methodName.startsWith( "set" ) )
                       && ( methodName.endsWith( propertyName ) ) )
                    {
                        Class[] parameterTypes = methods[j].getParameterTypes( );
                        if( parameterTypes.length != 1 ) {
                            new ErrorManager().error(
                                "Only one Parameter is allowed for the setter " +
                                " Method: " + methodName + 
                                " has invalid signature", new Exception(), 
                                ErrorManager.GENERIC_FAILURE );
                        }

                        String parameterType = parameterTypes[0].getName(); 
                        Object[] parameters = new Object[1];

                        if( parameterType.equals( "java.lang.String") ) {
                            parameters[0] = propertyValue;
                        } else if( parameterType.equals( "byte" ) ) {
                            parameters[0] = 
                                new Byte( propertyValue.getBytes()[0]);
                        } else if( parameterType.equals( "int" ) ) {
                            parameters[0] = new Integer(propertyValue);
                        } else if( parameterType.equals( "float" ) ) {
                            parameters[0] = new Float(propertyValue);
                        } else if( parameterType.equals( "double") ) {
                            parameters[0] = new Double(propertyValue);
                        } else if( parameterType.equals( "char" ) ) {
                            parameters[0] = 
                                new Character(propertyValue.charAt(0));
                        } else if( parameterType.equals("boolean") ) {
                            parameters[0] = new Boolean(propertyValue);
                        } else if( parameterType.equals("long") ) {
                            parameters[0] = new Long(propertyValue);
                        } else if( parameterType.equals("short") ) {
                            parameters[0] = new Short(propertyValue);
                        } else {
                            new ErrorManager().error(
                                "Only the basic primitive types can be set " +
                                "as properties to NotificationListener and " +
                                " NotificationFilter ", new Exception(), 
                                ErrorManager.GENERIC_FAILURE );
                            continue;
                        }
                        methods[j].invoke( o,  parameters );
                    }
                }
            }
        } catch( Exception e ) {
            new ErrorManager().error(
                "Error While Setting properties to Notification Listener or " +
                " Filter ", e, ErrorManager.GENERIC_FAILURE );
        }