mirror of
https://gitee.com/BDWare/contract-java-example.git
synced 2026-01-29 08:49:30 +00:00
release
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
package org.bdware.sc.contractexample;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.irp.irplib.core.IrpMessage;
|
||||
import org.bdware.irp.irplib.core.IrpMessageCode;
|
||||
import org.bdware.irp.irplib.core.IrpRequest;
|
||||
import org.bdware.irp.irplib.core.IrpResponse;
|
||||
import org.bdware.irp.irpserver.IrpHandler;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.sql.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class IrsHandler implements IrpHandler {
|
||||
static Logger LOGGER = LogManager.getLogger(IrsHandler.class);
|
||||
private final Resolver resolver;
|
||||
|
||||
public IrsHandler(Resolver resolver) {
|
||||
this.resolver = resolver;
|
||||
}
|
||||
|
||||
private Map<String, String> jsonToMap(JsonObject result) {
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
for (String key : result.keySet()) ret.put(key, result.get(key).toString());
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
//TODO RETURN newDelegateRouterResponse instead
|
||||
@Override
|
||||
public IrpMessage batchCreateDOID(IrpMessage request) {
|
||||
return IrpResponse.newErrorResponse(IrpMessageCode.OC_BATCH_CREATE_DOID, IrpMessageCode.RC_ERROR, "unsupported operation");
|
||||
}
|
||||
|
||||
//for any input, fix it to scheme://prefix/suffix format.
|
||||
public String fixScheme(String doid) {
|
||||
if (!doid.contains(":")) {
|
||||
doid = resolver.config.scheme + ":" + "//" + doid;
|
||||
} else {
|
||||
if (doid.contains(":") && !doid.contains(":/"))
|
||||
doid = doid.replace(":", "://");
|
||||
if (doid.contains(":/") && !doid.contains("://")) {
|
||||
doid = doid.replace(":/", "://");
|
||||
}
|
||||
}
|
||||
return doid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrpMessage resolveDOID(IrpMessage request) {
|
||||
try {
|
||||
LOGGER.info(request);
|
||||
IrpRequest req = (IrpRequest) request;
|
||||
String doid = req.getDoid();
|
||||
// support old resolve request in irp-sdk 1.0.1
|
||||
doid = fixScheme(doid);
|
||||
if (!doid.matches(".*://.*")) {
|
||||
return IrpResponse.newErrorResponse(IrpMessageCode.OC_RESOLUTION_DOID, IrpMessageCode.RC_ERROR, "unknown scheme");
|
||||
} else {
|
||||
String[] domainAndID = doid.split("://");
|
||||
if (domainAndID[0].equals(resolver.config.scheme)) {
|
||||
String[] prefixAndSufix = domainAndID[1].split("/");
|
||||
if (prefixAndSufix[0].equals(resolver.config.prefix)) {
|
||||
JsonObject jsonObject = resolveInternal(prefixAndSufix[1]);
|
||||
//TODO connect and respon.
|
||||
Map<String, String> keypair = new HashMap<>();
|
||||
for (String key : jsonObject.keySet()) {
|
||||
if (!key.equals("sm2keypair"))
|
||||
keypair.put(key, jsonObject.get(key).toString());
|
||||
}
|
||||
return IrpResponse.newIrsResolveResponse(domainAndID[1], keypair);
|
||||
} else
|
||||
return IrpResponse.newDelegateRouterResponse(resolver.config.upperRouter);
|
||||
} else {
|
||||
return IrpResponse.newDelegateRouterResponse(resolver.config.upperRouter);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return IrpResponse.newErrorResponse(IrpMessageCode.OC_RESOLUTION_DOID, IrpMessageCode.RC_ERROR, fromException(e));
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] fromException(Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
return bo.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IrpMessage createDOID(IrpMessage request) {
|
||||
return IrpResponse.newErrorResponse(IrpMessageCode.OC_CREATE_DOID, IrpMessageCode.RC_ERROR, "unsupported operation");
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrpMessage updateDOID(IrpMessage request) {
|
||||
return IrpResponse.newErrorResponse(IrpMessageCode.OC_UPDATE_DOID, IrpMessageCode.RC_ERROR, "unsupported operation");
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrpMessage deleteDOID(IrpMessage request) {
|
||||
return IrpResponse.newErrorResponse(IrpMessageCode.OC_DELETE_DOID, IrpMessageCode.RC_ERROR, "unsupported operation");
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrpMessage verifyIrs(IrpMessage request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrpMessage resolvePrefix(IrpMessage request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrpMessage createPrefix(IrpMessage request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrpMessage updatePrefix(IrpMessage request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrpMessage deletePrefix(IrpMessage request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrpMessage verifyChildRouter(IrpMessage request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IrpMessage queryIdentifierByOffset(IrpMessage request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public JsonObject resolveInternal(String appName) {
|
||||
try {
|
||||
//load info from db
|
||||
Connection conn = Storage.instance.getDBConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet result = stmt.executeQuery("select * from app where name = '" + appName + "';");
|
||||
JsonObject ele = parseFirstElementInResultSet(result);
|
||||
result.close();
|
||||
if (ele == null) {
|
||||
return null;
|
||||
}
|
||||
ele.remove("cover");
|
||||
System.out.println("[GDRouter] resolve step1:" + ele.toString());
|
||||
|
||||
stmt = conn.createStatement();
|
||||
result = stmt.executeQuery("select * from contract where \"appId\" = " + ele.get("id").getAsInt() + ";");
|
||||
JsonObject ele2 = parseFirstElementInResultSet(result);
|
||||
result.close();
|
||||
if (ele2 != null && ele2.has("name"))
|
||||
ele.add("contractName", ele2.get("name"));
|
||||
System.out.println("[GDRouter] resolve step2:" + ele.toString());
|
||||
return ele;
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
JsonObject result = new JsonObject();
|
||||
result.addProperty("msg", new String(bo.toByteArray()));
|
||||
System.out.println("[GDRouter] resolve exception:" + result.toString());
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private JsonObject parseFirstElementInResultSet(ResultSet result) throws SQLException {
|
||||
ResultSetMetaData meta = result.getMetaData();
|
||||
boolean hasElement = result.next();
|
||||
if (!hasElement) {
|
||||
return null;
|
||||
}
|
||||
JsonObject ele = new JsonObject();
|
||||
for (int j = 1; j <= meta.getColumnCount(); j++) {
|
||||
int type = meta.getColumnType(j);
|
||||
switch (type) {
|
||||
case -6:
|
||||
case 5:
|
||||
case 4:
|
||||
ele.addProperty(meta.getColumnName(j), result.getInt(j));
|
||||
break;
|
||||
case -5:
|
||||
ele.addProperty(meta.getColumnName(j), result.getLong(j));
|
||||
break;
|
||||
case 6:
|
||||
case 7:
|
||||
case 8:
|
||||
ele.addProperty(meta.getColumnName(j), result.getDouble(j));
|
||||
break;
|
||||
default:
|
||||
ele.addProperty(meta.getColumnName(j), result.getString(j));
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
return ele;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.bdware.sc.contractexample;
|
||||
|
||||
public class ResolveConfig {
|
||||
String upperRouter;
|
||||
String ip;
|
||||
|
||||
String prefix;
|
||||
String scheme;
|
||||
int port;
|
||||
String dbUrl;
|
||||
String dbUserName;
|
||||
String dbPwd;
|
||||
public String token;
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package org.bdware.sc.contractexample;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.irp.irpserver.IrpListenerInfo;
|
||||
import org.bdware.irp.irpserver.IrpServerImpl;
|
||||
import org.bdware.irp.irpserver.IrpServerInfo;
|
||||
import org.bdware.sc.boundry.utils.SQLUtil;
|
||||
import org.bdware.sc.engine.JSONTool;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.PrintStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Resolver {
|
||||
static Logger LOGGER = LogManager.getLogger(Resolver.class);
|
||||
private IrsHandler testIrsHandler;
|
||||
|
||||
public Resolver(ResolveConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public static Resolver instance;
|
||||
|
||||
public static Resolver main(ScriptObjectMirror obj) {
|
||||
|
||||
if (Storage.instance == null) {
|
||||
|
||||
}
|
||||
JsonObject jo = JSONTool.convertMirrorToJson(obj).getAsJsonObject();
|
||||
ResolveConfig config = JsonUtil.fromJson(jo.toString(), ResolveConfig.class);
|
||||
instance = new Resolver(config);
|
||||
instance.initAndStartServer();
|
||||
return instance;
|
||||
}
|
||||
|
||||
public ResolveConfig config;
|
||||
|
||||
public static JsonObject resolve(String name) {
|
||||
try {
|
||||
name = instance.testIrsHandler.fixScheme(name);
|
||||
String[] schemeAndID = name.split("://");
|
||||
System.out.println("[GDResolver] " + name + " --> " + schemeAndID.length);
|
||||
if (!schemeAndID[0].equals(instance.config.scheme)) {
|
||||
JsonObject ret = new JsonObject();
|
||||
ret.addProperty("msg", "unknown scheme, need delegate!");
|
||||
ret.addProperty("doid", name);
|
||||
ret.addProperty("code", 1);
|
||||
return ret;
|
||||
} else {
|
||||
if (schemeAndID[1].startsWith(instance.config.prefix + "/")) {
|
||||
String appName = schemeAndID[1].split("/")[1];
|
||||
JsonObject resolveResult = instance.testIrsHandler.resolveInternal(appName);
|
||||
if (resolveResult == null) {
|
||||
resolveResult = new JsonObject();
|
||||
resolveResult.addProperty("code", 2);
|
||||
resolveResult.addProperty("msg", "can't resolve:" + appName);
|
||||
return resolveResult;
|
||||
}
|
||||
if (resolveResult != null && resolveResult.has("contractName")) {
|
||||
String contractName = resolveResult.get("contractName").getAsString();
|
||||
String key = resolveResult.get("sm2keypair").getAsString();
|
||||
String publicKey = JsonParser.parseString(key).getAsJsonObject().get("publicKey").getAsString();
|
||||
File findDir = findDir(contractName, publicKey);
|
||||
File sqlConf = new File(findDir, "sql.conf");
|
||||
if (sqlConf.exists()) {
|
||||
JsonElement jo = JsonParser.parseReader(new FileReader(sqlConf));
|
||||
resolveResult.add("dbInfo", jo);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
resolveResult.addProperty("code", 0);
|
||||
return resolveResult;
|
||||
} else {
|
||||
JsonObject ret = new JsonObject();
|
||||
ret.addProperty("msg", "unknown prefix, need delegate!");
|
||||
ret.addProperty("doid", name);
|
||||
ret.addProperty("code", 1);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
JsonObject ret = new JsonObject();
|
||||
ret.addProperty("msg", new String(IrsHandler.fromException(e)));
|
||||
ret.addProperty("doid", name);
|
||||
ret.addProperty("code", 1);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
private static File findDir(String contractName, String publicKey) {
|
||||
File f = new File("/bdcontract/BDWareProjectDir/private");
|
||||
if (f.exists()) {
|
||||
File dir = new File(f, publicKey);
|
||||
if (dir.exists()) {
|
||||
dir = new File(dir, contractName);
|
||||
return dir;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ResolveConfig getConfig() {
|
||||
return instance.config;
|
||||
}
|
||||
|
||||
|
||||
public static String peekDB(ScriptObjectMirror obj) {
|
||||
try {
|
||||
JsonObject jo = JSONTool.convertMirrorToJson(obj).getAsJsonObject();
|
||||
Connection conn = SQLUtil.getConnection(instance.config.dbUrl, instance.config.dbUserName, instance.config.dbPwd);
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet result = stmt.executeQuery("select * from app ;");
|
||||
ResultSetMetaData meta = result.getMetaData();
|
||||
JsonArray array = new JsonArray();
|
||||
for (; result.next(); ) {
|
||||
JsonObject ele = new JsonObject();
|
||||
array.add(ele);
|
||||
for (int j = 1; j <= meta.getColumnCount(); j++) {
|
||||
int type = meta.getColumnType(j);
|
||||
switch (type) {
|
||||
case -6:
|
||||
case 5:
|
||||
case 4:
|
||||
ele.addProperty(meta.getColumnName(j), result.getInt(j));
|
||||
break;
|
||||
case -5:
|
||||
ele.addProperty(meta.getColumnName(j), result.getLong(j));
|
||||
break;
|
||||
case 6:
|
||||
case 7:
|
||||
case 8:
|
||||
ele.addProperty(meta.getColumnName(j), result.getDouble(j));
|
||||
break;
|
||||
default:
|
||||
ele.addProperty(meta.getColumnName(j), result.getString(j));
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return array.toString();
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
return new String(bo.toByteArray());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void initAndStartServer() {
|
||||
//init irpserver
|
||||
IrpListenerInfo listenerInfo = new IrpListenerInfo(config.ip, config.port, "TCP");
|
||||
List<IrpListenerInfo> listenerInfos = new ArrayList<>();
|
||||
listenerInfos.add(listenerInfo);
|
||||
IrpServerInfo info = new IrpServerInfo("bdware", "no description", listenerInfos);
|
||||
IrpServerImpl server = new IrpServerImpl(info);
|
||||
testIrsHandler = new IrsHandler(this);
|
||||
|
||||
server.setIrpServerHandler(testIrsHandler);
|
||||
server.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.bdware.sc.contractexample;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import org.bdware.sc.boundry.utils.RocksDBUtil;
|
||||
import org.bdware.sc.boundry.utils.SQLUtil;
|
||||
import org.rocksdb.RocksIterator;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.sql.Connection;
|
||||
|
||||
public class Storage {
|
||||
static {
|
||||
SQLUtil.initDriver("org.postgresql.Driver");
|
||||
}
|
||||
|
||||
public static Storage instance = new Storage();
|
||||
public RocksDBUtil grsStorage = RocksDBUtil.loadDB("./grsTable", false);
|
||||
Connection conn;
|
||||
|
||||
public Connection getDBConnection() {
|
||||
ResolveConfig resolveConfig = Resolver.getConfig();
|
||||
try {
|
||||
if (instance.conn != null && instance.conn.isValid(2)) {
|
||||
return instance.conn;
|
||||
}
|
||||
instance.conn = SQLUtil.getConnection(resolveConfig.dbUrl, resolveConfig.dbUserName, resolveConfig.dbPwd);
|
||||
return instance.conn;
|
||||
} catch (Exception e) {
|
||||
instance.conn = SQLUtil.getConnection(resolveConfig.dbUrl, resolveConfig.dbUserName, resolveConfig.dbPwd);
|
||||
return instance.conn;
|
||||
}
|
||||
}
|
||||
|
||||
public JsonObject peekDB() {
|
||||
RocksIterator iter = grsStorage.newIterator();
|
||||
JsonObject ret = new JsonObject();
|
||||
try {
|
||||
for (iter.seekToFirst(); iter.isValid(); iter.next()) {
|
||||
String jsonStr = new String(iter.value());
|
||||
ret.addProperty(new String(iter.key()), jsonStr);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
ret.addProperty("result", "failed");
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
ret.addProperty("errorMessage", bo.toString());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
22
backend/src/test/java/IrpTest.java
Normal file
22
backend/src/test/java/IrpTest.java
Normal file
@@ -0,0 +1,22 @@
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.irp.client.IrpClientImpl;
|
||||
import org.bdware.irp.exception.IrpClientException;
|
||||
import org.bdware.irp.stateinfo.StateInfoBase;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IrpTest {
|
||||
static Logger LOGGER = LogManager.getLogger(IrpTest.class);
|
||||
|
||||
@Test
|
||||
public void resolve() {
|
||||
IrpClientImpl client = new IrpClientImpl();
|
||||
client.connect("tcp://112.74.48.247:21044");
|
||||
try {
|
||||
StateInfoBase result = client.resolve("bdtest.gd/dddd");
|
||||
LOGGER.info(result.handleValues.toString());
|
||||
} catch (IrpClientException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user