一个ServiceLocator模式的实现

简介:

废话不说,代码说话:

None.gif import javax.naming.*;
None.gif import javax.naming.NamingException;
None.gif import javax.rmi.PortableRemoteObject;
None.gif import javax.ejb.EJBHome;
None.gif import javax.ejb.EJBLocalHome;
None.gif import javax.sql.DataSource;
None.gif import java.util.*;
None.gif import java.sql.*;
None.gif
ExpandedBlockStart.gif /**
InBlock.gif *  实现 service locater 模式,用于由客户端来调用以通过JNDI查
InBlock.gif *  找相关的 ejb或是其它服务的入口.
ExpandedBlockEnd.gif * 
*/

ExpandedBlockStart.gif public  final  class ServiceLocater  {
InBlock.gif
InBlock.gif  protected static ServiceLocater inst = new ServiceLocater();
InBlock.gif  private InitialContext ic = null;
InBlock.gif  private Map ejbHomeCache = null;
InBlock.gif  private Map dataSourceCache = null;
ExpandedSubBlockStart.gif  protected ServiceLocater() {
ExpandedSubBlockStart.gif    try {
InBlock.gif      dataSourceCache = Collections.synchronizedMap(new HashMap());
InBlock.gif      ejbHomeCache = Collections.synchronizedMap(new HashMap());
InBlock.gif      ic = new InitialContext();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gif    catch (Exception e) {
InBlock.gif      e.printStackTrace();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gif  /**
InBlock.gif   * 取得 servicelocater的单子实例.
ExpandedSubBlockEnd.gif   * 
*/

ExpandedSubBlockStart.gif  synchronized public static ServiceLocater getInstance() {
InBlock.gif    return inst;
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gif  /**
InBlock.gif   *查找并返回一个数据源
InBlock.gif   * 
@param name String 数据源名称
InBlock.gif   * 
@return DataSource ,查找不到则抛出异常.
InBlock.gif   * 
@throws NamingException ,查找不到或是类型不对。
ExpandedSubBlockEnd.gif   * 
*/

ExpandedSubBlockStart.gif  private DataSource lookUpDataSource(String name) throws NamingException {
InBlock.gif    DataSource tmpDS = (DataSource)this.dataSourceCache.get(name);
ExpandedSubBlockStart.gif    if (tmpDS == null{
ExpandedSubBlockStart.gif      try {
InBlock.gif        tmpDS = (DataSource)this.ic.lookup(name);
InBlock.gif        this.dataSourceCache.put(name, tmpDS);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gif      catch (NamingException namE) {
InBlock.gif        throw namE;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gif      catch (Exception otherE) {
InBlock.gif        throw new NamingException(otherE.getMessage());
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif    return tmpDS;
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gif  /**
InBlock.gif   * 查找并返回一个远程接口
InBlock.gif   * 
@param jndiHomeName ebj名字
InBlock.gif   * 
@param className  ejb类名字
InBlock.gif   * 
@return
InBlock.gif   * 
@throws ServiceLocatorException
ExpandedSubBlockEnd.gif   
*/

InBlock.gif  public EJBHome getRemoteHome(String jndiHomeName, Class className) throws
ExpandedSubBlockStart.gif      ServiceLocatorException {
InBlock.gif    EJBHome home = (EJBHome)this.ejbHomeCache.get(jndiHomeName);
ExpandedSubBlockStart.gif    if (home == null{
ExpandedSubBlockStart.gif      try {
InBlock.gif        Object objref = ic.lookup(jndiHomeName);
InBlock.gif        Object obj = PortableRemoteObject.narrow(objref, className);
InBlock.gif        home = (EJBHome) obj;
InBlock.gif        this.ejbHomeCache.put(jndiHomeName, home);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gif      catch (NamingException ne) {
InBlock.gif        throw new ServiceLocatorException(ne);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gif      catch (Exception e) {
InBlock.gif        throw new ServiceLocatorException(e);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif    return home;
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gif  /**
InBlock.gif   * 查找并返回一个本地接口
InBlock.gif   * 
@param jndiHomeName  jndiHomeName名字
InBlock.gif   * 
@return 一个本地接口
InBlock.gif   * 
@throws ServiceLocatorException
ExpandedSubBlockEnd.gif   
*/

InBlock.gif  public EJBLocalHome getLocalHome(String jndiHomeName) throws
ExpandedSubBlockStart.gif      ServiceLocatorException {
InBlock.gif    EJBLocalHome home = null;
ExpandedSubBlockStart.gif    try {
InBlock.gif      home = (EJBLocalHome) ic.lookup(jndiHomeName);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gif    catch (NamingException ne) {
InBlock.gif      throw new ServiceLocatorException(ne);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gif    catch (Exception e) {
InBlock.gif      throw new ServiceLocatorException(e);
ExpandedSubBlockEnd.gif    }

InBlock.gif    return home;
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gif  /**
InBlock.gif   *查找一个数据源,并取得一个连接.
InBlock.gif   * 
@param name String 数据源名称
InBlock.gif   * 
@return DataSource ,查找不到则抛出异常.
InBlock.gif   * 
@throws NamingException ,查找不到或是类型不对。
ExpandedSubBlockEnd.gif   * 
*/

InBlock.gif  public Connection getConnection(String DataSourceJNDIName) throws
ExpandedSubBlockStart.gif      SQLException {
ExpandedSubBlockStart.gif    try {
InBlock.gif      Connection conn = this.lookUpDataSource(DataSourceJNDIName).getConnection();
InBlock.gif      conn.setAutoCommit(false);
InBlock.gif      //conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
InBlock.gif
      return conn;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gif    catch (Exception e) {
InBlock.gif      e.printStackTrace();
InBlock.gif      throw new SQLException(e.getMessage());
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedBlockEnd.gif}
文章转自庄周梦蝶  ,原文发布5.16
目录
相关文章
|
4天前
|
设计模式 运维 安全
边车模式的介绍
边车模式的介绍
10 0
|
2月前
|
设计模式 算法 编译器
【C/C++ PIMPL模式 】 深入探索C++中的PIMPL模式
【C/C++ PIMPL模式 】 深入探索C++中的PIMPL模式
53 0
|
2月前
一般模式
【2月更文挑战第20天】一般模式。
10 1
|
前端开发 JavaScript Java
MVX模式是什么?
MVX模式是什么?
225 0
|
分布式计算 自然语言处理 并行计算
运用Aggregator模式实现MapReduc
运用Aggregator模式实现MapReduc
运用Aggregator模式实现MapReduc
|
C语言
模式
模式
115 0
|
前端开发 JavaScript 编译器
模式二之框架模式
模式二之框架模式
|
并行计算 搜索推荐 算法