it167.com  设为主页
 收藏本站
 
  资讯:业界动态 | 软件动态 | 人物专栏 | 安全资讯 | 网络生活 | 电子商务 | 小游戏 | 视频 | 美女图片 | 音乐
  网络编程 | 网站运营 | 网页制作 | 图形图象 | 操作系统 | 媒体动画 | 软件教学 | 网络应用 | 邮件系统 | 网络安全 | 认证考试
asp | .net | php | jsp | Sql | java | Dreamweaver | FrontPages | Javascript | css | Coreldraw | photoshop | Flash | Coreldraw
当前位置: > 主页>网络编程>Java>JAVA开发技巧>运用反射实现ejb动态委派(1)
最新新闻

·机会与整合 边缘化互联
·TOM-Skype新增三大本地
·雅虎抢闸邮箱竞赛 网易
·新浪抢攻北京奥运
·洞悉网络口碑的掘金机会
·拆解网络病毒黑金交易
·木马下载器近期出现新变
·《互联网周刊》第17期文
·Web2.0是否催生自吹自擂
·三张宝宝裸照招来MSN封
热门新闻
·Java SE 6 的HTTP 协议
·Java中对HashMap的深度
·Java调用Oracle的过程和
·开源技术 Eclipse使用技
·深入了解WebLogic的类装
·Java进阶:Struts多模块
·Java初学者入门经典:面
·Jave学习精华:Jsp小结
·Java学习:EJB的专用术语
·编程必备经典:Java常见
推荐新闻
 
 

运用反射实现ejb动态委派(1) 

作者:   来源:it167   点击:   日期:2007-01-28

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

}

}}

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

}}



共3页: 上一页 1 [2] [3] 下一页
文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:
·Hibernate对继承关系的映射 ·初识Eclipse User Assistance
·struts和hibernate谈J2EE架构数据表示 ·Java中四种操作XML方式的比较(1)
·配置Hibernate+多对一实体映像(1) ·CMP实体bean实战开发

   文章评论:(0条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:it167  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。