package com.tenwa.workflow; import com.amarsoft.are.ARE; import com.amarsoft.are.jbo.JBOException; import com.amarsoft.are.jbo.JBOFactory; import com.amarsoft.are.jbo.JBOTransaction; import com.amarsoft.are.lang.Element; import com.amarsoft.are.util.json.JSONDecoder; import com.amarsoft.are.util.json.JSONElement; import com.amarsoft.are.util.json.JSONException; import com.amarsoft.are.util.json.JSONObject; import com.amarsoft.awe.AWEException; import com.amarsoft.awe.util.IJavaMethodRight; import com.amarsoft.awe.util.JavaMethodArg; import com.amarsoft.awe.util.JavaMethodReturn; import com.amarsoft.context.ASUser; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import com.amarsoft.awe.util.Transaction; public class RewriteJavaMethod { public static final String SUCCESS_MSG = "SUCCESS"; public static final String ERROR_MSG = "ERROR"; public static JavaMethodReturn runTrans(String sClassName, String sMethod, String args, ASUser user,Map model){ JavaMethodReturn ret = new JavaMethodReturn(); JBOTransaction tx = null; try { tx = JBOFactory.createJBOTransaction(); ret = runMethod(sClassName, sMethod, args, tx, user,model); if (!ret.isOk()) tx.rollback(); else tx.commit(); } catch (AWEException e) { if (tx != null) try { tx.rollback(); } catch (JBOException e1) { e1.printStackTrace(); } ret.setCode(e.getMesssageNo()); ret.setResult(e.getMessage()); ARE.getLog().error(e.getMessage(), e); e.printStackTrace(); } catch (JBOException e1) { ret.setCode(e1.getErrorCode()); ret.setResult(e1.getErrorMessage()); ARE.getLog().error(e1.getMessage(), e1); e1.printStackTrace(); } return ret; } public static JavaMethodReturn runSqlca(String sClassName, String sMethod, String args, String sDataSource, ASUser user,Map model) { JavaMethodReturn ret = new JavaMethodReturn(); Transaction Sqlca = null; try { Sqlca = Transaction.createTransaction(sDataSource); ret = runMethod(sClassName, sMethod, args, Sqlca, user,model); if (!ret.isOk()) try { Sqlca.rollback(); Sqlca.disConnect(); Sqlca = null; } catch (JBOException e1) { e1.printStackTrace(); } } catch (AWEException e) { if (Sqlca != null) { try { Sqlca.rollback(); Sqlca.disConnect(); Sqlca = null; } catch (JBOException e1) { e1.printStackTrace(); } } ret.setCode(e.getMesssageNo()); ret.setResult(e.getMessage()); ARE.getLog().error(e.getMessage(), e); e.printStackTrace(); } finally { if (Sqlca != null) { try { Sqlca.commit(); Sqlca.disConnect(); Sqlca = null; } catch (JBOException e1) { e1.printStackTrace(); } } } return ret; } public static JavaMethodReturn run(String sClassName, String sMethod, String args, ASUser user,Map model) { JavaMethodReturn ret = new JavaMethodReturn(); try { ret = runMethod(sClassName, sMethod, args, null, user,model); } catch (AWEException e) { ret.setCode(e.getMesssageNo()); ret.setResult(e.getMessage()); ARE.getLog().error(e.getMessage(), e); e.printStackTrace(); } return ret; } public static JavaMethodReturn runMethod(String sClassName, String sMethod, String args, Object obj, ASUser user,Map model) throws AWEException { return runMethod(sClassName, sMethod, getArgJSONObject(args), obj, user,model); } public static JSONObject getArgJSONObject(String args) throws AWEException { try { if (args == null) return JSONObject.createObject(); if (args.startsWith("{")) { return JSONDecoder.decode(args); } JSONObject argJsonObject = JSONObject.createObject(); if (args.indexOf("=") != -1) { String[] as = args.split(","); for (int i = 0; i < as.length; i++) { String[] v = as[i].split("="); if (v.length > 1) { argJsonObject.add(JSONElement.valueOf(v[0].trim(), v[1].trim())); } } } return argJsonObject; } catch (JSONException e) { throw new AWEException(e); } } public static JavaMethodReturn runMethod(String sClassName, String sMethod, String[] keys, String[] vs, Object obj, ASUser user,Map model) throws AWEException { JSONObject args = JSONObject.createObject(); for (int i = 0; i < keys.length; i++) { args.add(JSONElement.valueOf(keys[i], vs[i])); } return runMethod(sClassName, sMethod, args, user,model); } public static JavaMethodReturn run(JavaMethodArg arg, ASUser user) throws Exception { JSONObject argJsonObject = JSONObject.createObject(); for (String key : arg.getArgs().keySet()) { argJsonObject.add(JSONElement.valueOf(key, arg.getArgs().get(key))); } return runMethod(arg.getClassName(), arg.getMethodName(), argJsonObject, user,null); } public static JavaMethodReturn runMethod(String sClassName, String sMethod, JSONObject args, ASUser user,Map model) throws AWEException { return runMethod(sClassName, sMethod, args, null, user,model); } public static JavaMethodReturn runMethod(String sClassName, String sMethodName, JSONObject args, Object obj, ASUser user,Map model) throws AWEException { JavaMethodReturn ret = new JavaMethodReturn(); try { Class c = Class.forName(sClassName); StringBuffer sbf = new StringBuffer("[Method] Java"); sbf.append(" ClassName"); sbf.append("["); sbf.append(sClassName); sbf.append("]"); sbf.append(" Method"); sbf.append("["); sbf.append(sMethodName); sbf.append("]"); Object object = c.newInstance(); Method m = null; Object o = null; Class[] parameterTypes = null; Object[] argObjs = null; int arglen = args.size(); if ((obj instanceof Transaction)) { parameterTypes = new Class[arglen + 1]; parameterTypes[arglen] = Transaction.class; argObjs = new Object[arglen + 1]; argObjs[arglen] = obj; } else if ((obj instanceof JBOTransaction)) { parameterTypes = new Class[arglen + 1]; parameterTypes[arglen] = JBOTransaction.class; argObjs = new Object[arglen + 1]; argObjs[arglen] = obj; } else { parameterTypes = new Class[arglen]; argObjs = new Object[arglen]; } for (int i = 0; i < arglen; i++) { JSONElement e = (JSONElement)args.get(i); parameterTypes[i] = e.getValue().getClass(); argObjs[i] = e.getValue(); } Method[] allmethods = c.getDeclaredMethods(); for (Method method : allmethods) { if ((method.getName().equals(sMethodName)) && (compareParameterTypes(method.getParameterTypes(), parameterTypes))) { m = method; break; } } if (m != null) { if (((object instanceof IJavaMethodRight)) && (!((IJavaMethodRight)object).checkRight(user))) { ret.setCode("AWES0005"); ret.setResult("没有方法执行权限"); ARE.getLog().warn("AWES0005 User [" + user.getUserID() + "][" + user.getUserName() + "] [Method] [" + sClassName + "][" + sMethodName + "] No Right!"); return ret; } o = m.invoke(object, argObjs); } else { for (int i = 0; i < args.size(); i++) { JSONElement e = (JSONElement)args.get(i); sbf.append(" Para"); sbf.append("["); sbf.append(e.getName()); sbf.append("]"); sbf.append("="); sbf.append("["); sbf.append(e.getValue()); sbf.append("]"); String argMethodName = "set" + e.getName().substring(0, 1).toUpperCase() + e.getName().substring(1); TypeValue tv = getClassTypeAndValue(e.getValue()); if (tv != null) { Method argMethod = null; try { argMethod = c.getMethod(argMethodName, new Class[] { tv.type }); } catch (Exception e1) { }if (argMethod != null) { argMethod.invoke(object, new Object[] { tv.value }); } else if (tv.type == JSONObject.class) { ARE.getLog().warn("建议方法" + argMethodName + "的参数为JSONObject"); if (((JSONObject)tv.value).getType() == 0) { argMethod = c.getMethod(argMethodName, new Class[] { HashMap.class }); Object[] oArgs = { changeHashMap((JSONObject)tv.value) }; argMethod.invoke(object, oArgs); } else { argMethod = c.getMethod(argMethodName, new Class[] { ArrayList.class }); Object[] oArgs = { changeArrayList((JSONObject)tv.value) }; argMethod.invoke(object, oArgs); } } } } if (((object instanceof IJavaMethodRight)) && (!((IJavaMethodRight)object).checkRight(user))) { ret.setCode("AWES0005"); ret.setResult("没有方法执行权限"); ARE.getLog().warn("AWES0005 User [" + user.getUserID() + "][" + user.getUserName() + "] [Method] [" + sClassName + "][" + sMethodName + "] No Right!"); return ret; } ARE.getLog().trace(sbf.toString()); if ((obj instanceof Transaction)) { try { m = c.getMethod(sMethodName, new Class[] { Transaction.class,Map.class }); o = m.invoke(object, new Object[] { obj ,model}); } catch (NoSuchMethodException ne) { } } else if ((obj instanceof JBOTransaction)) { m = c.getMethod(sMethodName, new Class[] { JBOTransaction.class,Map.class }); o = m.invoke(object, new Object[] { obj ,model}); } else { m = c.getMethod(sMethodName, new Class[0]); o = m.invoke(object, new Object[0]); } } if (o == null) { ARE.getLog().warn("AWES0005 [Method] [" + sClassName + "][" + sMethodName + "] Return Object is null!"); } ret.setResult((String)o); } catch (Exception e) { if ((e instanceof NoSuchMethodException)) { ret.setCode("AWES0005"); ret.setResult("后台服务方法不存在"); ARE.getLog().warn("AWES0005 [Method] [" + sClassName + "][" + sMethodName + "] Method Not Exist!", e); } else if ((e instanceof ClassNotFoundException)) { ret.setCode("AWES0005"); ret.setResult("后台服务类不存在"); ARE.getLog().warn("AWES0005 [Method] [" + sClassName + "][" + sMethodName + "] Class Not Found!", e); } else if ((e instanceof IllegalArgumentException)) { ret.setCode("AWES0005"); ret.setResult("后台服务类参数不一致"); ARE.getLog().warn("AWES0005 [Method] [" + sClassName + "][" + sMethodName + "] is IllegalArgument!", e); } else { if ((e instanceof InvocationTargetException)) { Throwable e1 = ((InvocationTargetException)e).getTargetException(); ARE.getLog().error("AWES0009:RunJavaMethod异常", e1); throw new AWEException("AWES0009", e1); } ret.setCode("AWES0005"); ret.setResult("后台服务方法调用错误"); ARE.getLog().warn("AWES0005 [Method] [" + sClassName + "][" + sMethodName + "] is Not Exist!", e); } } return ret; } private static ArrayList changeArrayList(JSONObject value) { ArrayList array = new ArrayList(); if (value.getType() != 1) return array; for (int i = 0; i < value.size(); i++) { Object obj = value.get(i).getValue(); if ((obj instanceof JSONObject)) { JSONObject jObj = (JSONObject)obj; if (jObj.getType() == 0) array.add(changeHashMap(jObj)); else array.add(changeArrayList(jObj)); } else { array.add(obj); } } return array; } private static HashMap changeHashMap(JSONObject value) { HashMap map = new HashMap(); if (value.getType() != 0) return map; for (int i = 0; i < value.size(); i++) { Element e = value.get(i); String name = e.getName(); Object obj = value.get(i).getValue(); if ((obj instanceof JSONObject)) { JSONObject jObj = (JSONObject)obj; if (jObj.getType() == 0) map.put(name, changeHashMap(jObj)); else map.put(name, changeArrayList(jObj)); } else { map.put(name, obj); } } return map; } private static TypeValue getClassTypeAndValue(Object value) { if (value == null) return null; TypeValue result = new TypeValue(); if ((value instanceof JSONObject)) { result.type = JSONObject.class; result.value = value; } else { result.type = String.class; result.value = value.toString(); } return result; } private static boolean compareParameterTypes(Class[] src, Class[] dest) { if ((src == null) && (dest == null)) return true; if (src == null) return false; if (dest == null) return false; if (src.length != dest.length) return false; for (int i = 0; i < src.length; i++) { if (!src[i].isAssignableFrom(dest[i])) return false; } return true; } private static class TypeValue { Class type; Object value; } }