📄 ejbinvoke.java
字号:
/**
* @作者: 胡俊
* @创建时间: 2008-03-05
* @更新时间: 2008-03-05
* @描述:
*
*/
package com.nari.pmos.proxy.common;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.ejb.EJBException;
import javax.ejb.EJBLocalHome;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* @author hujun
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class EjbInvoke {
private InvokeEnv invokeEnv = null;
private Properties _prop = null;
// 存放ejb的名称
private Map ejbLocalHomes = Collections.synchronizedMap(new HashMap());
private InitialContext initCtx = null; // 缓存上下文环境
/**
*
*/
public EjbInvoke() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param invokeEnv
* The invokeEnv to set.
*/
public void setInvokeEnv(InvokeEnv invokeEnv) {
this.invokeEnv = invokeEnv;
}
private void setContextProperties(String initContextFactory,
String providerUrl) {
_prop = new Properties();
_prop.put(Context.INITIAL_CONTEXT_FACTORY, initContextFactory);
_prop.put(Context.PROVIDER_URL, providerUrl);
}
private String ifName2jndiName(String iName) {
// 取interface名称的最后一节
int i = iName.lastIndexOf(".");
String jndiName = iName.substring(i + 1);
// 用后缀替代Proxy
jndiName = jndiName.replaceAll("Proxy", "Local");
// 加上前缀拼装成完整的名称
jndiName = invokeEnv.getJndiPrefix() + jndiName;
return jndiName;
}
/*
* 得到 ejb local home接口
*/
private EJBLocalHome getLocalHome(String beanJndiLookupName)
throws EJBException {
try {
// 20070422 缓存上下文环境
// InitialContext ctx = null;
System.out.println("!!! BeanName: " + beanJndiLookupName
+ " !!!");
if (initCtx == null) {
if (invokeEnv.getAccessMode().equals("reference")) {
// 本地引用
initCtx = new InitialContext();
} else if (invokeEnv.getAccessMode().equals("local")) {
// 特殊的本地查找办法
if (_prop == null)
setContextProperties(invokeEnv.getInitCtxFactory(),
invokeEnv.getProviderURL());
if (_prop != null)
initCtx = new InitialContext(_prop);
else
initCtx = new InitialContext();
} else {
// 远程调用不建议、不支持
}
}
EJBLocalHome obHome = (EJBLocalHome) initCtx
.lookup(beanJndiLookupName);
return obHome;
} catch (NamingException ne) {
initCtx = null;
throw new EJBException(ne);
}
}
/*
* 得到 ejb local 接口
*/
private Object getLocal(String beanJndiLookupName) {
try {
EJBLocalHome obHome = (EJBLocalHome) ejbLocalHomes
.get(beanJndiLookupName);
if (obHome == null) {
obHome = getLocalHome(beanJndiLookupName);
ejbLocalHomes.put(beanJndiLookupName, obHome);
}
// get the method of create
Method m = obHome.getClass().getDeclaredMethod("create",
new Class[0]);
// invoke the create method
Object obj = m.invoke(obHome, new Object[0]);
return obj;
} catch (NoSuchMethodException ne) {
System.out.println("--- getLocal NoSuchMethodException ---");
ne.printStackTrace();
// throw new EJBException (ne);
} catch (InvocationTargetException ie) {
System.out.println("--- getLocal InvocationTargetException ---");
ie.printStackTrace();
// throw new EJBException (ie);
} catch (IllegalAccessException iae) {
System.out.println("--- getLocal IllegalAccessException ---");
iae.printStackTrace();
// throw new EJBException (iae);
} catch (EJBException e) {
System.out.println("--- getLocal ??? getLocal ---");
e.printStackTrace();
}
return null;
};
public Object executeLocalMethod(String user, String pwd,
String interfaceName, String methodName, Class[] classes,
Object[] objects, String[] calsses_type)
throws ApplicationException {
boolean checkOK = false;
String s = "";
// 授权处理
// === 因jboss323无法正确转换 int double,所以作如下处理 ===
int n = classes.length;
int i = 0;
for (i = 0; i < n; i++) {
if (classes[i].getName().equals("java.lang.Integer")
&& calsses_type[i].equals("int"))
classes[i] = int.class;
else if (classes[i].getName().equals("java.lang.Double")
&& calsses_type[i].equals("double"))
classes[i] = double.class;
else if (classes[i].getName().equals("java.lang.Boolean")
&& calsses_type[i].equals("boolean"))
classes[i] = boolean.class;
}
String beanJndiLookupName = this.ifName2jndiName(interfaceName);
Object ejbObj = getLocal(beanJndiLookupName);
if (ejbObj == null) {
ApplicationException e = new ApplicationException(
beanJndiLookupName + " not be found");
throw e;
}
// s = jndiLocalName2ModuleName(beanJndiLookupName);
// checkOK = CheckUserFunction(user, pwd, s +"."+methodName);
if (this.invokeEnv.getCheckLevel().equals("0"))
checkOK = true; // 不作函数级的校验
if (checkOK) {
try {
Method m = ejbObj.getClass().getDeclaredMethod(methodName,
classes);
try {
Object r = m.invoke(ejbObj, objects);
// 日志处理
return r;
} catch (IllegalAccessException e) {
System.out
.println("--- checkOK IllegalAccessException ---");
e.printStackTrace();
} catch (InvocationTargetException e) {
System.out
.println("--- checkOK InvocationTargetException ---");
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
System.out
.println("--- 999 checkOK NoSuchMethodException begin ---");
e.printStackTrace();
}
} else {
ApplicationException e = new ApplicationException("[" + user + " "
+ s + "." + methodName
+ "not pass the authentication or authorization!!!");
throw e;
}
// 日志处理
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -