/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.remoting.transport.rmi;

import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.RemoteStub;
import java.rmi.server.UnicastRemoteObject;

import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.ServerInvoker;

/**
 * RMIServerInvoker
 * 
 * @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
 * @version $Revision: 1.3.2.1 $
 */
public class RMIServerInvoker extends ServerInvoker implements RMIServerInvokerInf
{
    private final RemoteStub stub;

    public RMIServerInvoker (InvokerLocator locator)
        throws RemoteException
    {
        super(locator);
        if (locator.getPort()!=-1)
        {
           stub = (RemoteStub)UnicastRemoteObject.exportObject(this,locator.getPort());
        }
        else
        {
            stub = UnicastRemoteObject.exportObject(this);
        }
    }
    /**
     * destroy the RMI Server Invoker, which will unexport the RMI server
     */
    public void destroy ()
    {
        try
        {
            UnicastRemoteObject.unexportObject(this,true);
        }
        catch (java.rmi.NoSuchObjectException e)
        {

        }
    }
    protected void finalize () throws Throwable
    {
        destroy();
        super.finalize();
    }

    /**
     * returns true if the transport is bi-directional in nature, for example,
     * SOAP in unidirectional and SOCKETs are bi-directional (unless behind a firewall
     * for example).
     *
     * @return
     */
    public boolean isTransportBiDirectional ()
    {
        return true;
    }

    public final RemoteStub getStub ()
    {
        return stub;
    }

    public byte[] transport (byte buffer[])
            throws RemoteException, IOException
    {
        return invoke(buffer);
    }
}
