Package org.cafesip.jiplet.console.server

Source Code of org.cafesip.jiplet.console.server.AgentConnectionPool

/*
* Created on Dec 19, 2004
*
* Copyright 2005 CafeSip.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.cafesip.jiplet.console.server;

import java.io.IOException;
import java.util.ArrayList;

import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerFactory;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

/**
* @author amit
*/
public class AgentConnectionPool
{
    private String rmiHost;

    private int rmiPort = -1;

    private MBeanServerConnection connection;

    private JMXConnector connector;

    /**
     * Constructor for JBOSS
     * @throws NamingException
     * @throws IOException
     */
    public AgentConnectionPool() throws NamingException, IOException
   
        ArrayList list = MBeanServerFactory.findMBeanServer(null);
        if ((list != null) && (list.size() > 0))
        {
            connection = (MBeanServer) list.get(0);
        }
        else
        {
            InitialContext ic = new InitialContext();           
            RMIAdaptor adaptor  = (RMIAdaptor) ic.lookup("jmx/invoker/RMIAdaptor");       
            connection = adaptor;
        }
    }

    /**
     * Constructor for the MX4J RMI adaptor
     *
     * @param rmiHost
     * @param rmiPort
     * @throws IOException
     */
    public AgentConnectionPool(String rmiHost, int rmiPort) throws IOException
    {
        this.rmiHost = rmiHost;
        this.rmiPort = rmiPort;
        connect();
    }

    private void connect() throws IOException
    {
        //   The address of the connector server
        JMXServiceURL address = new JMXServiceURL("service:jmx:rmi://"
                + rmiHost + "/jndi/rmi://" + rmiHost + ":" + rmiPort
                + "/jipletagent");
        //   Create the JMXCconnectorServer
        connector = JMXConnectorFactory.connect(address, null);

        //  Obtain a "stub" for the remote MBeanServer
        connection = connector.getMBeanServerConnection();
    }

    public MBeanServerConnection getConnection()
    {
        return connection;
    }

    public void returnConnection(MBeanServerConnection connection)
    {
    }
}
TOP

Related Classes of org.cafesip.jiplet.console.server.AgentConnectionPool

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.