/***************************************
 *                                     *
 *  JBoss: The OpenSource J2EE WebOS   *
 *                                     *
 *  Distributable under LGPL license.  *
 *  See terms of license at gnu.org.   *
 *                                     *
 ***************************************/
package org.jboss.mx.remoting;

import java.io.Serializable;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Iterator;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;

import org.jboss.remoting.ident.Identity;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.ConnectionFailedException;
import org.jboss.remoting.network.NetworkRegistry;
import org.jboss.remoting.network.NetworkInstance;
import org.jboss.remoting.network.filter.IdentityFilter;
import org.jboss.mx.remoting.JMXUtil;

/**
 * MBeanServerLocator is an object that is used to identify and locate
 * an MBeanServer on the network via JMX Remoting.  <P>
 *
 * The MBeanServerLocator can be serialized and passed across the network,
 * as long as the target server has access back to the MBeanServer via
 * JMX Remoting Connector and has been detected by a JMX Remoting Detector. <P>
 *
 * @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
 * @version $Revision: 1.1.8.1 $
 */
public class MBeanServerLocator implements Serializable
{
    static final long serialVersionUID = 7632696197699845344L;

    private final Identity identity;
    private boolean autoLocate=true;
    protected transient MBeanServer server;

    public MBeanServerLocator (Identity identity)
    {
        this.identity = identity;
    }
    public int hashCode ()
    {
        return identity.hashCode();
    }
    public boolean equals (Object obj)
    {
        if (obj instanceof MBeanServerLocator)
        {
            return identity.equals(((MBeanServerLocator)obj).identity);
        }
        return false;
    }
    public String toString ()
    {
        return "MBeanServerLocator ["+identity.getJMXId()+"]";
    }
    /**
     * return the MBeanServer ID
     *
     * @return
     */
    public String getServerId ()
    {
        return identity.getJMXId();
    }
    /**
     * return the identity of the server
     *
     * @return
     */
    public final Identity getIdentity ()
    {
        return identity;
    }
    /**
     * return the MBeanServer InstanceID
     *
     * @return
     */
    public String getInstanceId ()
    {
        return identity.getInstanceId();
    }
    /**
     * return the InetAddress for the MBeanServer
     *
     * @return
     */
    public InetAddress getAddress ()
    {
        return identity.getAddress();
    }
    /**
     * return a proxy to the MBeanServer
     *
     * @return
     */
    public MBeanServer getMBeanServer ()
    {
        // we use a weak reference so that if the MBeanServer is available to
        // be garbage collected, we shouldn't stand in the way ... and hold a strong
        // reference to it
        if (server==null)
        {
            // try to get a reference
            server = resolveServer();
        }

        if (server==null)
        {
            // if the reference is still null, return null
            throw new ConnectionFailedException("Couldn't find server at: "+identity);
        }
        else
        {
            return server;
        }
    }
    /**
     * try and resolve the serverid to a MBeanServer instance or proxy to a
     * remote server
     *
     * @return
     */
    protected MBeanServer resolveServer () throws ConnectionFailedException
    {
        Object server = null;
        String id = System.getProperty("jboss.identity");
        if (id!=null && id.equals(identity.getInstanceId()))
        {
            // it's local
            ArrayList list=MBeanServerFactory.findMBeanServer(null);
            if (list.isEmpty()==false)
            {
                Iterator iter = list.iterator();
                while(iter.hasNext())
                {
                    MBeanServer s = (MBeanServer)iter.next();
                    try
                    {
                        if (JMXUtil.getServerId(s).equals(identity.getJMXId()))
                        {
                            return s;
                        }
                    }
                    catch (Exception ex) {}
                }
            }
        }
        // try and get from registry
        server = MBeanServerRegistry.getMBeanServerFor(getServerId());
        if (server==null)
        {
            // not in registry, query for the network instance and try and create
            NetworkInstance ni[]=NetworkRegistry.getInstance().queryServers(new IdentityFilter(identity));
            if (ni!=null && ni.length>0)
            {
                InvokerLocator locators[]=ni[0].getLocators();
                String jmxId = ni[0].getIdentity().getJMXId();
                ArrayList list = MBeanServerFactory.findMBeanServer(jmxId);
                if (list!=null && !list.isEmpty())
                {
                    return (MBeanServer)list.get(0);
                }
                try
                {
                    server = MBeanTransportPreference.getServerByTransport(identity,locators);
                }
                catch ( Exception e )
                {
                    e.printStackTrace();
                }
            }
        }
        return (server==null) ? null : (MBeanServer) server;
    }

    /**
     * resolve the server object automagically on deserialization
     *
     * @param stream
     * @throws java.io.IOException
     * @throws java.lang.ClassNotFoundException
     */
    private void readObject(java.io.ObjectInputStream stream)
         throws IOException, ClassNotFoundException
    {
        stream.defaultReadObject();
        if (autoLocate)
        {
            resolveServer();
        }
    }

    /**
     * set true (default) to automatically locate the appropriate MBeanServer on deserialization
     * or false to only locate on demand to the call to <tt>getMBeanServer</tt>.
     *
     * @param autoLocate
     */
    public void setAutoLocate (boolean autoLocate)
    {
        this.autoLocate = autoLocate;
    }
}