一个ServiceLocator模式的实现

 废话不说,代码说话:
None.gif import  javax.naming. * ;
None.gif
import  javax.naming.NamingException;
None.gif
import  javax.rmi.PortableRemoteObject;
None.gif
import  javax.ejb.EJBHome;
None.gif
import  javax.ejb.EJBLocalHome;
None.gif
import  javax.sql.DataSource;
None.gif
import  java.util. * ;
None.gif
import  java.sql. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif *  实现 service locater 模式,用于由客户端来调用以通过JNDI查
InBlock.gif *  找相关的 ejb或是其它服务的入口.
ExpandedBlockEnd.gif * 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   final   class  ServiceLocater  dot.gif {
InBlock.gif
InBlock.gif  
protected static ServiceLocater inst = new ServiceLocater();
InBlock.gif  
private InitialContext ic = null;
InBlock.gif  
private Map ejbHomeCache = null;
InBlock.gif  
private Map dataSourceCache = null;
ExpandedSubBlockStart.gifContractedSubBlock.gif  
protected ServiceLocater() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
try dot.gif{
InBlock.gif      dataSourceCache 
= Collections.synchronizedMap(new HashMap());
InBlock.gif      ejbHomeCache 
= Collections.synchronizedMap(new HashMap());
InBlock.gif      ic 
= new InitialContext();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
catch (Exception e) dot.gif{
InBlock.gif      e.printStackTrace();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/** *//**
InBlock.gif   * 取得 servicelocater的单子实例.
ExpandedSubBlockEnd.gif   * 
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif  
synchronized public static ServiceLocater getInstance() dot.gif{
InBlock.gif    
return inst;
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/** *//**
InBlock.gif   *查找并返回一个数据源
InBlock.gif   * 
@param name String 数据源名称
InBlock.gif   * 
@return DataSource ,查找不到则抛出异常.
InBlock.gif   * 
@throws NamingException ,查找不到或是类型不对。
ExpandedSubBlockEnd.gif   * 
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif  
private DataSource lookUpDataSource(String name) throws NamingException dot.gif{
InBlock.gif    DataSource tmpDS 
= (DataSource)this.dataSourceCache.get(name);
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (tmpDS == nulldot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
try dot.gif{
InBlock.gif        tmpDS 
= (DataSource)this.ic.lookup(name);
InBlock.gif        
this.dataSourceCache.put(name, tmpDS);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gifContractedSubBlock.gif      
catch (NamingException namE) dot.gif{
InBlock.gif        
throw namE;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gifContractedSubBlock.gif      
catch (Exception otherE) dot.gif{
InBlock.gif        
throw new NamingException(otherE.getMessage());
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return tmpDS;
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/** *//**
InBlock.gif   * 查找并返回一个远程接口
InBlock.gif   * 
@param jndiHomeName ebj名字
InBlock.gif   * 
@param className  ejb类名字
InBlock.gif   * 
@return
InBlock.gif   * 
@throws ServiceLocatorException
ExpandedSubBlockEnd.gif   
*/

InBlock.gif  
public EJBHome getRemoteHome(String jndiHomeName, Class className) throws
ExpandedSubBlockStart.gifContractedSubBlock.gif      ServiceLocatorException 
dot.gif{
InBlock.gif    EJBHome home 
= (EJBHome)this.ejbHomeCache.get(jndiHomeName);
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (home == nulldot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
try dot.gif{
InBlock.gif        Object objref 
= ic.lookup(jndiHomeName);
InBlock.gif        Object obj 
= PortableRemoteObject.narrow(objref, className);
InBlock.gif        home 
= (EJBHome) obj;
InBlock.gif        
this.ejbHomeCache.put(jndiHomeName, home);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gifContractedSubBlock.gif      
catch (NamingException ne) dot.gif{
InBlock.gif        
throw new ServiceLocatorException(ne);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gifContractedSubBlock.gif      
catch (Exception e) dot.gif{
InBlock.gif        
throw new ServiceLocatorException(e);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return home;
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/** *//**
InBlock.gif   * 查找并返回一个本地接口
InBlock.gif   * 
@param jndiHomeName  jndiHomeName名字
InBlock.gif   * 
@return 一个本地接口
InBlock.gif   * 
@throws ServiceLocatorException
ExpandedSubBlockEnd.gif   
*/

InBlock.gif  
public EJBLocalHome getLocalHome(String jndiHomeName) throws
ExpandedSubBlockStart.gifContractedSubBlock.gif      ServiceLocatorException 
dot.gif{
InBlock.gif    EJBLocalHome home 
= null;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
try dot.gif{
InBlock.gif      home 
= (EJBLocalHome) ic.lookup(jndiHomeName);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
catch (NamingException ne) dot.gif{
InBlock.gif      
throw new ServiceLocatorException(ne);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
catch (Exception e) dot.gif{
InBlock.gif      
throw new ServiceLocatorException(e);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return home;
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/** *//**
InBlock.gif   *查找一个数据源,并取得一个连接.
InBlock.gif   * 
@param name String 数据源名称
InBlock.gif   * 
@return DataSource ,查找不到则抛出异常.
InBlock.gif   * 
@throws NamingException ,查找不到或是类型不对。
ExpandedSubBlockEnd.gif   * 
*/

InBlock.gif  
public Connection getConnection(String DataSourceJNDIName) throws
ExpandedSubBlockStart.gifContractedSubBlock.gif      SQLException 
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
try dot.gif{
InBlock.gif      Connection conn 
= this.lookUpDataSource(DataSourceJNDIName).getConnection();
InBlock.gif      conn.setAutoCommit(
false);
InBlock.gif      
//conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
InBlock.gif
      return conn;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
catch (Exception e) dot.gif{
InBlock.gif      e.printStackTrace();
InBlock.gif      
throw new SQLException(e.getMessage());
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值