Naming access with Service Locator mode

zhaozj2021-02-16  56

Naming access service with Service Locator mode

In B / S development, we often use the name service, such as JNDI, XMLns, etc. Name Services different from different manufacturers. Each time you need a name service, you need an appropriate name environment information, then look up the service, and the cost of repeated lookup is high.

In addition, in the persistence framework, all service access is required to be packaged into the object, and the developer does not need to know the platform (database) type behind the name service, and any security information or address. When multiple EJBs and multiple data source connections between a large software product (we currently have only one written data source webgl) requires a class to implement unified access management.

Therefore, this requires us to package these accesss. At this time we can use the Service Locator mode.

We will use a file service.properties to manage all naming services. The example code enables access to EJB local and data sources.

The format is as follows:

Defaultds = WebGL

NTClassRef = nthome.class

Save it in the path referenced by the classpath.

Source code:

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 service.

Private static hashtable ejbhomecache = null;

Private static hashtable datasourcecache = NULL;

Private static property 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 multiple call saves resources * /

Public static service () {) pUBLIC STATIC Service () {

Return ServiceLocatorRef;

}

/ *

* Get the key value by the key, generally in the data source, XMLNS access to this method

* /

Static Private String GetServiceName (String ServiceID) throws service {

String serviceName = null;

IF (ServiceCache.Containskey) {

ServiceName = (String) ServiceCache.get (ServiceID);

}

Else {

Throw new serviceelocatorexception

"Unable to Locate the Service Statement Requested";

}

Return ServiceName;

}

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

* EJB local class reference

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

Static Private Class getEjbhomeref (String ServiceID) Throws

ServiceLocatorException {

Class Homef = NULL;

IF (ServiceCache.Containskey) {

HOMEREF = (Class) ServiceCache.get (ServiceID);

}

Else {

Throw new serviceelocatorexception

"Unable to Locate the Service Statement Requested";

}

Return HOMEREF;

}

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

* Get EJBHOME objects

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

Public ejbhome getejbhome (String ServiceID) throws serviceLocatorException {

Ejbhome ejbhome = null;

Try {

// Check if the cache has an ejbhome interface

IF (ejbhomecache.containskey (serviceid)) {

ejbhome = (ejbhome) EJBHomeCache.get (ServiceID);

Return ejbhome;

}

Else {

// If there is no existence, resolve and save it to the cache

Context ctx = new initialContext ();

Object jndiref = ctx.lookup (serviceID);

Object portableobj = portableremoteObject.narrow (JNDIREF,

getjbhomeref (ServiceID);

Ejbhome = (ejbhome) PortableObj;

ejbhomecache.put (serviceid, ejbhome);

Return ejbhome;

}

}

Catch (namingexception e) {

Throw new serviceelocatorexception

"Naming Exception Error In Service ()", E);

}

}

/ *

* Get JNDI data sources

* /

Public Connection GetDbconn (String ServiceID) throwsserviceLocatorException {

Connection conn = NULL;

String serviceName = getServiceName (ServiceID);

Try {

/ * Checking to see if the recommended 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 ServiceLocatorXception ("An Exception Has Occurred"

"in serviceLocator.getdbconn ()", e);

}

}

}

Abnormal processing class:

Package com.learn;

Public Class ServiceLocatorException Extends DataAccessException {

Public serviceLocatorException (String pexceptionmsg) {

Super (PEXCEPTIONMSG);

}

Public ServiceLocatorException (String PexceptionMSG, Throwable PEXCEPTION) {

Super (PEXCEPTIONMSG, PEXCEPTION);

}

}

Reference book:

"Practical J2EE Design Mode Programming Guide"

"WEB service essence"

"J2EE and BEA WebLogic Server"

Please advise: lisong@nyi.com.cn

转载请注明原文地址:https://www.9cbs.com/read-23513.html

New Post(0)