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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

運(yùn)用反射實(shí)現(xiàn)ejb動(dòng)態(tài)委派(1)

2019-11-18 13:36:48
字體:
供稿:網(wǎng)友

每個(gè)bean可能會(huì)有很多方法,一般我們通過一個(gè)delegate來調(diào)用sessionbean中的方法,而非直接調(diào)用sessionbean,delegate中只是簡單的對(duì)每個(gè)相對(duì)應(yīng)的sessionbean的public方法的簡單封裝,在調(diào)用的時(shí)候省去了每次對(duì)home的查找和ejb對(duì)象的create,但是可能我們的bean會(huì)有很多方法,假如每個(gè)bean都寫這樣一個(gè)delegate,這樣工作量就會(huì)很大,而且也不便于以后系統(tǒng)的移植,比如說,原來使用ejb實(shí)現(xiàn),現(xiàn)在要改用jdo直接操作數(shù)據(jù)庫,而通過運(yùn)用java的reflect技術(shù),就能較好地實(shí)現(xiàn)這些要求。首先,定義了一個(gè)FacadeDelegate的抽象類,用來實(shí)現(xiàn)對(duì)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是一個(gè)用來根據(jù)是local還是remote調(diào)用ejb對(duì)象而通過不同的方法查找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為資源類,其中通過對(duì)配置文件的讀取,取得一些指定的配置信息。

RemoteServerLocator和LocalServerLocator是兩個(gè)根據(jù)不同的調(diào)用方式取得home借口的具體實(shí)現(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;

}

}}



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 错那县| 湾仔区| 门头沟区| 黄陵县| 玉田县| 噶尔县| 丰台区| 布拖县| 东乡族自治县| 于田县| 抚顺市| 鸡西市| 巴林左旗| 天门市| 揭东县| 邻水| 临清市| 济阳县| 襄垣县| 平利县| 建宁县| 区。| 额济纳旗| 雅安市| 民丰县| 安国市| 宜丰县| 丽水市| 阿勒泰市| 张北县| 甘德县| 重庆市| 会理县| 福贡县| 石柱| 简阳市| 霸州市| 灵丘县| 郓城县| 黄冈市| 宝鸡市|