fix save command history npe, improve load/save history logic #1704 (#1705)

This commit is contained in:
gongdewei 2021-02-23 02:54:47 -06:00 committed by GitHub
parent 2d7e19f133
commit 1376c88663
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 15 deletions

View File

@ -179,7 +179,6 @@ public class ArthasBootstrap {
private void initBeans() {
this.resultViewResolver = new ResultViewResolver();
this.historyManager = new HistoryManagerImpl();
}

View File

@ -1,5 +1,7 @@
package com.taobao.arthas.core.shell.history.impl;
import com.alibaba.arthas.deps.org.slf4j.Logger;
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;
import com.taobao.arthas.core.shell.history.HistoryManager;
import com.taobao.arthas.core.util.Constants;
import com.taobao.arthas.core.util.FileUtils;
@ -17,28 +19,38 @@ public class HistoryManagerImpl implements HistoryManager {
*/
private static final int MAX_HISTORY_SIZE = 500;
private static final Logger logger = LoggerFactory.getLogger(HistoryManagerImpl.class);
private List<String> history = new ArrayList<String>();
public HistoryManagerImpl() {
}
@Override
public void saveHistory() {
FileUtils.saveCommandHistoryString(history, new File(Constants.CMD_HISTORY_FILE));
public synchronized void saveHistory() {
try {
FileUtils.saveCommandHistoryString(history, new File(Constants.CMD_HISTORY_FILE));
} catch (Throwable e) {
logger.error("save command history failed", e);
}
}
@Override
public void loadHistory() {
history = FileUtils.loadCommandHistoryString(new File(Constants.CMD_HISTORY_FILE));
public synchronized void loadHistory() {
try {
history = FileUtils.loadCommandHistoryString(new File(Constants.CMD_HISTORY_FILE));
} catch (Throwable e) {
logger.error("load command history failed", e);
}
}
@Override
public void clearHistory() {
public synchronized void clearHistory() {
this.history.clear();
}
@Override
public void addHistory(String commandLine) {
public synchronized void addHistory(String commandLine) {
while (history.size() >= MAX_HISTORY_SIZE) {
history.remove(0);
}
@ -46,14 +58,13 @@ public class HistoryManagerImpl implements HistoryManager {
}
@Override
public List<String> getHistory() {
return history;
public synchronized List<String> getHistory() {
return new ArrayList<String>(history);
}
@Override
public void setHistory(List<String> history) {
public synchronized void setHistory(List<String> history) {
this.history = history;
}
}

View File

@ -586,7 +586,6 @@ public class HttpApiHandler {
private Job createJob(String line, Session session, ResultDistributor resultDistributor) {
historyManager.addHistory(line);
historyManager.saveHistory();
return createJob(CliTokens.tokenize(line), session, resultDistributor);
}

View File

@ -158,8 +158,10 @@ public class FileUtils {
try {
out = new BufferedOutputStream(openOutputStream(file, false));
for (String command: history) {
out.write(command.getBytes("utf-8"));
out.write('\n');
if (!StringUtils.isBlank(command)) {
out.write(command.getBytes("utf-8"));
out.write('\n');
}
}
} catch (IOException e) {
// ignore
@ -181,7 +183,9 @@ public class FileUtils {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
String line;
while ((line = br.readLine()) != null) {
history.add(line);
if (!StringUtils.isBlank(line)) {
history.add(line);
}
}
} catch (IOException e) {
// ignore