diff --git a/src/main/java/org/bdware/sc/ContractClient.java b/src/main/java/org/bdware/sc/ContractClient.java index 20bf304..e4fb730 100644 --- a/src/main/java/org/bdware/sc/ContractClient.java +++ b/src/main/java/org/bdware/sc/ContractClient.java @@ -24,6 +24,7 @@ import org.bdware.sc.util.JsonUtil; import java.io.File; import java.io.InputStream; import java.io.PrintStream; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.*; @@ -31,7 +32,6 @@ public class ContractClient { private static final Logger LOGGER = LogManager.getLogger(ContractClient.class); public static String cmi = ""; public ContractMeta contractMeta; - public boolean isDebug; transient SocketGet get; int port; String pid; @@ -196,7 +196,6 @@ public class ContractClient { contractMeta.thisPermission = get.syncGet("", "showPermission", ""); // LOGGER.info("initProps ---- position-----8"); isRunning = true; - contractMeta.isDebug = Boolean.parseBoolean(get.syncGet("", "getDebug", "")); // LOGGER.info("initProps ---- position-----9"); get.syncGet("", "registerMangerPort", ContractManager.cPort.getCMPort() + ""); contractMeta.contract = @@ -221,11 +220,9 @@ public class ContractClient { public String startProcess(PrintStream ps) throws Exception { isRunning = false; port = -1; - String darg = "-Djava.library.path="; String classpath; File jniPath; - if (ContractManager.yjsPath == null) { jniPath = new File("./jni/"); classpath = System.getProperty("java.class.path"); @@ -233,7 +230,6 @@ public class ContractClient { classpath = ContractManager.yjsPath; jniPath = new File(classpath).getParentFile(); } - String osJni = ((HardwareInfo.type == OSType.linux) ? "/jni/linux" : "/jni/mac"); darg += jniPath.getAbsolutePath() + osJni; if (!new File(classpath).exists()) { @@ -249,39 +245,63 @@ public class ContractClient { new JsonPrimitive("incorrect path: yjs.jar, missing libs")); return JsonUtil.toJson(r); } - // ProcessBuilder builder = - // new ProcessBuilder( - // "java", - // "-Dfile.encoding=UTF-8", - // darg, - // "-cp", - // jniPath.getAbsolutePath() + "/libs/*:" + classpath, - // "org.bdware.sc.ContractProcess", - // "-port=" + cPort.getPort()); int startPort = ContractManager.cPort.getPortAndInc(); - //(isDebug ? "-Dlog4j.configurationFile=./log4j2.debug.properties" : ""), - ProcessBuilder builder = - new ProcessBuilder( - "java", - "-Dfile.encoding=UTF-8", - darg, - "-jar", - classpath, - "-port=" + startPort, - "-cmi=" + cmi, // cmi 区分不同CM的cp - (isDebug ? "-debug" : "")); + List pbParameters = new ArrayList<>(); + pbParameters.add("java"); + pbParameters.add("-Dfile.encoding=UTF-8"); + pbParameters.add(darg); + if (contractMeta.contract.getRemoteDebugPort() != 0) { + pbParameters.add(String.format("-agentlib:jdwp=transport=dt_socket,address=%d,server=y,suspend=n", contractMeta.contract.getRemoteDebugPort())); + } + if (contractMeta.contract.isDebug()) { + pbParameters.add("-Dlog4j.configurationFile=./log4j2.debug.properties"); + } else { + pbParameters.add("-Dlog4j.configurationFile=./log4j2.properties"); + } + pbParameters.add("-jar"); + pbParameters.add(classpath); + pbParameters.add("-port=" + startPort); + pbParameters.add("-cmi=" + cmi); + if (contractMeta.contract.isDebug()) + pbParameters.add("-debug"); + ProcessBuilder builder; + String[] result = new String[pbParameters.size()]; + pbParameters.toArray(result); + Constructor pbc = ProcessBuilder.class.getDeclaredConstructor(String[].class); + builder = pbc.newInstance(new Object[]{result}); + +// if (remoteDebug.length() == 0) +// builder = +// new ProcessBuilder( +// "java", +// "-Dfile.encoding=UTF-8", +// darg, +// "-jar", +// classpath, +// "-port=" + startPort, +// "-cmi=" + cmi, // cmi 区分不同CM的cp +// (isDebug ? "-debug" : "")); +// else +// builder = +// +// new ProcessBuilder( +// "java", +// "-Dfile.encoding=UTF-8", +// darg, remoteDebug, +// "-jar", +// classpath, +// "-port=" + startPort, +// "-cmi=" + cmi, // cmi 区分不同CM的cp +// (isDebug ? "-debug" : "")); + File directory = new File("./"); LOGGER.debug("[CMD] path: " + directory.getAbsolutePath()); LOGGER.debug(JsonUtil.toPrettyJson(builder.command())); - Map map = builder.environment(); map.put("java.library.path", jniPath.getAbsolutePath() + osJni); builder.directory(directory); LOGGER.debug("start process:"); - - process = builder.start(); - this.pid = getPid(process); LOGGER.info("[CP PPID] " + pid); PrintStream printStream = new PrintStream(process.getOutputStream()); @@ -318,7 +338,7 @@ public class ContractClient { ContractManager.cPort.updateDb(port, true); get = new SocketGet("127.0.0.1", port); get.syncGet("", "setDBInfo", ContractManager.dbPath); - if (isDebug || get != null) { + if (contractMeta.contract.isDebug() || get != null) { String tagA = (ps == System.out ? "[Contract_" + port + "_out] " : ""); String tagB = (ps == System.out ? "[Contract_" + port + "_err] " : ""); outputTracer.track(process, sc, tagA, ps); @@ -335,9 +355,11 @@ public class ContractClient { if (multiContractMeta != null && multiContractMeta.getMembers() != null) { String setMemberResult = get.syncGet( "", "setMembers", JsonUtil.toJson(multiContractMeta.getMembers())); - LOGGER.debug("setMember:" + setMemberResult); - + LOGGER.info("setMember:" + setMemberResult); + } else { + LOGGER.info("setMember ignore, meta:" + (multiContractMeta == null) + (multiContractMeta == null ? "NULL" : " members:" + multiContractMeta.getMembers())); } + if (isBundlePath(contractMeta.contract.getScriptStr())) { status = get.syncGet( @@ -450,10 +472,6 @@ public class ContractClient { return contractMeta.logDetail.get(action); } - public boolean isDebug() { - return contractMeta.isDebug; - } - public long getTimes() { return times; } diff --git a/src/main/java/org/bdware/sc/ContractManager.java b/src/main/java/org/bdware/sc/ContractManager.java index 0962590..ee042e5 100644 --- a/src/main/java/org/bdware/sc/ContractManager.java +++ b/src/main/java/org/bdware/sc/ContractManager.java @@ -465,14 +465,6 @@ public class ContractManager { return statusRecorder.getContractClient(meta.id); } - public String startContractAndRedirectWithDebug(Contract c) { - return startContractAndRedirect(c, System.out, null, true); - } - - public String startContractAndRedirect(Contract c, PrintStream ps) { - return startContractAndRedirect(c, ps, null, false); - } - public String getContractStateful(String contractID) { ContractClient cc = getClient(contractID); if (null == cc) { @@ -793,7 +785,7 @@ public class ContractManager { addLocalContractLog(action, meta.contract.getID(), meta.name, meta.contract.getOwner()); } - public String startContractAndRedirect(Contract c, PrintStream ps, String alias, boolean isDebug) { + public String startContractAndRedirect(Contract c, PrintStream ps, String alias) { long freeMemory = getFreeMemory(); if (statusRecorder.runningProcess.size() > 5 && (freeMemory < memoryLimit)) { statusRecorder.hangLeastUsedContractProcess(); @@ -821,51 +813,12 @@ public class ContractManager { if (null == c.getOwner()) { c.setOwner(c.getPublicKey()); } - LOGGER.debug("contract pubKey: " + c.getPublicKey()); // 合约启动时读取Manifest文件设置合约DOI setContractDOI(c); setContractStateful(c); - - // if (c.getDoipFlag()) { - // // 合约部署时,更新合约HandleRecord - // long tmpStart = System.currentTimeMillis(); - // try { - // if (c.getDOI() == null || c.getDOI().equals("")) { - // updateContractHandleRecord( // 创建DO - // c, - // new ResultCallback() { - // @Override - // public void onResult(String str) { - // c.setDOI(str); - // // c.setID(str); - // LOGGER.debug("contract DOI: " + c.getDOI()); - // } - // }); - // } else { - // if (findConflictOfDOI(c.getDOI())) { - // r = new ContractResult(Status.Error, "Duplicate contract - // DOI."); - // return JsonUtil.toJson(r); - // } - // } - // } catch (Exception e) { - // ByteArrayOutputStream bo = new ByteArrayOutputStream(); - // e.printStackTrace(new PrintStream(bo)); - // c.setDOI("registerFailed"); - // } - // LOGGER.info("DOIP executeTime: " + (System.currentTimeMillis() - - // tmpStart)); - // } else c.setDOI("registerDisabled"); - - // if (contracts.containsKey(c.getID())) { - // r = new ContractResult(Status.Error, "contract existed"); - // return JsonUtil.toJson(r); - // } - ContractClient client = new ContractClient(c); - client.isDebug = isDebug; String ret; String conflictCheck; addLocalContractLog("startContract", c.getID(), client.contractMeta.name, c.getOwner()); @@ -1054,7 +1007,7 @@ public class ContractManager { } public String startContract(Contract c) { - return startContractAndRedirect(c, System.out); + return startContractAndRedirect(c, System.out, null); } public String queryDEPort(String contractID) { @@ -1309,7 +1262,7 @@ public class ContractManager { rcb.onResult(JsonUtil.parseObjectAsJsonObject(cr)); return; } - } else if (null != request.getPublicKey() && !request.verifySignature()) { + } else if (null != request.getPublicKey() && request.getPublicKey().length() > 30 && !request.verifySignature()) { request.setPublicKey(null); } } @@ -1673,6 +1626,7 @@ public class ContractManager { boolean isNodeManager = checkNodeManager(owner); List ret = new ArrayList<>(); for (ContractMeta client : statusRecorder.getStatus().values()) { + if (client.contract == null) continue; if (!client.contract.getOwner().equals(owner) && !isNodeManager) { continue; } @@ -1805,7 +1759,7 @@ public class ContractManager { public String resetDebugFlag(String contractName, boolean isDebug) { ContractClient client = getByName(contractName); String result = client.get.syncGet("", "changeDebugFlag", String.valueOf(isDebug)); - client.contractMeta.isDebug = isDebug; + client.contractMeta.contract.setDebug(isDebug); return result; } /* diff --git a/src/main/java/org/bdware/sc/ContractMeta.java b/src/main/java/org/bdware/sc/ContractMeta.java index 7b91e9f..ff595c9 100644 --- a/src/main/java/org/bdware/sc/ContractMeta.java +++ b/src/main/java/org/bdware/sc/ContractMeta.java @@ -1,5 +1,7 @@ package org.bdware.sc; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.bdware.sc.bean.Contract; import org.bdware.sc.bean.FunctionDesp; import org.bdware.sc.bean.IDSerializable; @@ -20,7 +22,6 @@ public class ContractMeta implements IDSerializable { ContractStatusEnum status; String name; String id; - boolean isDebug; Map declaredEvents; List exportedFunctions; Map logDetail; @@ -96,7 +97,7 @@ public class ContractMeta implements IDSerializable { // public setMask(){} public boolean getIsDebug() { - return isDebug; + return contract.isDebug(); } public FunctionDesp getExportedFunction(String action) { @@ -124,10 +125,11 @@ public class ContractMeta implements IDSerializable { contract = c; id = c.getID(); status = ContractStatusEnum.HANGED; - isDebug = false; } + static Logger LOGGER = LogManager.getLogger(ContractMeta.class); + public void setContractExecutor(ContractExecutor executor) { this.contractExecutor = executor; } -} +} \ No newline at end of file diff --git a/src/main/java/org/bdware/sc/ContractStatusRecorder.java b/src/main/java/org/bdware/sc/ContractStatusRecorder.java index 88210fa..40ae144 100644 --- a/src/main/java/org/bdware/sc/ContractStatusRecorder.java +++ b/src/main/java/org/bdware/sc/ContractStatusRecorder.java @@ -95,7 +95,7 @@ public class ContractStatusRecorder extends StatusRecorder { } private static String resumeStartContract(Contract contract) { - return ContractManager.instance.startContractAndRedirect(contract, null); + return ContractManager.instance.startContractAndRedirect(contract, null, null); } public boolean hasPID(int pid) { diff --git a/src/main/java/org/bdware/sc/MultiContractRecorder.java b/src/main/java/org/bdware/sc/MultiContractRecorder.java index 4cb3c27..64b8ee4 100644 --- a/src/main/java/org/bdware/sc/MultiContractRecorder.java +++ b/src/main/java/org/bdware/sc/MultiContractRecorder.java @@ -31,12 +31,17 @@ public class MultiContractRecorder extends StatusRecorder { public MultiContractMeta getMultiContractMeta(String idOrNameOrDOI) { if (idOrNameOrDOI == null) return null; ContractMeta meta = ContractManager.instance.statusRecorder.getContractMeta(idOrNameOrDOI); + return getMultiContractMeta(meta); + } + + public MultiContractMeta getMultiContractMeta(ContractMeta meta) { if (meta == null) return null; return getStatus().get(meta.id); } public MultiContractMeta createIfNotExist(String contractID) { - MultiContractMeta ret = getMultiContractMeta(contractID); + ContractMeta meta = ContractManager.instance.statusRecorder.createIfNotExist(contractID); + MultiContractMeta ret = getMultiContractMeta(meta); if (null == ret) { LOGGER.info("requests don't contain contract " + contractID); ret = new MultiContractMeta(contractID); diff --git a/src/main/java/org/bdware/sc/units/MultiContractMeta.java b/src/main/java/org/bdware/sc/units/MultiContractMeta.java index a8fa797..06d5401 100644 --- a/src/main/java/org/bdware/sc/units/MultiContractMeta.java +++ b/src/main/java/org/bdware/sc/units/MultiContractMeta.java @@ -164,10 +164,6 @@ public class MultiContractMeta implements IDSerializable { } return sb.toString(); } - public void setMembers(String[] m) { - members = m; - } - public void setMembers(JsonArray members) { String[] copied = new String[members.size()]; for (int i = 0; i < members.size(); i++) { diff --git a/src/test/java/org/bdware/sc/test/ReflectionTest.java b/src/test/java/org/bdware/sc/test/ReflectionTest.java new file mode 100644 index 0000000..923fadc --- /dev/null +++ b/src/test/java/org/bdware/sc/test/ReflectionTest.java @@ -0,0 +1,20 @@ +package org.bdware.sc.test; + +import com.google.gson.Gson; + +import java.lang.reflect.Method; + +public class ReflectionTest { + public static class Test { + public static void go(String... args) { + System.out.println(new Gson().toJson(args)); + } + } + + @org.junit.Test + public void go() throws Exception { + Method m = Test.class.getDeclaredMethod("go",String[].class); + String[] abc = new String[]{"ab", "cd"}; + m.invoke(null, new Object[]{abc}); + } +}