利用反射实现ejb动态委派[Java编程]
本文“利用反射实现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());
}
}
以上是“利用反射实现ejb动态委派[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |