ejbhomefactory.java
来自「J2EE 1.4程序设计教程图书配套的源代码」· Java 代码 · 共 45 行
JAVA
45 行
package com.ibm.ejb;
import java.util.Map;
import javax.ejb.EJBHome;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBHomeFactory {
private static EJBHomeFactory;
private Map homeInterfaces;
private Context context;
// This is private, and can't be instantiated directly
private EJBHomeFactory() throws NamingException {
homeInterfaces = new HashMap();
// Get the context for caching purposes
context = new InitialContext();
/**
* In non-J2EE applications, you might need to load up
* a properties file and get this context manually. I've
* kept this simple for demonstration purposes.
*/
}
public static EJBHomeFactory getInstance() throws NamingException {
// Not completely thread-safe, but good enough
// (see note in article)
if (instance == null) {
instance = new EJBHomeFactory();
}
return instance;
}
public EJBHome lookup(String jndiName, Class homeInterfaceClass)
throws NamingException {
// 查看是否缓存了这个接口
EJBHome homeInterface = (EJBHome)homeInterfaces.get(homeClass);
// 如果没有的话,则使用给出的JNDI名字进行查找
if (homeInterface == null) {
Object obj = context.lookup(jndiName);
homeInterface =
(EJBHome)PortableRemoteObject.narrow(obj, homeInterfaceClass);
// If this is a new ref, save for caching purposes
homeInterfaces.put(homeInterfaceClass, homeInterface);
}
return homeInterface;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?