程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 運用反射實現ejb動態委派

運用反射實現ejb動態委派

編輯:關於JAVA

每個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());
      }
     }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved