package WEB-INF.classes.wenrgise.common.utility;

import java.util.HashMap;
import javax.ejb.EJBLocalHome;
import javax.naming.Context;
import javax.naming.NamingException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.WorkFlowContextProvider;

public class WorkFlowServiceLocator {
  private HashMap homeCache;
  
  private final HashMap cacheMap = new HashMap();
  
  private static wenrgise.common.utility.WorkFlowServiceLocator serviceLocator = new wenrgise.common.utility.WorkFlowServiceLocator();
  
  private WorkFlowServiceLocator() {
    try {
      this.homeCache = new HashMap();
    } catch (Throwable e) {
      e.printStackTrace();
    } 
  }
  
  public static wenrgise.common.utility.WorkFlowServiceLocator getLocator() {
    return serviceLocator;
  }
  
  public Object getService(String jndiName) throws EnrgiseSystemException {
    try {
      if (!this.homeCache.containsKey(jndiName)) {
        Context context = WorkFlowContextProvider.getContext();
        this.homeCache.put(jndiName, context.lookup(jndiName));
      } 
    } catch (NamingException oNa) {
      System.out.println(String.valueOf(String.valueOf(String.valueOf("The problem is ").concat(String.valueOf(oNa.getMessage()))).concat(String.valueOf(" the type is "))).concat(String.valueOf(oNa.getClass().getName())));
      throw new EnrgiseSystemException(oNa);
    } catch (Exception oEx) {
      System.out.println(String.valueOf(String.valueOf(String.valueOf("The problem is ").concat(String.valueOf(oEx.getMessage()))).concat(String.valueOf(" the type is "))).concat(String.valueOf(oEx.getClass().getName())));
      throw new EnrgiseSystemException(oEx);
    } 
    return this.homeCache.get(jndiName);
  }
  
  public EJBLocalHome getLocalHome(String jndiHomeName) throws EnrgiseSystemException {
    EJBLocalHome home = null;
    try {
      jndiHomeName = jndiHomeName.trim();
      Context context = WorkFlowContextProvider.getLocalContext();
      if (this.cacheMap.containsKey(jndiHomeName)) {
        home = (EJBLocalHome)this.cacheMap.get(jndiHomeName);
      } else {
        home = (EJBLocalHome)context.lookup(String.valueOf("java:comp/env/").concat(String.valueOf(jndiHomeName)));
        this.cacheMap.put(jndiHomeName, home);
      } 
    } catch (NamingException ne) {
      ne.printStackTrace();
      throw new EnrgiseSystemException(ne);
    } 
    return home;
  }
}
