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 2cf5a4a7..681bb84c 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 @@ -15,10 +15,12 @@ */ package org.springframework.shell.samples.helloworld.commands; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.roo.shell.CliCommand; import org.springframework.roo.shell.CommandMarker; import org.springframework.roo.support.util.StringUtils; -import org.springframework.shell.plugin.BannerProvider; +import org.springframework.shell.plugin.support.DefaultBannerProvider; import org.springframework.stereotype.Component; /** @@ -26,14 +28,9 @@ import org.springframework.stereotype.Component; * */ @Component -public class MyBannerProvider implements BannerProvider, CommandMarker { - - /* (non-Javadoc) - * @see org.springframework.core.Ordered#getOrder() - */ - public int getOrder() { - return 1; - } +@Order(Ordered.HIGHEST_PRECEDENCE) +public class MyBannerProvider extends DefaultBannerProvider + implements CommandMarker { /* (non-Javadoc) * @see org.springframework.shell.plugin.BannerProvider#getBanner() @@ -66,5 +63,11 @@ public class MyBannerProvider implements BannerProvider, CommandMarker { public String getWelcomMessage() { return "Welcome to vHelper CLI"; } + + @Override + public String name() { + return "my banner provider"; + } + } diff --git a/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyHistoryFileNameProvider.java b/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyHistoryFileNameProvider.java index d537dd74..523e14d8 100644 --- a/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyHistoryFileNameProvider.java +++ b/samples/helloworld/src/main/java/org/springframework/shell/samples/helloworld/commands/MyHistoryFileNameProvider.java @@ -16,7 +16,9 @@ package org.springframework.shell.samples.helloworld.commands; -import org.springframework.shell.plugin.HistoryFileNameProvider; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.shell.plugin.support.DefaultHistoryFileNameProvider; import org.springframework.stereotype.Component; /** @@ -25,14 +27,16 @@ import org.springframework.stereotype.Component; * */ @Component -public class MyHistoryFileNameProvider implements HistoryFileNameProvider { - - public int getOrder() { - return 1; - } +@Order(Ordered.HIGHEST_PRECEDENCE) +public class MyHistoryFileNameProvider extends DefaultHistoryFileNameProvider{ public String getHistoryFileName() { return "my.log"; } + @Override + public String name() { + return "my banner provider"; + } + } 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 index 386eecf8..674872c9 100644 --- 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 @@ -15,7 +15,9 @@ */ package org.springframework.shell.samples.helloworld.commands; -import org.springframework.shell.plugin.PromptProvider; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.shell.plugin.support.DefaultPromptProvider; import org.springframework.stereotype.Component; /** @@ -23,14 +25,8 @@ import org.springframework.stereotype.Component; * */ @Component -public class MyPromptProvider implements PromptProvider { - - /* (non-Javadoc) - * @see org.springframework.core.Ordered#getOrder() - */ - public int getOrder() { - return 1; - } +@Order(Ordered.HIGHEST_PRECEDENCE) +public class MyPromptProvider extends DefaultPromptProvider { /* (non-Javadoc) * @see org.springframework.shell.plugin.PromptProvider#getPromptText() @@ -38,5 +34,10 @@ public class MyPromptProvider implements PromptProvider { public String getPromptText() { return "vHelper>"; } + + @Override + public String name() { + return "my banner provider"; + } } diff --git a/src/main/java/org/springframework/shell/JLineShell.java b/src/main/java/org/springframework/shell/JLineShell.java index 715dfd3c..795fb859 100644 --- a/src/main/java/org/springframework/shell/JLineShell.java +++ b/src/main/java/org/springframework/shell/JLineShell.java @@ -10,9 +10,12 @@ import java.io.PrintStream; import java.io.PrintWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.ArrayList; +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; @@ -27,6 +30,7 @@ import jline.WindowsTerminal; 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; @@ -42,6 +46,7 @@ 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; @@ -82,6 +87,9 @@ public abstract class JLineShell extends AbstractShell private ApplicationContext applicatonContext; private boolean printBanner = true; + + private static AnnotationAwareOrderComparator annocationOrderComparator = new AnnotationAwareOrderComparator(); + private String historyFileName; private String promptText; private String version; @@ -545,17 +553,8 @@ public abstract class JLineShell extends AbstractShell * * @return history file name */ - private String getHistoryFileName() { - String historyFileName = null; - Map historyFileProviders = getBeansInFactory(HistoryFileNameProvider.class); - int order = Integer.MAX_VALUE; - for(HistoryFileNameProvider p : historyFileProviders.values()){ - if(p.getOrder() < order){ - order = p.getOrder(); - historyFileName = p.getHistoryFileName(); - } - } - return historyFileName; + private String getHistoryFileName() { + return getHighestPriorityProvider(HistoryFileNameProvider.class).getHistoryFileName(); } /** @@ -565,16 +564,7 @@ public abstract class JLineShell extends AbstractShell * @return */ private String getPromptText(){ - String promptText = null; - Map promptProviders = getBeansInFactory(PromptProvider.class); - int order = Integer.MAX_VALUE; - for(PromptProvider p : promptProviders.values()){ - if(p.getOrder() < order){ - order = p.getOrder(); - promptText = p.getPromptText(); - } - } - return promptText; + return getHighestPriorityProvider(PromptProvider.class).getPromptText(); } /** @@ -585,23 +575,20 @@ public abstract class JLineShell extends AbstractShell */ private String[] getBannerText(){ String[] bannerText = new String[2]; - Map bannerProviders = getBeansInFactory(BannerProvider.class); - int order = Integer.MAX_VALUE; - for(BannerProvider p : bannerProviders.values()){ - if(p.getOrder() < order){ - order = p.getOrder(); - bannerText[0] = p.getBanner(); - bannerText[1] = p.getWelcomMessage(); - } - } + BannerProvider provider = getHighestPriorityProvider(BannerProvider.class); + bannerText[0] = provider.getBanner(); + bannerText[1] = provider.getWelcomMessage(); return bannerText; } - private Map getBeansInFactory(Class t){ - Map result = BeanFactoryUtils.beansOfTypeIncludingAncestors( + private T getHighestPriorityProvider(Class t){ + Map providers = BeanFactoryUtils.beansOfTypeIncludingAncestors( this.applicatonContext, t); - return result; + List sortedProviders = new ArrayList(providers.values()); + Collections.sort(sortedProviders, annocationOrderComparator); + T highestPriorityProvider = sortedProviders.get(0); + return highestPriorityProvider; } @Override diff --git a/src/main/java/org/springframework/shell/plugin/BannerProvider.java b/src/main/java/org/springframework/shell/plugin/BannerProvider.java index 12509cb3..c691b192 100644 --- a/src/main/java/org/springframework/shell/plugin/BannerProvider.java +++ b/src/main/java/org/springframework/shell/plugin/BannerProvider.java @@ -15,7 +15,6 @@ */ package org.springframework.shell.plugin; -import org.springframework.core.Ordered; /** * Banner provider. Plugin should implements this interface to replace the version banner. @@ -25,7 +24,7 @@ import org.springframework.core.Ordered; * @since 1.0 * */ -public interface BannerProvider extends Ordered { +public interface BannerProvider extends PluginProvider{ String getBanner(); diff --git a/src/main/java/org/springframework/shell/plugin/HistoryFileNameProvider.java b/src/main/java/org/springframework/shell/plugin/HistoryFileNameProvider.java index 2a4b0652..a03723e2 100644 --- a/src/main/java/org/springframework/shell/plugin/HistoryFileNameProvider.java +++ b/src/main/java/org/springframework/shell/plugin/HistoryFileNameProvider.java @@ -15,7 +15,6 @@ */ package org.springframework.shell.plugin; -import org.springframework.core.Ordered; /** * History file name provider. Plugin should implements this interface to customize history file. @@ -25,7 +24,7 @@ import org.springframework.core.Ordered; * @since 1.0 * */ -public interface HistoryFileNameProvider extends Ordered{ +public interface HistoryFileNameProvider extends PluginProvider{ /** * get history file name diff --git a/src/main/java/org/springframework/shell/plugin/PluginProvider.java b/src/main/java/org/springframework/shell/plugin/PluginProvider.java new file mode 100644 index 00000000..f727b57b --- /dev/null +++ b/src/main/java/org/springframework/shell/plugin/PluginProvider.java @@ -0,0 +1,26 @@ +/* + * 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; + +/** + * @author Jarred Li + * + */ +public interface PluginProvider { + + String name(); + +} diff --git a/src/main/java/org/springframework/shell/plugin/PromptProvider.java b/src/main/java/org/springframework/shell/plugin/PromptProvider.java index 04239eab..8d3a0f29 100644 --- a/src/main/java/org/springframework/shell/plugin/PromptProvider.java +++ b/src/main/java/org/springframework/shell/plugin/PromptProvider.java @@ -15,7 +15,6 @@ */ package org.springframework.shell.plugin; -import org.springframework.core.Ordered; /** * Shell prompt provider. Plugin should implements this interface to customize prompt. @@ -24,7 +23,7 @@ import org.springframework.core.Ordered; * @author Jarred Li * */ -public interface PromptProvider extends Ordered { +public interface PromptProvider extends PluginProvider{ String getPromptText(); 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 e5dfba9d..fea54498 100644 --- a/src/main/java/org/springframework/shell/plugin/support/DefaultBannerProvider.java +++ b/src/main/java/org/springframework/shell/plugin/support/DefaultBannerProvider.java @@ -17,6 +17,8 @@ package org.springframework.shell.plugin.support; import static org.springframework.roo.support.util.StringUtils.LINE_SEPARATOR; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.roo.shell.CommandMarker; import org.springframework.roo.support.util.VersionUtils; import org.springframework.shell.Constant; @@ -30,13 +32,14 @@ import org.springframework.stereotype.Component; * */ @Component +@Order(Ordered.LOWEST_PRECEDENCE) public class DefaultBannerProvider implements BannerProvider, CommandMarker { /* (non-Javadoc) * @see org.springframework.core.Ordered#getOrder() */ public int getOrder() { - return Integer.MAX_VALUE; + return Ordered.LOWEST_PRECEDENCE; } /* (non-Javadoc) @@ -72,4 +75,12 @@ public class DefaultBannerProvider implements BannerProvider, CommandMarker { return Constant.WELCOME_MESSAGE; } + /* (non-Javadoc) + * @see org.springframework.shell.plugin.PluginProvider#name() + */ + @Override + public String name() { + return "default banner provider"; + } + } diff --git a/src/main/java/org/springframework/shell/plugin/support/DefaultHistoryFileNameProvider.java b/src/main/java/org/springframework/shell/plugin/support/DefaultHistoryFileNameProvider.java index dc141065..3e1ba420 100644 --- a/src/main/java/org/springframework/shell/plugin/support/DefaultHistoryFileNameProvider.java +++ b/src/main/java/org/springframework/shell/plugin/support/DefaultHistoryFileNameProvider.java @@ -15,6 +15,8 @@ */ package org.springframework.shell.plugin.support; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.shell.Constant; import org.springframework.shell.plugin.HistoryFileNameProvider; import org.springframework.stereotype.Component; @@ -26,13 +28,14 @@ import org.springframework.stereotype.Component; * */ @Component +@Order(Ordered.LOWEST_PRECEDENCE) public class DefaultHistoryFileNameProvider implements HistoryFileNameProvider{ /* (non-Javadoc) * @see org.springframework.core.Ordered#getOrder() */ public int getOrder() { - return Integer.MAX_VALUE; + return Ordered.LOWEST_PRECEDENCE; } /* (non-Javadoc) @@ -41,5 +44,13 @@ public class DefaultHistoryFileNameProvider implements HistoryFileNameProvider{ public String getHistoryFileName() { return Constant.HISTORY_FILE_NAME; } + + /* (non-Javadoc) + * @see org.springframework.shell.plugin.PluginProvider#name() + */ + @Override + public String name() { + return "default banner provider"; + } } diff --git a/src/main/java/org/springframework/shell/plugin/support/DefaultPromptProvider.java b/src/main/java/org/springframework/shell/plugin/support/DefaultPromptProvider.java index 98838d66..92a7d6ed 100644 --- a/src/main/java/org/springframework/shell/plugin/support/DefaultPromptProvider.java +++ b/src/main/java/org/springframework/shell/plugin/support/DefaultPromptProvider.java @@ -15,24 +15,27 @@ */ package org.springframework.shell.plugin.support; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.shell.Constant; import org.springframework.shell.plugin.PromptProvider; import org.springframework.stereotype.Component; /** - * Default prompt provider. The promp text is {@link org.springframework.shell.Constant.COMMAND_LINE_PROMPT} + * Default prompt provider. The prompt text is {@link org.springframework.shell.Constant.COMMAND_LINE_PROMPT} * * @author Jarred Li * */ @Component +@Order(Ordered.LOWEST_PRECEDENCE) public class DefaultPromptProvider implements PromptProvider{ /* (non-Javadoc) * @see org.springframework.core.Ordered#getOrder() */ public int getOrder() { - return Integer.MAX_VALUE; + return Ordered.LOWEST_PRECEDENCE; } /* (non-Javadoc) @@ -41,5 +44,13 @@ public class DefaultPromptProvider implements PromptProvider{ public String getPromptText() { return Constant.COMMAND_LINE_PROMPT; } + + /* (non-Javadoc) + * @see org.springframework.shell.plugin.PluginProvider#name() + */ + @Override + public String name() { + return "default banner provider"; + } } diff --git a/src/test/java/org/springframework/shell/plugin/support/DefaultHistoryFileProviderTest.java b/src/test/java/org/springframework/shell/plugin/support/DefaultHistoryFileProviderTest.java index a6ba66c7..de71397d 100644 --- a/src/test/java/org/springframework/shell/plugin/support/DefaultHistoryFileProviderTest.java +++ b/src/test/java/org/springframework/shell/plugin/support/DefaultHistoryFileProviderTest.java @@ -15,9 +15,9 @@ */ package org.springframework.shell.plugin.support; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; -import org.junit.Assert; import org.junit.Test; import org.springframework.shell.Constant; import org.springframework.shell.plugin.HistoryFileNameProvider; @@ -30,14 +30,6 @@ public class DefaultHistoryFileProviderTest { private HistoryFileNameProvider historyFile = new DefaultHistoryFileNameProvider(); - /** - * Test method for {@link org.springframework.shell.plugin.support.DefaultHistoryFileNameProvider#getOrder()}. - */ - @Test - public void testGetOrder() { - Assert.assertTrue(historyFile.getOrder() == 0); - } - /** * Test method for {@link org.springframework.shell.plugin.support.DefaultHistoryFileNameProvider#getHistoryFileName()}. */ diff --git a/src/test/java/org/springframework/shell/plugin/support/DefaultPromptProviderTest.java b/src/test/java/org/springframework/shell/plugin/support/DefaultPromptProviderTest.java index cac00c18..580bd36d 100644 --- a/src/test/java/org/springframework/shell/plugin/support/DefaultPromptProviderTest.java +++ b/src/test/java/org/springframework/shell/plugin/support/DefaultPromptProviderTest.java @@ -15,7 +15,7 @@ */ package org.springframework.shell.plugin.support; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -47,13 +47,6 @@ public class DefaultPromptProviderTest { prompt = null; } - /** - * Test method for {@link org.springframework.shell.plugin.support.DefaultPromptProvider#getOrder()}. - */ - @Test - public void testGetOrder() { - assertEquals(0, prompt.getOrder()); - } /** * Test method for {@link org.springframework.shell.plugin.support.DefaultPromptProvider#getPromptText()}.