* fixed #60 - created @OrderAwarePluginRegistry@ that uses Spring ordering API to ensure correct order of plugins
* let @PluginRegistryBeanFactoryPostProcessor@ use the new one instead of the old one * renamed old @PluginRegistry@ class intro @SimplePluginRegistry@ * extracted @PluginRegistry@ interface * added some unittests for both implementations git-svn-id: svn+ssh://svn.synyx.de/var/svn/synyx/opensource/hera/trunk@4487 5a64d73e-33d6-4ccc-9058-23f8668ecac9
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package org.synyx.hera.core;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
|
||||
/**
|
||||
* {@link PluginRegistry} implementation that can handle {@link Plugin}s using
|
||||
* the {@link Ordered} interface or {@link Order} annotation.
|
||||
*
|
||||
* @author Oliver Gierke - gierke@synyx.de
|
||||
*/
|
||||
public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends
|
||||
SimplePluginRegistry<T, S> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Comparator<T> comparator = new AnnotationAwareOrderComparator();
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link SimplePluginRegistry}.
|
||||
*
|
||||
* @param <T>
|
||||
* @param <S>
|
||||
* @return
|
||||
*/
|
||||
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create() {
|
||||
|
||||
return new OrderAwarePluginRegistry<T, S>();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.synyx.hera.core.PluginRegistry#setPlugins(java.util.List)
|
||||
*/
|
||||
@Override
|
||||
public void setPlugins(List<? extends T> plugins) {
|
||||
|
||||
Collections.sort(plugins, comparator);
|
||||
super.setPlugins(plugins);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.synyx.hera.core.PluginRegistry#addPlugin(org.synyx.hera.core.Plugin)
|
||||
*/
|
||||
@Override
|
||||
public void addPlugin(T plugin) {
|
||||
|
||||
super.addPlugin(plugin);
|
||||
Collections.sort(getPlugins(), comparator);
|
||||
}
|
||||
}
|
||||
@@ -1,240 +1,110 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.synyx.hera.core;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Registry for plugins. Allows sophisticated typesafe access to implementations
|
||||
* of interfaces extending {link Plugin}.
|
||||
*
|
||||
* @param <T> the concrete plugin interface
|
||||
* @param <S> the delimiter type
|
||||
* @author Oliver Gierke - gierke@synyx.de
|
||||
*/
|
||||
public class PluginRegistry<T extends Plugin<S>, S> implements Iterable<T> {
|
||||
|
||||
// Registered plugins
|
||||
private List<T> plugins;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@code PluginRegistry}.
|
||||
*/
|
||||
public PluginRegistry() {
|
||||
|
||||
plugins = new ArrayList<T>();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link PluginRegistry}.
|
||||
*
|
||||
* @param <T>
|
||||
* @param <S>
|
||||
* @return
|
||||
*/
|
||||
public static <S, T extends Plugin<S>> PluginRegistry<T, S> create() {
|
||||
|
||||
return new PluginRegistry<T, S>();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Register plugins.
|
||||
*
|
||||
* @param plugins the plugins to set
|
||||
*/
|
||||
public void setPlugins(List<? extends T> plugins) {
|
||||
|
||||
this.plugins = new ArrayList<T>();
|
||||
this.plugins.addAll(plugins);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a given plugin to the registry.
|
||||
*
|
||||
* @param plugin
|
||||
*/
|
||||
public void addPlugin(T plugin) {
|
||||
|
||||
this.plugins.add(plugin);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the first plugin found for the given originating system. Thus,
|
||||
* further configured plugins are ignored.
|
||||
*
|
||||
* @param originatingSystem
|
||||
* @return a plugin for the given originating system or {@code null} if none
|
||||
* found
|
||||
*/
|
||||
public T getPluginFor(S delimiter) {
|
||||
|
||||
List<T> plugins = getPluginsFor(delimiter);
|
||||
|
||||
if (0 < plugins.size()) {
|
||||
return plugins.get(0);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns all plugins for the given delimiter.
|
||||
*
|
||||
* @param delimiter
|
||||
* @return a list of plugins or an empty list if none found
|
||||
*/
|
||||
public List<T> getPluginsFor(S delimiter) {
|
||||
|
||||
List<T> result = new ArrayList<T>();
|
||||
|
||||
for (T plugin : plugins) {
|
||||
if (plugin.supports(delimiter)) {
|
||||
result.add(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves a required plugin from the registry or throw the given
|
||||
* exception if none can be found. If more than one plugins are found the
|
||||
* first one will be returned.
|
||||
*
|
||||
* @param <E> the exception type to be thrown in case no plugin can be found
|
||||
* @param delimiter
|
||||
* @param ex the exception to be thrown in case no plugin can be found
|
||||
* @return a single plugin for the given delimiter
|
||||
* @throws E if no plugin can be found for the given delimiter
|
||||
*/
|
||||
public <E extends Exception> T getPluginFor(S delimiter, E ex) throws E {
|
||||
|
||||
T plugin = getPluginFor(delimiter);
|
||||
|
||||
if (null == plugin) {
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return plugin;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves all plugins for the given delimiter or throws an exception if
|
||||
* no plugin can be found.
|
||||
*
|
||||
* @param <E> the exception type to be thrown
|
||||
* @param delimiter
|
||||
* @param ex
|
||||
* @return all plugins for the given delimiter
|
||||
* @throws E if no plugin can be found
|
||||
*/
|
||||
public <E extends Exception> List<T> getPluginsFor(S delimiter, E ex)
|
||||
throws E {
|
||||
|
||||
List<T> plugins = getPluginsFor(delimiter);
|
||||
|
||||
if (0 == plugins.size()) {
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the first {@link Plugin} supporting the given delimiter or the
|
||||
* given plugin if none can be found.
|
||||
*
|
||||
* @param delimiter
|
||||
* @param plugin
|
||||
* @return a single {@link Plugin} supporting the given delimiter or the
|
||||
* given {@link Plugin} if none found
|
||||
*/
|
||||
public T getPluginFor(S delimiter, T plugin) {
|
||||
|
||||
T candidate = getPluginFor(delimiter);
|
||||
|
||||
return null == candidate ? plugin : candidate;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns all {@link Plugin}s supporting the given delimiter or the given
|
||||
* plugins if none found.
|
||||
*
|
||||
* @param delimiter
|
||||
* @param plugins
|
||||
* @return all {@link Plugin}s supporting the given delimiter or the given
|
||||
* {@link Plugin}s if none found
|
||||
*/
|
||||
public List<T> getPluginsFor(S delimiter, List<T> plugins) {
|
||||
|
||||
List<T> candidates = getPluginsFor(delimiter);
|
||||
|
||||
return candidates.size() == 0 ? plugins : candidates;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of registered plugins.
|
||||
*
|
||||
* @return the number of plugins in the registry
|
||||
*/
|
||||
public int countPlugins() {
|
||||
|
||||
return plugins.size();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns all registered plugins. Only use this method if you really need
|
||||
* to access all plugins. For distinguished access to certain plugins favour
|
||||
* accessor methods like {link #getPluginFor} over this one. This method
|
||||
* should only be used for testing purposes to check registry configuration.
|
||||
* <p>
|
||||
* TODO: decide whether to make this method public
|
||||
*
|
||||
* @return all plugins of the registry
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
private List<? extends T> getPlugins() {
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Iterable#iterator()
|
||||
*/
|
||||
public Iterator<T> iterator() {
|
||||
|
||||
return plugins.iterator();
|
||||
}
|
||||
}
|
||||
package org.synyx.hera.core;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Registry for plugins. Allows sophisticated typesafe access to implementations
|
||||
* of interfaces extending {link Plugin}.
|
||||
*
|
||||
* @param <T> the concrete plugin interface
|
||||
* @param <S> the delimiter type
|
||||
* @author Oliver Gierke - gierke@synyx.de
|
||||
*/
|
||||
public interface PluginRegistry<T extends Plugin<S>, S> extends Iterable<T> {
|
||||
|
||||
/**
|
||||
* Register plugins.
|
||||
*
|
||||
* @param plugins the plugins to set
|
||||
*/
|
||||
void setPlugins(List<? extends T> plugins);
|
||||
|
||||
|
||||
/**
|
||||
* Adds a given plugin to the registry.
|
||||
*
|
||||
* @param plugin
|
||||
*/
|
||||
void addPlugin(T plugin);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the first plugin found for the given originating system. Thus,
|
||||
* further configured plugins are ignored.
|
||||
*
|
||||
* @param originatingSystem
|
||||
* @return a plugin for the given originating system or {@code null} if none
|
||||
* found
|
||||
*/
|
||||
T getPluginFor(S delimiter);
|
||||
|
||||
|
||||
/**
|
||||
* Returns all plugins for the given delimiter.
|
||||
*
|
||||
* @param delimiter
|
||||
* @return a list of plugins or an empty list if none found
|
||||
*/
|
||||
List<T> getPluginsFor(S delimiter);
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves a required plugin from the registry or throw the given
|
||||
* exception if none can be found. If more than one plugins are found the
|
||||
* first one will be returned.
|
||||
*
|
||||
* @param <E> the exception type to be thrown in case no plugin can be found
|
||||
* @param delimiter
|
||||
* @param ex the exception to be thrown in case no plugin can be found
|
||||
* @return a single plugin for the given delimiter
|
||||
* @throws E if no plugin can be found for the given delimiter
|
||||
*/
|
||||
<E extends Exception> T getPluginFor(S delimiter, E ex) throws E;
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves all plugins for the given delimiter or throws an exception if
|
||||
* no plugin can be found.
|
||||
*
|
||||
* @param <E> the exception type to be thrown
|
||||
* @param delimiter
|
||||
* @param ex
|
||||
* @return all plugins for the given delimiter
|
||||
* @throws E if no plugin can be found
|
||||
*/
|
||||
<E extends Exception> List<T> getPluginsFor(S delimiter, E ex) throws E;
|
||||
|
||||
|
||||
/**
|
||||
* Returns the first {@link Plugin} supporting the given delimiter or the
|
||||
* given plugin if none can be found.
|
||||
*
|
||||
* @param delimiter
|
||||
* @param plugin
|
||||
* @return a single {@link Plugin} supporting the given delimiter or the
|
||||
* given {@link Plugin} if none found
|
||||
*/
|
||||
T getPluginFor(S delimiter, T plugin);
|
||||
|
||||
|
||||
/**
|
||||
* Returns all {@link Plugin}s supporting the given delimiter or the given
|
||||
* plugins if none found.
|
||||
*
|
||||
* @param delimiter
|
||||
* @param plugins
|
||||
* @return all {@link Plugin}s supporting the given delimiter or the given
|
||||
* {@link Plugin}s if none found
|
||||
*/
|
||||
List<? extends T> getPluginsFor(S delimiter, List<? extends T> plugins);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of registered plugins.
|
||||
*
|
||||
* @return the number of plugins in the registry
|
||||
*/
|
||||
int countPlugins();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.synyx.hera.core;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Basic implementation of {@link PluginRegistry}. Simply holds all given
|
||||
* plugins in a list.
|
||||
*
|
||||
* @param <T> the concrete plugin interface
|
||||
* @param <S> the delimiter type
|
||||
* @author Oliver Gierke - gierke@synyx.de
|
||||
*/
|
||||
public class SimplePluginRegistry<T extends Plugin<S>, S> implements
|
||||
PluginRegistry<T, S> {
|
||||
|
||||
// Registered plugins
|
||||
private List<T> plugins;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@code PluginRegistry}.
|
||||
*/
|
||||
public SimplePluginRegistry() {
|
||||
|
||||
plugins = new ArrayList<T>();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link SimplePluginRegistry}.
|
||||
*
|
||||
* @param <T>
|
||||
* @param <S>
|
||||
* @return
|
||||
*/
|
||||
public static <S, T extends Plugin<S>> PluginRegistry<T, S> create() {
|
||||
|
||||
return new SimplePluginRegistry<T, S>();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.synyx.hera.core.PluginRegistry#setPlugins(java.util.List)
|
||||
*/
|
||||
public void setPlugins(List<? extends T> plugins) {
|
||||
|
||||
this.plugins = new ArrayList<T>();
|
||||
this.plugins.addAll(plugins);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.synyx.hera.core.PluginRegistry#addPlugin(T)
|
||||
*/
|
||||
public void addPlugin(T plugin) {
|
||||
|
||||
this.plugins.add(plugin);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.synyx.hera.core.PluginRegistry#getPluginFor(S)
|
||||
*/
|
||||
public T getPluginFor(S delimiter) {
|
||||
|
||||
List<T> plugins = getPluginsFor(delimiter);
|
||||
|
||||
if (0 < plugins.size()) {
|
||||
return plugins.get(0);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.synyx.hera.core.PluginRegistry#getPluginsFor(S)
|
||||
*/
|
||||
public List<T> getPluginsFor(S delimiter) {
|
||||
|
||||
List<T> result = new ArrayList<T>();
|
||||
|
||||
for (T plugin : plugins) {
|
||||
if (plugin.supports(delimiter)) {
|
||||
result.add(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.synyx.hera.core.PluginRegistry#getPluginFor(S, E)
|
||||
*/
|
||||
public <E extends Exception> T getPluginFor(S delimiter, E ex) throws E {
|
||||
|
||||
T plugin = getPluginFor(delimiter);
|
||||
|
||||
if (null == plugin) {
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return plugin;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.synyx.hera.core.PluginRegistry#getPluginsFor(S, E)
|
||||
*/
|
||||
public <E extends Exception> List<T> getPluginsFor(S delimiter, E ex)
|
||||
throws E {
|
||||
|
||||
List<T> plugins = getPluginsFor(delimiter);
|
||||
|
||||
if (0 == plugins.size()) {
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.synyx.hera.core.PluginRegistry#getPluginFor(S, T)
|
||||
*/
|
||||
public T getPluginFor(S delimiter, T plugin) {
|
||||
|
||||
T candidate = getPluginFor(delimiter);
|
||||
|
||||
return null == candidate ? plugin : candidate;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.synyx.hera.core.PluginRegistry#getPluginsFor(S, java.util.List)
|
||||
*/
|
||||
public List<? extends T> getPluginsFor(S delimiter,
|
||||
List<? extends T> plugins) {
|
||||
|
||||
List<T> candidates = getPluginsFor(delimiter);
|
||||
|
||||
return candidates.size() == 0 ? plugins : candidates;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.synyx.hera.core.PluginRegistry#countPlugins()
|
||||
*/
|
||||
public int countPlugins() {
|
||||
|
||||
return plugins.size();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns all registered plugins. Only use this method if you really need
|
||||
* to access all plugins. For distinguished access to certain plugins favour
|
||||
* accessor methods like {link #getPluginFor} over this one. This method
|
||||
* should only be used for testing purposes to check registry configuration.
|
||||
* <p>
|
||||
* TODO: decide whether to make this method public
|
||||
*
|
||||
* @return all plugins of the registry
|
||||
*/
|
||||
protected List<? extends T> getPlugins() {
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Iterable#iterator()
|
||||
*/
|
||||
public Iterator<T> iterator() {
|
||||
|
||||
return plugins.iterator();
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.ManagedList;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.synyx.hera.core.PluginRegistry;
|
||||
import org.synyx.hera.core.SimplePluginRegistry;
|
||||
|
||||
|
||||
/**
|
||||
@@ -72,7 +72,7 @@ public class BeanListBeanFactoryPostProcessor implements
|
||||
|
||||
/**
|
||||
* Setter to inject required plugin registry configuration. The map's keys
|
||||
* will be used as bean ids for the resulting {@link PluginRegistry}
|
||||
* will be used as bean ids for the resulting {@link SimplePluginRegistry}
|
||||
* instances. These registry instances will contain all beans having the
|
||||
* given type.
|
||||
*
|
||||
|
||||
@@ -19,13 +19,13 @@ import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.synyx.hera.core.PluginRegistry;
|
||||
import org.synyx.hera.core.OrderAwarePluginRegistry;
|
||||
|
||||
|
||||
/**
|
||||
* This {@link BeanFactoryPostProcessor} automatically looksup bean instances
|
||||
* from the {@link BeanFactory} hierarchy and registers {@link PluginRegistry}
|
||||
* instances for them.
|
||||
* from the {@link BeanFactory} hierarchy and registers
|
||||
* {@link OrderAwarePluginRegistry} instances for them.
|
||||
*
|
||||
* @see org.synyx.hera.core.support.BeanListBeanFactoryPostProcessor
|
||||
* @author Oliver Gierke - gierke@synyx.de
|
||||
@@ -36,12 +36,13 @@ public class PluginRegistryBeanFactoryPostProcessor extends
|
||||
/**
|
||||
* Additionally wraps the
|
||||
* {@link org.springframework.beans.factory.config.ListFactoryBean}
|
||||
* {@link BeanDefinition} into a {@link PluginRegistry}.
|
||||
* {@link BeanDefinition} into a {@link OrderAwarePluginRegistry}.
|
||||
*
|
||||
* @see com.synyx.minos.core.plugin.support.BeanListBeanFactoryPostProcessor#
|
||||
* wrapBeanDefinition
|
||||
* (org.springframework.beans.factory.config.BeanDefinition)
|
||||
* @return a {@link BeanDefinition} containing a {@link PluginRegistry}
|
||||
* @return a {@link BeanDefinition} containing a
|
||||
* {@link OrderAwarePluginRegistry}
|
||||
*/
|
||||
@Override
|
||||
protected BeanDefinition wrapListBeanDefinition(
|
||||
@@ -49,7 +50,8 @@ public class PluginRegistryBeanFactoryPostProcessor extends
|
||||
|
||||
// Create PluginRegistry bean definition to wrap actual bean definition
|
||||
BeanDefinitionBuilder builder =
|
||||
BeanDefinitionBuilder.rootBeanDefinition(PluginRegistry.class);
|
||||
BeanDefinitionBuilder
|
||||
.rootBeanDefinition(OrderAwarePluginRegistry.class);
|
||||
|
||||
builder.addPropertyValue("plugins", beanDefinition);
|
||||
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
package org.synyx.hera.core;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
|
||||
/**
|
||||
* Unit test for {@link OrderAwarePluginRegistry} that especially concentrates
|
||||
* on testing ordering functionality.
|
||||
*
|
||||
* @author Oliver Gierke - gierke@synyx.de
|
||||
*/
|
||||
public class OrderAwarePluginRegistryUnitTest extends
|
||||
SimplePluginRegistryUnitTest {
|
||||
|
||||
private PluginRegistry<TestPlugin, String> registry;
|
||||
|
||||
private TestPlugin firstPlugin;
|
||||
private TestPlugin secondPlugin;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
super.setUp();
|
||||
|
||||
registry = OrderAwarePluginRegistry.create();
|
||||
|
||||
firstPlugin = new FirstImplementation();
|
||||
secondPlugin = new SecondImplementation();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds the plugin implementations in order of their names, expecting the
|
||||
* registry to order them correctly.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void honorsOrderOnAddPlugins() throws Exception {
|
||||
|
||||
registry.setPlugins(Arrays.asList(firstPlugin, secondPlugin));
|
||||
|
||||
assertOrder();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void assertsOrderOnAddingPlugins() throws Exception {
|
||||
|
||||
registry.setPlugins(Arrays.asList(firstPlugin));
|
||||
registry.addPlugin(secondPlugin);
|
||||
|
||||
assertOrder();
|
||||
}
|
||||
|
||||
|
||||
private void assertOrder() {
|
||||
|
||||
List<TestPlugin> plugins = registry.getPluginsFor(null);
|
||||
|
||||
assertEquals(2, plugins.size());
|
||||
assertEquals(secondPlugin, plugins.get(0));
|
||||
assertEquals(firstPlugin, plugins.get(1));
|
||||
|
||||
assertEquals(secondPlugin, registry.getPluginFor(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple test interface.
|
||||
*
|
||||
* @author Oliver Gierke - gierke@synyx.de
|
||||
*/
|
||||
private static interface TestPlugin extends Plugin<String> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin implementation, that is orderd right AFTER the second one.
|
||||
*
|
||||
* @author Oliver Gierke - gierke@synyx.de
|
||||
*/
|
||||
@Order(5)
|
||||
private static class FirstImplementation implements TestPlugin {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.synyx.hera.core.Plugin#supports(java.lang.Object)
|
||||
*/
|
||||
public boolean supports(String delimiter) {
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin implementation that is ordered BEFORE the first one.
|
||||
*
|
||||
* @author Oliver Gierke - gierke@synyx.de
|
||||
*/
|
||||
@Order(1)
|
||||
private static class SecondImplementation implements TestPlugin {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.synyx.hera.core.Plugin#supports(java.lang.Object)
|
||||
*/
|
||||
public boolean supports(String delimiter) {
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.synyx.hera.core;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Unit test for {@link PluginRegistry}.
|
||||
*
|
||||
* @author Oliver Gierke - gierke@synyx.de
|
||||
*/
|
||||
public class PluginRegistryUnitTest {
|
||||
|
||||
private SamplePlugin provider;
|
||||
|
||||
private PluginRegistry<SamplePlugin, String> registry;
|
||||
|
||||
|
||||
/**
|
||||
* Initializes a {@code PluginRegistry} and equips it with an {@code
|
||||
* EmailNotificationProvider}.
|
||||
*/
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
provider = new SamplePluginImplementation();
|
||||
|
||||
registry = new PluginRegistry<SamplePlugin, String>();
|
||||
registry.setPlugins(Arrays.asList(provider));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Asserts asking for a plugin with the {@code PluginMetadata} provided by
|
||||
* the {@link EmailNotificationProvider}.
|
||||
*/
|
||||
@Test
|
||||
public void assertFindsEmailNotificationProvider() {
|
||||
|
||||
String metadata = "FOO";
|
||||
|
||||
List<SamplePlugin> plugins = registry.getPluginsFor(metadata);
|
||||
Assert.assertNotNull(plugins);
|
||||
Assert.assertEquals(1, plugins.size());
|
||||
|
||||
SamplePlugin provider = plugins.get(0);
|
||||
Assert.assertTrue(provider instanceof SamplePluginImplementation);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ package org.synyx.hera.core;
|
||||
public class SamplePluginHost {
|
||||
|
||||
private PluginRegistry<SamplePlugin, String> registry =
|
||||
PluginRegistry.create();
|
||||
SimplePluginRegistry.create();
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.synyx.hera.core;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Unit test for {@link SimplePluginRegistry}.
|
||||
*
|
||||
* @author Oliver Gierke - gierke@synyx.de
|
||||
*/
|
||||
public class SimplePluginRegistryUnitTest {
|
||||
|
||||
private SamplePlugin provider;
|
||||
|
||||
private PluginRegistry<SamplePlugin, String> registry;
|
||||
|
||||
|
||||
/**
|
||||
* Initializes a {@code PluginRegistry} and equips it with an {@code
|
||||
* EmailNotificationProvider}.
|
||||
*/
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
provider = new SamplePluginImplementation();
|
||||
|
||||
registry = new SimplePluginRegistry<SamplePlugin, String>();
|
||||
registry.setPlugins(Arrays.asList(provider));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Asserts asking for a plugin with the {@code PluginMetadata} provided by
|
||||
* the {@link EmailNotificationProvider}.
|
||||
*/
|
||||
@Test
|
||||
public void assertFindsEmailNotificationProvider() {
|
||||
|
||||
String metadata = "FOO";
|
||||
|
||||
List<SamplePlugin> plugins = registry.getPluginsFor(metadata);
|
||||
assertNotNull(plugins);
|
||||
assertEquals(1, plugins.size());
|
||||
|
||||
SamplePlugin provider = plugins.get(0);
|
||||
assertTrue(provider instanceof SamplePluginImplementation);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Expects the given exception to be thrown if no {@link Plugin} found.
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void throwsExceptionIfNoPluginFound() {
|
||||
|
||||
registry.getPluginFor("BAR", new IllegalArgumentException());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Expects the given exception to be thrown if no {@link Plugin}s found.
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void throwsExceptionIfNoPluginsFound() {
|
||||
|
||||
registry.getPluginsFor("BAR", new IllegalArgumentException());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Expect the defualt plugin to be returned if none found.
|
||||
*/
|
||||
@Test
|
||||
public void returnsDefaultIfNoneFound() {
|
||||
|
||||
SamplePlugin defaultPlugin = new SamplePluginImplementation();
|
||||
|
||||
assertEquals(defaultPlugin, registry.getPluginFor("BAR", defaultPlugin));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Expect the given default plugins to be returned if none found.
|
||||
*/
|
||||
@Test
|
||||
public void returnsDefaultsIfNoneFound() {
|
||||
|
||||
List<? extends SamplePlugin> defaultPlugins =
|
||||
Arrays.asList(new SamplePluginImplementation());
|
||||
|
||||
assertEquals(defaultPlugins, registry.getPluginsFor("BAR",
|
||||
defaultPlugins));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user