国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

用SERVICE LOCATOR 模式實現命名訪問

2019-11-18 15:22:23
字體:
來源:轉載
供稿:網友

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

}

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陆川县| 马龙县| 惠州市| 呼和浩特市| 安泽县| 上蔡县| 楚雄市| 洞头县| 眉山市| 囊谦县| 遵化市| 五家渠市| 金乡县| 南澳县| 宣威市| 铅山县| 东平县| 晋宁县| 丹凤县| 湖州市| 阳东县| 江都市| 旬阳县| 安新县| 滨海县| 运城市| 建平县| 屏山县| 赤壁市| 怀集县| 辽源市| 陕西省| 西乡县| 潜江市| 德化县| 区。| 吴堡县| 大关县| 永登县| 江口县| 甘洛县|