/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.jvnet.glassfish.comms.clb.core.monitor;
import com.sun.enterprise.admin.monitor.registry.MonitoredObjectType;
import com.sun.enterprise.admin.monitor.registry.MonitoringLevel;
import com.sun.enterprise.admin.monitor.registry.MonitoringRegistrationException;
import com.sun.enterprise.admin.monitor.registry.StatsHolder;
import com.sun.enterprise.admin.monitor.registry.spi.DottedNameFactory;
import com.sun.enterprise.admin.monitor.registry.spi.MonitoringObjectNames;
import com.sun.enterprise.admin.monitor.registry.spi.StatsDescriptionHelper;
import com.sun.enterprise.admin.pluggable.MonitoringManager;
import com.sun.enterprise.server.ApplicationServer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.j2ee.statistics.Stats;
import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
import com.sun.enterprise.config.serverbeans.ElementProperty;
import com.sun.enterprise.config.serverbeans.MonitoringService;
import java.util.ResourceBundle;
import org.glassfish.comms.api.management.monitor.MonitoringManagerStore;
import org.glassfish.comms.api.management.monitor.statistics.CLBBackendStats;
import org.glassfish.comms.api.management.monitor.statistics.CLBFrontendStats;
import org.jvnet.glassfish.comms.util.LogUtil;
/**
*
* @author kshitiz
*/
public class CLBMonitoringManager implements MonitoringManager {
private static final String CLB = "converged-load-balancer";
private static final String CLB_PROPERTY_NAME = CLB;
private static final String BACKEND = "back-end";
private static final String FRONTEND = "front-end";
private static final Logger logger = LogUtil.CLB_LOGGER.getLogger();
private static final MonitoredObjectType clbNode =
MonitoredObjectType.newMonitoredObjectType(CLB, true);
private static final CLBMonitoringManager instance =
new CLBMonitoringManager();
private static final String DEFAULT_PACKAGE =
"org.jvnet.glassfish.comms.clb.core.monitor";
private static final String DEFAULT_FILE = "LocalStrings";
private ResourceBundle resourceBundle;
private StatsHolder rootStatsHolder = null;
private boolean backendStatsEnabled;
private boolean frontendStatsEnabled;
private boolean clbMonitoringEnabled;
private CLBStatsHolder clbStatsHolder;
private CLBMonitoringStatsUpdater clbStatsUpdater;
private boolean isRegistered;
private MonitoringLevel clbMonitoringLevel;
private CLBMonitoringManager() {
backendStatsEnabled = false;
frontendStatsEnabled = false;
clbMonitoringEnabled = false;
isRegistered = false;
clbStatsHolder = new CLBStatsHolder();
resourceBundle =
ResourceBundle.getBundle(DEFAULT_PACKAGE + "." + DEFAULT_FILE);
}
public static CLBMonitoringManager getInstance(){
return instance;
}
public synchronized void register() {
if(isRegistered){
return;
}
MonitoringManagerStore store = MonitoringManagerStore.getInstance();
store.registerMonitoringManager(CLB, this,
new CLBMonitoringLevelListener());
isRegistered = true;
}
public boolean handleModuleMonitoringLevelChange(String arg0, MonitoringLevel arg1) {
//will never be called
throw new UnsupportedOperationException();
}
public void registerAllStats(StatsHolder rootStatsHolder) {
this.rootStatsHolder = rootStatsHolder;
final StatsHolder clbStatsNode = rootStatsHolder.addChild(
CLB, clbNode);
assert (clbStatsNode != null) :
"Initialization Error in registering CLB stats";
clbStatsNode.setObjectName(MonitoringObjectNames.
getMonitoredObjectName(clbNode.getTypeName()));
clbStatsNode.setDottedName(DottedNameFactory.
getDottedName(clbNode.getTypeName()));
clbStatsNode.registerMBean();
StatsDescriptionHelper.getInstance().
addResourceBundle(resourceBundle);
try {
if(backendStatsEnabled){
registerCLBStats(BACKEND, new CLBBackendStatsImpl(),
CLBBackendStats.class);
}
if(frontendStatsEnabled){
registerCLBStats(FRONTEND, new CLBFrontendStatsImpl(),
CLBFrontendStats.class);
}
} catch (MonitoringRegistrationException ex) {
logger.log(Level.SEVERE,
"clb.monitoring_initialization_failed", ex.getMessage());
if(logger.isLoggable(Level.FINE)){
logger.log(Level.FINE, "clb.caught_an_exception", ex);
}
return;
}
clbMonitoringLevel = getCLBMonitoringLevel();
if (clbMonitoringLevel.equals(MonitoringLevel.LOW) ||
clbMonitoringLevel.equals(MonitoringLevel.HIGH)){
logger.log(Level.INFO, "clb.monitoring_turned_on",
new Object[]{clbMonitoringLevel});
createCLBStatsUpdater(clbMonitoringLevel);
clbMonitoringEnabled = true;
registerMBeans();
} else {
logger.log(Level.INFO, "clb.monitoring_turned_off");
}
}
public MonitoringLevel getCLBMonitoringLevel() {
MonitoringLevel monitoringLevel = MonitoringLevel.OFF;
try {
Config cfg = ServerBeansFactory.getConfigBean(ApplicationServer
.getServerContext().getConfigContext());
MonitoringService monService = cfg.getMonitoringService();
ElementProperty monLevel = monService.getModuleMonitoringLevels()
.getElementPropertyByName(CLB_PROPERTY_NAME);
if (monLevel != null) {
String val = monLevel.getValue();
if(val.equals(MonitoringLevel.HIGH.toString())){
monitoringLevel = MonitoringLevel.HIGH;
}else if(val.equals(MonitoringLevel.LOW.toString())){
monitoringLevel = MonitoringLevel.LOW;
}
}
} catch (Exception ex) {
logger.log(Level.WARNING, "clb.unable_to_get_monitoring_level",
ex.getMessage());
if(logger.isLoggable(Level.FINE)){
logger.log(Level.FINE, "clb.caught_an_exception", ex);
}
}
logger.log(Level.INFO, "clb.monitoring_level",
new Object[]{monitoringLevel});
return monitoringLevel;
}
public StatsHolder getCLBMonitoringStatsHolder(){
if(rootStatsHolder == null){
return null;
}
return rootStatsHolder.getChild(CLB);
}
public boolean isCLBMonitoringInitialized() {
StatsHolder clb = getCLBMonitoringStatsHolder();
return clb != null;
}
private void registerMBeans() {
if(backendStatsEnabled){
StatsHolder node = getCLBMonitoringStatsHolder().getChild(BACKEND);
node.registerMBean();
}
if(frontendStatsEnabled){
StatsHolder node = getCLBMonitoringStatsHolder().getChild(FRONTEND);
node.registerMBean();
}
}
public void registerCLBStats(String nodeName, Stats statsImpl,
Class statsInterface) throws MonitoringRegistrationException {
final StatsHolder clbStats = getCLBMonitoringStatsHolder();
MonitoredObjectType moType = MonitoredObjectType.
newMonitoredObjectType(nodeName, false);
final StatsHolder moNode = clbStats.addChild(nodeName, moType);
assert (moNode != null) :
"MRH:registerStats - Initialization failed for " + nodeName;
moNode.setObjectName(MonitoringObjectNames.
getMonitoredObjectName(moType.getTypeName()));
moNode.setDottedName(DottedNameFactory.
getDottedName(moType.getTypeName(), clbStats.getName()));
moNode.setStats(statsImpl);
moNode.setStatsClass(statsInterface);
logger.log(Level.INFO, "clb.created_clb_monitoring_node",
new Object[]{nodeName});
}
private void unregisterMBeans() {
if(backendStatsEnabled){
StatsHolder node = getCLBMonitoringStatsHolder().getChild(BACKEND);
node.unregisterMBean();
}
if(frontendStatsEnabled){
StatsHolder node = getCLBMonitoringStatsHolder().getChild(FRONTEND);
node.unregisterMBean();
}
}
public void setLevel(MonitoringLevel to) {
handleLevelChange(clbMonitoringLevel, to);
}
public void changeLevel(MonitoringLevel from, MonitoringLevel to,
MonitoredObjectType monitoredObjectType) {
if(!monitoredObjectType.equals(clbNode)){
return;
}
handleLevelChange(from, to);
}
public void handleLevelChange(MonitoringLevel from, MonitoringLevel to){
logger.log(Level.INFO, "clb.monitoring_level_changed",
new Object[]{from, to});
if(to.equals(from)){
return;
}
if (to.equals(MonitoringLevel.OFF)) {
clbMonitoringEnabled = false;
unregisterMBeans();
} else {
if (from.equals(MonitoringLevel.OFF)) {
registerMBeans();
clbStatsHolder.resetStatistics();
} else if (from.equals(MonitoringLevel.LOW)) {
clbStatsHolder.resetStatistics();
} else if (from.equals(MonitoringLevel.HIGH)) {
clbStatsHolder.resetHighStatistics();
}
createCLBStatsUpdater(to);
clbMonitoringEnabled = true;
}
clbMonitoringLevel = to;
}
public void enableBackendStats(){
backendStatsEnabled = true;
if(!isCLBMonitoringInitialized()){
return;
}
try {
registerCLBStats(BACKEND, new CLBBackendStatsImpl(),
CLBBackendStats.class);
} catch (MonitoringRegistrationException ex) {
logger.log(Level.SEVERE,
"clb.monitoring_initialization_failed", ex.getMessage());
if(logger.isLoggable(Level.FINE)){
logger.log(Level.FINE, "clb.caught_an_exception", ex);
}
}
if(clbMonitoringEnabled){
registerMBeans();
}
}
public void enableFrontendStats(){
frontendStatsEnabled = true;
if(!isCLBMonitoringInitialized()){
return;
}
try {
registerCLBStats(FRONTEND, new CLBFrontendStatsImpl(),
CLBFrontendStats.class);
} catch (MonitoringRegistrationException ex) {
logger.log(Level.SEVERE,
"clb.monitoring_initialization_failed", ex.getMessage());
if(logger.isLoggable(Level.FINE)){
logger.log(Level.FINE, "clb.caught_an_exception", ex);
}
}
if(clbMonitoringEnabled){
registerMBeans();
}
}
public boolean isCLBMonitoringEnabled(){
return clbMonitoringEnabled;
}
public CLBStatsHolder getCLBStatsHolder(){
return clbStatsHolder;
}
private void createCLBStatsUpdater(MonitoringLevel clbMonitoringLevel) {
if(clbMonitoringLevel.equals(
MonitoringLevel.LOW)){
clbStatsUpdater = new CLBMonitoringStatsUpdaterLow();
}else if(clbMonitoringLevel.equals(
MonitoringLevel.HIGH)){
clbStatsUpdater = new CLBMonitoringStatsUpdaterHigh();
}
}
public CLBMonitoringStatsUpdater getCLBStatsUpdater() {
return clbStatsUpdater;
}
}