jndi

public class BaseServiceHandle
{

 public static InitialContext cxt = null ;
 
 
 /**
  * @param serviceClass
  * @return
  * @throws BaseException
  */
 public static Object getService(Class serviceClass) throws BaseException
 {
  String jndi_name = "";
  try
  {
   if(cxt == null )
   {
    cxt = getInitialContext();
   }
   String name = serviceClass.getName();
   jndi_name = name.replace('.', '/') + "/remote";
   Object object =  ServiceDataCache.getValue(jndi_name) ;
   if(object  == null)
   {
    object = cxt.lookup(jndi_name);
    ServiceDataCache.put(jndi_name, object);
    return  object ;
   }
   else
    return object ;
  }
  catch (NamingException er)
  {
   throw new JNDIException(ExceptionConstant.SERVICENOTFOUND,
     serviceClass.getName(), jndi_name,
     er);
  }
 }

 public static InitialContext getInitialContext() throws NamingException
 {
  Hashtable props = new Hashtable();

  // 不同容器的INITIAL_CONTEXT_FACTORY,PROVIDER_URL可能不一樣
  props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
    "org.jnp.interfaces.NamingContextFactory");
  props.put(InitialContext.PROVIDER_URL, "jnp://localhost:1099");

  InitialContext initialContext = new InitialContext(props);
  return initialContext;
 }
}


public class InventoryNoticeCtrl
{

}

 


 private InventoryNoticeService getService()
 {
  InventoryNoticeService srv = null;
  try
  {
   srv = (InventoryNoticeService) BaseServiceHandle
     .getService(InventoryNoticeService.class);
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  return srv;
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章