/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software
 * License version 1.1, a copy of which has been included with this
 * distribution in the LICENSE.APL file.  
 */

package org.apache.log4j.rmi;

import java.rmi.RemoteException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.server.ServerNotActiveException;
import java.net.MalformedURLException;

import org.apache.log4j.Category;
import org.apache.log4j.spi.LoggingEvent;
//import org.apache.log4j.Hierarchy;
import org.apache.log4j.helpers.LogLog;

/** Unicast remote object implementing java.rmi.Remote interface.
 *
 * @author Alessandro Di Maria
 * @version 1.0
 */
public class RemoteNode extends UnicastRemoteObject implements IRemoteNode {
    
    /**
     * host of the server where RemoteNode is located
     */
    private String host = "localhost";
    
    /**
     * port to
     */
    private String port;
    
    /**
     * name of RemoteNode which is bound in the rmiregistry
     */
    private String name = "RemoteNode";
    
    /**
     * setter for host
     * @param host hostname or IP of the server
     */
    public void setHost(String host){
        this.host = host;
    }
    
    /**
     * setter for port
     * @param port number
     */
    public void setPort(String port){
        this.port = port;
    }
    
    /**
     * setter for port
     * @param port number
     */
    public void setPort(int port){
        this.port = String.valueOf(port);
    }
    
    /**
     * setter for name
     * @param name of the RemoteNode which will be bound in the rmiregitery
     */
    public void setName(String name){
        this.name = name;
    }

    public RemoteNode() throws RemoteException {
        super();
    }

    /**
     * @param port number
     */
    public RemoteNode(int port) throws RemoteException {
        super(port);
        this.port = String.valueOf(port);
    }

    /**
     * registers the RemoteNode with the rmiregistery using host,port and name
     */
    public void register(){
        String strURL;
        
        if (System.getSecurityManager()==null){
            System.setSecurityManager(new RMISecurityManager());
            LogLog.debug("new RMISecurityManger");
        }
        if (port==null){
            strURL ="//"+host+"/"+name;
        } else {
            strURL ="//"+host+":"+port+"/"+name;
        }
        try{      		
            Naming.rebind(strURL,this);
            LogLog.debug("RemoteNode bound in rmiRegistery");
        } catch(RemoteException re){
            LogLog.error("RemoteNode not bound in registery",re);
        } catch(MalformedURLException mue){
            LogLog.error("URL ["+strURL+"] for RemoteNode malformed",mue);
        }        
    }
    
    public void receiveEvent(LoggingEvent event) throws RemoteException {
        try{
            event.pushNDC(this.getClientHost());
            Category remoteCategory = Category.getInstance(event.categoryName);
            remoteCategory.callAppenders(event);            
        } catch(ServerNotActiveException snae){
            LogLog.warn("ClientHost not pushed to NDC");
        }        
    }    
}

