Package org.jvnet.glassfish.comms.admin.monitor.registry.spi.extensions

Source Code of org.jvnet.glassfish.comms.admin.monitor.registry.spi.extensions.SipMonitoringManagerImpl

/*
* 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.admin.monitor.registry.spi.extensions;

import com.ericsson.ssa.container.startup.SipMonitoring;
import com.ericsson.ssa.sip.Layer;
import com.sun.enterprise.admin.common.constant.AdminConstants;
import com.sun.enterprise.admin.monitor.registry.MonitoredObjectType;
import com.sun.enterprise.admin.monitor.registry.MonitoringLevel;
import com.sun.enterprise.admin.monitor.registry.MonitoringLevelListener;
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 com.sun.enterprise.config.ConfigException;
import com.sun.enterprise.config.serverbeans.SipListener;
import java.util.Iterator;
import java.util.ResourceBundle;
import org.jvnet.glassfish.comms.admin.monitor.NetworkManagerStatsImpl;
import org.jvnet.glassfish.comms.admin.monitor.ResponseOutStatsImpl;
import org.jvnet.glassfish.comms.admin.monitor.ResponseInStatsImpl;
import org.jvnet.glassfish.comms.admin.monitor.RequestOutStatsImpl;
import org.jvnet.glassfish.comms.admin.monitor.RequestInStatsImpl;
import org.jvnet.glassfish.comms.admin.monitor.OverloadProtectionManagerStatsImpl;
import org.jvnet.glassfish.comms.admin.monitor.DialogManagerStatsImpl;
import org.jvnet.glassfish.comms.admin.monitor.TransactionManagerStatsImpl;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.j2ee.statistics.Stats;
import javax.management.ObjectName;
import org.jvnet.glassfish.comms.admin.monitor.SipStats;

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.HashMap;
import org.glassfish.comms.api.management.monitor.MonitoringManagerStore;
import org.jvnet.glassfish.comms.util.LogUtil;
/**
* Manager class responsible for registration/unregistration of all the
*  SIP Stats. In addition, this class is responsible for handling the
* monitoring level change events pertaining to SIP monitoring
*/
public class SipMonitoringManagerImpl implements MonitoringManager, MonitoringLevelListener {

    private static final String SIP_SERVICE = "sip-service";
    private static final Logger logger = LogUtil.SIP_LOGGER.getLogger();
    private static final SipMonitoringManagerImpl smm = new SipMonitoringManagerImpl();
    private static final String DEFAULT_PACKAGE = "org.jvnet.glassfish.comms.admin.monitor.registry.spi.extensions";
    private static final String DEFAULT_FILE = "LocalStrings";
    private static final MonitoredObjectType sipNode = MonitoredObjectType.newMonitoredObjectType(SIP_SERVICE, true);
    private StatsHolder rsHolder = null;
    private HashMap<String, Layer> nodes = new HashMap<String, Layer>();
    private ResourceBundle resourceBundle = null;

    /** Creates a new instance of SipMonitoringManagerImpl */
    private SipMonitoringManagerImpl() {       
        resourceBundle = ResourceBundle.getBundle(DEFAULT_PACKAGE + "." + DEFAULT_FILE);
    }

    public static SipMonitoringManagerImpl getInstance() {
        return smm;
    }

    private StatsHolder getRootStatsHolder() {
        return rsHolder;
    }

    private final StatsHolder getSipStatsHolder() {
        if(getRootStatsHolder() == null){
            return null;
        }
        return getRootStatsHolder().getChild(SIP_SERVICE);
    }
   

    public boolean handleModuleMonitoringLevelChange(String arg0, MonitoringLevel arg1) {
        //will never be called
        throw new UnsupportedOperationException();
    }

    /**
     * Method that handles the registration of all the
     * stats for various layers of SIP stats
     */
    public void registerAllStats(StatsHolder rootStatsHolder) {       
        rsHolder = rootStatsHolder;
        initializeSipService();
       
        try {
            StatsDescriptionHelper.getInstance().addResourceBundle(resourceBundle);
            registerSipStats(SipMonitoring.SESSION_MANAGER, new DialogManagerStatsImpl());
            registerSipStats(SipMonitoring.NETWORK_MANAGER, new NetworkManagerStatsImpl());
            registerSipStats(SipMonitoring.TRANSACTION_MANAGER, new TransactionManagerStatsImpl());
            registerSipStats(SipMonitoring.OVERLOAD_PROTECTION_MANAGER, new OverloadProtectionManagerStatsImpl());
            registerSipStats(SipMonitoring.NETWORK_MANAGER, new ResponseOutStatsImpl(), "response-sent", false);
            registerSipStats(SipMonitoring.NETWORK_MANAGER, new ResponseInStatsImpl(), "response-received", false);
            registerSipStats(SipMonitoring.NETWORK_MANAGER, new RequestOutStatsImpl(), "request-sent", false);
            registerSipStats(SipMonitoring.NETWORK_MANAGER, new RequestInStatsImpl(), "request-received", false);
            enableSipMonitoring();
            if (SipMonitoring.isEnabled(SIP_SERVICE)){
                registerMBeans();
            }
        } catch (MonitoringRegistrationException ex) {
            Logger.getLogger(SipMonitoringManagerImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void enableSipMonitoring() {
       
        try {
            SipListener[] sipListeners = ServerBeansFactory.getConfigBean(ApplicationServer.getServerContext().getConfigContext()).getSipService().getSipListener();
            for (SipListener sipListener : sipListeners) {
                enableSipListenerMonitoring(sipListener.getId(), false);
            }
        } catch(ConfigException ce) {
            Logger.getLogger(SipMonitoringManagerImpl.class.getName()).log(Level.WARNING, "SipServiceBeanNotFound ", ce);
        }
    }

   //To unregister the monitoring mbean for the given sip-listener id.
    public void disableSipListenerMonitoring(String id) {
        Iterator it = getSipStatsHolder().getAllChildren().iterator();
        while (it.hasNext()) {
            StatsHolder c = (StatsHolder) it.next();
            ObjectName on = c.getObjectName();
            String str = on.toString();
            if( str.contains(id)) {
                c.unregisterMBean();
            }
        }
    }

    public void enableSipListenerMonitoring(String id) {
        enableSipListenerMonitoring(id, true);
    }

    public void enableSipListenerMonitoring(String id, boolean register) {
        NetworkManagerStatsImpl sipStats = new NetworkManagerStatsImpl(id);
        registerSipStats(SipMonitoring.NETWORK_MANAGER, sipStats, id, register);
    }
   
    public void registerSipStats(String nodeName, SipStats statsImpl, String id, boolean register) {
        if (statsImpl == null) {
            throw new IllegalArgumentException("registerStats  - null stats provided");
        }

        /*For SipListener : newMonitoredObjectType is created based on the listener-id and
        it is added as a child under sip-service(getSipStatsHolder.addChild(..)) inorder
        to enable or disable monitoring based on the monitoring level. This monitoring level
        check if changed dynamically is done in MonitoringConfigChangeListener class ,
        else with in the same class by shouldRegisterMBeans() method.*/

        MonitoredObjectType moType = MonitoredObjectType.newMonitoredObjectType(id, false);
        StatsHolder sips = getSipStatsHolder();
        final StatsHolder moNode = sips.addChild(id, moType);
        moNode.setObjectName(MonitoringObjectNames.getMonitoredObjectName(nodeName, sips.getName(), id));
        moNode.setDottedName(DottedNameFactory.getDottedName(nodeName, sips.getName(), id));
        moNode.setStats(statsImpl);
        moNode.setStatsClass(statsImpl.getInterfaceClass());
        if (register && SipMonitoring.isEnabled(SIP_SERVICE)){
            moNode.registerMBean();
        }
    }

    public void initializeSipService() {
        final StatsHolder ssNode = rsHolder.addChild(SIP_SERVICE, sipNode);
        assert (ssNode != null) : "Initialization Error in SipServiceCreation";
        ssNode.setObjectName(MonitoringObjectNames.getMonitoredObjectName(sipNode.getTypeName()));
        ssNode.setDottedName(DottedNameFactory.getDottedName(sipNode.getTypeName()));
        ssNode.registerMBean();
    }

    public boolean isSipServiceInitialized() {
        StatsHolder sips = getSipStatsHolder();
        return sips != null;
    }   

    public void registerSipStats(String nodeName, SipStats statsImpl) throws MonitoringRegistrationException {

        if (statsImpl == null) {
            throw new IllegalArgumentException("registerStats  - null stats provided");
        }
        final StatsHolder sips = getSipStatsHolder();
        MonitoredObjectType moType = MonitoredObjectType.newMonitoredObjectType(nodeName, false);
        final StatsHolder moNode = sips.addChild(nodeName, moType);
        assert (moNode != null) : "MRH:registerStats - Initialization failed for " + nodeName;
        moNode.setObjectName(MonitoringObjectNames.getMonitoredObjectName(moType.getTypeName()));
        moNode.setDottedName(DottedNameFactory.getDottedName(moType.getTypeName(), sips.getName()));
        moNode.setStats(statsImpl);
        moNode.setStatsClass(statsImpl.getInterfaceClass());
        if (logger.isLoggable(Level.FINE)) {
            logger.fine(nodeName + " stats is registered");
        }
        Layer provider = nodes.get(nodeName);
        if(provider != null){
            ((SipStats) (moNode.getStats())).setStatsProvider(provider);
        }
    }

    public void enableSipStats(String nodeName, Layer provider, MonitoringLevel level) {
        if(isSipServiceInitialized()){
            final StatsHolder node = getSipStatsHolder().getChild(nodeName);
            ((SipStats) (node.getStats())).setStatsProvider(provider);
        }
        addNode(nodeName, provider);
        if (logger.isLoggable(Level.FINE)) {
            logger.info(nodeName + " stats is connected to " + provider.getClass().getName());
        }
    }

    public void addNode(String node, Layer provider) {
        nodes.put(node, provider);
    }

    public void register() {
        MonitoringManagerStore store = MonitoringManagerStore.getInstance();
        store.registerMonitoringManager(SIP_SERVICE, this,
                this);
    }

    private void unregisterMBeans() {
        Iterator it = getSipStatsHolder().getAllChildren().iterator();
        while (it.hasNext()) {
            StatsHolder c = (StatsHolder) it.next();
            c.unregisterMBean();
        }
    }

    private void registerMBeans() {
        Iterator it = getSipStatsHolder().getAllChildren().iterator();
        while (it.hasNext()) {
            StatsHolder c = (StatsHolder) it.next();
            c.registerMBean();
        }
    }

    public void setLevel(MonitoringLevel to) {
        handleLevelChange(SipMonitoring.getLevel(SIP_SERVICE), to);
    }

    public void changeLevel(MonitoringLevel from, MonitoringLevel to, Stats stats) {
        handleLevelChange(from, to);
    }
   
    public void changeLevel(MonitoringLevel from, MonitoringLevel to, MonitoredObjectType type) {
        handleLevelChange(from, to);
    }
   
    public void handleLevelChange(MonitoringLevel from, MonitoringLevel to){
        logger.log(Level.INFO, "sip.monitoring_level_changed",
                new Object[]{from, to});
        if (to.equals(from)) {
            return;
        }
        if (to.equals(MonitoringLevel.OFF)) {
            //unregisterMBean if registered
            unregisterMBeans();
        } else {
            //registerMBean if unregistered
            if (from.equals(MonitoringLevel.OFF)) {
                registerMBeans();               
            }
        }
        SipMonitoring.setLevel(SIP_SERVICE, to);           
    }
}
TOP

Related Classes of org.jvnet.glassfish.comms.admin.monitor.registry.spi.extensions.SipMonitoringManagerImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.