use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class MsgLog method error.
public static void error(Throwable throwable) {
String logContent = isStringBlank(getId()) ? (getLogTrace() + ":") : (getLogTrace() + "[" + getId() + "]" + ":");
if (!(throwable instanceof NulsException) || !(throwable instanceof NulsRuntimeException)) {
throwable = new NulsException(ErrorCode.FAILED, throwable);
}
LOG.error(logContent, throwable);
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class MsgLog method trace.
/**
* 提供trace级别基本的日志输出
*
* @param msg 需要显示的消息
* @param throwable 异常信息
*/
public static void trace(String msg, Throwable throwable) {
String logContent = isStringBlank(getId()) ? (getLogTrace() + ":" + msg) : (getLogTrace() + "[" + getId() + "]" + ":" + msg);
if (!(throwable instanceof NulsException) || !(throwable instanceof NulsRuntimeException)) {
throwable = new NulsException(ErrorCode.FAILED, throwable);
}
LOG.trace(logContent, throwable);
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class VersionManager method updateFile.
private static void updateFile(String oldVersion, String newVersion, List<NulsVersion> versionList, URL libsUrl) throws NulsException, IOException {
String jsonStr = null;
try {
jsonStr = new String(HttpDownloadUtils.download(VERDION_JSON_URL), NulsContext.DEFAULT_ENCODING);
} catch (IOException e) {
Log.error(e);
throw new NulsException(ErrorCode.FAILED, "Download version json faild!");
}
Map<String, Object> map = null;
try {
map = JSONUtils.json2map(jsonStr);
} catch (Exception e) {
Log.error(e);
throw new NulsException(ErrorCode.FAILED, "Parse version json faild!");
}
String version = (String) map.get("version");
String sign = (String) map.get("sign");
// when the json change at the moment,restart;
if (!newVersion.equals(version)) {
start();
return;
}
Map<String, String> jarMap = new HashMap<>();
fillJarMap(libsUrl, jarMap);
// jarMap is empty means it's development environment
if (jarMap.isEmpty()) {
return;
}
List<Map<String, Object>> libList = (List<Map<String, Object>>) map.get("libs");
// check the temp folder exist,and delete
URL rootUrl = VersionManager.class.getResource("");
File tempFolder = new File(rootUrl.getPath() + "/temp");
if (tempFolder.exists()) {
deleteFile(tempFolder);
}
tempFolder.mkdir();
List<String> newVersionJarList = new ArrayList<>();
for (Map<String, Object> lib : libList) {
String file = (String) lib.get("file");
newVersionJarList.add(file);
String libSign = (String) lib.get("sign");
// todo check sign
if (jarMap.get(file) == null) {
downloadLib(tempFolder.getPath(), file, sign);
}
}
File bakFolder = new File(rootUrl.getPath() + "/bak");
if (bakFolder.exists()) {
deleteFile(bakFolder);
}
bakFolder.mkdir();
List<String> removeList = new ArrayList<>();
try {
for (String key : jarMap.keySet()) {
if (newVersionJarList.contains(key)) {
continue;
}
File jar = new File(jarMap.get(key));
boolean b = jar.renameTo(new File(bakFolder.getPath() + "/" + key));
if (!b) {
throw new NulsException(ErrorCode.FAILED, "move the file fiald:" + key);
}
removeList.add(jar.getName());
}
} catch (NulsException e) {
List<String> failedList = new ArrayList<>();
for (String fileName : removeList) {
File file = new File(bakFolder.getPath() + "/" + fileName);
boolean b = file.renameTo(new File(libsUrl.getPath() + "/" + fileName));
if (!b) {
failedList.add(bakFolder.getPath() + "/" + fileName);
}
}
failedOpration(failedList);
}
File[] files = tempFolder.listFiles();
if (files == null || files.length == 0) {
return;
}
List<String> moved = new ArrayList<>();
try {
for (File file : files) {
boolean b = file.renameTo(new File(libsUrl.getPath() + "/" + file.getName()));
if (!b) {
throw new NulsException(ErrorCode.FAILED, "move the file fiald:" + file.getPath());
}
moved.add(file.getName());
}
} catch (NulsException e) {
for (String fileName : moved) {
File newFile = new File(libsUrl.getPath() + "/" + fileName);
if (newFile.exists()) {
newFile.delete();
}
}
File[] bakFiles = bakFolder.listFiles();
List<String> failedList = new ArrayList<>();
for (File file : bakFiles) {
boolean b = file.renameTo(new File(libsUrl.getPath() + "/" + file.getName()));
if (!b) {
failedList.add(file.getPath());
}
}
failedOpration(failedList);
}
// todo 重启动,或者发送通知
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class EventManager method getInstance.
public static BaseEvent getInstance(byte[] bytes) throws NulsException {
EventHeader header = new EventHeader();
header.parse(new NulsByteBuffer(bytes));
Class<? extends BaseEvent> clazz = EVENT_MAP.get(header.getModuleId() + "_" + header.getEventType());
if (null == clazz) {
return null;
}
BaseEvent event;
try {
event = clazz.newInstance();
} catch (Exception e) {
Log.error(e);
throw new NulsException(ErrorCode.DATA_PARSE_ERROR);
}
try {
event.parse(new NulsByteBuffer(bytes));
} catch (Exception e) {
Log.error(e);
throw e;
}
return event;
}
Aggregations