mirror of
https://gitee.com/BDWare/consistency-sdk
synced 2025-01-09 17:34:12 +00:00
initial commit
This commit is contained in:
commit
a2fa767d89
26
.gitignore
vendored
Normal file
26
.gitignore
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
/build/
|
||||
/testoutput/
|
||||
*/build/*
|
||||
# Compiled class file
|
||||
*.class
|
||||
.DS_Store
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.nar
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
38
build.gradle
Normal file
38
build.gradle
Normal file
@ -0,0 +1,38 @@
|
||||
plugins {
|
||||
id 'java-library'
|
||||
}
|
||||
|
||||
group 'com.bdware.sc'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
main {
|
||||
java {
|
||||
srcDir 'src/main/java'
|
||||
}
|
||||
resources {
|
||||
srcDir 'src/main/resources'
|
||||
}
|
||||
}
|
||||
test {
|
||||
java {
|
||||
srcDir 'src/test/java'
|
||||
}
|
||||
resources {
|
||||
srcDir 'src/test/resources'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api project(":cm")
|
||||
api project(":front-base")
|
||||
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
}
|
@ -0,0 +1,176 @@
|
||||
package org.bdware.sdk.consistency;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.bean.ContractExecType;
|
||||
import org.bdware.sdk.consistency.api.ContractExecutorFactory;
|
||||
import org.bdware.sdk.consistency.api.context.ISDKContext;
|
||||
import org.bdware.server.CMDConf;
|
||||
import org.bdware.server.trustedmodel.ContractExecutor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
public class ConsistencyPluginManager {
|
||||
private static final Logger LOGGER = LogManager.getLogger(ConsistencyPluginManager.class);
|
||||
|
||||
private static final String CONFIG_PATH = "cmconfig.json";
|
||||
|
||||
private ISDKContext SDKContext;
|
||||
|
||||
public static void setContext(ISDKContext SDKContext) {
|
||||
getInstance().SDKContext = SDKContext;
|
||||
}
|
||||
|
||||
public static ConsistencyPluginManager getInstance() {
|
||||
return Inner.instance;
|
||||
}
|
||||
|
||||
public static ISDKContext getContext() {
|
||||
return getInstance().SDKContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 静态内部类单例 懒加载
|
||||
*/
|
||||
private static class Inner {
|
||||
private static final ConsistencyPluginManager instance = new ConsistencyPluginManager();
|
||||
}
|
||||
|
||||
private final Map<String, ContractExecutorFactory> factoriesMap = new HashMap<>();
|
||||
|
||||
private URLClassLoader urlClassLoader;
|
||||
|
||||
private ConsistencyPluginManager() {
|
||||
loadPlugins();
|
||||
}
|
||||
|
||||
private void loadPlugins() {
|
||||
// 读取配置
|
||||
CMDConf cmdConf = getCmdConf();
|
||||
if (cmdConf.consistencyPlugins.isEmpty()) {
|
||||
LOGGER.info("Consistency SDK: no plugin detected");
|
||||
return;
|
||||
}
|
||||
|
||||
// 装配urlClassLoader
|
||||
String[] consistencyPluginURLs = cmdConf.consistencyPlugins.split(",");
|
||||
URL[] urls = new URL[consistencyPluginURLs.length];
|
||||
for (int i = 0; i < consistencyPluginURLs.length; i++) {
|
||||
try {
|
||||
if(consistencyPluginURLs[i].endsWith(".jar")) {
|
||||
urls[i] = new URL("file:" + consistencyPluginURLs[i]);
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
urlClassLoader = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
|
||||
|
||||
// 扫描jar包 转载插件
|
||||
for (String path : consistencyPluginURLs) {
|
||||
parseJar(path);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
private CMDConf getCmdConf() {
|
||||
File confFile = new File(CONFIG_PATH);
|
||||
File confTemplate = new File(CONFIG_PATH + ".template");
|
||||
if (!confTemplate.exists()) {
|
||||
CMDConf conf = new CMDConf();
|
||||
conf.write(confTemplate.getAbsolutePath());
|
||||
}
|
||||
if (!confFile.exists() && confTemplate.exists()) {
|
||||
try {
|
||||
FileUtils.copyFile(confTemplate, confFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return CMDConf.parseFile(CONFIG_PATH);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从jar包中解析ContractExecutorFactory子类
|
||||
*
|
||||
* @param path
|
||||
*/
|
||||
private void parseJar(String path) {
|
||||
try {
|
||||
File file = new File(path);
|
||||
if (file.exists()) {
|
||||
JarFile jarFile = new JarFile(file.getCanonicalPath());
|
||||
Enumeration<JarEntry> enumeration = jarFile.entries();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
JarEntry entry = enumeration.nextElement();
|
||||
String entryName = entry.getName();
|
||||
if (entryName.endsWith(".class") && entryName.lastIndexOf(".") >= entryName.lastIndexOf("/")) {
|
||||
try {
|
||||
String className = entryName.replaceAll("/", ".").substring(0, entryName.length() - 6);
|
||||
Class<?> clazz = urlClassLoader.loadClass(className);
|
||||
// ContractExecutorFactory子类 && 非抽象类
|
||||
if (ContractExecutorFactory.class.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers())) {
|
||||
ContractExecutorFactory factory = (ContractExecutorFactory)(clazz.newInstance());
|
||||
factoriesMap.put(factory.getExecutorName(), factory);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public ContractExecutor createContractExecutor(String name, Map<String, Object> args) {
|
||||
return factoriesMap.containsKey(name) ? factoriesMap.get(name).getInstance(args) : null;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public ContractExecutor createContractExecutor(ContractExecType type, Map<String, Object> args) {
|
||||
String name = "";
|
||||
switch (type) {
|
||||
case Sole:
|
||||
name = "Sole";
|
||||
break;
|
||||
case PBFT:
|
||||
name = "PBFT";
|
||||
break;
|
||||
case Sharding:
|
||||
name = "Sharding";
|
||||
break;
|
||||
case RequestOnce:
|
||||
name = "RequestOnce";
|
||||
break;
|
||||
case ResponseOnce:
|
||||
name = "ResponseOnce";
|
||||
break;
|
||||
case SelfAdaptiveSharding:
|
||||
name = "SASharding";
|
||||
break;
|
||||
case RequestAllResponseAll:
|
||||
name = "RARA";
|
||||
break;
|
||||
case RequestAllResponseHalf:
|
||||
name = "RARH";
|
||||
break;
|
||||
case RequestAllResponseFirst:
|
||||
name = "RARF";
|
||||
break;
|
||||
}
|
||||
return createContractExecutor(name, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package org.bdware.sdk.consistency.api;
|
||||
|
||||
import org.bdware.server.trustedmodel.ContractExecutor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface ContractExecutorFactory {
|
||||
String getExecutorName();
|
||||
|
||||
ContractExecutor getInstance(Map<String, Object> args);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package org.bdware.sdk.consistency.api;
|
||||
|
||||
public interface NotifiableResultMerger {
|
||||
String getContractID();
|
||||
|
||||
String getInfo();
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package org.bdware.sdk.consistency.api.context;
|
||||
|
||||
import org.bdware.sc.ContractManager;
|
||||
|
||||
public interface ICMActions {
|
||||
ContractManager getManager();
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package org.bdware.sdk.consistency.api.context;
|
||||
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
|
||||
public interface IGlobalConf {
|
||||
String getNodeID();
|
||||
|
||||
SM2KeyPair getKeyPair();
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package org.bdware.sdk.consistency.api.context;
|
||||
|
||||
import org.bdware.sc.units.RecoverFlag;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface IMasterServerRecoverMechAction {
|
||||
Map<String, Map<String, RecoverFlag>> getRecoverStatusMap();
|
||||
|
||||
void restartContractFromCommonMode(String nodeID, String contractID);
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package org.bdware.sdk.consistency.api.context;
|
||||
|
||||
import org.bdware.sc.units.RequestCache;
|
||||
import org.bdware.server.action.SyncResult;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface IMasterServerTCPAction {
|
||||
SyncResult getSync();
|
||||
|
||||
Map<String, RequestCache> getReqCache();
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package org.bdware.sdk.consistency.api.context;
|
||||
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
|
||||
public interface INetworkManager {
|
||||
void sendToAgent(String pubkey, String content);
|
||||
|
||||
boolean hasAgentConnection(String pubKey);
|
||||
|
||||
ResultCallback createResultCallback(String requestID, ResultCallback rc, int count);
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package org.bdware.sdk.consistency.api.context;
|
||||
|
||||
public interface ISDKContext {
|
||||
IMasterServerTCPAction getMasterServerTCPAction();
|
||||
|
||||
INetworkManager getNetworkManager();
|
||||
|
||||
ICMActions getCMActions();
|
||||
|
||||
IMasterServerRecoverMechAction getMasterServerRecoverMechAction();
|
||||
|
||||
IGlobalConf getGlobalConf();
|
||||
}
|
Loading…
Reference in New Issue
Block a user