package com.jiuqi.mobile.nigo.comeclose.ws.server;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import com.jiuqi.mobile.nigo.comeclose.bean.LoadOnGetList;
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.$Gson$Types;
import com.jiuqi.mobile.nigo.comeclose.json.JSONArray;
import com.jiuqi.mobile.nigo.comeclose.json.JSONConvertor2;
import com.jiuqi.mobile.nigo.comeclose.json.JSONException;
import com.jiuqi.mobile.nigo.comeclose.json.JSONObject;
import com.jiuqi.mobile.nigo.comeclose.json.JsonArrayResult;
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 WSServerUtils2 {
public static void main(String[] args) {
System.out.println(Object.class.isAssignableFrom(WSServerUtils2.class));
System.out.println(WSServerUtils2.class.isAssignableFrom(Object.class));
}
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) && "loginForClient".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;
// }
// }
JsonArrayResult paramResult = JSONConvertor2.unSerializableArray_Result(params);
Object obj = ManagerFactory.instanceManager(intfClass, session);
Method method = getMethod(obj.getClass(), methodName, paramResult.getParamClass());//implClass.getMethod(methodName, parameterTypes);// ReturnObject形式返回
// method = obj.getClass().getMethod(methodName, parameterTypes);
// Object ret = method.invoke(implClass.newInstance(), paramObjs);
Object ret = method.invoke(obj, paramResult.getObjs());
JSONObject responseData = autoReturnJson(paramResult, ret, LoadOnGetList.class, List.class);
// 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);
e.printStackTrace();
}
return returnJSON;
}
// private static JSONObject getReturnJson(JsonArrayResult paramResult,
// Object ret) {
// JSONObject responseData;
// if(ret instanceof LoadOnGetList<?>){
// responseData = returnJson(paramResult,LoadOnGetList.class, ret);
// }else if(ret instanceof List<?>){
// responseData = returnJson(paramResult,List.class, ret);
// }else{
// responseData = JSONConvertor2.serializable(ret);
// }
// return responseData;
// }
private static JSONObject autoReturnJson(JsonArrayResult paramResult,
Object ret, Class<?>... havJsonClass){
if(null == ret){
return JSONConvertor2.serializable(ret);
}
if(ret.getClass().isArray()){
return JSONConvertor2.serializable(ret, ret.getClass(), true);
}
for(Class<?> c : havJsonClass){
if(c.isAssignableFrom(ret.getClass())){
return returnJson(paramResult, ret.getClass(), ret);
}
}
return JSONConvertor2.serializable(ret);
}
private static JSONObject returnJson(JsonArrayResult paramResult, Class<?> rawClass, Object ret) {
// 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, true);
JSONObject responseData;
if(paramResult.getObjs().length>0 && paramResult.getObjs()[0] instanceof Class<?>){
Class<?> c = (Class<?>) paramResult.getObjs()[0];
ParameterizedType type = $Gson$Types.newParameterizedTypeWithOwner(null,rawClass, c);
responseData = JSONConvertor2.serializable(ret, type, true);
}else{
responseData = JSONConvertor2.serializable(ret);
}
return responseData;
// JSONObject responseData;
// Object o = paramResult.getObjs()[0];
// Class<?> c = null;
// if(o instanceof Class<?>){
// c = (Class<?>) o;
// }
// ParameterizedType type = $Gson$Types.newParameterizedTypeWithOwner(null,rawClass, c);
// responseData = JSONConvertor2.serializable(ret, type, true);
// return responseData;
}
public static Method getMethod(Class<?> implClass,String methodName,Class<?>...params) throws SecurityException, NoSuchMethodException{
if(null == params || 0 == params.length){
return implClass.getMethod(methodName);
}
//20130813
Method cacheMethod = null;
try {
cacheMethod = implClass.getDeclaredMethod(methodName, params);
} catch (Exception e) {
}
if(null != cacheMethod){
return cacheMethod;
}
//
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;
}
//20130813
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(!equals1(p,t)){
continue L1;
}
}
return m;
}
//
return null;
}
private static boolean equals1(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);
}else{
return t.isAssignableFrom(p);
}
// return false;
}
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);
}
}
}