limit history commands size

This commit is contained in:
Jarred Li
2012-04-14 11:43:01 +08:00
parent 5dca1a102a
commit f12f986e1f
2 changed files with 62 additions and 47 deletions

View File

@@ -50,11 +50,10 @@
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>1.2.14</version> <version>1.2.14</version>
</dependency> </dependency>
<!-- consider removing dependency on commons-io -->
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>1.4</version> <version>2.3</version>
</dependency> </dependency>
<!-- External modules --> <!-- External modules -->

View File

@@ -27,6 +27,7 @@ import jline.ANSIBuffer.ANSICodes;
import jline.ConsoleReader; import jline.ConsoleReader;
import jline.WindowsTerminal; import jline.WindowsTerminal;
import org.apache.commons.io.input.ReversedLinesFileReader;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@@ -40,7 +41,6 @@ import org.springframework.roo.shell.event.ShellStatus.Status;
import org.springframework.roo.shell.event.ShellStatusListener; import org.springframework.roo.shell.event.ShellStatusListener;
import org.springframework.roo.support.util.Assert; import org.springframework.roo.support.util.Assert;
import org.springframework.roo.support.util.ClassUtils; import org.springframework.roo.support.util.ClassUtils;
import org.springframework.roo.support.util.FileCopyUtils;
import org.springframework.roo.support.util.IOUtils; import org.springframework.roo.support.util.IOUtils;
import org.springframework.roo.support.util.OsUtils; import org.springframework.roo.support.util.OsUtils;
import org.springframework.roo.support.util.StringUtils; import org.springframework.roo.support.util.StringUtils;
@@ -63,8 +63,7 @@ import org.springframework.shell.plugin.PromptProvider;
* @author Jarred Li * @author Jarred Li
* @since 1.0 * @since 1.0
*/ */
public abstract class JLineShell extends AbstractShell public abstract class JLineShell extends AbstractShell implements CommandMarker, Shell, Runnable {
implements CommandMarker, Shell, Runnable {
// Constants // Constants
private static final String ANSI_CONSOLE_CLASSNAME = "org.fusesource.jansi.AnsiConsole"; private static final String ANSI_CONSOLE_CLASSNAME = "org.fusesource.jansi.AnsiConsole";
@@ -87,14 +86,14 @@ public abstract class JLineShell extends AbstractShell
private ApplicationContext applicatonContext; private ApplicationContext applicatonContext;
private boolean printBanner = true; private boolean printBanner = true;
private static AnnotationAwareOrderComparator annocationOrderComparator = new AnnotationAwareOrderComparator(); private static AnnotationAwareOrderComparator annocationOrderComparator = new AnnotationAwareOrderComparator();
private String historyFileName; private String historyFileName;
private String promptText; private String promptText;
private String version; private String version;
private String welcomeMessage; private String welcomeMessage;
private int historySize; private int historySize;
public void run() { public void run() {
@@ -134,21 +133,10 @@ public abstract class JLineShell extends AbstractShell
openFileLogIfPossible(); openFileLogIfPossible();
// Try to build previous command history from the project's log // Try to build previous command history from the project's log
try {
String logFileContents = FileCopyUtils.copyToString(new File(this.historyFileName)); String[] filteredLogEntries = filterLogEntry();
String[] logEntries = logFileContents.split(StringUtils.LINE_SEPARATOR); for (String logEntry : filteredLogEntries) {
// LIFO reader.getHistory().addToHistory(logEntry);
int size = 0;
for (String logEntry : logEntries) {
if (!logEntry.startsWith("//")) {
reader.getHistory().addToHistory(logEntry);
size++;
if(size > historySize){
break;
}
}
}
} catch (IOException ignored) {
} }
flashMessageRenderer(); flashMessageRenderer();
@@ -171,7 +159,7 @@ public abstract class JLineShell extends AbstractShell
}, "Spring Roo JLine Shutdown Hook")); }, "Spring Roo JLine Shutdown Hook"));
// Handle any "execute-then-quit" operation // Handle any "execute-then-quit" operation
String rooArgs = System.getProperty("roo.args"); String rooArgs = System.getProperty("roo.args");
if (rooArgs != null && !"".equals(rooArgs)) { if (rooArgs != null && !"".equals(rooArgs)) {
setShellStatus(Status.USER_INPUT); setShellStatus(Status.USER_INPUT);
@@ -187,12 +175,41 @@ public abstract class JLineShell extends AbstractShell
// Normal RPEL processing // Normal RPEL processing
promptLoop(); promptLoop();
} }
} }
public void printBannerAndWelcome(){ /**
if(printBanner){ * read history commands from history log. the history size if determined by --histsize options.
logger.info(this.version); *
* @return history commands
*/
private String[] filterLogEntry() {
ArrayList<String> entries = new ArrayList<String>();
try {
ReversedLinesFileReader reader = new ReversedLinesFileReader(new File(this.historyFileName));
int size = 0;
String line = null;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("//")) {
size++;
if (size > historySize) {
break;
}
else {
entries.add(line);
}
}
}
} catch (IOException e) {
logger.warning("read history file failed");
}
Collections.reverse(entries);
return entries.toArray(new String[0]);
}
public void printBannerAndWelcome() {
if (printBanner) {
logger.info(this.version);
logger.info(getWelcomeMessage()); logger.info(getWelcomeMessage());
} }
} }
@@ -545,73 +562,72 @@ public abstract class JLineShell extends AbstractShell
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicatonContext = applicationContext; this.applicatonContext = applicationContext;
} }
public void costomizePlugin(){ public void costomizePlugin() {
this.historyFileName = getHistoryFileName(); this.historyFileName = getHistoryFileName();
this.promptText = getPromptText(); this.promptText = getPromptText();
this.version = getBannerText()[0]; this.version = getBannerText()[0];
this.welcomeMessage = getBannerText()[1]; this.welcomeMessage = getBannerText()[1];
} }
/** /**
* get history file name from provider. The provider has highest order * get history file name from provider. The provider has highest order
* <link>org.springframework.core.Ordered.getOder</link> will win. * <link>org.springframework.core.Ordered.getOder</link> will win.
* *
* @return history file name * @return history file name
*/ */
private String getHistoryFileName() { private String getHistoryFileName() {
return getHighestPriorityProvider(HistoryFileNameProvider.class).getHistoryFileName(); return getHighestPriorityProvider(HistoryFileNameProvider.class).getHistoryFileName();
} }
/** /**
* get prompt text from provider. The provider has highest order * get prompt text from provider. The provider has highest order
* <link>org.springframework.core.Ordered.getOder</link> will win. * <link>org.springframework.core.Ordered.getOder</link> will win.
* *
* @return prompt text * @return prompt text
*/ */
private String getPromptText(){ private String getPromptText() {
return getHighestPriorityProvider(PromptProvider.class).getPromptText(); return getHighestPriorityProvider(PromptProvider.class).getPromptText();
} }
/** /**
* Get Banner and Welcome Message from provider. The provider has highest order * Get Banner and Welcome Message from provider. The provider has highest order
* <link>org.springframework.core.Ordered.getOder</link> will win. * <link>org.springframework.core.Ordered.getOder</link> will win.
* @return BannerText[0]: Banner * @return BannerText[0]: Banner
* BannerText[1]: Welcome Message. * BannerText[1]: Welcome Message.
*/ */
private String[] getBannerText(){ private String[] getBannerText() {
String[] bannerText = new String[2]; String[] bannerText = new String[2];
BannerProvider provider = getHighestPriorityProvider(BannerProvider.class); BannerProvider provider = getHighestPriorityProvider(BannerProvider.class);
bannerText[0] = provider.getBanner(); bannerText[0] = provider.getBanner();
bannerText[1] = provider.getWelcomMessage(); bannerText[1] = provider.getWelcomMessage();
return bannerText; return bannerText;
} }
private <T extends PluginProvider> T getHighestPriorityProvider(Class<T> t){ private <T extends PluginProvider> T getHighestPriorityProvider(Class<T> t) {
Map<String, T> providers = BeanFactoryUtils.beansOfTypeIncludingAncestors( Map<String, T> providers = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.applicatonContext, t);
this.applicatonContext, t);
List<T> sortedProviders = new ArrayList<T>(providers.values()); List<T> sortedProviders = new ArrayList<T>(providers.values());
Collections.sort(sortedProviders, annocationOrderComparator); Collections.sort(sortedProviders, annocationOrderComparator);
T highestPriorityProvider = sortedProviders.get(0); T highestPriorityProvider = sortedProviders.get(0);
return highestPriorityProvider; return highestPriorityProvider;
} }
/** /**
* get the version information * get the version information
* *
*/ */
@Override @Override
public String version(String text){ public String version(String text) {
return this.version; return this.version;
} }
/** /**
* get the welcome message at start. * get the welcome message at start.
* *
* @return welcome message * @return welcome message
*/ */
public String getWelcomeMessage(){ public String getWelcomeMessage() {
return this.welcomeMessage; return this.welcomeMessage;
} }
@@ -636,5 +652,5 @@ public abstract class JLineShell extends AbstractShell
public void setHistorySize(int historySize) { public void setHistorySize(int historySize) {
this.historySize = historySize; this.historySize = historySize;
} }
} }