帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > JAVA编程
用SERVICE LOCATOR 模式实现命名访问
作者:未知 发布时间:2005-03-12 来源:JSP天空网
用SERVICE LOCATOR 模式实现命名访问

--------------------------------------------------------------------------------



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



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



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

}



}
  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·谈谈JDBC
·JDBC专题介绍
·JDBC接口技术
·利用weblogic的POOL(连接池)连接
·Jsp中调用Oracle存储过程的小例子
·JSP数据库操作例程
·JSP数据库连接大全
·用连接池提高Servlet访问数据库的效
·一种简单JDBC连接池的实现
·数据库连接池Java实现小结
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统