From 80ce09134289cb935e27e9e996e0521b358facce Mon Sep 17 00:00:00 2001 From: gongdewei Date: Thu, 21 May 2020 22:02:02 +0800 Subject: [PATCH] transform command: history --- .../command/basic1000/HistoryCommand.java | 11 +++ .../core/command/model/HistoryModel.java | 27 +++++++ .../core/shell/history/HistoryManager.java | 21 ++++++ .../history/impl/HistoryManagerImpl.java | 70 +++++++++++++++++++ .../taobao/arthas/core/util/FileUtils.java | 49 +++++++++++++ 5 files changed, 178 insertions(+) create mode 100644 core/src/main/java/com/taobao/arthas/core/command/model/HistoryModel.java create mode 100644 core/src/main/java/com/taobao/arthas/core/shell/history/HistoryManager.java create mode 100644 core/src/main/java/com/taobao/arthas/core/shell/history/impl/HistoryManagerImpl.java diff --git a/core/src/main/java/com/taobao/arthas/core/command/basic1000/HistoryCommand.java b/core/src/main/java/com/taobao/arthas/core/command/basic1000/HistoryCommand.java index f2c919fdd..a0a407a10 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/basic1000/HistoryCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/basic1000/HistoryCommand.java @@ -4,8 +4,11 @@ import java.util.ArrayList; import java.util.List; import com.taobao.arthas.core.command.Constants; +import com.taobao.arthas.core.command.model.HistoryModel; import com.taobao.arthas.core.shell.command.AnnotatedCommand; import com.taobao.arthas.core.shell.command.CommandProcess; +import com.taobao.arthas.core.shell.history.HistoryManager; +import com.taobao.arthas.core.shell.history.impl.HistoryManagerImpl; import com.taobao.arthas.core.shell.session.Session; import com.taobao.arthas.core.shell.term.impl.TermImpl; import com.taobao.middleware.cli.annotations.Argument; @@ -69,6 +72,14 @@ public class HistoryCommand extends AnnotatedCommand { process.write(sb.toString()); } + } else { + if (clear) { + HistoryManagerImpl.getInstance().clearHistory(); + } else { + //http api + List history = HistoryManagerImpl.getInstance().getHistory(); + process.appendResult(new HistoryModel(new ArrayList(history))); + } } process.end(); diff --git a/core/src/main/java/com/taobao/arthas/core/command/model/HistoryModel.java b/core/src/main/java/com/taobao/arthas/core/command/model/HistoryModel.java new file mode 100644 index 000000000..34e545d8e --- /dev/null +++ b/core/src/main/java/com/taobao/arthas/core/command/model/HistoryModel.java @@ -0,0 +1,27 @@ +package com.taobao.arthas.core.command.model; + +import java.util.List; + +/** + * @author gongdewei 2020/4/8 + */ +public class HistoryModel extends ResultModel { + + private List history; + + public HistoryModel() { + } + + public HistoryModel(List history) { + this.history = history; + } + + public List getHistory() { + return history; + } + + @Override + public String getType() { + return "history"; + } +} diff --git a/core/src/main/java/com/taobao/arthas/core/shell/history/HistoryManager.java b/core/src/main/java/com/taobao/arthas/core/shell/history/HistoryManager.java new file mode 100644 index 000000000..6b01c5c79 --- /dev/null +++ b/core/src/main/java/com/taobao/arthas/core/shell/history/HistoryManager.java @@ -0,0 +1,21 @@ +package com.taobao.arthas.core.shell.history; + +import java.util.List; + +/** + * @author gongdewei 2020/4/8 + */ +public interface HistoryManager { + + void addHistory(String commandLine); + + List getHistory(); + + void setHistory(List history); + + void saveHistory(); + + void loadHistory(); + + void clearHistory(); +} 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 new file mode 100644 index 000000000..26f6dabd1 --- /dev/null +++ b/core/src/main/java/com/taobao/arthas/core/shell/history/impl/HistoryManagerImpl.java @@ -0,0 +1,70 @@ +package com.taobao.arthas.core.shell.history.impl; + +import com.taobao.arthas.core.shell.history.HistoryManager; +import com.taobao.arthas.core.util.Constants; +import com.taobao.arthas.core.util.FileUtils; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * @author gongdewei 2020/4/8 + */ +public class HistoryManagerImpl implements HistoryManager { + /** + * The max number of history item that will be saved in memory. + */ + private static final int MAX_HISTORY_SIZE = 500; + + private List history = new ArrayList(); + + private static HistoryManager instance; + + public static HistoryManager getInstance() { + if (instance == null) { + synchronized (HistoryManagerImpl.class) { + instance = new HistoryManagerImpl(); + } + } + return instance; + } + + private HistoryManagerImpl() { + } + + @Override + public void saveHistory() { + FileUtils.saveCommandHistoryString(history, new File(Constants.CMD_HISTORY_FILE)); + } + + @Override + public void loadHistory() { + history = FileUtils.loadCommandHistoryString(new File(Constants.CMD_HISTORY_FILE)); + } + + @Override + public void clearHistory() { + this.history.clear(); + } + + @Override + public void addHistory(String commandLine) { + while (history.size() >= MAX_HISTORY_SIZE) { + history.remove(0); + } + history.add(commandLine); + } + + @Override + public List getHistory() { + return history; + } + + @Override + public void setHistory(List history) { + this.history = history; + } + + +} 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 973acc23e..c2aba4ad7 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 @@ -148,6 +148,55 @@ public class FileUtils { return history; } + /** + * save the command history to the given file, data will be overridden. + * @param history the command history + * @param file the file to save the history + */ + public static void saveCommandHistoryString(List history, File file) { + OutputStream out = null; + try { + out = new BufferedOutputStream(openOutputStream(file, false)); + for (String command: history) { + out.write(command.getBytes("utf-8")); + out.write('\n'); + } + } catch (IOException e) { + // ignore + } finally { + try { + if (out != null) { + out.close(); + } + } catch (IOException ioe) { + // ignore + } + } + } + + public static List loadCommandHistoryString(File file) { + BufferedReader br = null; + List history = new ArrayList(); + try { + br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8")); + String line; + while ((line = br.readLine()) != null) { + history.add(line); + } + } catch (IOException e) { + // ignore + } finally { + try { + if (br != null) { + br.close(); + } + } catch (IOException ioe) { + // ignore + } + } + return history; + } + public static String readFileToString(File file, Charset encoding) throws IOException { FileInputStream stream = new FileInputStream(file); try {