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

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章