diff --git a/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyPromptProvider.java b/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyPromptProvider.java new file mode 100644 index 00000000..386eecf8 --- /dev/null +++ b/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyPromptProvider.java @@ -0,0 +1,42 @@ +/* + * Copyright 2011-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell.samples.helloworld.commands; + +import org.springframework.shell.plugin.PromptProvider; +import org.springframework.stereotype.Component; + +/** + * @author Jarred Li + * + */ +@Component +public class MyPromptProvider implements PromptProvider { + + /* (non-Javadoc) + * @see org.springframework.core.Ordered#getOrder() + */ + public int getOrder() { + return 1; + } + + /* (non-Javadoc) + * @see org.springframework.shell.plugin.PromptProvider#getPromptText() + */ + public String getPromptText() { + return "vHelper>"; + } + +} diff --git a/src/main/java/org/springframework/shell/Bootstrap.java b/src/main/java/org/springframework/shell/Bootstrap.java index 2b5febdb..06c91e56 100644 --- a/src/main/java/org/springframework/shell/Bootstrap.java +++ b/src/main/java/org/springframework/shell/Bootstrap.java @@ -31,7 +31,6 @@ import org.springframework.util.StopWatch; public class Bootstrap { private static Bootstrap bootstrap; - //TODO using JLineShellComponenet to override and get access to "getSimpleParser" method on the shell - look into autowire...and move back to reference Shell interface. private JLineShellComponent shell; private ConfigurableApplicationContext ctx; private static StopWatch sw = new StopWatch("Spring Sehll"); diff --git a/src/main/java/org/springframework/shell/Constant.java b/src/main/java/org/springframework/shell/Constant.java new file mode 100644 index 00000000..192e11f9 --- /dev/null +++ b/src/main/java/org/springframework/shell/Constant.java @@ -0,0 +1,28 @@ +/* + * Copyright 2011-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell; + +/** + * @author Jarred Li + * + */ +public interface Constant { + + String HISTORY_FILE_NAME = "spring-shell.log"; + + String COMMAND_LINE_PROMPT = "spring>"; + +} diff --git a/src/main/java/org/springframework/shell/ExampleService.java b/src/main/java/org/springframework/shell/ExampleService.java deleted file mode 100644 index 09b99cf1..00000000 --- a/src/main/java/org/springframework/shell/ExampleService.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.springframework.shell; - -import org.springframework.stereotype.Component; - - -/** - * {@link Service} with hard-coded input data. - */ -@Component -public class ExampleService implements Service { - - /** - * Reads next record from input - */ - public String getMessage() { - return "Hello world!"; - } - -} diff --git a/src/main/java/org/springframework/shell/JLineShell.java b/src/main/java/org/springframework/shell/JLineShell.java index 2cb0fc0f..ecbf195a 100644 --- a/src/main/java/org/springframework/shell/JLineShell.java +++ b/src/main/java/org/springframework/shell/JLineShell.java @@ -15,7 +15,6 @@ import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Map; -import java.util.ResourceBundle; import java.util.Set; import java.util.logging.Handler; import java.util.logging.Level; @@ -29,7 +28,6 @@ import jline.WindowsTerminal; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; import org.springframework.roo.shell.AbstractShell; import org.springframework.roo.shell.CommandMarker; import org.springframework.roo.shell.ExitShellRequest; @@ -43,8 +41,8 @@ import org.springframework.roo.support.util.FileCopyUtils; import org.springframework.roo.support.util.IOUtils; import org.springframework.roo.support.util.OsUtils; import org.springframework.roo.support.util.StringUtils; -import org.springframework.shell.commands.HintCommands; import org.springframework.shell.plugin.HistoryFileProvider; +import org.springframework.shell.plugin.PromptProvider; /** @@ -69,10 +67,6 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, private static final boolean APPLE_TERMINAL = Boolean.getBoolean("is.apple.terminal"); private static final char ESCAPE = 27; private static final String BEL = "\007"; - - //TODO make configurable - private static final String COMMAND_LINE_PROMPT = "spring>"; - // Fields private ConsoleReader reader; private boolean developmentMode = false; @@ -203,7 +197,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, if (reader.getTerminal().isANSISupported()) { ANSIBuffer ansi = JLineLogHandler.getANSIBuffer(); if (path == null || "".equals(path)) { - shellPrompt = ansi.yellow(COMMAND_LINE_PROMPT).toString(); + shellPrompt = ansi.yellow(getPromptText()).toString(); } else { if (overrideStyle) { @@ -212,7 +206,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, else { ansi.cyan(path); } - shellPrompt = ansi.yellow(" " + COMMAND_LINE_PROMPT).toString(); + shellPrompt = ansi.yellow(" " + getPromptText()).toString(); } } else { @@ -464,21 +458,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, } } - private String getHistoryFileName() { - String historyFileName = null; - Map historyFileProviders = BeanFactoryUtils.beansOfTypeIncludingAncestors( - this.applicatonContext, HistoryFileProvider.class); - int order = Integer.MIN_VALUE; - System.out.println("history file provider count:" + Arrays.toString(historyFileProviders.keySet().toArray())); - for(HistoryFileProvider p : historyFileProviders.values()){ - if(p.getOrder() > order){ - order = p.getOrder(); - historyFileName = p.getHistoryFileName(); - } - } - historyFileName = (historyFileName == null)?"spring-shell.log":historyFileName; - return historyFileName; - } + @Override protected void logCommandToOutput(final String processedLine) { @@ -541,4 +521,39 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicatonContext = applicationContext; } + + private String getHistoryFileName() { + String historyFileName = null; + Map historyFileProviders = getBeansInFactory(HistoryFileProvider.class); + int order = Integer.MIN_VALUE; + for(HistoryFileProvider p : historyFileProviders.values()){ + if(p.getOrder() > order){ + order = p.getOrder(); + historyFileName = p.getHistoryFileName(); + } + } + historyFileName = (historyFileName == null)?Constant.HISTORY_FILE_NAME:historyFileName; + return historyFileName; + } + + private String getPromptText(){ + String promptText = null; + Map promptProviders = getBeansInFactory(PromptProvider.class); + int order = Integer.MIN_VALUE; + for(PromptProvider p : promptProviders.values()){ + if(p.getOrder() > order){ + order = p.getOrder(); + promptText = p.getPromptText(); + } + } + promptText = (promptText == null)?Constant.COMMAND_LINE_PROMPT:promptText; + return promptText; + } + + private Map getBeansInFactory(Class t){ + Map result = BeanFactoryUtils.beansOfTypeIncludingAncestors( + this.applicatonContext, t); + return result; + } + } diff --git a/src/main/java/org/springframework/shell/Service.java b/src/main/java/org/springframework/shell/Service.java deleted file mode 100644 index b02542ef..00000000 --- a/src/main/java/org/springframework/shell/Service.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.springframework.shell; - -public interface Service { - - String getMessage(); - -} diff --git a/src/main/java/org/springframework/shell/plugin/PromptProvider.java b/src/main/java/org/springframework/shell/plugin/PromptProvider.java new file mode 100644 index 00000000..5ffd36ea --- /dev/null +++ b/src/main/java/org/springframework/shell/plugin/PromptProvider.java @@ -0,0 +1,30 @@ +/* + * Copyright 2011-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell.plugin; + +import org.springframework.core.Ordered; + +/** + * Provider for prompt + * + * @author Jarred Li + * + */ +public interface PromptProvider extends Ordered { + + String getPromptText(); + +} diff --git a/src/main/java/org/springframework/shell/plugin/support/DefaultHistoryFileProvider.java b/src/main/java/org/springframework/shell/plugin/support/DefaultHistoryFileProvider.java index c7814fdd..169b7f5b 100644 --- a/src/main/java/org/springframework/shell/plugin/support/DefaultHistoryFileProvider.java +++ b/src/main/java/org/springframework/shell/plugin/support/DefaultHistoryFileProvider.java @@ -15,7 +15,7 @@ */ package org.springframework.shell.plugin.support; -import org.springframework.core.Ordered; +import org.springframework.shell.Constant; import org.springframework.shell.plugin.HistoryFileProvider; import org.springframework.stereotype.Component; @@ -37,7 +37,7 @@ public class DefaultHistoryFileProvider implements HistoryFileProvider{ * @see org.springframework.shell.plugin.HistoryFileProvider#getHistoryFileName() */ public String getHistoryFileName() { - return "spring-shell.log"; + return Constant.HISTORY_FILE_NAME; } } diff --git a/src/main/java/org/springframework/shell/plugin/support/DefaultPromptProvider.java b/src/main/java/org/springframework/shell/plugin/support/DefaultPromptProvider.java new file mode 100644 index 00000000..39ad579e --- /dev/null +++ b/src/main/java/org/springframework/shell/plugin/support/DefaultPromptProvider.java @@ -0,0 +1,41 @@ +/* + * Copyright 2011-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell.plugin.support; + +import org.springframework.shell.Constant; +import org.springframework.shell.plugin.PromptProvider; + +/** + * @author Jarred Li + * + */ +public class DefaultPromptProvider implements PromptProvider{ + + /* (non-Javadoc) + * @see org.springframework.core.Ordered#getOrder() + */ + public int getOrder() { + return 0; + } + + /* (non-Javadoc) + * @see org.springframework.shell.plugin.PromptProvider#getPromptText() + */ + public String getPromptText() { + return Constant.COMMAND_LINE_PROMPT; + } + +}