国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

運用反射實現ejb動態委派

2019-11-18 12:36:14
字體:
來源:轉載
供稿:網友

  每個bean可能會有很多方法,一般我們通過一個delegate來調用sessionbean中的方法,而非直接調用sessionbean,delegate中只是簡單的對每個相對應的sessionbean的public方法的簡單封裝,在調用的時候省去了每次對home的查找和ejb對象的create,但是可能我們的bean會有很多方法,假如每個bean都寫這樣一個delegate,這樣工作量就會很大,而且也不便于以后系統的移植,比如說,原來使用ejb實現,現在要改用jdo直接操作數據庫,而通過運用java的reflect技術,就能較好地實現這些要求。首先,定義了一個FacadeDelegate的抽象類,用來實現對sessionbean的home的查找,代碼如下:
  
  import javax.ejb.*;
  
  import testejb.util.common.*;
  
  import testejb.util.resource.*;
  
  public abstract class FacadeDelegate{
  
   PRivate static String type = Resource.RemoteType;
  
   public FacadeDelegate() {
  
   }
  
   public EJBHome getHome(String jindiName,Class className)
  
   {
  
    EJBHome home = null;
  
    ServerLocatorAdapter adapter = ServerLocatorAdapter.getInstance();
  
    try
  
    {
  
     home = (EJBHome)adapter.getHome(type, jindiName, className);
  
    }
  
    catch(Exception e)
  
    {
  
     System.err.println(e.getMessage() + jindiName + className.toString());
  
    }
  
    return home;
  
   }
  
  
  
  }
  
  其中ServerLocatorAdapter是一個用來根據是local還是remote調用ejb對象而通過不同的方法查找home的類,假如type為local則調用LocalServerLocate中的方法,假如type為remote則調用RemoteServerLocate中的方法,獲得home。代碼如下:
  
  import java.util.*;
  
  import java.lang.reflect.*;
  
  import testejb.util.resource.*;
  
  public class ServerLocatorAdapter {
  
   private Map cache;//用來緩存home
  
   private static ServerLocatorAdapter me;
  
   public static ServerLocatorAdapter getInstance()
  
   {
  
    if(me == null)
  
     me = new ServerLocatorAdapter();
  
    return me;
  
   }
  
   //取得home
  
  public Object getHome(String type,String jndiHomeName,Class className) throws Exception
  
   {
  
    Object home = null;
  
    if(cache.containsKey(jndiHomeName))
  
     return cache.get(jndiHomeName);
  
    if(Resource.LocalType.equals(type))
  
    {
  
     home = getLocalHome(jndiHomeName,className);
  
     cache.put(jndiHomeName,home);
  
     return home;
  
    }
  
    if(Resource.RemoteType.equals(type))
  
    {
  
     home = getRemoteHome(jndiHomeName,className);
  
     cache.put(jndiHomeName,home);
  
     return home;
  
    }
  
    return home;
  
   }
  
   //取得local home
  
   private Object getLocalHome(String jndiHomeName,Class className) throws Exception
  
   {
  
  Class myClass = Class.forName(Resource.LocalClass);
  
  // Resource. LocalClass =”testejb.util.common. LocalServerLocator
  
  Method method = myClass.getMethod(Resource.LocalConstractMethod,null);
  
  // Resource. LocalConstractMethod =” getInstance”
  
    LocalServerLocator local = null;
  
    local = (LocalServerLocator)method.invoke(myClass,null);
  
    return local.getLocalHome(jndiHomeName,className);
  
  }
  
  //取得remote home
  
   private Object getRemoteHome(String jndiHomeName,Class className) throws Exception
  
   {
  
  Class myClass = Class.forName(Resource.RemoteClass);
  
  // Resource.RemoteClass =”testejb.util.common.RemoteServerLocator”
  
  Method method = myClass.getMethod(Resource.RemoteConstractMethod,null);
  
  // Resource.RemoteConstractMethod=” getInstance”
  
    RemoteServerLocator remote = null;
  
    remote = (RemoteServerLocator)method.invoke(myClass,null);
  
    return remote.getHome(jndiHomeName,className);
  
   }
  
   private ServerLocatorAdapter() {
  
    // 為cache提供線程安全的保證
  
    cache = Collections.synchronizedMap(new HashMap());
  
   }
  
  }
  
  其中Resource為資源類,其中通過對配置文件的讀取,取得一些指定的配置信息。
  
  RemoteServerLocator和LocalServerLocator是兩個根據不同的調用方式取得home借口的具體實現類,代碼如下:
  
  LocalServerLocator:
  
  import javax.naming.*;
  
  import javax.rmi.PortableRemoteObject;
  
  import java.util.*;
  
  import javax.ejb.*;
  
  public class LocalServerLocator {
  
   private Context ic;
  
   private Map cache;//緩存home
  
   private static LocalServerLocator me;
  
   public static LocalServerLocator getInstance()
  
   {
  
    if(me == null)
  
    {
  
     try
  
     {
  
      me = new LocalServerLocator();
  
     }
  
     catch(Exception e)
  
     {
  
      System.err.println(e.getCause());
  
      System.err.println(e.getMessage());
  
     }
  
    }
  
    return me;
  
   }
  
   public EJBLocalHome getLocalHome(String jndiHomeName, Class className) throws Exception {
  
      EJBLocalHome home = null;
  
      try {
  
        if (cache.containsKey(jndiHomeName)) {
  
          home = (EJBLocalHome) cache.get(jndiHomeName);
  
        } else {
  
          Object objref = ic.lookup(jndiHomeName);
  
          home = (EJBLocalHome) objref;
  
          cache.put(jndiHomeName, home);
  
        }
  
      } catch (NamingException ne) {
  
        System.err.println(jndiHomeName);
  
        throw ne;
  
      } catch (Exception e) {
  
        throw e;
  
      }
  
      return home;
  
    }
  
   private LocalServerLocator() throws Exception{
  
    try
  
    {
  
     ic = new InitialContext();
  
     // 為cache提供線程安全的保證
  
     cache = Collections.synchronizedMap(new HashMap());
  
    }
  
    catch(NamingException ne)
  
    {
  
     throw ne;
  
    }
  
    catch(Exception e)
  
    {
  
     throw e;
  
    }
  
   }
  
  }
  
  RemoteServerLocator
  
  import javax.naming.*;
  
  import javax.rmi.PortableRemoteObject;
  
  import java.util.*;
  
  import javax.ejb.*;
  
  public class RemoteServerLocator{
  
   private Context ic;
  
   private Map cache;
  
   private static RemoteServerLocator me;
  
   public static RemoteServerLocator getInstance()
  
   {
  
    if(me == null)
  
    {
  
     try
  
     {
  
      me = new RemoteServerLocator();
  
     }
  
     catch(Exception e)
  
     {
  
      System.err.println(e.getMessage());
  
     }
  
    }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富平县| 浦江县| 西乌珠穆沁旗| 泗水县| 馆陶县| 博客| 疏勒县| 错那县| 田阳县| 松滋市| 洞头县| 永平县| 东阳市| 芦山县| 密云县| 杨浦区| 泸州市| 山东| 娱乐| 温泉县| 古交市| 抚远县| 伊川县| 仪征市| 古田县| 奉新县| 黑龙江省| 宁波市| 新竹县| 冕宁县| 上杭县| 丹凤县| 清流县| 麦盖提县| 太保市| 阜新市| 济南市| 海盐县| 聂荣县| 衡东县| 丽水市|