// 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 java.util.*;
import java.io.*;
import java.net.*;
import javax.management.*;
import com.ireasoning.util.*;
import com.ireasoning.protocol.snmp.*;
import mx4j.adaptor.http.HttpAdaptor;
import mx4j.adaptor.rmi.jrmp.*;
import mx4j.util.*;
import javax.naming.Context;

/**
 * A Http adaptor and JRMP server for remotely managing agent.
 * You can use web browser pointing to http://localhost:8000
 * to view MBeans information. Or use MC4J to connect to JRMP server
 */
public class AgentMX4J
{
    /**
     * Starts a http adaptor at port 8000
     */
    static void startHttpAdaptor(MBeanServer server) throws Exception
    {
        HttpAdaptor adapter = new HttpAdaptor();
        ObjectName name = new ObjectName("Server:name=HttpAdaptor");
        server.registerMBean(adapter, name);
        adapter.setPort(8000);//use port 8000
        adapter.setHost("localhost");
        adapter.start();
    }
    
    public static void main( String[] args )
    {
        try
        {
            MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();

            startHttpAdaptor(mbeanServer);
            // startJRMPAdaptor(mbeanServer);
            // use default agent port 161
            int port = 161;
            String configFile = "SnmpAgent.xml";
            Agent agent = new Agent(mbeanServer, port, configFile);
            agent.start();
        }
        catch(Exception e)
        {
            Logger.error(e);
        }
    }

    // /**
    //  * Starts a JRMP server
    //  */
    // static void startJRMPAdaptor(MBeanServer server) throws Exception
    // {
    //     ObjectName naming = new ObjectName("Naming:type=rmiregistry");
    //     server.createMBean("mx4j.tools.naming.NamingService", naming, null);
    //     server.invoke(naming, "start", null, null);
    //
    //     // Create the JRMP adaptor
    //     ObjectName adaptor = new ObjectName("Adaptor:protocol=JRMP");
    //     server.createMBean("mx4j.adaptor.rmi.jrmp.JRMPAdaptor", adaptor, null);
    //     JRMPAdaptorMBean mbean = (JRMPAdaptorMBean)StandardMBeanProxy.create(JRMPAdaptorMBean.class, server, adaptor);
    //
    //     // Set the JNDI name with which will be registered
    //     String jndiName = "jrmp";
    //     mbean.setJNDIName(jndiName);
    //
    //     // Optionally, you can specify the JNDI properties,
    //     // instead of having in the classpath a jndi.properties file
    //     mbean.putJNDIProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
    //     mbean.putJNDIProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
    //
    //     // Registers the JRMP adaptor in JNDI and starts it
    //     mbean.start();
    // }
}