package com.fp.facadeclient.helper;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.fp.common.logger.APPLogger;
import com.fp.facadeclient.ejb.FacadeBeanRemote;

/**
 * Clase utilitaria que mantiene una coneccion con el bean de fachada de entrada al core que esta desplegado en jboss 7.
 * @author Jorge Vaca.
 * @version 2.1
 */
public class ServiceLocatorJboss7 {

    /**
     * Almacena una instancia de ServiceLocatorJboss.
     */
    private static ServiceLocatorJboss7 cache;

    private FacadeBeanRemote facadebeanremote;

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

    /**
     * Entrega un proxy de los ejb remotos.
     * @return FacadeBeanRemote
     */
    public FacadeBeanRemote getFacadeRemote() throws Exception {
        if (this.facadebeanremote == null) {
            APPLogger.getLogger().error("Obtiene una coneccion con el core");
            this.facadebeanremote = ServiceLocatorJboss7.lookupRemoteStatelessFacadeBean();
        }
        return this.facadebeanremote;
    }

    /**
     * Looks up and returns the proxy to remote stateless calculator bean
     * @return
     * @throws NamingException
     */
    private static FacadeBeanRemote lookupRemoteStatelessFacadeBean() throws NamingException {
        Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        Context context = new InitialContext(jndiProperties);
        // The app name is the application name of the deployed EJBs. This is typically the ear name
        // without the .ear suffix. However, the application name could be overridden in the application.xml of the
        // EJB deployment on the server.
        // Since we haven't deployed the application as a .ear, the app name for us will be an empty string
        final String appName = "maiaear-2.1";
        // This is the module name of the deployed EJBs on the server. This is typically the jar name of the
        // EJB deployment, without the .jar suffix, but can be overridden via the ejb-jar.xml
        // In this example, we have deployed the EJBs in a jboss-as-ejb-remote-app.jar, so the module name is
        // jboss-as-ejb-remote-app
        final String moduleName = "facade";
        // AS7 allows each deployment to have an (optional) distinct name. We haven't specified a distinct name for
        // our EJB deployment, so this is an empty string
        final String distinctName = "";
        // The EJB name which by default is the simple class name of the bean implementation class
        // final String beanName = FacadeBean.class.getSimpleName();
        final String beanName = "facadebean"; // esto porque en el ejb esta el nombre del bean cambiado.
        // the remote view fully qualified class name
        final String viewClassName = FacadeBeanRemote.class.getName();
        // let's do the lookup
        String aux = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName;
        return (FacadeBeanRemote) context.lookup(aux);
    }

    // LIbrerias
    // jboss-client-7.1.0.Final.jar esta bajo jbos-as-7.1.0.final\bin\client

}
