오래된글/Articles

Getting all EJB home interface object from a HashMap

Records that rule memory 2018. 4. 19. 15:03
728x90

public class SingleHome{

 private String ref="java:comp/env/ejb";

 //EJB reference name use ejb/remote interface name

 private static HashMap homeMap;

 private SingleHome(){}

 

 public static SingleHome getSingleHome(){

   if (homeMap==null){

     homeMap=new HashMap();

   }

   return SingleHome();

 }


 public Object getDestHome(String remoteInterfaceName){

   String strLookup="ref"+remoteInterfaceName;

   String strClass=remoteInterfaceName+"Home";

   if (homeMap.containsKey(remoteInterfaceName))

     return homeMap.get(remoteInterfaceName);

   else {

     try{

       Context ctx=new InitialContext();

       Object obj=ctx.lookup(strLookup);

       Class clss=Class.forName(strClass);

       Object home=PortableRemoteObject.narrow(obj,clss);

     }catch (Exception e) {

       System.out.println(e.getMessage());

       System.out.println("you must guaranty EJB remote interface name as parameter.");

     }

     homemap.put(remoteInterfaceName,home);

     return home;

   }

}



//A class to invoke business methods in remote interface object


public class ABC{

...


SingleHome sHome=SingleHome.getSingleHome();

XXX eHome=(XXX)sHome.sHome.getDestHome("YYY");

//XXX is home interface name, YYY is remote interface name

//Getting remote interface object with mothods in home interface object

...


}


http://www.theserverside.com/patterns/thread.tss?thread_id=24494


728x90