package com.fp.facadeclient.helper;

import java.util.Properties;

import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;

import com.fp.common.logger.APPLogger;
import com.fp.common.properties.PropertiesHandler;
import com.fp.facadeclient.ejb.FacadeBeanRemote;
import javax.naming.NamingException;

/**
 * Clase utilitaria que mantiene una coneccion con el bean de fachada de entrada al core. 
 * @author Jorge Vaca.
 * @version 2.1
 */
public class ServiceLocator {

    /**
     * Version de la Clase
     */
    private static final long serialVersionUID = 1L;
    @EJB
    private FacadeBeanRemote facadeRemote;
    /**
     * Almacena una instancia de ServiceLocator.
     */
    private static ServiceLocator cache;

    /**
     * Entrega una instancia de ServiceLocator.
     * @return	ServiceLocator
     */
    public static ServiceLocator getInstance() {
        if (cache != null) {
            return cache;
        }
        synchronized (ServiceLocator.class) {
            if (cache == null) {
                cache = new ServiceLocator();
            }
        }
        return cache;
    }

    /**
     * Entrega el valor de: facadeRemote
     * @return FacadeBeanRemote
     */
    public FacadeBeanRemote getFacadeRemote() throws Exception {
        if (this.facadeRemote == null) {
            this.fillFacadeBeanRemote();
        }
        return facadeRemote;
    }

    /**
     * Obtiene una coneccion a bean de entrada al core. 
     * @throws Exception
     */
    private void fillFacadeBeanRemote() {
        if (this.facadeRemote == null) {
            String server = null;
            try {
                APPLogger.getLogger().info("\nOBTENIENDO CONTEXT DEL SESSION BEAN");
                PropertiesHandler ph = new PropertiesHandler("fb-facade");
                Properties props = new Properties();
                props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                
                server = "jnp://" + ph.getStringValue("server") + ":" + ph.getStringValue("port");
                props.put(Context.PROVIDER_URL, server);
                Context jndiContext = new InitialContext(props);
                String a = ph.getStringValue("ear") + ph.getStringValue("bean");
                Object obj = jndiContext.lookup(a);
                this.facadeRemote = (FacadeBeanRemote) obj;
                APPLogger.getLogger().info("\nCONTEXT DEL SESSION BEAN OBTENIDO: " + server);
            } catch (NamingException ex) {
                APPLogger.getLogger().error("\nNamingException ServiceLocator: " + server, ex);
            } catch (Exception ex) {
                APPLogger.getLogger().error("\nException ServiceLocator: " + server, ex);
            }

        }
    }

    /**
     * Obtiene una coneccion a bean de entrada al core. 
     * @throws Exception
     */
    protected void fillFacadeBeanRemoteTest() throws Exception {
        if (this.facadeRemote == null) {
            try {
                APPLogger.getLogger().info("\nOBTENIENDO UN CONTEXT DEL SESSION BEAN");
                Properties props = new Properties();
                props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                props.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1299");
                Context jndiContext = new InitialContext(props);
                String a = "maiaear-2.1/" + "FacadeBean/remote";
                Object obj = jndiContext.lookup(a);
                this.facadeRemote = (FacadeBeanRemote) obj;
                APPLogger.getLogger().info("\nOK CONTEXT");
            } catch (Exception ex) {
                APPLogger.getLogger().error(ex);
            }
        }
    }
}
