diff --git a/docs/src/reference/docbook/reference/shell.xml b/docs/src/reference/docbook/reference/shell.xml index 83d9796b..3b999783 100644 --- a/docs/src/reference/docbook/reference/shell.xml +++ b/docs/src/reference/docbook/reference/shell.xml @@ -148,16 +148,16 @@ public class HelloWorldCommands implements CommandMarker { SystemPropertyCommands - system properties- shows the shell's system properties + + VersionCommands - version- shows the shell's version + + - There are also a few commands that are provided by the - AbstractShell class, these are + There are two commands in provided by the + AbstractShell class related to useage of block comments - - script - Parses the specified resource file - and executes its commands - /* and */- The begin and end characters for block comments diff --git a/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyBannerProvider.java b/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyBannerProvider.java index 32481e2a..b1d5e820 100644 --- a/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyBannerProvider.java +++ b/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyBannerProvider.java @@ -29,10 +29,8 @@ import org.springframework.stereotype.Component; */ @Component @Order(Ordered.HIGHEST_PRECEDENCE) -public class MyBannerProvider extends DefaultBannerProvider - implements CommandMarker { +public class MyBannerProvider extends DefaultBannerProvider { - @CliCommand(value = { "version" }, help = "Displays current CLI version") public String getBanner() { StringBuffer buf = new StringBuffer(); buf.append("=======================================" + OsUtils.LINE_SEPARATOR); @@ -42,7 +40,6 @@ public class MyBannerProvider extends DefaultBannerProvider buf.append("=======================================" + OsUtils.LINE_SEPARATOR); buf.append("Version:" + this.getVersion()); return buf.toString(); - } public String getVersion() { diff --git a/src/main/java/org/springframework/shell/commands/VersionCommands.java b/src/main/java/org/springframework/shell/commands/VersionCommands.java new file mode 100644 index 00000000..f9c70f24 --- /dev/null +++ b/src/main/java/org/springframework/shell/commands/VersionCommands.java @@ -0,0 +1,32 @@ +package org.springframework.shell.commands; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.shell.core.CommandMarker; +import org.springframework.shell.core.annotation.CliCommand; +import org.springframework.shell.plugin.BannerProvider; +import org.springframework.shell.plugin.PluginUtils; +import org.springframework.stereotype.Component; + +/** + * Essential built-in shell commands. + * + * @author Mark Pollack + * @author Erwin Vervaet + */ +@Component +public class VersionCommands implements CommandMarker, ApplicationContextAware { + + private ApplicationContext ctx; + + @CliCommand(value = { "version" }, help = "Displays shell version") + public String version() { + return PluginUtils.getHighestPriorityProvider(ctx, BannerProvider.class).getVersion(); + } + + public void setApplicationContext(ApplicationContext applicationContext) + throws BeansException { + this.ctx = applicationContext; + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/shell/core/JLineShellComponent.java b/src/main/java/org/springframework/shell/core/JLineShellComponent.java index d2e5a544..0a806997 100644 --- a/src/main/java/org/springframework/shell/core/JLineShellComponent.java +++ b/src/main/java/org/springframework/shell/core/JLineShellComponent.java @@ -15,9 +15,6 @@ */ package org.springframework.shell.core; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; import java.util.Map; import org.springframework.beans.BeansException; @@ -27,11 +24,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.SmartLifecycle; -import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.shell.CommandLine; import org.springframework.shell.plugin.BannerProvider; import org.springframework.shell.plugin.HistoryFileNameProvider; -import org.springframework.shell.plugin.NamedProvider; +import org.springframework.shell.plugin.PluginUtils; import org.springframework.shell.plugin.PromptProvider; /** @@ -51,8 +47,6 @@ public class JLineShellComponent extends JLineShell implements SmartLifecycle, A private ApplicationContext applicationContext; private boolean printBanner = true; - private static AnnotationAwareOrderComparator annotationOrderComparator = new AnnotationAwareOrderComparator(); - private String historyFileName; private String promptText; private String productName; @@ -166,7 +160,7 @@ public class JLineShellComponent extends JLineShell implements SmartLifecycle, A * @return history file name */ protected String getHistoryFileName() { - HistoryFileNameProvider historyFileNameProvider = getHighestPriorityProvider(HistoryFileNameProvider.class); + HistoryFileNameProvider historyFileNameProvider = PluginUtils.getHighestPriorityProvider(this.applicationContext,HistoryFileNameProvider.class); String providerHistoryFileName = historyFileNameProvider.getHistoryFileName(); if (providerHistoryFileName != null) { return providerHistoryFileName; @@ -182,7 +176,7 @@ public class JLineShellComponent extends JLineShell implements SmartLifecycle, A * @return prompt text */ protected String getPromptText() { - PromptProvider promptProvider = getHighestPriorityProvider(PromptProvider.class); + PromptProvider promptProvider = PluginUtils.getHighestPriorityProvider(this.applicationContext,PromptProvider.class); String providerPromptText = promptProvider.getPrompt(); if (providerPromptText != null) { return providerPromptText; @@ -201,7 +195,7 @@ public class JLineShellComponent extends JLineShell implements SmartLifecycle, A */ private String[] getBannerText() { String[] bannerText = new String[4]; - BannerProvider provider = getHighestPriorityProvider(BannerProvider.class); + BannerProvider provider = PluginUtils.getHighestPriorityProvider(this.applicationContext,BannerProvider.class); bannerText[0] = provider.getBanner(); bannerText[1] = provider.getWelcomeMessage(); bannerText[2] = provider.getVersion(); @@ -209,14 +203,6 @@ public class JLineShellComponent extends JLineShell implements SmartLifecycle, A return bannerText; } - - private T getHighestPriorityProvider(Class t) { - Map providers = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.applicationContext, t); - List sortedProviders = new ArrayList(providers.values()); - Collections.sort(sortedProviders, annotationOrderComparator); - T highestPriorityProvider = sortedProviders.get(0); - return highestPriorityProvider; - } public void printBannerAndWelcome() { if (printBanner) { diff --git a/src/main/java/org/springframework/shell/plugin/PluginUtils.java b/src/main/java/org/springframework/shell/plugin/PluginUtils.java new file mode 100644 index 00000000..c1dbe4e7 --- /dev/null +++ b/src/main/java/org/springframework/shell/plugin/PluginUtils.java @@ -0,0 +1,50 @@ +/* + * 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 java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.BeanFactoryUtils; +import org.springframework.context.ApplicationContext; +import org.springframework.core.annotation.AnnotationAwareOrderComparator; + +/** + * Utilities dealing with shell plugins. + * + * @author Erwin Vervaet + */ +public final class PluginUtils { + + private PluginUtils() { + } + + /** + * Returns the highest priority {@link PluginProvider} of specified type defined in + * given application context. + * + * @since 1.0.1 + */ + public static T getHighestPriorityProvider(ApplicationContext applicationContext, Class t) { + Map providers = BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, t); + List sortedProviders = new ArrayList(providers.values()); + Collections.sort(sortedProviders, new AnnotationAwareOrderComparator()); + T highestPriorityProvider = sortedProviders.get(0); + return highestPriorityProvider; + } +} diff --git a/src/test/java/org/springframework/shell/BootstrapTest.java b/src/test/java/org/springframework/shell/BootstrapTest.java index fcae4605..38e58a8c 100644 --- a/src/test/java/org/springframework/shell/BootstrapTest.java +++ b/src/test/java/org/springframework/shell/BootstrapTest.java @@ -18,7 +18,7 @@ public class BootstrapTest { JLineShellComponent shell = bootstrap.getJLineShellComponent(); //This is a brittle assertion - as additiona 'test' commands are added to the suite, this number will increase. - Assert.assertEquals("Number of CommandMarkers is incorrect", 9, shell.getSimpleParser().getCommandMarkers().size()); + Assert.assertEquals("Number of CommandMarkers is incorrect", 10, shell.getSimpleParser().getCommandMarkers().size()); Assert.assertEquals("Number of Converters is incorrect", 16, shell.getSimpleParser().getConverters().size()); } catch (RuntimeException t) { throw t;