SHL-69 - Extract "version" command from AbstractShell

This commit is contained in:
mpollack
2013-07-25 19:38:00 -04:00
parent e9f3de10cb
commit 344385116c
6 changed files with 94 additions and 29 deletions

View File

@@ -148,16 +148,16 @@ public class HelloWorldCommands implements CommandMarker {
<para><classname>SystemPropertyCommands</classname> - <literal>system properties</literal>- shows the shell's system properties</para>
</listitem>
<listitem>
<para><classname>VersionCommands</classname> - <literal>version</literal>- shows the shell's version</para>
</listitem>
</itemizedlist>
<para>There are also a few commands that are provided by the
<classname>AbstractShell</classname> class, these are</para>
<para>There are two commands in provided by the
<classname>AbstractShell</classname> class related to useage of block comments</para>
<itemizedlist>
<listitem>
<para><literal>script</literal> - Parses the specified resource file
and executes its commands</para>
</listitem>
<listitem>
<para><literal>/*</literal> and <literal>*/</literal>- The begin and end characters for block comments</para>

View File

@@ -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() {

View File

@@ -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;
}
}

View File

@@ -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 extends NamedProvider> T getHighestPriorityProvider(Class<T> t) {
Map<String, T> providers = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.applicationContext, t);
List<T> sortedProviders = new ArrayList<T>(providers.values());
Collections.sort(sortedProviders, annotationOrderComparator);
T highestPriorityProvider = sortedProviders.get(0);
return highestPriorityProvider;
}
public void printBannerAndWelcome() {
if (printBanner) {

View File

@@ -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 extends NamedProvider> T getHighestPriorityProvider(ApplicationContext applicationContext, Class<T> t) {
Map<String, T> providers = BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, t);
List<T> sortedProviders = new ArrayList<T>(providers.values());
Collections.sort(sortedProviders, new AnnotationAwareOrderComparator());
T highestPriorityProvider = sortedProviders.get(0);
return highestPriorityProvider;
}
}

View File

@@ -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;