oke(myClass,null);
return local.getLocalHome(jndiHomeName,className);
}
//取得remote home
private Object getRemoteHome(String jndiHomeName,Class className) throws Exception
{
Class myClass = Class.forName(Resource.RemoteClass);
// Resource.RemoteClass =”testejb.util.common.RemoteServerLocator”
Method method = myClass.getMethod(Resource.RemoteConstractMethod,null);
// Resource.RemoteConstractMethod=” getInstance”
RemoteServerLocator remote = null;
remote = (RemoteServerLocator)method.invoke(myClass,null);
return remote.getHome(jndiHomeName,className);
}
private ServerLocatorAdapter() {
// 为cache提供线程安全的保证
cache = Collections.synchronizedMap(new HashMap());
}
}
运用反射实现ejb动态委派(2)
时间:2011-01-08
其中Resource为资源类,其中通过对配置文件的读取,取得一些指定的配置信息。
RemoteServerLocator和LocalServerLocator是两个根据不同的调用方式取得home借口的具体实现类,代码如下:
LocalServerLocator:
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.util.*;
import javax.ejb.*;
public class LocalServerLocator {
private Context ic;
private Map cache;//缓存home
private static LocalServerLocator me;
public static LocalServerLocator getInstance()
{
if(me == null)
{
try
{
me = new LocalServerLocator();
}
catch(Exception e)
{
System.err.println(e.getCause());
System.err.println(e.getMessage());
}
}
return me;
}
public EJBLocalHome getLocalHome(String jndiHomeName, Class className) throws Exception {
EJBLocalHome home = null;
try {
if (cache.containsKey(jndiHomeName)) {
home = (EJBLocalHome) cache.get(jndiHomeName);
} else {
Object objref = ic.lookup(jndiHomeName);
home = (EJBLocalHome) objref;
cache.put(jndiHomeName, home);
}
} catch (NamingException ne) {
System.err.println(jndiHomeName);
throw ne;
} catch (Exception e) {
throw e;
}
return home;
}
private LocalServerLocator() throws Exception{
try
{
ic = new InitialContext();
// 为cache提供线程安全的保证
cache = Collections.synchronizedMap(new HashMap());
}
catch(NamingException ne)
{
throw ne;
}
catch(Exception e)
{
throw e;
}
}
}
RemoteServerLocator
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.util.*;
import javax.ejb.*;
pu
|