diff --git a/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java b/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java index 7031c987f..a627da9a0 100644 --- a/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java +++ b/core/src/main/java/com/taobao/arthas/core/server/ArthasBootstrap.java @@ -179,7 +179,6 @@ public class ArthasBootstrap { private void initBeans() { this.resultViewResolver = new ResultViewResolver(); - this.historyManager = new HistoryManagerImpl(); } diff --git a/core/src/main/java/com/taobao/arthas/core/shell/history/impl/HistoryManagerImpl.java b/core/src/main/java/com/taobao/arthas/core/shell/history/impl/HistoryManagerImpl.java index 5b22f79bd..c6853a9b7 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/history/impl/HistoryManagerImpl.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/history/impl/HistoryManagerImpl.java @@ -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 history = new ArrayList(); 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 getHistory() { - return history; + public synchronized List getHistory() { + return new ArrayList(history); } @Override - public void setHistory(List history) { + public synchronized void setHistory(List history) { this.history = history; } - } diff --git a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/api/HttpApiHandler.java b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/api/HttpApiHandler.java index 158e67791..23c3a8ae7 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/api/HttpApiHandler.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/api/HttpApiHandler.java @@ -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); } diff --git a/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java b/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java index c2aba4ad7..4b640b156 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java +++ b/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java @@ -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