FileDocCategorySizeDatePackage
JDBCPoolMonitoringLevelListener.javaAPI DocGlassfish v2 API9760Fri May 04 22:35:18 BST 2007com.sun.enterprise.resource.monitor

JDBCPoolMonitoringLevelListener

public class JDBCPoolMonitoringLevelListener extends Object implements com.sun.enterprise.admin.monitor.registry.MonitoringLevelListener
Provides an implementation of the MonitoringLevelListener interface to receive callbacks from admin regarding change in the monitoring level. Though there are 3 monitoring levels defined by JSR77, we support only 2 levels - OFF and ON (HIGH/LOW). So essentially, HIGH and LOW for us is only ON
author
Aditya Gore
since
s1aspe 8.0

Fields Summary
private com.sun.enterprise.admin.monitor.registry.MonitoringRegistry
registry_
private static final com.sun.enterprise.admin.monitor.registry.MonitoringLevel
OFF
private static final com.sun.enterprise.admin.monitor.registry.MonitoringLevel
HIGH
private static final com.sun.enterprise.admin.monitor.registry.MonitoringLevel
LOW
private static Logger
_logger
Constructors Summary
public JDBCPoolMonitoringLevelListener()


      

    
Methods Summary
public voidchangeLevel(com.sun.enterprise.admin.monitor.registry.MonitoringLevel from, com.sun.enterprise.admin.monitor.registry.MonitoringLevel to, javax.management.j2ee.statistics.Stats handback)
This is the callback method called when the monitoring level changes from HIGH/LOW <-> OFF

Here since the MonitoringLevel is a "type-safe enum", we can directly check equality using ==

param
from - the old level
param
to - the new level
param
handback - the stats implementation object which was registered with the MonitoringRegistry

    
public voidchangeLevel(com.sun.enterprise.admin.monitor.registry.MonitoringLevel from, com.sun.enterprise.admin.monitor.registry.MonitoringLevel to, com.sun.enterprise.admin.monitor.registry.MonitoredObjectType type)

        //If we were called, the type has to be JDBC_CONN_POOL so
	//we can safely ignore the type

	if ( from == to ) {
	    //Its a no-op, so return
	    return;
	}
        AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
	        ServerContext ctxt = ApplicationServer.getServerContext();
		if (ctxt != null) {
                    registry_ = ctxt.getMonitoringRegistry();
		}
                return null;
            }
        });


	if (from == OFF || from == LOW)  {
	    if (to == HIGH) {
		logFine("Changing level from " + from +"  to HIGH");	
                transitionToHigh();
	    }
	}

	if (from == HIGH  || from == LOW ) {
	    if ( to == OFF ) {
                logFine("Switching level from " + from + " to OFF");
                switchOffMonitoring();
            }
	}

    	if (from == OFF || from == HIGH )  {
	    if ( to == LOW  ) {
                logFine("Changing level from " + from + " to LOW");	
                transitionToLow();
	    }
	}
    
private com.sun.enterprise.resource.MonitorableResourcePool[]getPoolList()

        ResourcesUtil resUtil = ResourcesUtil.createInstance();
	JdbcConnectionPool[] jp = resUtil.getJdbcConnectionPools();

	if (jp == null) {
	    return null;
	}
	
        MonitorableResourcePool[] pools = new MonitorableResourcePool[ jp.length ];
        ConcurrentHashMap poolTable = getPoolManager().getPoolTable();
        
	for( int i = 0 ; i < jp.length; i++ ) {
            ResourcePool p = (ResourcePool) poolTable.get( jp[i].getName() );
            if (p != null && (p instanceof MonitorableResourcePool ) ) {
	        pools[i] = (MonitorableResourcePool)p;
            }
	}
	
	return pools;
    
private com.sun.enterprise.PoolManagergetPoolManager()

	return Switch.getSwitch().getPoolManager();
    
private voidlogFine(java.lang.String msg)

        if (_logger.isLoggable( Level.FINE ) &&  msg != null ) {
            _logger.fine( msg );
        }
    
public voidsetLevel(com.sun.enterprise.admin.monitor.registry.MonitoringLevel level)
This is the callback for change in monitoring levels. This shall go off once the admin team removes this method

private voidswitchOffMonitoring()

        //deregister
        AccessController.doPrivileged( new PrivilegedAction() {
            public Object run() {
                MonitorableResourcePool[] pools = getPoolList();
                if (pools == null) {
                    return null;
                }
                for (MonitorableResourcePool pool : pools) {
                    if (pool != null) {
                        try {
                            registry_.unregisterJDBCConnectionPoolStats(
                                    pool.getPoolName());
                            getPoolManager().disableMonitoring(
                                    pool.getPoolName());
                        } catch (Exception mre) {
                            _logger.log(Level.WARNING, "poolmon.cannot_unreg: "
                                    + pool.getPoolName(), mre.getMessage());
                        }
                    }
                }
                return null;
            }
            
        });
    
private voidtransitionToHigh()

        transitionToSpecifiedLevel(MonitoringLevel.HIGH);
    
private voidtransitionToLow()

        transitionToSpecifiedLevel(MonitoringLevel.LOW);   
    
private voidtransitionToSpecifiedLevel(com.sun.enterprise.admin.monitor.registry.MonitoringLevel monitoringLevel)

         AccessController.doPrivileged( new PrivilegedAction() {
            public Object run() {
                //we should create a new pool object everytime since
                //the stats have to be collected afresh
                MonitorableResourcePool[] pools = getPoolList();
                if (pools == null) {
                    return null;
                }

                for (MonitorableResourcePool pool : pools) {
                    if (pool != null) {
                        try {
                            JDBCConnectionPoolStatsImpl stats = new
                                    JDBCConnectionPoolStatsImpl(pool);

                            if(monitoringLevel == MonitoringLevel.HIGH )
                                getPoolManager().setMonitoringEnabledHigh(pool.getPoolName());
                            else if(monitoringLevel == MonitoringLevel.LOW )
                                getPoolManager().setMonitoringEnabledLow(pool.getPoolName());

                            registry_.registerJDBCConnectionPoolStats(
                                    stats, pool.getPoolName(), null);
                        } catch (Exception mre) {
                            try {
                                _logger.log(Level.INFO, "poolmon.cannot_reg: " +
                                        pool.getPoolName(), mre.getMessage());
                                getPoolManager().disableMonitoring(
                                        pool.getPoolName());
                            } catch (Exception ex) {
                                //FIXME: ignore?
                            }
                        }
                    }
                }
                return null;
            }
        });