target create
This commit is contained in:
snoop 2017-06-27 19:35:21 +09:00
parent 845076056d
commit fc2eaaf0d2
3 changed files with 74 additions and 26 deletions

View File

@ -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.SensorDAO;
import com.loafle.overflow.module.sensor.dao.SensorItemDAO; import com.loafle.overflow.module.sensor.dao.SensorItemDAO;
import com.loafle.overflow.module.target.dao.TargetDAO; import com.loafle.overflow.module.target.dao.TargetDAO;
import com.loafle.overflow.module.target.service.TargetService;
import io.grpc.ServerBuilder; import io.grpc.ServerBuilder;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -37,9 +39,11 @@ public class DBProxy {
private io.grpc.Server server; private io.grpc.Server server;
private static Map<String, JpaRepository> daoMap = null; private static Map<String, JpaRepository> daoMap = null;
private static Map<String, Object> serviceMap = null;
public DBProxy() { public DBProxy() {
daoMap = new ConcurrentHashMap(); daoMap = new ConcurrentHashMap();
serviceMap = new ConcurrentHashMap();
ApplicationContext ctx = new AnnotationConfigApplicationContext("com.loafle.overflow"); ApplicationContext ctx = new AnnotationConfigApplicationContext("com.loafle.overflow");
@ -60,6 +64,9 @@ public class DBProxy {
daoMap.put("infraOSDaemon", ctx.getBean(InfraOSDaemonDAO.class)); daoMap.put("infraOSDaemon", ctx.getBean(InfraOSDaemonDAO.class));
daoMap.put("infraOSPort", ctx.getBean(InfraOSPortDAO.class)); daoMap.put("infraOSPort", ctx.getBean(InfraOSPortDAO.class));
daoMap.put("infraService", ctx.getBean(InfraServiceDAO.class)); daoMap.put("infraService", ctx.getBean(InfraServiceDAO.class));
serviceMap.put("serviceTarget", ctx.getBean(TargetService.class));
} }
public void start(int port) throws IOException { public void start(int port) throws IOException {
@ -91,29 +98,35 @@ public class DBProxy {
io.grpc.stub.StreamObserver<com.loafle.overflow.db.api.DBOutput> responseObserver) { io.grpc.stub.StreamObserver<com.loafle.overflow.db.api.DBOutput> responseObserver) {
String targetDAO = request.getTargetDao(); String targetDAO = request.getTargetDao();
JpaRepository dao = daoMap.get(targetDAO); Object obj = null;
if(dao != null) { obj =daoMap.get(targetDAO);
try { if(obj == null) {
obj = serviceMap.get(targetDAO);
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));
} }
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(); String methodName = request.getMethod();
Map<String, String> params = request.getParamsMap(); Map<String, String> params = request.getParamsMap();
@ -133,10 +146,30 @@ public class DBProxy {
List<Object> valueList = new ArrayList<Object>(); List<Object> valueList = new ArrayList<Object>();
for( String className : params.keySet() ){ for( String className : params.keySet() ){
//className - model name(with package) , value - model JsonString //className - model name(with package) , value - model JsonString
Class<?> cls = Class.forName(className);
Object obj = mapper.readValue((String)params.get(className), cls); Object obj = null;
valueList.add(obj); if(className.contains("|")) {
paramTypes.add(cls); 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);
}
} }

View File

@ -84,4 +84,16 @@ public class TargetServiceTest {
} }
@Test
public void testString() {
String str = "list|model";
if(str.contains("|")) {
System.out.println(str);
}
}
} }

View File

@ -4,6 +4,7 @@ import com.loafle.overflow.AppConfig;
import com.loafle.overflow.JdbcConfiguration; import com.loafle.overflow.JdbcConfiguration;
import com.loafle.overflow.module.domain.model.Domain; import com.loafle.overflow.module.domain.model.Domain;
import com.loafle.overflow.module.noauthprobe.dao.NoAuthProbeDAO; import com.loafle.overflow.module.noauthprobe.dao.NoAuthProbeDAO;
import com.loafle.overflow.module.target.service.TargetService;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -25,6 +26,8 @@ public class MethodSeekerTest {
@Autowired @Autowired
private NoAuthProbeDAO noAuthProbeDAO; private NoAuthProbeDAO noAuthProbeDAO;
@Autowired
private TargetService targetService;
@Test @Test
public void getMethod() throws Exception { public void getMethod() throws Exception {
@ -40,12 +43,12 @@ public class MethodSeekerTest {
@Test @Test
public void RelfetMehotds() { 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) { for (Method m : mArray) {
if( m.getName().equals("findOne")) { if( m.getName().equals("saveAllTarget")) {
System.out.println(m.getParameterTypes()); System.out.println(m.getParameterTypes());
} }