diff --git a/src/main/java/com/loafle/overflow/proxy/db/DBProxy.java b/src/main/java/com/loafle/overflow/proxy/db/DBProxy.java index e6921dc..353889b 100644 --- a/src/main/java/com/loafle/overflow/proxy/db/DBProxy.java +++ b/src/main/java/com/loafle/overflow/proxy/db/DBProxy.java @@ -13,7 +13,9 @@ import com.loafle.overflow.module.probe.dao.ProbeTaskDAO; import com.loafle.overflow.module.sensor.dao.SensorDAO; import com.loafle.overflow.module.sensor.dao.SensorItemDAO; import com.loafle.overflow.module.target.dao.TargetDAO; +import com.loafle.overflow.module.target.service.TargetService; import io.grpc.ServerBuilder; +import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -37,9 +39,11 @@ public class DBProxy { private io.grpc.Server server; private static Map daoMap = null; + private static Map serviceMap = null; public DBProxy() { daoMap = new ConcurrentHashMap(); + serviceMap = new ConcurrentHashMap(); ApplicationContext ctx = new AnnotationConfigApplicationContext("com.loafle.overflow"); @@ -60,6 +64,9 @@ public class DBProxy { daoMap.put("infraOSDaemon", ctx.getBean(InfraOSDaemonDAO.class)); daoMap.put("infraOSPort", ctx.getBean(InfraOSPortDAO.class)); daoMap.put("infraService", ctx.getBean(InfraServiceDAO.class)); + + + serviceMap.put("serviceTarget", ctx.getBean(TargetService.class)); } public void start(int port) throws IOException { @@ -91,29 +98,35 @@ public class DBProxy { io.grpc.stub.StreamObserver responseObserver) { String targetDAO = request.getTargetDao(); - JpaRepository dao = daoMap.get(targetDAO); + Object obj = null; - if(dao != null) { - try { - - String jsonResult = doQuery(request, dao); - - DBOutput reply = DBOutput.newBuilder() - .setResult(jsonResult) - .build(); - responseObserver.onNext(reply); - responseObserver.onCompleted(); - }catch(Exception e) { - e.printStackTrace(); - responseObserver.onError(e); - } - - }else { - responseObserver.onError(new Exception("Not assigned DAO :" + targetDAO)); + obj =daoMap.get(targetDAO); + if(obj == null) { + obj = serviceMap.get(targetDAO); } + if(obj == null) { + responseObserver.onError(new Exception("Not assigned DAO :" + targetDAO)); + return; + } + + try { + + String jsonResult = doQuery(request, obj); + + DBOutput reply = DBOutput.newBuilder() + .setResult(jsonResult) + .build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + }catch(Exception e) { + e.printStackTrace(); + responseObserver.onError(e); + } + + } - private String doQuery(DBInput request, JpaRepository dao) throws Exception { + private String doQuery(DBInput request, Object dao) throws Exception { String methodName = request.getMethod(); Map params = request.getParamsMap(); @@ -133,10 +146,30 @@ public class DBProxy { List valueList = new ArrayList(); for( String className : params.keySet() ){ //className - model name(with package) , value - model JsonString - Class cls = Class.forName(className); - Object obj = mapper.readValue((String)params.get(className), cls); - valueList.add(obj); - paramTypes.add(cls); + + Object obj = null; + if(className.contains("|")) { + int idx = className.indexOf("|"); + String firstClassName = className.substring(0, idx); + String lastClassName = className.substring(idx+1); + Class firstCls = Class.forName(firstClassName); + Class lastCls = Class.forName(lastClassName); + mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + obj = mapper.readValue((String)params.get(className), mapper.getTypeFactory().constructCollectionType(firstCls, lastCls)); + + //FIXME:random sort.. T__T + paramTypes.add(0,firstCls); + valueList.add(0, obj); + + } else { + Class cls = Class.forName(className); + obj = mapper.readValue((String)params.get(className), cls); + paramTypes.add(cls); + valueList.add(obj); + } + + + } diff --git a/src/test/java/com/loafle/overflow/module/target/service/TargetServiceTest.java b/src/test/java/com/loafle/overflow/module/target/service/TargetServiceTest.java index ee52065..bcb238f 100644 --- a/src/test/java/com/loafle/overflow/module/target/service/TargetServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/target/service/TargetServiceTest.java @@ -84,4 +84,16 @@ public class TargetServiceTest { } + @Test + public void testString() { + + + String str = "list|model"; + + if(str.contains("|")) { + System.out.println(str); + } + + } + } \ No newline at end of file diff --git a/src/test/java/com/loafle/overflow/proxy/db/MethodSeekerTest.java b/src/test/java/com/loafle/overflow/proxy/db/MethodSeekerTest.java index 932bc46..72b7222 100644 --- a/src/test/java/com/loafle/overflow/proxy/db/MethodSeekerTest.java +++ b/src/test/java/com/loafle/overflow/proxy/db/MethodSeekerTest.java @@ -4,6 +4,7 @@ import com.loafle.overflow.AppConfig; import com.loafle.overflow.JdbcConfiguration; import com.loafle.overflow.module.domain.model.Domain; import com.loafle.overflow.module.noauthprobe.dao.NoAuthProbeDAO; +import com.loafle.overflow.module.target.service.TargetService; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -25,6 +26,8 @@ public class MethodSeekerTest { @Autowired private NoAuthProbeDAO noAuthProbeDAO; + @Autowired + private TargetService targetService; @Test public void getMethod() throws Exception { @@ -40,12 +43,12 @@ public class MethodSeekerTest { @Test public void RelfetMehotds() { - JpaRepository jpa = this.noAuthProbeDAO; + Object o = this.targetService; - Method[] mArray = jpa.getClass().getMethods(); + Method[] mArray = o.getClass().getMethods(); for (Method m : mArray) { - if( m.getName().equals("findOne")) { + if( m.getName().equals("saveAllTarget")) { System.out.println(m.getParameterTypes()); }