📄 ejblocator.java
字号:
package gov.gdlt.ssgly.taxcore.comm.servicelocator;
import java.util.Map;
import java.util.HashMap;
import java.util.Properties;
import javax.ejb.EJBLocalHome;
import javax.ejb.EJBHome;
import javax.rmi.PortableRemoteObject;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import gov.gdlt.ssgly.taxcore.comm.log.LogWritter;
import gov.gdlt.ssgly.taxcore.comm.config.ApplicationContext;
/**
* Service Locator模式的实现,在此提供给客户端定位后台EJB的方法。
*
* <p>Title: EJBLocator</p>
* <p>Description: 税收管理员系统</p>
* <p>Copyright: Copyright (c) 2005 广州市地方税务局</p>
* <p>Company: 信息中心</p>
* @author Lemon
* @version 1.0
* @since 2005.8.8.
*/
public class EJBLocator {
private static EJBLocator instance;
private Map homeInterfaces;
private Context context;
// This is private, and can't be instantiated directly
private void initRemote() throws NamingException {
homeInterfaces = new HashMap();
// Get the context for caching purposes
String url = ApplicationContext.singleton().getValueAsString(ApplicationContext.EJB_PROVIDER_URL);
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
ApplicationContext.WEBLOGIC_INITIAL_CONTEXT_FACTORY);
properties.put(Context.PROVIDER_URL, url);
context = new InitialContext(properties);
} catch (Exception e) {
LogWritter.sysError("Unable to connect to WebLogic server at " +
url +
"Please make sure that the server is running.",
e);
//throw e;
}
}
private void initLocal() throws NamingException {
homeInterfaces = new HashMap();
// Get the context for caching purposes
context = new InitialContext();
}
private EJBLocator() {
}
public static EJBLocator getInstance() throws NamingException {
if (instance == null) {
instance = new EJBLocator();
instance.initLocal();
}
return instance;
}
public static EJBLocator getRemoteInstance() throws NamingException {
if (instance == null) {
instance = new EJBLocator();
instance.initRemote();
}
return instance;
}
public EJBLocalHome getLocalHome(String jndiName, Class localHomeClass) throws
NamingException {
// See if we already have this interface cached
EJBLocalHome localHome = (EJBLocalHome) homeInterfaces.get(
localHomeClass);
// If not, look up with the supplied JNDI name
if (localHome == null) {
localHome = (EJBLocalHome) context.lookup(jndiName);
// If this is a new ref, save for caching purposes
homeInterfaces.put(localHomeClass, localHome);
}
return localHome;
}
public EJBHome getRemoteHome(String jndiName, Class homeClass) throws
NamingException {
// See if we already have this interface cached
EJBHome remoteHome = (EJBHome) homeInterfaces.get(
homeClass);
// If not, look up with the supplied JNDI name
if (remoteHome == null) {
remoteHome = (EJBHome) context.lookup(jndiName);
Object objref = context.lookup(jndiName);
remoteHome = (EJBHome) PortableRemoteObject.narrow(objref,
homeClass);
// If this is a new ref, save for caching purposes
homeInterfaces.put(homeClass, remoteHome);
}
return remoteHome;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -