用SERVICE LOCATOR 模式实现命名访问

来源:互联网 发布:赣南脐橙淘宝宣传语 编辑:程序博客网 时间:2024/05/27 21:49
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

SERVICE LOCATOR 模式实现命名访问服务

 

B/S开发中, 我们经常要用到名称服务,如JNDIXMLNS等。名称服务随不同厂家而不同。每次需要获得名称服务时,需要适当的名称环境信息,然后查找服务,重复查找的成本很高。

 

此外,在持久性框架中,要求将所有的服务访问都包装到对象中,开发人员不需要知道名称服务后面的平台(数据库)类型,及任何安全信息或地址。在一个大型软件产品间存在多个EJB和多个数据源连接时(我们目前只有一个写死的数据源WEBGL)需要一个类来实现统一的访问管理。

 

因此,这就要求我们对这些访问进行封装隔离。这时我们就可以利用SERVICE LOCATOR模式。

 

 

我们将利用一个文件SERVICE.properties来管理所有的命名服务。例子代码实现了EJB本地,数据源的访问。

格式如下:

DEFAULTDS=webgl

NTCLASSREF=NTHome.class

 

把它保存在CLASSPATH引用的路径里。

 

源代码:

 

Package  com.learn;

 

import java.util.Hashtable;

import java.util.Properties;

import java.io.*;

import javax.ejb.EJBHome;

import javax.naming.InitialContext;

import javax.naming.Context;

import javax.naming.NamingException;

import javax.rmi.PortableRemoteObject;

import java.sql.Connection;

import java.sql.SQLException;

import javax.sql.DataSource;

 

public class SERVICELOCATOR {

  private static SERVICELOCATOR SERVICELOCATORRef = null;

  private static Hashtable ejbHomeCache = null;

  private static Hashtable dataSourceCache = null;

  private static Properties SERVICECache = null;

  static {

    SERVICELOCATORRef = new SERVICELOCATOR();

  }

 

  private SERVICELOCATOR() {

    ejbHomeCache = new Hashtable();

    dataSourceCache = new Hashtable();

    try {

      String SERVICEFileName = "SERVICE.properties";

      SERVICECache.load(new FileInputStream(SERVICEFileName));

    }

    catch (IOException e) {

      System.out.println(e.toString());

    }

  }

 

  /**

   * 使用singleton.模式静态对象多次调用节省资源   */

 

  public static SERVICELOCATOR getInstance() {

    return SERVICELOCATORRef;

  }

 

  /*

   * 由键获得键值,一般在数据源,XMLNS访问用到这个方法

   */

  static private String getSERVICEName(String SERVICEId)

    throws SERVICELOCATORException{

    String SERVICEName=null;

    if (SERVICECache.containsKey(SERVICEId)) {

      SERVICEName = (String) SERVICECache.get(SERVICEId);

    }

    else {

      throw new SERVICELOCATORException(

        "Unable to locate the SERVICE statement requested");

    }

    return SERVICEName;

  }

 

/*************************************************

 * EJB本地类引用

*************************************************/  

  static private Class getEJBHomeRef(String SERVICEId) throws

    SERVICELOCATORException {

    Class homeRef = null;

    if (SERVICECache.containsKey(SERVICEId)) {

      homeRef = (Class) SERVICECache.get(SERVICEId);

    }

    else {

      throw new SERVICELOCATORException(

        "Unable to locate the SERVICE statement requested");

    }

    return homeRef;

  }

 

/************************************************************************

 * 获得EJBHome对象

 ***********************************************************************/  

  public EJBHome getEJBHome(String SERVICEId) throws SERVICELOCATORException {

    EJBHome ejbHome = null;

    try {

      //先检查缓存是否存在EJBHome接口

      if (ejbHomeCache.containsKey(SERVICEId)) {

        ejbHome = (EJBHome) ejbHomeCache.get(SERVICEId);

        return ejbHome;

      }

      else {

        //如果没有存在,则解析并存到缓存中

        Context ctx = new InitialContext();

        Object jndiRef = ctx.lookup(SERVICEId);

        Object portableObj = PortableRemoteObject.narrow(jndiRef,

          getEJBHomeRef(SERVICEId));

        ejbHome = (EJBHome) portableObj;

        ejbHomeCache.put(SERVICEId, ejbHome);

        return ejbHome;

      }

    }

    catch (NamingException e) {

      throw new SERVICELOCATORException(

        "Naming exception error in SERVICELOCATOR.getEJBHome()", e);

    }

  }

 

  /*

   * 获得JNDI数据源

   */

  public Connection getDBConn(String SERVICEId) throws

    SERVICELOCATORException {

    Connection conn = null;

    String SERVICEName=getSERVICEName(SERVICEId);

    try {

      /*Checking to see if the requested DataSource is in the Cache*/

      if (dataSourceCache.containsKey(SERVICEId)) {

        DataSource ds = (DataSource) dataSourceCache.get(SERVICEId);

        conn = ( (DataSource) ds).getConnection();

        return conn;

      }

      else {

        /*

         * The DataSource was not in the cache.  Retrieve it from JNDI

         * and put it in the cache.

         */

        Context ctx = new InitialContext();

        DataSource newDataSource = (DataSource) ctx.lookup(SERVICEName);

        dataSourceCache.put(SERVICEId, newDataSource);

        conn = newDataSource.getConnection();

        return conn;

      }

    }

    catch (SQLException e) {

      throw new SERVICELOCATORException("A SQL error has occurred in " +

                                        "SERVICELOCATOR.getDBConn()", e);

    }

    catch (NamingException e) {

      throw new SERVICELOCATORException("A JNDI Naming exception has occurred " +

                                        " in SERVICELOCATOR.getDBConn()", e);

    }

    catch (Exception e) {

      throw new SERVICELOCATORException("An exception has occurred " +

                                        " in SERVICELOCATOR.getDBConn()", e);

    }

  }

 

}

 

 

异常处理类:

 

package com.learn;

public class SERVICELOCATORException extends DataAccessException{

  public SERVICELOCATORException(String pExceptionMsg){

    super(pExceptionMsg);

  }

 

  public SERVICELOCATORException(String pExceptionMsg, Throwable pException){

    super(pExceptionMsg, pException);

  }

 

}

 

 

参考书籍:

        《实用J2EE设计模式编程指南》

              WEB服务精髓》

         J2EE BEA WEBLOGIC SERVER

 

 

请大家指教:lisong@anyi.com.cn

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击