麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

運用反射實現ejb動態委派(1)

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

每個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;

}

}}



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: videos真实高潮xxxx | 久久毛片免费观看 | 男女羞羞视频在线观看免费 | 免费看黄色一级大片 | 一级毛片在线视频 | 曰韩一级片 | 国产一区二区欧美精品 | 久久网国产 | 色成人在线 | 成人啪啪18免费网站 | 青青草成人免费视频在线 | 精品久久久久久久久久久αⅴ | 中文字幕网在线 | 免费毛片播放 | 国产亚洲自拍一区 | 国产在线精品一区二区三区不卡 | 久久精品欧美一区二区 | chinesexxx少妇露脸 | a一级黄| 精品一区二区三区免费 | av免费在线播放 | 澳门一级淫片免费视频 | 国产午夜精品久久久久婷 | 一级免费a | 91成人午夜性a一级毛片 | 亚洲成人网一区 | 性毛片视频 | 欧美一级成人一区二区三区 | 国产麻豆交换夫妇 | 国产精品亚洲三区 | 国产91对白叫床清晰播放 | 欧美一级黄色片在线观看 | 精品一区二区免费 | 久久一区二区三区av | 亚洲综合视频网 | 91超在线 | 91色琪琪电影亚洲精品久久 | 天天看成人免费毛片视频 | 在线 日本 制服 中文 欧美 | 国产成人在线免费看 | 伦一区二区三区中文字幕v亚洲 |