package com.jiuqi.mobile.nigo.comeclose.ws.server; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import com.jiuqi.mobile.nigo.comeclose.exception.LoginException; import com.jiuqi.mobile.nigo.comeclose.exception.NiGoException; import com.jiuqi.mobile.nigo.comeclose.exception.WSFailException; import com.jiuqi.mobile.nigo.comeclose.json.JSONArray; import com.jiuqi.mobile.nigo.comeclose.json.JSONConvertor; import com.jiuqi.mobile.nigo.comeclose.json.JSONException; import com.jiuqi.mobile.nigo.comeclose.json.JSONObject; import com.jiuqi.mobile.nigo.comeclose.manager.ManagerAnnotation; import com.jiuqi.mobile.nigo.comeclose.manager.ManagerFactory; import com.jiuqi.mobile.nigo.comeclose.manager.base.ILoginManager; import com.jiuqi.mobile.nigo.comeclose.ws.WSConsts; public class WSServerUtils { public static JSONObject invoke(String session, String intfName, String methodName, JSONArray params) throws JSONException { JSONObject returnJSON = new JSONObject(); //校验session if((null == session || "".equals(session.trim()))){ if(!(ILoginManager.class.getName().equals(intfName) && "login".equals(methodName))){ returnException(returnJSON, new LoginException("请先登录!")); } } // try { // Class<?> implClass = WSAnnotationUtils.getImplClass(intfName); Class<?> intfClass = Class.forName(intfName, true, Thread.currentThread().getContextClassLoader()); ManagerAnnotation ma = intfClass.getAnnotation(ManagerAnnotation.class); if(null == ma){ throw new WSFailException("ManagerAnnotation没有找到"+intfName); } Class<?> implClass = Class.forName(ma.implClass(), true, Thread.currentThread().getContextClassLoader()); int paramsSize = params.length(); Class<?>[] parameterTypes = new Class[paramsSize]; Object[] paramObjs = new Object[paramsSize]; for (int i = 0; i < paramsSize; i++) { try{ JSONObject paramJson = params.getJSONObject(i); String paramClassName = paramJson.getString(WSConsts.CLASS_TAG); Class<?> paramClass = Class.forName(paramClassName); Object param = JSONConvertor.unSerializable(paramClass, paramJson); parameterTypes[i] = paramClass; paramObjs[i] = param; }catch(JSONException e){ Object o = params.get(i); parameterTypes[i] = o.getClass(); paramObjs[i] = o; } } Object obj = ManagerFactory.instanceManager(intfClass, session); Method method = getMethod(obj.getClass(), methodName, parameterTypes);//implClass.getMethod(methodName, parameterTypes);// ReturnObject形式返回 // method = obj.getClass().getMethod(methodName, parameterTypes); // Object ret = method.invoke(implClass.newInstance(), paramObjs); Object ret = method.invoke(obj, paramObjs); Object responseData; if(null != ret && ret.getClass().isArray()){ Wrapper w = new Wrapper((Object[]) ret); responseData = JSONConvertor.serializable(w); }else{ responseData = JSONConvertor.serializable(ret); } returnJSON.put(WSConsts.REQUEST_ERRORED, false); returnJSON.put(WSConsts.RESPONSE_DATA, responseData); } catch (Throwable e) { returnException(returnJSON, e); } return returnJSON; } public static Method getMethod(Class<?> implClass,String methodName,Class<?>...params) throws SecurityException, NoSuchMethodException{ if(null == params || 0 == params.length){ return implClass.getMethod(methodName); } Method[] ms = implClass.getMethods(); L1:for(Method m : ms){ Class<?>[] types = m.getParameterTypes(); if(null == types || 0==types.length || types.length != params.length){ continue; } for(int i=0; i<types.length; i++){ Class<?> p = params[i]; Class<?> t = types[i]; if(!equals(p,t)){ continue L1; } } return m; } return null; } private static boolean equals(Class<?> p, Class<?> t) { if(p.equals(t)){ return true; }else if(p.equals(Integer.class)){ return t.equals(int.class); }else if(p.equals(Long.class)){ return t.equals(long.class); }else if(p.equals(Float.class)){ return t.equals(float.class); }else if(p.equals(Double.class)){ return t.equals(double.class); }else if(p.equals(Byte.class)){ return t.equals(byte.class); }else if(p.equals(Short.class)){ return t.equals(short.class); }else if(p.equals(Character.class)){ return t.equals(char.class); }else if(p.equals(Boolean.class)){ return t.equals(boolean.class); } return false; } public static void returnException(JSONObject returnJSON, Throwable e) throws JSONException { if(e instanceof InvocationTargetException && null != ((InvocationTargetException) e).getTargetException()) { e = ((InvocationTargetException) e).getTargetException(); } returnJSON.put(WSConsts.REQUEST_ERRORED, true); returnJSON.put(WSConsts.RESPONSE_ERROR_MSG, e.getMessage()); if(e instanceof NiGoException){ returnJSON.put(WSConsts.RESPONSE_ERROR_EXCEPTION_CLASS, e.getClass()); }else{ returnJSON.put(WSConsts.RESPONSE_ERROR_EXCEPTION_CLASS, NiGoException.class); } } }