diff --git a/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java b/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java index 7bf22ab..78afd81 100644 --- a/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java +++ b/src/main/java/com/loafle/overflow/proxy/ServiceInvoker.java @@ -1,6 +1,5 @@ package com.loafle.overflow.proxy; -import com.google.gson.internal.Primitives; import com.google.protobuf.ByteString; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; @@ -10,7 +9,6 @@ import org.springframework.stereotype.Component; import java.io.IOException; import java.lang.reflect.*; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -27,23 +25,6 @@ public class ServiceInvoker { private Map serviceCacheMap; - private static final Map primitiveMap; - - static { - Map primitives = new HashMap<>(8); - - primitives.put("boolean", boolean.class); - primitives.put("byte", byte.class); - primitives.put("char", char.class); - primitives.put("double", double.class); - primitives.put("float", float.class); - primitives.put("int", int.class); - primitives.put("long", long.class); - primitives.put("short", short.class); - - primitiveMap = Collections.unmodifiableMap(primitives); - } - public ServiceInvoker(ApplicationContext context) { this.context = context; objectMapper = new ObjectMapper(); @@ -88,40 +69,7 @@ public class ServiceInvoker { return methodCache; } - methodCache.parameterCaches = new ParameterCache[paramCount]; - - ParameterCache parameterCache = null; - - Type[] parameterTypes = method.getGenericParameterTypes(); - Type parameterType = null; - - ParameterizedType parameterizedType = null; - Type[] actualTypeArguments = null; - - for (int i = 0; i < paramCount; i++) { - parameterCache = new ParameterCache(); - methodCache.parameterCaches[i] = parameterCache; - parameterType = parameterTypes[i]; - - if (parameterType instanceof ParameterizedType) { - parameterizedType = (ParameterizedType)parameterType; - parameterCache.clazz = Class.forName(parameterizedType.getRawType().getTypeName()); - actualTypeArguments = parameterizedType.getActualTypeArguments(); - - if (null != actualTypeArguments && 0 < actualTypeArguments.length) { - parameterCache.genericClazzes = new Class[actualTypeArguments.length]; - for (int j = 0; j < actualTypeArguments.length; j++) { - parameterCache.genericClazzes[j] = Class.forName(actualTypeArguments[j].getTypeName()); - } - } - } else { - if (Primitives.isPrimitive(parameterType)) { - parameterCache.clazz = primitiveMap.get(parameterType.getTypeName()); - } else { - parameterCache.clazz = Class.forName(parameterType.getTypeName()); - } - } - } + methodCache.parameterTypes = method.getGenericParameterTypes(); return methodCache; } @@ -147,11 +95,7 @@ public class ServiceInvoker { private static class MethodCache { private Method method; private Class returnClazz; - private ParameterCache[] parameterCaches; - } - private static class ParameterCache { - private Class clazz; - private Class[] genericClazzes; + private Type[] parameterTypes; } } @@ -166,88 +110,37 @@ public class ServiceInvoker { return serviceCache; } - private Object getValue(Cache.ParameterCache parameterCache, String json) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - Object result = null; - if (List.class == parameterCache.clazz) { - if (null == parameterCache.genericClazzes || 1 != parameterCache.genericClazzes.length) { - throw new IllegalArgumentException(); - } - result = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, parameterCache.genericClazzes[0])); - } else if (Map.class == parameterCache.clazz) { - if (null == parameterCache.genericClazzes || 2 != parameterCache.genericClazzes.length) { - throw new IllegalArgumentException(); - } - result = objectMapper.readValue(json, objectMapper.getTypeFactory().constructMapType(Map.class, parameterCache.genericClazzes[0], parameterCache.genericClazzes[1])); - } else if (parameterCache.clazz.isPrimitive()) { - Class wrapperClazz = Primitives.wrap(parameterCache.clazz); - Constructor con = wrapperClazz.getConstructor(String.class); - result = con.newInstance(json); - } else { - result = objectMapper.readValue(json, parameterCache.clazz); - } - - return result; + private Object getValue(Type parameterType, String json) throws IOException { + return objectMapper.readValue(json, objectMapper.getTypeFactory().constructType(parameterType)); } - private Object[] getParameters(Cache.ParameterCache[] parameterCaches, List params) throws IOException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException { - if (null == parameterCaches || null == params) { + + private Object[] getParameters(Type[] parameterTypes, List params) throws IOException { + if (null == parameterTypes || null == params) { return null; } - if (parameterCaches.length != params.size()) { + if (parameterTypes.length != params.size()) { throw new IllegalArgumentException(); } - Object[] result = new Object[parameterCaches.length]; + Object[] result = new Object[parameterTypes.length]; - for (int i = 0; i < parameterCaches.length; i++) { - result[i] = getValue(parameterCaches[i], params.get(i)); + for (int i = 0; i < parameterTypes.length; i++) { + result[i] = getValue(parameterTypes[i], params.get(i).toStringUtf8()); } return result; } - private Object[] getParametersForByteString(Cache.ParameterCache[] parameterCaches, List params) throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { - if (null == parameterCaches || null == params) { - return null; - } - if (parameterCaches.length != params.size()) { - throw new IllegalArgumentException(); - } - - Object[] result = new Object[parameterCaches.length]; - - for (int i = 0; i < parameterCaches.length; i++) { - result[i] = getValue(parameterCaches[i], params.get(i).toStringUtf8()); - } - - return result; - } - - public String invoke(String serviceName, String methodName, List params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException, InstantiationException { + public String invoke(String serviceName, String methodName, List params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException { Cache serviceCache = getServiceCache(serviceName); Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName); Object[] parameters = null; - if (null != methodCache.parameterCaches && 0 < methodCache.parameterCaches.length) { - parameters = getParameters(methodCache.parameterCaches, params); - } - - Object result = methodCache.method.invoke(serviceCache.getBean(), parameters); - - String jsonInString = objectMapper.writeValueAsString(result); - - return jsonInString; - } - - public String invokeForByteString(String serviceName, String methodName, List params) throws NoSuchMethodException, ClassNotFoundException, IOException, InvocationTargetException, IllegalAccessException, InstantiationException { - Cache serviceCache = getServiceCache(serviceName); - Cache.MethodCache methodCache = serviceCache.getMethodCache(methodName); - - Object[] parameters = null; - if (null != methodCache.parameterCaches && 0 < methodCache.parameterCaches.length) { - parameters = getParametersForByteString(methodCache.parameterCaches, params); + if (null != methodCache.parameterTypes && 0 < methodCache.parameterTypes.length) { + parameters = getParameters(methodCache.parameterTypes, params); } Object result = methodCache.method.invoke(serviceCache.getBean(), parameters); diff --git a/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java b/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java index 6c14e4a..50f7d43 100644 --- a/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java +++ b/src/main/java/com/loafle/overflow/proxy/ServiceProxy.java @@ -66,7 +66,7 @@ public class ServiceProxy { String result = null; try { - result =this.serviceInvoker.invokeForByteString(request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList()); + result =this.serviceInvoker.invoke(request.getTarget(), request.getMethod(), request.getParamsList().asByteStringList()); ServerOutput reply = ServerOutput.newBuilder() .setResult(result) @@ -88,9 +88,6 @@ public class ServiceProxy { } catch (IllegalAccessException e) { e.printStackTrace(); responseObserver.onError(e); - } catch (InstantiationException e) { - e.printStackTrace(); - responseObserver.onError(e); } diff --git a/src/test/java/com/loafle/overflow/proxy/ServiceInvokerTest.java b/src/test/java/com/loafle/overflow/proxy/ServiceInvokerTest.java index 3ef4bcf..ffb2111 100644 --- a/src/test/java/com/loafle/overflow/proxy/ServiceInvokerTest.java +++ b/src/test/java/com/loafle/overflow/proxy/ServiceInvokerTest.java @@ -1,6 +1,7 @@ package com.loafle.overflow.proxy; import com.google.common.primitives.Primitives; +import com.google.protobuf.ByteString; import com.loafle.overflow.module.target.service.TargetDiscoveryService; import com.loafle.overflow.spring.AppConfig; import com.loafle.overflow.spring.JdbcConfiguration; @@ -29,27 +30,27 @@ public class ServiceInvokerTest { @Test public void invoke() throws Exception { -// List params = new ArrayList<>(2); -// params.add("[{\"id\": 1," + -// "\"ip\":2312132112," + -// "\"mac\":12312312," + -// "\"os\": \"Windows\"," + -// "\"target\":true}]"); -// params.add("{\"id\": 1," + -// "\"ip\":2312132112," + -// "\"probeKey\":\"sdfsdfsdfsdfsd\"}"); -// -// serviceInvoker.invoke("TargetDiscoveryService", "saveAllTarget", params); + List params = new ArrayList<>(2); + params.add(ByteString.copyFromUtf8("[{\"id\": 1," + + "\"ip\":2312132112," + + "\"mac\":12312312," + + "\"os\": \"Windows\"," + + "\"target\":true}]")); + params.add(ByteString.copyFromUtf8("{\"id\": 1," + + "\"ip\":2312132112," + + "\"probeKey\":\"sdfsdfsdfsdfsd\"}")); -// List params = new ArrayList<>(1); -// params.add("1"); -// -// serviceInvoker.invoke("InfraOSService", "read", params); + serviceInvoker.invoke("TargetDiscoveryService", "saveAllTarget", params); - List params = new ArrayList<>(1); - params.add("1"); + List params2 = new ArrayList<>(1); + params2.add(ByteString.copyFromUtf8("1")); - serviceInvoker.invoke("MemberService", "read", params); + serviceInvoker.invoke("InfraOSService", "read", params2); + + List params3 = new ArrayList<>(1); + params3.add(ByteString.copyFromUtf8("1")); + + serviceInvoker.invoke("MemberService", "read", params3); }