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

)

)
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Achieved with the model name to visit SERVICE LOCATOR

  • The EJB3 Persistence

    EJB3 persistence with Hibernate is very similar to the mechanism: Environment: Server: JBOSS5.0 Database: MySQL5.0 1. Set up a data source First of all, in jboss-5.0.0.GA \ server \ default \ deploy, the establishment of a database used to connect the dat

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • can not be represented as java.sql.Timestamp

    Development of procedures for the use of hibernate when, some time there is no need to fill in the fields, but after the hibernate query time reported "Java.sql.SQLException: Value'0000-00-00 'can not be represented as java.sql.Timestamp ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Hibernate annotation using notebook

    These are the basic common @Entity --Declared an entity bean @Table(name="promotion_info") --For the entity bean mapping for the specified table (Table name ="promotion_info) @Id --Declare that the identifying attribute of the entity bean @GeneratedValue

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries