网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.规则与自由:为何选择CORBA和Java.
.把JBoss缓存用作POJO缓存的实战演.
.探索Application Server的世界.
.Apusic 应用服务器简介.
.用JAVA访问共享文件系统.
.Eclipse插件之WebLogic Plugin 2.
.没有迭代到的元素被动态删除时的.
.Java学习之Java的运行环境.
.pop 方法.
.关于解决 Java 编程语言线程问题.
.创建并解析XML文件Java实例.
.为apache加速.
.Eclipse入门之使用指南及开发Ecl.
.Java之util类.
.使用PreparedStatement减少开发的.
.轻松掌握Java泛型(第4部分).
.J2SE 5.0的static import.
.另类查询 Hibernate HQL 深度历险.
.Enterprise JavaBeans导论7.
.JAVA 的MD5加密算法源代码.

运用反射实现ejb动态委派

发表日期:2008-1-5



  每个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());
  
     }
  
    }
上一篇:EJB实质问题 人气:421
下一篇:EJB最佳实践:实体bean保护 人气:418
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐