package com.jiuqi.lbsinterface.ws.server;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.jiuqi.lbsinterface.exception.LoginException;
import com.jiuqi.lbsinterface.exception.NiGoException;
import com.jiuqi.lbsinterface.exception.WSFailException;
import com.jiuqi.lbsinterface.json.JSONArray;
import com.jiuqi.lbsinterface.json.JSONConvertor2;
import com.jiuqi.lbsinterface.json.JSONException;
import com.jiuqi.lbsinterface.json.JSONObject;
import com.jiuqi.lbsinterface.json.JsonArrayResult;
import com.jiuqi.lbsinterface.manager.IPositionManager;
import com.jiuqi.lbsinterface.manager.ManagerAnnotation;
import com.jiuqi.lbsinterface.manager.ManagerFactory;
import com.jiuqi.lbsinterface.ws.WSConsts;
public class WSServerUtils2 {
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 (!(IPositionManager.class.getName().equals(intfName) && "login"
.equals(methodName))) {
returnException(returnJSON, new LoginException("请先登录!"));
}
}
//
try {
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());
JsonArrayResult paramResult = JSONConvertor2
.unSerializableArray_Result(params);
Object obj = ManagerFactory.instanceManager(intfClass, session);
Method method = getMethod(obj.getClass(), methodName,
paramResult.getParamClass());
Object ret = method.invoke(obj, paramResult.getObjs());
JSONObject responseData;
// if (ret instanceof LoadOnGetList<?>) {
// Object o = paramResult.getObjs()[0];
// Class<?> c = null;
// if (o instanceof Class<?>) {
// c = (Class<?>) o;
// }
// ParameterizedType type = $Gson$Types
// .newParameterizedTypeWithOwner(null,
// LoadOnGetList.class, c);
// responseData = JSONConvertor2.serializable(ret, type);
// } else if (ret instanceof List<?>) {
// Object o = paramResult.getObjs()[0];
// Class<?> c = null;
// if (o instanceof Class<?>) {
// c = (Class<?>) o;
// }
// ParameterizedType type = $Gson$Types
// .newParameterizedTypeWithOwner(null, List.class, c);
// responseData = JSONConvertor2.serializable(ret, type);
// } else {
// responseData = JSONConvertor2.serializable(ret);
// }
responseData = JSONConvertor2.serializable(ret);
returnJSON.put(WSConsts.REQUEST_ERRORED, false);
returnJSON.put(WSConsts.RESPONSE_DATA, responseData);
} catch (Throwable e) {
returnException(returnJSON, e);
e.printStackTrace();
}
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) {
if (!m.getName().equals(methodName)) {
continue;
}
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);
}
}
}