/*
 * RmiTreeClientImpl.java
 *
 * Created on October 14, 2003, 8:29 PM
 */

package hep.aida.ref.remote.rmi.client;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.Map;

import hep.aida.dev.IDevMutableStore;

import hep.aida.ref.remote.RemoteClient;
import hep.aida.ref.remote.RemoteConnectionException;
import hep.aida.ref.remote.interfaces.AidaTreeServer;
import hep.aida.ref.remote.interfaces.AidaUpdateEvent;
import hep.aida.ref.remote.rmi.RmiRemoteUtils;
import hep.aida.ref.remote.rmi.interfaces.RmiClient;
import hep.aida.ref.remote.rmi.interfaces.RmiServer;

/**
 * This class is implementation of AidaTreeClient interface,
 * based on functionality of the RemoteClient class. It implements
 * only one method - getServer(), that creates an AidaTreeServer
 * wrapper (RmiAidaTreeServer) around the real RmiServer reference.
 * This complex scheme is put in place to isolate the transport layer
 * (RMI, CORBA, etc.) from the functional layer of working with the
 * AIDA Tree, queues, events, threads.
 *
 * @author  serbo
 */
public class RmiRemoteClient extends RemoteClient {
    
    private Map options;
    
    /** Creates a new instance of RmiAidaTreeClient */
    public RmiRemoteClient(IDevMutableStore store, Map options) {
        super(store);
        this.options = options;
    }
    
    public RmiRemoteClient(IDevMutableStore store, boolean duplex, Map options) {
        super(store, duplex);
        this.options = options;
    }
    
    
    protected void init() {   
        super.init();
        clientID = "RmiRemoteClient/"+RmiRemoteUtils.getCurrentDateString();
    }
    
    
    // Try to handle all RMI export and connection here
    protected AidaTreeServer getServer() {
        AidaTreeServer treeServer = null;
        
        try {
            // Try to resolve RMI server
            RmiServer server = null;
            String rmiServerName = null;
            Iterator it = options.keySet().iterator();
            while (it.hasNext()) {
                String key = ((String) it.next());
                String value = ((String) options.get(key));
                remoteLogger.fine("Key = "+key+" \tValue = "+value);
                if (key.equalsIgnoreCase("RmiServerName")) {
                    rmiServerName = value; 
               }
            }
        
            server = (RmiServer) Naming.lookup(rmiServerName);

            // Create AidaTreeServer
            treeServer = new RmiRemoteServer(server);
        } catch (Exception e) {
            throw new RemoteConnectionException(e.getMessage(), e);
        }
        return treeServer;
    }    

    public boolean disconnect() {
        boolean ok = super.disconnect();
        //if (options != null) options.clear();
        options = null;
        return ok;
    }
}
