// Home | Go Back //

/*
 * Copyright (c) 2002-2003 iReasoning Inc. All Rights Reserved.
 * 
 * This SOURCE CODE FILE, which has been provided by iReasoning Inc. as part
 * of an iReasoning Software product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of iReasoning Inc.  
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD IREASONING SOFTWARE, ITS
 * RELATED COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY
 * CLAIMS OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR
 * DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES
 * ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR
 * DISTRIBUTION OF PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR
 * DERIVED FROM THIS SOURCE CODE FILE.
 */

package agent.mib2;
import com.ireasoning.core.jmx.MBeanManager;
import java.io.*;
import java.util.*;
import com.ireasoning.util.*;
import com.ireasoning.protocol.snmp.*;
import javax.management.*;


/**
 * Class for registering MBeans
 */
public class AgentMib
{
    private static MBeanManager _mgr = MBeanManager.getInstance();
    // Root node of OID tree
    private static OIDTreeNode _root;
    
    /**
     * Registers all necessary MBeans
     */
    public static void registerMBeans(MBeanServer server, OIDTreeNode root)
    {
        _root = root;
        try
        {
            registerSnmpGroup();
            registerSystemGroup();
            registerAtTable();
            registerTcpConnTable();
        }
        catch(Exception e)
        {
            Logger.error(e);
            throw new SnmpException(e.toString());
        }
    }
    
    /**
     * Unregisters MBeans
     */
    public static void unregisterMBeans()
    {
        try
        {
            unregisterSnmpGroup();
            unregisterSystemGroup();
            unregisterAtTable();
            unregisterTcpConnTable();
        }
        catch(Exception e)
        {
            Logger.error(e);
            throw new SnmpException(e.toString());
        }
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerSnmpGroup() throws Exception
    {
        if ( _snmpGroup == null )
        {
            _snmpGroup = new SnmpGroup(_root, ".1.3.6.1.2.1.11", null);
        }
        ObjectName objName = new ObjectName("ireasoning:name=SnmpGroup,noPdu=true");
        if(!_mgr.isRegistered(objName))
        {
            _mgr.registerMBean(_snmpGroup, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterSnmpGroup() throws Exception
    {
        _mgr.unregisterMBean(new ObjectName("ireasoning:name=SnmpGroup,noPdu=true"));
        _snmpGroup = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerSystemGroup() throws Exception
    {
        if ( _systemGroup == null )
        {
            _systemGroup = new SystemGroup(_root, ".1.3.6.1.2.1.1", null);
        }
        ObjectName objName = new ObjectName("ireasoning:name=SystemGroup,noPdu=true");
        if(!_mgr.isRegistered(objName))
        {
            _mgr.registerMBean(_systemGroup, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterSystemGroup() throws Exception
    {
        _mgr.unregisterMBean(new ObjectName("ireasoning:name=SystemGroup,noPdu=true"));
        _systemGroup = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerAtTable() throws Exception
    {
        if ( _atTable == null )
        {
            _atTable = new AtTable(_root, ".1.3.6.1.2.1.3.1", null);
        }
        ObjectName objName = new ObjectName("ireasoning:name=AtTable,noPdu=true");
        if(!_mgr.isRegistered(objName))
        {
            _mgr.registerMBean(_atTable, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterAtTable() throws Exception
    {
        _mgr.unregisterMBean(new ObjectName("ireasoning:name=AtTable,noPdu=true"));
        _atTable = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerTcpConnTable() throws Exception
    {
        if ( _tcpConnTable == null )
        {
            _tcpConnTable = new TcpConnTable(_root, ".1.3.6.1.2.1.6.13", null);
        }
        ObjectName objName = new ObjectName("ireasoning:name=TcpConnTable,noPdu=true");
        if(!_mgr.isRegistered(objName))
        {
            _mgr.registerMBean(_tcpConnTable, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterTcpConnTable() throws Exception
    {
        _mgr.unregisterMBean(new ObjectName("ireasoning:name=TcpConnTable,noPdu=true"));
        _tcpConnTable = null;
    }
    
    /**
     * Returns SnmpGroup object. If SnmpGroup is null, it 'll register the corresponding MBean first
     */
    public static SnmpGroup getSnmpGroup( )
    {
        if( _snmpGroup == null )
        {
            try
            {
                registerSnmpGroup();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _snmpGroup;
    }
    
    /**
     * Returns SystemGroup object. If SystemGroup is null, it 'll register the corresponding MBean first
     */
    public static SystemGroup getSystemGroup( )
    {
        if( _systemGroup == null )
        {
            try
            {
                registerSystemGroup();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _systemGroup;
    }
    
    /**
     * Returns AtTable object. If AtTable is null, it 'll register the corresponding MBean first
     */
    public static AtTable getAtTable( )
    {
        if( _atTable == null )
        {
            try
            {
                registerAtTable();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _atTable;
    }
    
    /**
     * Returns TcpConnTable object. If TcpConnTable is null, it 'll register the corresponding MBean first
     */
    public static TcpConnTable getTcpConnTable( )
    {
        if( _tcpConnTable == null )
        {
            try
            {
                registerTcpConnTable();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _tcpConnTable;
    }
    
    
    static SnmpGroup _snmpGroup;
    static SystemGroup _systemGroup;
    static AtTable _atTable;
    static TcpConnTable _tcpConnTable;
}