Achieved with the model name to visit SERVICE LOCATOR

Achieved with the model name to visit SERVICE LOCATOR

In the B / S development, we often use the name of the service, such as JNDI, XMLNS, etc.. Name of service varies with different manufacturers. Need to get the name of each service, the need to appropriate the name of environmental information, and then find the service, repeat the costly search.

In addition, the persistence framework, required to access all the services are packaged into objects, developers do not need to know the name of the service behind the platform (database) type, and any security information or address. Between a large-scale software products in multiple EJB and connect multiple data sources (we have only one data source written death WEBGL) need a class to implement a unified access management.

Therefore, it requires us to isolate these visits packages. Then we can use the SERVICE LOCATOR mode.

We will use a file naming service.properties to manage all services. Example code implements EJB local, data sources.

The following format:

DEFAULTDS = webgl

NTCLASSREF = NTHome.class

Save it in the CLASSPATH reference path.

Source:

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

)

)

/ **

* Use singleton. Mode static object called multiple times to save resources * /

public static ServiceLocator getInstance () (

return serviceLocatorRef;

)

/ *

* By the keys were keys, usually in the data source, XMLNS use this method to access

* /

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 local class reference

*************************************************/

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;

)

/************************************************* ***********************

* Get EJBHome object

************************************************** *********************/

public EJBHome getEJBHome (String serviceId) throws ServiceLocatorException (

EJBHome ejbHome = null;

try (

/ / First check the cache if there EJBHome Interface

if (ejbHomeCache.containsKey (serviceId)) (

ejbHome = (EJBHome) ejbHomeCache.get (serviceId);

return ejbHome;

)

else (

/ / If not exist, the coexistence of the cache analysis

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

)

)

/ *

* Get JNDI data source

* /

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

)

)
)

Exception class:

package com.learn;

public class ServiceLocatorException extends DataAccessException (

public ServiceLocatorException (String pExceptionMsg) (

super (pExceptionMsg);

)

public ServiceLocatorException (String pExceptionMsg, Throwable pException) (

super (pExceptionMsg, pException);

)

)

分类:Java 时间:2010-08-27 人气:149
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.980 (s). 12 q(s)