/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.jms.connector.jrmp;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.jms.Connection;
import javax.jms.ConnectionMetaData;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.TemporaryQueue;
import javax.jms.TemporaryTopic;
import javax.jms.Topic;

import org.jfox.jms.JMSConnectionRemote;
import org.jfox.jms.connector.AbstractConnectorService;
import org.jfox.jms.connector.ConnectorHelper;
import org.jfox.jms.connector.Protocol;
import org.jfox.jndi.InitialContextHelper;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class JRMPConnectorService extends AbstractConnectorService {
    private Remote stub = null;
    private Protocol protocol = Protocol.JRMP;
    // 0 is auto set port
    private int port = 0;

    // --- from javax.jms.ConnectionFactory
    public Connection createConnection() throws RemoteException, JMSException {
        throw new JMSException("not support now!");
    }

    public Connection createConnection(String userName, String password) throws RemoteException, JMSException {
        throw new JMSException("not support now!");
    }

    // --- from javax.jms.Session
    public Queue createQueue(String queueName) throws RemoteException, JMSException {
        throw new JMSException("not support now!");
    }

    public Topic createTopic(String topicName) throws RemoteException, JMSException {
        throw new JMSException("not support now!");
    }

    public TemporaryQueue createTemporaryQueue() throws RemoteException, JMSException {
        throw new JMSException("not support now!");
    }

    public TemporaryTopic createTemporaryTopic() throws RemoteException, JMSException {
        throw new JMSException("not support now!");
    }

    // --- from javax.jms.Connection
    public String getClientID() throws RemoteException, JMSException {
        throw new JMSException("not support now!");
    }

    public void setClientID(String clientID) throws RemoteException, JMSException {
        throw new JMSException("not support now!");
    }

    public ConnectionMetaData getMetaData() throws RemoteException, JMSException {
        throw new JMSException("not support now!");
    }

    protected void doInit() throws Exception {
        super.doInit();
        try {
            UnicastRemoteObject.unexportObject(this, true);
        }
        catch(Exception e) {
            // ignore
        }
    }

    protected void doStart() throws Exception {
        stub = UnicastRemoteObject.exportObject(this, port);
        InitialContextHelper.getInitialContext().bind(ConnectorHelper.getConnectorJNDIName(protocol), stub);
        // 启动一个线程，用来每隔一段时间 ping 一下，防止 rmi 线程死掉
        Thread pingThread = new Thread(this, getClass().getName());
        pingThread.setPriority(Thread.MIN_PRIORITY);
        pingThread.start();

    }

    protected void doStop() throws Exception {
        InitialContextHelper.getInitialContext().unbind(ConnectorHelper.getConnectorJNDIName(protocol));
        UnicastRemoteObject.unexportObject(this, false);
    }

    protected void doDestroy() throws Exception {
        stub = null;
    }

    public void setPort(int port) throws RemoteException {
        if(port > 0) {
            this.port = port;
        }
    }

    public int getPort() throws RemoteException {
        return port;
    }

    public Protocol getProtocol() throws RemoteException {
        return protocol;
    }

    public void registerConnection(String clientId, JMSConnectionRemote conn) throws RemoteException {
        logger.info("register connection: " + clientId);
    }

    public void unregisterConnection(String clientId) throws RemoteException {
    }

    public boolean isConnectionRegistered(String clientId) throws RemoteException {
        return false;
    }

    public static void main(String[] args) {

    }
}
