import java.lang.reflect.*;
import java.rmi.*;
import javax.ejb.*;
import javax.naming.*;
import com.gdpost.isp.lis.util.exception.*;
import com.gdpost.isp.lis.util.logging.*;
import com.gdpost.isp.lis.util.naming.*;
/**
* <p>Title: stateless sessionBean的动态代理。这是一个单实例类,
* 只能通过getInstance方法获得这个实例。</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author wuwei
* @version 1.0
*/
public class DynamicEJBProxy {
// 私有构造函数,防止从外部创建实例
private DynamicEJBProxy () {}
private static DynamicEJBProxy singleton = new DynamicEJBProxy ();
/**
* 得到唯一的实例
* @return 本类唯一的一个实例
*/
public static DynamicEJBProxy getInstance () {
return singleton;
}
/**
* 此方法用来呼叫指定的stateless sessionBean的指定的业务方法。
* 实现过程为:
* <br> 1> 根据jndiName和homeInterface找到home接口
* <br> 2> 呼叫home接口的create方法,返回remote接口
* <br> 3> 呼叫remote接口的业务方法,保存调用结果
* <br> 4> 呼叫remote接口的remove方法
* <br> 5> 返回第3步的调用接过
* @param jndiName sessionBean的JNDI名字
* @param homeInterface sessionBean的home接口类型
* @param methodName sessionBean的业务方法名字
* @param args sessionBean的业务方法参数
* @return sessionBean的业务方法返回结果
* @throws UserException 被呼叫的sessionBean的业务方法抛出的业务异常
*/
public Object invoke (String jndiName,
Class homeInterface,
String methodName,
Object[] args)
throws UserException {
try {
// 得到home stub
EJBHome homestub =
NamingLocator.getInstance().lookup (
jndiName, homeInterface);
// 调用home stub上的create方法,得到remote stub
Object remotestub = homestub.getClass ().getMethod (
"create", null).invoke (homestub, null);
// 得到remote stub上的要调用的业务方法的参数类型
Class[] paramTypes = new Class[args.length];
for (int i = 0; i < args.length; ++i) {
paramTypes[i] = args[i].getClass ();
}
// 调用remote stub上的业务方法
Object returnValue = remotestub.getClass ().getMethod (
methodName, paramTypes).invoke (remotestub, args);
// 调用remote stub上的remove方法
remotestub.getClass ().getMethod (
"remove", null).invoke (remotestub, null);
// 返回调用结果
return returnValue;
}
catch (NamingException e) {
LoggingUtil.loggingExceptionInLIS (e);
throw new UserException ("399490000", 3, 6, "", "查询jndi出现异常");
}
catch (NoSuchMethodException e) {
LoggingUtil.loggingExceptionInLIS (e);
throw new UserException ("399490000", 3, 6, "", "ejb没有"+methodName+"方法");
}
catch (IllegalAccessException e) {
LoggingUtil.loggingExceptionInLIS (e);
throw new UserException ("399490000", 3, 6, "", e.getMessage());
}
catch (InvocationTargetException e) {
// 得到sessionBean的create方法、业务方法以及remove方法抛出的异常
Throwable cause = e.getTargetException();
LoggingUtil.loggingExceptionInLIS (cause);
// 判断异常的类型
if (cause instanceof CreateException) {
throw new UserException ("399490000", 3, 6, "",
"ejb抛出CreateException");
}
else if (cause instanceof RemoveException) {
throw new UserException ("399490000", 3, 6, "",
"ejb抛出RemoveException");
}
else if (cause instanceof RemoteException) {
throw new UserException ("399490000", 3, 6, "",
"ejb抛出RemoteException");
}
else if (cause instanceof UserException) {
// 真正的业务异常
throw (UserException) cause;
}
else {
// 未知类型的异常
throw new UserException ("399490000", 3, 6, "",
"ejb抛出未知类型的异常");
}
}
}
}
|