refactor plugin, add Order annoaton support

This commit is contained in:
Jarred Li
2012-04-13 15:39:01 +08:00
parent 5d8a8d9c46
commit 16610f1627
13 changed files with 121 additions and 85 deletions

View File

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

View File

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

View File

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

View File

@@ -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<String, HistoryFileNameProvider> 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<String, PromptProvider> 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<String, BannerProvider> 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 <T> Map<String, T> getBeansInFactory(Class<T> t){
Map<String, T> result = BeanFactoryUtils.beansOfTypeIncludingAncestors(
private <T extends PluginProvider> T getHighestPriorityProvider(Class<T> t){
Map<String, T> providers = BeanFactoryUtils.beansOfTypeIncludingAncestors(
this.applicatonContext, t);
return result;
List<T> sortedProviders = new ArrayList<T>(providers.values());
Collections.sort(sortedProviders, annocationOrderComparator);
T highestPriorityProvider = sortedProviders.get(0);
return highestPriorityProvider;
}
@Override

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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()}.
*/

View File

@@ -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()}.