diff --git a/src/main/java/org/springframework/shell/Constant.java b/src/main/java/org/springframework/shell/Constant.java index ac6861a1..47265eee 100644 --- a/src/main/java/org/springframework/shell/Constant.java +++ b/src/main/java/org/springframework/shell/Constant.java @@ -25,6 +25,7 @@ public interface Constant { String COMMAND_LINE_PROMPT = "spring>"; - String WELCOME_MESSAGE = "Welcome to Spring Shell. For assistance press or type \"hint\" then hit ENTER."; + String PRODUCT_NAME = "Spring Shell"; + String WELCOME_MESSAGE = "Welcome to " + PRODUCT_NAME + ". For assistance press or type \"hint\" then hit ENTER."; } diff --git a/src/main/java/org/springframework/shell/JLineShell.java b/src/main/java/org/springframework/shell/JLineShell.java index b1c972d9..491851b0 100644 --- a/src/main/java/org/springframework/shell/JLineShell.java +++ b/src/main/java/org/springframework/shell/JLineShell.java @@ -16,7 +16,6 @@ import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.logging.Handler; @@ -29,10 +28,6 @@ import jline.ConsoleReader; import jline.WindowsTerminal; import org.apache.commons.io.input.ReversedLinesFileReader; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.BeanFactoryUtils; -import org.springframework.context.ApplicationContext; -import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.roo.shell.AbstractShell; import org.springframework.roo.shell.CommandMarker; import org.springframework.roo.shell.ExitShellRequest; @@ -45,10 +40,7 @@ import org.springframework.roo.support.util.ClassUtils; 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.plugin.BannerProvider; -import org.springframework.shell.plugin.HistoryFileNameProvider; -import org.springframework.shell.plugin.PluginProvider; -import org.springframework.shell.plugin.PromptProvider; +import org.springframework.roo.support.util.VersionUtils; /** @@ -85,18 +77,6 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, private final Map rowErasureMap = new HashMap(); private boolean shutdownHookFired = false; // ROO-1599 - private ApplicationContext applicatonContext; - private boolean printBanner = true; - - private static AnnotationAwareOrderComparator annocationOrderComparator = new AnnotationAwareOrderComparator(); - - private String historyFileName; - private String promptText; - private String productName; - private String banner; - private String version; - private String welcomeMessage; - private int historySize; public void run() { @@ -120,7 +100,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, // reader.setDebug(new PrintWriter(new FileWriter("writer.debug", true))); openFileLogIfPossible(); - this.reader.getHistory().setMaxSize(this.historySize); + this.reader.getHistory().setMaxSize(getHistorySize()); // Try to build previous command history from the project's log String[] filteredLogEntries = filterLogEntry(); for (String logEntry : filteredLogEntries) { @@ -128,7 +108,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, } flashMessageRenderer(); - flash(Level.FINE, this.productName + " " + this.version, Shell.WINDOW_TITLE_SLOT); + flash(Level.FINE, this.getProductName() + " " + this.getVersion(), Shell.WINDOW_TITLE_SLOT); printBannerAndWelcome(); String startupNotifications = getStartupNotifications(); @@ -144,7 +124,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, shutdownHookFired = true; // We don't need to closeShell(), as the shutdown hook in o.s.r.bootstrap.Main calls stop() which calls JLineShellComponent.deactivate() and that calls closeShell() } - }, "Spring Roo JLine Shutdown Hook")); + }, getProductName() + " JLine Shutdown Hook")); // Handle any "execute-then-quit" operation @@ -175,7 +155,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, ArrayList entries = new ArrayList(); try { ReversedLinesFileReader reader = new ReversedLinesFileReader( - new File(this.historyFileName),4096,Charset.forName("UTF-8")); + new File(getHistoryFileName()),4096,Charset.forName("UTF-8")); int size = 0; String line = null; while ((line = reader.readLine()) != null) { @@ -225,10 +205,6 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, } public void printBannerAndWelcome() { - if (printBanner) { - logger.info(this.banner); - logger.info(getWelcomeMessage()); - } } public String getStartupNotifications() { @@ -254,7 +230,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(this.promptText).toString(); + shellPrompt = ansi.yellow(getPromptText()).toString(); } else { if (overrideStyle) { @@ -263,7 +239,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, else { ansi.cyan(path); } - shellPrompt = ansi.yellow(" " + this.promptText).toString(); + shellPrompt = ansi.yellow(" " + getPromptText()).toString(); } } else { @@ -337,7 +313,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, } } } - }, "Spring Roo JLine Flash Message Manager"); + }, getProductName() + " JLine Flash Message Manager"); t.start(); } @@ -508,9 +484,9 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, private void openFileLogIfPossible() { try { - fileLog = new FileWriter(this.historyFileName, true); + fileLog = new FileWriter(getHistoryFileName(), true); // First write, so let's record the date and time of the first user command - fileLog.write("// Spring Roo " + versionInfo() + " log opened at " + df.format(new Date()) + "\n"); + fileLog.write("// " + getProductName() + " " + versionInfo() + " log opened at " + df.format(new Date()) + "\n"); fileLog.flush(); } catch (IOException ignoreIt) { } @@ -532,7 +508,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, fileLog.flush(); // So tail -f will show it's working if (getExitShellRequest() != null) { // Shutting down, so close our file (we can always reopen it later if needed) - fileLog.write("// Spring Roo " + versionInfo() + " log closed at " + df.format(new Date()) + "\n"); + fileLog.write("// " + getProductName() + " " + versionInfo() + " log closed at " + df.format(new Date()) + "\n"); IOUtils.closeQuietly(fileLog); fileLog = null; } @@ -576,28 +552,14 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, int rowNumber; } - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicatonContext = applicationContext; - } - - public void costomizePlugin() { - this.historyFileName = getHistoryFileName(); - this.promptText = getPromptText(); - String[] banner = getBannerText(); - this.banner = banner[0]; - this.welcomeMessage = banner[1]; - this.version = banner[2]; - this.productName = banner[3]; - } - /** * get history file name from provider. The provider has highest order * org.springframework.core.Ordered.getOder will win. * * @return history file name */ - private String getHistoryFileName() { - return getHighestPriorityProvider(HistoryFileNameProvider.class).getHistoryFileName(); + protected String getHistoryFileName() { + return Constant.HISTORY_FILE_NAME; } /** @@ -606,53 +568,16 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker, * * @return prompt text */ - private String getPromptText() { - return getHighestPriorityProvider(PromptProvider.class).getPromptText(); + protected String getPromptText() { + return Constant.COMMAND_LINE_PROMPT; } - - /** - * Get Banner and Welcome Message from provider. The provider has highest order - * org.springframework.core.Ordered.getOder will win. - * @return BannerText[0]: Banner - * BannerText[1]: Welcome Message - * BannerText[2]: Version - * BannerText[3]: Product Name - */ - private String[] getBannerText() { - String[] bannerText = new String[4]; - BannerProvider provider = getHighestPriorityProvider(BannerProvider.class); - bannerText[0] = provider.getBanner(); - bannerText[1] = provider.getWelcomeMessage(); - bannerText[2] = provider.getVersion(); - bannerText[3] = provider.name(); - return bannerText; + + protected String getProductName() { + return Constant.PRODUCT_NAME; } - - - private T getHighestPriorityProvider(Class t) { - Map providers = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.applicatonContext, t); - List sortedProviders = new ArrayList(providers.values()); - Collections.sort(sortedProviders, annocationOrderComparator); - T highestPriorityProvider = sortedProviders.get(0); - return highestPriorityProvider; - } - - - /** - * get the welcome message at start. - * - * @return welcome message - */ - public String getWelcomeMessage() { - return this.welcomeMessage; - } - - - /** - * @param printBanner the printBanner to set - */ - public void setPrintBanner(boolean printBanner) { - this.printBanner = printBanner; + + protected String getVersion() { + return VersionUtils.versionInfo(); } /** diff --git a/src/main/java/org/springframework/shell/JLineShellComponent.java b/src/main/java/org/springframework/shell/JLineShellComponent.java index 4d04fe06..54f058c1 100644 --- a/src/main/java/org/springframework/shell/JLineShellComponent.java +++ b/src/main/java/org/springframework/shell/JLineShellComponent.java @@ -1,12 +1,24 @@ package org.springframework.shell; import java.net.URL; +import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactoryUtils; +import org.springframework.context.ApplicationContext; import org.springframework.context.Lifecycle; +import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.roo.shell.ExecutionStrategy; import org.springframework.roo.shell.Parser; import org.springframework.roo.shell.SimpleParser; +import org.springframework.shell.plugin.BannerProvider; +import org.springframework.shell.plugin.HistoryFileNameProvider; +import org.springframework.shell.plugin.PluginProvider; +import org.springframework.shell.plugin.PromptProvider; /** * Launcher for {@link JLineShell}. @@ -19,6 +31,20 @@ public class JLineShellComponent extends JLineShell implements Lifecycle { private volatile boolean running = false; private Thread shellThread; + private ApplicationContext applicatonContext; + private boolean printBanner = true; + + private static AnnotationAwareOrderComparator annotationOrderComparator = new AnnotationAwareOrderComparator(); + + private String historyFileName; + private String promptText; + private String productName; + private String banner; + private String version; + private String welcomeMessage; + + private int historySize; + // Fields private ExecutionStrategy executionStrategy = new SimpleExecutionStrategy(); //ProcessManagerHostedExecutionStrategy is not what i think we need outside of Roo. private SimpleParser parser = new SimpleParser(); @@ -35,7 +61,7 @@ public class JLineShellComponent extends JLineShell implements Lifecycle { public void start() { //customizePlug must run before start thread to take plugin's configuration into effect - super.costomizePlugin(); + customizePlugin(); shellThread = new Thread(this, "Spring Shell"); shellThread.start(); running = true; @@ -85,5 +111,123 @@ public class JLineShellComponent extends JLineShell implements Lifecycle { return null; } + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicatonContext = applicationContext; + } + + public void customizePlugin() { + this.historyFileName = getHistoryFileName(); + this.promptText = getPromptText(); + String[] banner = getBannerText(); + this.banner = banner[0]; + this.welcomeMessage = banner[1]; + this.version = banner[2]; + this.productName = banner[3]; + } + + /** + * get history file name from provider. The provider has highest order + * org.springframework.core.Ordered.getOder will win. + * + * @return history file name + */ + protected String getHistoryFileName() { + String providerHistoryFileName = getHighestPriorityProvider(HistoryFileNameProvider.class).getHistoryFileName(); + if (providerHistoryFileName != null) { + return providerHistoryFileName; + } else { + return historyFileName; + } + } + + /** + * get prompt text from provider. The provider has highest order + * org.springframework.core.Ordered.getOder will win. + * + * @return prompt text + */ + protected String getPromptText() { + String providerPromptText = getHighestPriorityProvider(PromptProvider.class).getPromptText(); + if (providerPromptText != null) { + return providerPromptText; + } else { + return promptText; + } + } + + /** + * Get Banner and Welcome Message from provider. The provider has highest order + * org.springframework.core.Ordered.getOder will win. + * @return BannerText[0]: Banner + * BannerText[1]: Welcome Message + * BannerText[2]: Version + * BannerText[3]: Product Name + */ + private String[] getBannerText() { + String[] bannerText = new String[4]; + BannerProvider provider = getHighestPriorityProvider(BannerProvider.class); + bannerText[0] = provider.getBanner(); + bannerText[1] = provider.getWelcomeMessage(); + bannerText[2] = provider.getVersion(); + bannerText[3] = provider.name(); + return bannerText; + } + + + private T getHighestPriorityProvider(Class t) { + Map providers = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.applicatonContext, t); + List sortedProviders = new ArrayList(providers.values()); + Collections.sort(sortedProviders, annotationOrderComparator); + T highestPriorityProvider = sortedProviders.get(0); + return highestPriorityProvider; + } + + public void printBannerAndWelcome() { + if (printBanner) { + logger.info(this.banner); + logger.info(getWelcomeMessage()); + } + } + + + /** + * get the welcome message at start. + * + * @return welcome message + */ + public String getWelcomeMessage() { + return this.welcomeMessage; + } + + + /** + * @param printBanner the printBanner to set + */ + 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; + } + + protected String getProductName() { + return productName; + } + + protected String getVersion() { + return version; + } + } \ No newline at end of file diff --git a/src/main/java/org/springframework/shell/plugin/support/DefaultBannerProvider.java b/src/main/java/org/springframework/shell/plugin/support/DefaultBannerProvider.java index f78a52b6..d1dcf2c8 100644 --- a/src/main/java/org/springframework/shell/plugin/support/DefaultBannerProvider.java +++ b/src/main/java/org/springframework/shell/plugin/support/DefaultBannerProvider.java @@ -80,7 +80,7 @@ public class DefaultBannerProvider implements BannerProvider, CommandMarker { */ @Override public String name() { - return "default banner provider"; + return Constant.PRODUCT_NAME; } }