📄 servicelocator.java
字号:
package shopping.util;
import java.net.*;
import java.util.*;
import javax.ejb.*;
import javax.jms.*;
import java.rmi.RemoteException;
import javax.naming.*;
import javax.rmi.*;
import javax.sql.*;
import javax.transaction.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* <p>Title: KnowledgeManagement</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2009</p>
*
* <p>Company: GZ-Giant</p>
*
* @author Alan
* @version 2.0
*/
public class ServiceLocator {
private InitialContext ic;
private Map cache = Collections.synchronizedMap(new HashMap());
private static ServiceLocator instance = new ServiceLocator();
public static ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext-ejb-config.xml");
/**
* 获取类实例
* @return ServiceLocator
*/
public static ServiceLocator getInstance() {
return instance;
}
/**
* 类初始化
* @throws ServiceLocatorException
*/
private ServiceLocator() throws ServiceLocatorException {
try {
ic = new InitialContext();
}
catch (Exception e) {
throw new ServiceLocatorException(e);
}
}
/**
* 根据数据源名称获取数据源
* @param dataSourceName String
* @return DataSource
* @throws ServiceLocatorException
*/
public DataSource getDataSource(String dataSourceName) throws ServiceLocatorException {
DataSource dataSource = (DataSource) cache.get(dataSourceName);
if (dataSource == null) {
try {
dataSource = (DataSource) ic.lookup(dataSourceName);
cache.put(dataSourceName, dataSource);
}
catch (Exception e) {
throw new ServiceLocatorException(e);
}
}
return dataSource;
}
/**
* 获取数据源
* @param dataSourceName String
* @param callerObj Object
* @return DataSource
* @throws ServiceLocatorException
*/
public DataSource getDataSource(String dataSourceName, Object callerObj) throws
ServiceLocatorException {
DataSource dataSource = (DataSource) cache.get(dataSourceName);
if (dataSource == null) {
try {
System.out.println(dataSourceName+dataSourceName);
dataSource = (DataSource) ic.lookup(dataSourceName);
cache.put(dataSourceName, dataSource);
}
catch (Exception e) {
throw new ServiceLocatorException(e);
}
}
return dataSource;
}
/**
*
* @param envName String
* @return URL
* @throws ServiceLocatorException
*/
public URL getUrl(String envName) throws ServiceLocatorException {
try {
return (URL) ic.lookup(envName);
}
catch (Exception e) {
throw new ServiceLocatorException(e);
}
}
/**
* @return the boolean value corresponding
* to the env entry such as SEND_CONFIRMATION_MAIL property.
*/
public boolean getBoolean(String envName) throws ServiceLocatorException {
try {
return ( (Boolean) ic.lookup(envName)).booleanValue();
}
catch (Exception e) {
throw new ServiceLocatorException(e);
}
}
/**
* @return the String value corresponding
* to the env entry name.
*/
public String getString(String envName) throws ServiceLocatorException {
try {
return (String) ic.lookup(envName);
}
catch (Exception e) {
throw new ServiceLocatorException(e);
}
}
/**
* getCatchList
*
* @param jndiName String
* @return Map
*/
public Map getCatchList(String jndiName) {
Map catchListMap = null;
try {
catchListMap = (Map) ic.lookup(jndiName);
}
catch (NamingException ex) {
//Debug.print("\u00CE\u00B4\u00C4\u00DC\u00BB\u00F1\u00C8\u00A1\u00BB\u00BA\u00B4\u00E6\u00C1\u00D0±í" + jndiName + "\u00A3\u00BA" + ex.toString(), this);
}
return catchListMap;
}
/**
* setCatchList
*
* @param jndiName String
* @param catchListMap Map
* @return boolean
*/
public boolean setCatchList(String jndiName, Map catchListMap) {
try {
ic.rebind(jndiName, catchListMap);
}catch (NamingException ex) {
//Debug.print("绑定缓存列表时出错" + ex.toString(), this);
return false;
}
return true;
}
public EJBHome getRemoteHome(String jndiHomeName, Class className) throws ServiceLocatorException {
EJBHome home = (EJBHome) cache.get(jndiHomeName);
if (home == null) {
try {
Object objref = ic.lookup(jndiHomeName);
Object obj = PortableRemoteObject.narrow(objref, className);
home = (EJBHome) obj;
cache.put(jndiHomeName, home);
}
catch (Exception e) {
throw new ServiceLocatorException(e);
}
}
return home;
}
public Object getEJB(String name){
ServiceConfigBean configBean = (ServiceConfigBean)ctx.getBean(name);
return configBean.getEjb();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -