内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

怎么運用反射實現(xiàn)ejb動態(tài)委派-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“怎么運用反射實現(xiàn)ejb動態(tài)委派”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“怎么運用反射實現(xiàn)ejb動態(tài)委派”這篇文章吧。

成都創(chuàng)新互聯(lián)服務(wù)項目包括石樓網(wǎng)站建設(shè)、石樓網(wǎng)站制作、石樓網(wǎng)頁制作以及石樓網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,石樓網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到石樓省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

每個bean可能會有很多方法,一般我們通過一個delegate來調(diào)用sessionbean中的方法,而非直接調(diào)用sessionbean,delegate中只是簡單的對每個相對應(yīng)的sessionbean的public方法的簡單封裝,在調(diào)用的時候省去了每次對home的查找和EJB對象的create,但是可能我們的bean會有很多方法,如果每個bean都寫這樣一個delegate,這樣工作量就會很大,而且也不便于以后系統(tǒng)的移植,比如說,原來使用ejb實現(xiàn),現(xiàn)在要改用jdo直接操作數(shù)據(jù)庫,而通過運用Java的reflect技術(shù),就能較好地實現(xiàn)這些要求。首先,定義了一個FacadeDelegate的抽象類,用來實現(xiàn)對sessionbean的home的查找,代碼如下:XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

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是一個用來根據(jù)是local還是remote調(diào)用ejb對象而通過不同的方法查找home的類,如果type為local則調(diào)用LocalServerLocate中的方法,如果type為remote則調(diào)用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是兩個根據(jù)不同的調(diào)用方式取得home借口的具體實現(xiàn)類,代碼如下:

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());

  }

  }

  return me;

  }

  public EJBHome getHome(String jndiHomeName, Class className) throws Exception {

  EJBHome home = null;

  try {

  if (cache.containsKey(jndiHomeName)) {

  home = (EJBHome) cache.get(jndiHomeName);

  } else {

  Object objref = ic.lookup(jndiHomeName);

   Object obj = PortableRemoteObject.narrow(objref, className);

  home = (EJBHome) obj;

  cache.put(jndiHomeName, home);

  }

  } catch (NamingException ne) {

  System.err.println(jndiHomeName);

  throw ne;

  } catch (Exception e) {

  throw e;

  }

  return home;

  }

  private RemoteServerLocator() throws Exception{

  try {

  ic = getInitialContext();

  // 為cache提供線程安全的保證

  cache = Collections.synchronizedMap(new HashMap());

  } catch (NamingException ne) {

  throw ne;

  } catch (Exception e) {

  throw e;

  }

  }

  private javax.naming.Context getInitialContext() throws NamingException {

  java.util.Hashtable JNDIPaRM = new java.util.Hashtable();

  JNDIParm.put(Context.PROVideR_URL, "your server address");

  JNDIParm.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

  return new InitialContext(JNDIParm);

  }

}

對上面這些調(diào)用機(jī)制有個了解之后,下面就是來具體的實現(xiàn)動態(tài)委派了,再此定義了一個FacadeDelegateImp類,繼承了FacadeDelegate類。先看一下代碼,然后對此作解釋,這樣比較清楚一些

import testejb.delegate.common.*;

import javax.ejb.*;

import java.lang.reflect.*;

import java.util.*;

public class FacadeDelegateImp extends FacadeDelegate {

  private static FacadeDelegateImp me;

  private Map cache;

  private HashMap methodMap;

  private Object object;

  public static FacadeDelegateImp getInstance()

  {

  if(me == null)

  me = new FacadeDelegateImp();

  return me;

}

//init方法是在調(diào)用invoke之前對要調(diào)用的sessionbean進(jìn)行初始化

  public void init(String jindiName, String className) {

  EJBHome home = null;

  if(cache.containsKey(jindiName))

  home = (EJBHome)cache.get(jindiName);

  else

  {

  try

  {

  home = super.getHome(jindiName, Class.forName(className));//調(diào)用父類的的方法取得home

  }

  catch(Exception e)

  {

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

  }

  cache.put(jindiName,className);

  }

  try

  {

  object = home.getClass().getMethod("create", null).invoke(home, null);//調(diào)用home的//create方法,取得ejbObject

  methodMap = new HashMap();

//將ejbObject中所有的方法存入methodMap中

  Method[] aryMethod = object.getClass().getMethods();

  if(aryMethod != null && aryMethod.length > 0)

  {

  for (int i = 0; i < aryMethod.length; i++) {

  methodMap.put(aryMethod[i].getName(), aryMethod[i]);

  }

  }

  }

  catch(Exception e)

  {

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

  }

  }

  //此init方法是對一般java類初始化

  public void init(String className,Object[] args)

  {

  boolean flage = false;

  if(cache.get(className) != null)

  object = cache.get(className);

  else

  {

  try {

  Class myClass = Class.forName(className);

  if (args != null && args.length > 0) {

  Class[] type = new Class[args.length];

  for (int i = 0; i < type.length; i++) {

  type[i] = args[i].getClass();

  }

  Constructor constructor = myClass.getConstructor(type);

  object = constructor.newInstance(args);

  cache.put(className, object);

  }

  else {

  object = myClass.newInstance();

  cache.put(className, object);

  }

  }

  catch (Exception e) {

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

   }

  }

  Method[] methods = object.getClass().getMethods();

  methodMap = new HashMap();

  for(int i = 0; i< methods.length; i++)

  methodMap.put(methods[i].getName(),methods[i]);

  }

  public Object invoke(String method, Object[] args,String jindiName, String className)

  {

  if("init".equals(method))

  {

  this.init(jindiName, className);

  return null;

  }

  if(methodMap == null)

  this.init(jindiName, className);

  Method tmpMethod = (Method)methodMap.get(method);

   if(tmpMethod != null)

  {

  try

  {

  return tmpMethod.invoke(object, args);

  }

  catch(Exception e)

  {

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

  }

  }

  return null;

  }

  public Object invoke(String method, Object[] args, String className)

  {

  if("init".equals(method))

  {

  this.init(className,args);

  return null;

  }

  if(methodMap == null)

  System.err.println("not init");

  Method tmpMethod = (Method)methodMap.get(method);

  if(tmpMethod != null)

  {

  try

  {

  return tmpMethod.invoke(object, args);

  }

  catch(Exception e)

  {

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

  }

  }

  return null;

  }

  private FacadeDelegateImp() {

  cache = Collections.synchronizedMap(new HashMap());

  }

}

以上是“怎么運用反射實現(xiàn)ejb動態(tài)委派”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

網(wǎng)頁名稱:怎么運用反射實現(xiàn)ejb動態(tài)委派-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://www.rwnh.cn/article16/ceiegg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)網(wǎng)站制作、品牌網(wǎng)站制作、外貿(mào)建站、搜索引擎優(yōu)化、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司
无为县| 灯塔市| 红河县| 石河子市| 广安市| 河南省| 固原市| 宝鸡市| 北辰区| 五原县| 集贤县| 凌海市| 黄山市| 岗巴县| 循化| 静乐县| 舒城县| 抚顺县| 开封市| 江口县| 大庆市| 新化县| 巴彦县| 如东县| 庆云县| 长顺县| 安国市| 溆浦县| 宝坻区| 锦屏县| 师宗县| 保山市| 台东县| 嘉兴市| 江孜县| 景谷| 丹棱县| 喀喇| 马尔康县| 洛南县| 乐陵市|