📄 servicelocator.java
字号:
package com.ebusiness.ebank.servicedelegate;/** * <p>Title: </p> * <p>Description: This class provides the center service for possible JNDI lookup * and stores the objects that is successfully located into HashMap for re-use * * It might be used by remote clients, so avoid using log4j</p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: eBusiness Inc., All right reserved</p> * @author unascribed * @version 1.0 */import java.util.HashMap;import java.util.Map;import java.util.Collections;import javax.ejb.EJBHome;import javax.ejb.EJBLocalHome;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.rmi.PortableRemoteObject;import javax.sql.DataSource;import com.ebusiness.ebank.exception.ErrorMessages;public class ServiceLocator{ private InitialContext ic; private Map cache; //used to hold references to Resources for re-use private static ServiceLocator me; static { me = new ServiceLocator(); } private ServiceLocator() { try { ic = new InitialContext(); cache = Collections.synchronizedMap(new HashMap()); System.out.println("\nServiceLocator was successfully instantiated."); } catch (NamingException ne) { System.out.println("\n" + ErrorMessages.FAIL_TO_INSTANTIATE_SERVICELOCATOR); } } public static ServiceLocator getInstance() { return me; } //After calling this method, you should cast EJBLocalHome to your concrete local home object public EJBLocalHome getLocalHome(String localHomeName) throws ServiceLocatorException { EJBLocalHome home = null; try { if (cache.containsKey(localHomeName)) { home = (EJBLocalHome) cache.get(localHomeName); } else { home = (EJBLocalHome) ic.lookup(localHomeName); cache.put(localHomeName, home); } } catch (NamingException ne) { cache.put(localHomeName, null); //The codes usually try local home first. If it fails, then try remote home. throw new ServiceLocatorException(ErrorMessages.FAIL_TO_LOOKUP_LOCALHOME + ": " + localHomeName + ".", ne); } return home; } //After calling this method, you should cast EJBHome to your concrete remote home object public EJBHome getRemoteHome(String remoteHomeName, Class className) throws ServiceLocatorException { EJBHome home = null; try { if (cache.containsKey(remoteHomeName)) { home = (EJBHome) cache.get(remoteHomeName); } else { Object objref = ic.lookup(remoteHomeName); Object obj = PortableRemoteObject.narrow(objref, className); home = (EJBHome)obj; cache.put(remoteHomeName, home); } } catch (NamingException ne) { throw new ServiceLocatorException(ErrorMessages.FAIL_TO_LOOKUP_REMOTEHOME + ": " + remoteHomeName + ". ", ne); } return home; } public DataSource getDataSource(String dataSourceName) throws ServiceLocatorException { DataSource dataSource = null; try { if (cache.containsKey(dataSourceName)) { dataSource = (DataSource) cache.get(dataSourceName); } else { dataSource = (DataSource)ic.lookup(dataSourceName); cache.put(dataSourceName, dataSource ); } } catch (NamingException ne) { throw new ServiceLocatorException(ErrorMessages.FAIL_TO_LOOKUP_DATASOURCE + ": " + dataSourceName + ". ", ne); } return dataSource; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -