每個(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;
}
}}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注