Skip to content

Commit

Permalink
SHl-22 limit history size, remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
leejianwei committed Apr 14, 2012
1 parent 02ab62b commit 5dca1a1
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 82 deletions.
2 changes: 1 addition & 1 deletion samples/helloworld/pom.xml
Expand Up @@ -88,7 +88,7 @@
<id>spring-maven-snapshot</id>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<!-- <updatePolicy>always</updatePolicy> -->
</snapshots>
<name>Springframework Maven SNAPSHOT Repository</name>
<url>http://maven.springframework.org/snapshot</url>
Expand Down
Expand Up @@ -17,7 +17,9 @@ public boolean isCommandAvailable() {

@CliCommand(value = "hw echo", help = "Print a hello world message")
public void config(
@CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message) {
System.out.println("Hello world " + message);
@CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message,
@CliOption(key = { "name" }, mandatory = true, help = "The hello world name ") final String name,
@CliOption(key = { "time" }, mandatory = false, help = "The hello world time ") final String time) {
System.out.println("Hello world " + message + "," + name + ". time:" + time);
}
}
Expand Up @@ -270,7 +270,7 @@ private Set<CliOption> getCliOptions(final Annotation[][] parameterAnnotations)
}

protected void commandNotFound(final Logger logger, final String buffer) {
logger.warning("Command '" + buffer + "' not found (for assistance press " + AbstractShell.completionKeys + " or type \"hint\" then hit ENTER)");
logger.warning("Command '" + buffer + "' not found (for assistance press " + AbstractShell.completionKeys + ")");
}

private Collection<MethodTarget> locateTargets(final String buffer, final boolean strictMatching, final boolean checkAvailabilityIndicators) {
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/springframework/shell/Bootstrap.java
Expand Up @@ -45,7 +45,7 @@ public static void main(String[] args) throws IOException {
}
ExitShellRequest exitShellRequest;
try {
bootstrap = new Bootstrap(options.applicationContextLocation);
bootstrap = new Bootstrap(null);
exitShellRequest = bootstrap.run(options.executeThenQuit);
} catch (RuntimeException t) {
throw t;
Expand All @@ -57,12 +57,11 @@ public static void main(String[] args) throws IOException {
}

public Bootstrap(String applicationContextLocation) throws IOException {
//setupLogging();
Assert.hasText(applicationContextLocation, "Application context location required");
createApplicationContext(applicationContextLocation);
createApplicationContext();

shell = ctx.getBean("shell", JLineShellComponent.class);
shell.setApplicationContext(ctx);
shell.setHistorySize(options.historySize);
if (options.executeThenQuit != null) {
shell.setPrintBanner(false);
}
Expand Down Expand Up @@ -96,7 +95,7 @@ public Bootstrap(String applicationContextLocation) throws IOException {

}

private void createApplicationContext(String applicationContextLocation) {
private void createApplicationContext() {
AnnotationConfigApplicationContext annctx = new AnnotationConfigApplicationContext();
createAndRegisterBeanDefinition(annctx, org.springframework.roo.shell.converters.StringConverter.class);
createAndRegisterBeanDefinition(annctx,
Expand Down
31 changes: 30 additions & 1 deletion src/main/java/org/springframework/shell/JLineShell.java
Expand Up @@ -95,6 +95,7 @@ public abstract class JLineShell extends AbstractShell
private String version;
private String welcomeMessage;

private int historySize;

public void run() {
try {
Expand Down Expand Up @@ -137,9 +138,14 @@ public void run() {
String logFileContents = FileCopyUtils.copyToString(new File(this.historyFileName));
String[] logEntries = logFileContents.split(StringUtils.LINE_SEPARATOR);
// LIFO
int size = 0;
for (String logEntry : logEntries) {
if (!logEntry.startsWith("//")) {
reader.getHistory().addToHistory(logEntry);
size++;
if(size > historySize){
break;
}
}
}
} catch (IOException ignored) {
Expand Down Expand Up @@ -561,7 +567,7 @@ private String getHistoryFileName() {
* get prompt text from provider. The provider has highest order
* <link>org.springframework.core.Ordered.getOder</link> will win.
*
* @return
* @return prompt text
*/
private String getPromptText(){
return getHighestPriorityProvider(PromptProvider.class).getPromptText();
Expand Down Expand Up @@ -591,11 +597,20 @@ private <T extends PluginProvider> T getHighestPriorityProvider(Class<T> t){
return highestPriorityProvider;
}

/**
* get the version information
*
*/
@Override
public String version(String text){
return this.version;
}

/**
* get the welcome message at start.
*
* @return welcome message
*/
public String getWelcomeMessage(){
return this.welcomeMessage;
}
Expand All @@ -607,5 +622,19 @@ public String getWelcomeMessage(){
public void setPrintBanner(boolean printBanner) {
this.printBanner = printBanner;
}

/**
* @return the historySize
*/
public int getHistorySize() {
return historySize;
}

/**
* @param historySize the historySize to set
*/
public void setHistorySize(int historySize) {
this.historySize = historySize;
}

}
Expand Up @@ -18,51 +18,61 @@
* @author vnagaraja
*/
public class SimpleShellCommandLineOptions {
String[] executeThenQuit = null;
Map<String, String> extraSystemProperties = new HashMap<String, String>();
int historySize = 3000;

public static final String DEFAULT_APP_CTX = "classpath*:/META-INF/spring/app-context.xml";
String applicationContextLocation = DEFAULT_APP_CTX;
String[] executeThenQuit = null;
Map<String, String> extraSystemProperties = new HashMap<String, String>();
public static SimpleShellCommandLineOptions parseCommandLine(String[] args) throws IOException {
SimpleShellCommandLineOptions options = new SimpleShellCommandLineOptions();
List<String> commands = new ArrayList<String>();
int i = 0;
while (i < args.length) {
String arg = args[i++];
if (arg.equals("--environment")) {
String environment = args[i++];
options.extraSystemProperties.put("napa.application.profile", environment);
}
else if (arg.equals("--cmdfile")) {
File f = new File(args[i++]);
commands.addAll(FileUtils.readLines(f));
}
else if (arg.equals("--histsize")) {
options.historySize = Integer.parseInt(args[i++]);
}
else if (arg.equals("--help")) {
printUsage();
System.exit(0);
}
else {
i--;
break;
}
}

public static SimpleShellCommandLineOptions parseCommandLine(String[] args) throws IOException {
SimpleShellCommandLineOptions options = new SimpleShellCommandLineOptions();
List<String> commands = new ArrayList<String>();
int i = 0;
while (i < args.length) {
String arg = args[i++];
if (arg.equals("-environment")) {
String environment = args[i++];
options.extraSystemProperties.put("napa.application.profile", environment);
} else if (arg.equals("-ctx")) {
options.applicationContextLocation = args[i++];
} else if (arg.equals("-cmdfile")) {
File f = new File(args[i++]);
commands.addAll(FileUtils.readLines(f));
} else {
i--;
break;
}
}
StringBuilder sb = new StringBuilder();
for (; i < args.length; i++) {
if (sb.length() > 0) {
sb.append(" ");
}
sb.append(args[i]);
}

StringBuilder sb = new StringBuilder();
for (; i < args.length; i++) {
if (sb.length() > 0) {
sb.append(" ");
}
sb.append(args[i]);
}
if (sb.length() > 0) {
String[] cmdLineCommands = sb.toString().split(";");
for (String s : cmdLineCommands) {
//add any command line commands after the commands loaded from the file
commands.add(s.trim());
}
}

if(sb.length()>0) {
String[] cmdLineCommands = sb.toString().split(";");
for(String s : cmdLineCommands) {
//add any command line commands after the commands loaded from the file
commands.add(s.trim());
}
}
if (commands.size() > 0)
options.executeThenQuit = commands.toArray(new String[commands.size()]);

if(commands.size()>0)
options.executeThenQuit = commands.toArray(new String[commands.size()]);

return options;
}
return options;
}

private static void printUsage(){
System.out.println("Usage:");
System.out.println("java -jar {jarname} --help --histsize {size} --cmdfile {file}");
}
}
30 changes: 0 additions & 30 deletions src/main/resources/META-INF/spring/app-context.xml

This file was deleted.

0 comments on commit 5dca1a1

Please sign in to comment.