mirror of
https://gitee.com/arthas/arthas.git
synced 2024-11-30 03:07:37 +08:00
This commit is contained in:
parent
2d7e19f133
commit
1376c88663
@ -179,7 +179,6 @@ public class ArthasBootstrap {
|
||||
|
||||
private void initBeans() {
|
||||
this.resultViewResolver = new ResultViewResolver();
|
||||
|
||||
this.historyManager = new HistoryManagerImpl();
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user