support interface

This commit is contained in:
CaiHQ 2023-07-24 12:33:23 +08:00
parent 2180260a20
commit 7e8076e075
8 changed files with 72 additions and 19 deletions

View File

@ -8,7 +8,7 @@ plugins {
apply from: '../spotless.gradle'
group = "org.bdware.sc"
version = "1.9.2"
version = "1.9.7"
tasks.withType(JavaCompile) {
// options.compilerArgs << '-Xlint:none'
// options.compilerArgs << '-Xlint:deprecation' << "-Werror"
@ -51,8 +51,8 @@ dependencies {
implementation 'com.sun.mail:javax.mail:1.6.2'
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'org.bdware.bdcontract:sdk-java:1.0.2'
implementation 'org.bdware.doip:doip-audit-tool:1.3.2'
implementation 'org.bdware.doip:doip-sdk:1.4.6'
implementation 'org.bdware.doip:doip-audit-tool:1.3.5'
implementation 'org.bdware.doip:doip-sdk:1.4.9'
implementation fileTree(dir: 'lib', include: '*.jar')
testImplementation 'junit:junit:4.13.2'
}
@ -71,7 +71,7 @@ jar {
// while develop at local use "false"
configurations.runtimeClasspath.filter {
it.getAbsolutePath().contains("/lib/")
// false
// false
}.collect {
it.isDirectory() ? it : zipTree(it)
}

View File

@ -3,6 +3,7 @@ package org.bdware.sc.compiler;
import org.bdware.sc.node.AnnotationNode;
import org.bdware.sc.node.ContractNode;
import org.bdware.sc.node.FunctionNode;
import org.bdware.sc.node.InterfaceNode;
public abstract class AnnotationProcessor {
public void processContract(AnnotationNode anno, ContractNode contractNode) {
@ -13,4 +14,8 @@ public abstract class AnnotationProcessor {
FunctionNode functionNode) throws Exception {
return;
}
public void processInterface(AnnotationNode anno, ContractNode contractNode,
InterfaceNode functionNode) throws Exception {
return;
}
}

View File

@ -34,7 +34,8 @@ public class YJSCompiler {
ContractNode contract;
private static final Logger LOGGER = LogManager.getLogger(YJSCompiler.class);
public YJSCompiler() {}
public YJSCompiler() {
}
public static ScriptFunction compileWithGlobal(Source source, Global global, Context context) {
Global oldGlobal = Context.getGlobal();
@ -55,7 +56,7 @@ public class YJSCompiler {
}
private static Context makeContext(final InputStream in, final OutputStream out,
final OutputStream err) {
final OutputStream err) {
final PrintStream pout =
out instanceof PrintStream ? (PrintStream) out : new PrintStream(out);
final PrintStream perr =
@ -67,7 +68,7 @@ public class YJSCompiler {
final ErrorManager errors = new ErrorManager(werr);
// Set up options.
final Options options = new Options("nashorn", werr);
options.process(new String[] {});
options.process(new String[]{});
// detect scripting mode by any source's first character being '#'
options.set("persistent.code.cache", true);
options.set("print.code", "true");
@ -120,7 +121,7 @@ public class YJSCompiler {
Set<String> todo = new HashSet<>();
Set<String> allEntries = new HashSet<>();
Enumeration<? extends ZipEntry> iter = zf.entries();
for (; iter.hasMoreElements();) {
for (; iter.hasMoreElements(); ) {
ZipEntry ele = iter.nextElement();
if (ele != null)
allEntries.add(ele.getName());
@ -131,21 +132,34 @@ public class YJSCompiler {
continue;
}
ZipEntry entry = zf.getEntry(str.startsWith("/") ? str : "/" + str);
LOGGER.info("load yjs:" + str);
if (null == entry) {
throw new IllegalStateException("missing import:" + str);
}
ContractNode cn = compile(zf.getInputStream(entry), str);
String cnPath = entry.getName();
int i = cnPath.lastIndexOf("/");
String cnDir = "";
if (i != -1) cnDir = cnPath.substring(0, i);
czb.put(str, cn);
for (ImportNode in : cn.getImports()) {
for (String enstr : allEntries)
if (enstr.startsWith(in.getPath()) && enstr.endsWith(".yjs"))
todo.add(enstr);
String path = in.getPath();
if (!path.startsWith("/"))
path = cnDir + "/" + path;
path = path.replaceAll("/\\./", "/");
todo.add(path);
}
}
toParse.clear();
for (String str : todo) {
if (!czb.containsPath(str)) {
if (allEntries.contains(str))
toParse.add(str);
else {
//TODO parse manifest.json first?
for (String entry : allEntries)
if (!czb.containsPath(entry) && entry.startsWith(str) && entry.endsWith(".yjs")) {
toParse.add(entry);
}
}
}
todo.clear();
@ -167,7 +181,7 @@ public class YJSCompiler {
+ " function getGlobal () { return Global; }}";
czb.put(globalBeanName + ".yjs",
compile(new ByteArrayInputStream(
globalBeanContract.getBytes(StandardCharsets.UTF_8)),
globalBeanContract.getBytes(StandardCharsets.UTF_8)),
globalBeanName + ".yjs"));
LOGGER.info("--compile-- " + globalBeanName);
czb.setMergedContractNode();
@ -217,6 +231,16 @@ public class YJSCompiler {
processor.processFunction(anno, contractNode, functionNode);
}
}
for (InterfaceNode interfaceNode : contractNode.getInterfaces()) {
List<AnnotationNode> annos = interfaceNode.annotations;// 函数里的annotation
if (annos != null)
for (AnnotationNode anno : annos) {
AnnotationProcessor processor = findProcessor(anno);
if (processor != null)
processor.processInterface(anno, contractNode, interfaceNode);
}
}
}
public static AnnotationProcessor findProcessor(AnnotationNode node) {

View File

@ -5,6 +5,7 @@ import org.bdware.sc.compiler.AnnotationProcessor;
import org.bdware.sc.node.AnnotationNode;
import org.bdware.sc.node.ContractNode;
import org.bdware.sc.node.FunctionNode;
import org.bdware.sc.node.InterfaceNode;
// DOOP is designed for DoipModule which contains specific functions for RepositoryHandler
@ -17,4 +18,10 @@ public class DOOP extends AnnotationProcessor {
functionNode.setIsDoipOperation(true);
functionNode.setDoipOperationInfo(DoipOperationInfo.create(anno, contractNode));
}
@Override
public void processInterface(AnnotationNode anno, ContractNode contractNode,
InterfaceNode interfaceNode) throws Exception {
interfaceNode.setIsDoipOperation(true);
interfaceNode.setDoipOperationInfo(DoipOperationInfo.create(anno, contractNode));
}
}

View File

@ -5,6 +5,7 @@ import org.bdware.sc.compiler.AnnotationProcessor;
import org.bdware.sc.node.AnnotationNode;
import org.bdware.sc.node.ContractNode;
import org.bdware.sc.node.FunctionNode;
import org.bdware.sc.node.InterfaceNode;
public class Join extends AnnotationProcessor {
@Override
@ -14,4 +15,9 @@ public class Join extends AnnotationProcessor {
// 增加标记在ContractNode中记录Join相关的函数和Join规则
functionNode.setJoinInfo(JoinInfo.create(anno, contractNode));
}
@Override
public void processInterface(AnnotationNode anno, ContractNode contractNode,
InterfaceNode interfaceNode) {
interfaceNode.setJoinInfo(JoinInfo.create(anno, contractNode));
}
}

View File

@ -5,6 +5,7 @@ import org.bdware.sc.compiler.AnnotationProcessor;
import org.bdware.sc.node.AnnotationNode;
import org.bdware.sc.node.ContractNode;
import org.bdware.sc.node.FunctionNode;
import org.bdware.sc.node.InterfaceNode;
public class Route extends AnnotationProcessor {
@Override
@ -12,4 +13,9 @@ public class Route extends AnnotationProcessor {
FunctionNode functionNode) {
functionNode.setRouteInfo(RouteInfo.create(anno, contractNode));
}
@Override
public void processInterface(AnnotationNode anno, ContractNode contractNode,
InterfaceNode interfaceNode) {
interfaceNode.setRouteInfo(RouteInfo.create(anno, contractNode));
}
}

View File

@ -4,6 +4,7 @@ import com.google.gson.JsonObject;
import io.netty.channel.ChannelHandlerContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.codec.JsonDoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
@ -17,6 +18,7 @@ import org.bdware.sc.boundry.JavaScriptEntry;
import org.bdware.sc.crdt.SharableVarManager;
import org.bdware.sc.entity.DoipMessagePacker;
import org.bdware.sc.node.FunctionNode;
import org.bdware.sc.util.JsonUtil;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
@ -100,6 +102,7 @@ public class DOOPRequestHandler implements DoipRequestHandler {
cr.setRequester(request.credential.getSigner());
}
cr.setAction(fn.functionName);
cr.setArg(JsonUtil.parseObjectAsJsonObject(JsonDoipMessage.fromDoipMessage(request)));
return cr;
}
}

View File

@ -80,23 +80,25 @@ public class DoipLocalSingleton {
config.privateKey = keyPair.getPrivateKeyStr();
config.publicKey = keyPair.getPublicKeyStr();
}
if (config.routerURI != null) {
if (config.routerURI != null && config.repoName != null) {
AuditIrpClient irpClient = new AuditIrpClient(config);
EndpointInfo endpointInfo = irpClient.getEndpointInfo();
repoID = endpointInfo.getDoId();
owner = endpointInfo.getPubKey();
infos.clear();
infos.add(new DoipListenerConfig(endpointInfo.getURI(), "2.1"));
port = new URI(endpointInfo.getURI()).getPort();
}
}
DoipServiceInfo info = new DoipServiceInfo(repoID, owner, repoType, infos);
server = new DoipServerImpl(info);
DOOPRequestHandler handler = ContractProcess.instance.doopRequestHandler;
server.setRequestCallback(handler);
if (config != null)
SharableVarManager.initSharableVarManager(info.id, config);
} catch (Exception e) {
e.printStackTrace();
}
DoipServiceInfo info = new DoipServiceInfo(repoID, owner, repoType, infos);
server = new DoipServerImpl(info);
DOOPRequestHandler handler = ContractProcess.instance.doopRequestHandler;
server.setRequestCallback(handler);
SharableVarManager.initSharableVarManager(info.id, config);
ResultChecker checker = new ResultChecker();
server.start(checker);
checker.waitForResult(1000);