用SERVICE LOCATOR 模式实现命名访问
来源:互联网 发布:赣南脐橙淘宝宣传语 编辑:程序博客网 时间:2024/05/27 21:49
在B/S开发中, 我们经常要用到名称服务,如JNDI,XMLNS等。名称服务随不同厂家而不同。每次需要获得名称服务时,需要适当的名称环境信息,然后查找服务,重复查找的成本很高。
此外,在持久性框架中,要求将所有的服务访问都包装到对象中,开发人员不需要知道名称服务后面的平台(数据库)类型,及任何安全信息或地址。在一个大型软件产品间存在多个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;
private static SERVICELOCATOR SERVICELOCATORRef = null;
private static Hashtable ejbHomeCache = null;
private static Hashtable dataSourceCache = null;
private static Properties SERVICECache = null;
static {
SERVICELOCATORRef = new 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() {
}
/*
* 由键获得键值,一般在数据源,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
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
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>- 用SERVICE LOCATOR 模式实现命名访问
- 用SERVICE LOCATOR 模式实现命名访问
- 用SERVICE LOCATOR 模式实现访问命名服务
- 用SERVICE LOCATOR 模式实现访问命名服务
- 服务定位器模式(service locator)
- Service Locator 模式 的学习
- Java Service Locator Pattern(服务器定位模式)
- 设计模式【服务器定位模式Service Locator Pattern】
- 【设计模式】服务定位器模式(Service Locator Pattern)
- Microsoft实现的IOC DI之 Unity 、Service Locator、MEF
- Common Service Locator library
- Common Service Locator
- Common Service Locator library
- Service Locator Pattern
- Service Locator Design Pattern
- Service Locator Patten 总结
- Service Locator服务定位器
- 设计模式学习—服务定位模式(Service Locator Design Pattern)
- 正则表达式语法
- Jbuilder Practice : “项目名.项目类型.local”的用
- Java-XML数据绑定工具大比拼
- Windows Server 2003 下"该内存不能为written"的解决方案
- 2个问题 Array and SQL
- 用SERVICE LOCATOR 模式实现命名访问
- 在JAVA中使用正则表达式
- 通过 Eclipse 发挥 XML 的威力 (2/2)
- Forte for Java开发指南
- Servlet及JSP中的多线程同步问题
- using struts in dreamweaver UltraDev 4.0
- 域初始化、静态块及构造方法等在创建类实例时的执行顺
- js向下拉列表的末尾添加一个 "nami" 选项
- JBuilder9+Weblogic7实战篇之工具篇(Weblogic)