* propagated name change in module directory names and pom

git-svn-id: svn+ssh://svn.synyx.de/var/svn/synyx/opensource/hera/trunk@3033 5a64d73e-33d6-4ccc-9058-23f8668ecac9
This commit is contained in:
Oliver Gierke
2008-10-15 16:18:00 +00:00
parent 12a8401c3e
commit 8bc770e679
43 changed files with 14 additions and 15 deletions

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

View File

@@ -0,0 +1,21 @@
package org.synyx.plugin.core;
/**
* Central interface for plugins for the system. This interface is meant to be
* extended by concrete plugin interfaces. Its core responsibility is to define
* a delimiter type and a selection callback with the delimiter as parameter.
* The delimiter is some kind of decision object concrete plugin implementations
* can use to decide if they are capable to be executed.
*
* @author Oliver Gierke - gierke@synyx.de
*/
public interface Plugin<S> {
/**
* Returns if a plugin should be invoked according to the given delimiter.
*
* @param delimiter
* @return if the plugin should be invoked
*/
boolean supports(S delimiter);
}

View File

@@ -0,0 +1,224 @@
package org.synyx.plugin.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 <T extends Plugin<S>, 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();
}
}

View File

@@ -0,0 +1,66 @@
package org.synyx.plugin.core.config;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* @author Oliver Gierke - gierke@synyx.de
*/
public class PluginListDefinitionParser extends
AbstractSingleBeanDefinitionParser {
/*
* (non-Javadoc)
*
* @see
* org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser
* #getBeanClassName(org.w3c.dom.Element)
*/
@Override
protected String getBeanClassName(Element element) {
return "org.synyx.plugin.core.support.BeanListBeanFactoryPostProcessor";
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser
* #doParse(org.w3c.dom.Element,
* org.springframework.beans.factory.support.BeanDefinitionBuilder)
*/
@Override
@SuppressWarnings("unchecked")
protected void doParse(Element element, BeanDefinitionBuilder builder) {
ManagedMap map = new ManagedMap();
map.put(element.getAttribute("id"), element.getAttribute("class"));
builder.addPropertyValue("lists", map);
String initFactories = element.getAttribute("init-factories");
if (StringUtils.hasText(initFactories)) {
builder.addPropertyValue("allowEagerInit", initFactories);
}
}
/*
* (non-Javadoc)
*
* @seeorg.springframework.beans.factory.xml.AbstractBeanDefinitionParser#
* shouldGenerateId()
*/
@Override
protected boolean shouldGenerateId() {
return true;
}
}

View File

@@ -0,0 +1,41 @@
/*
* 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.plugin.core.config;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
/**
* Simple namespace handler for {@literal dao-config} namespace.
*
* @author Eberhard Wolff
* @author Oliver Gierke - gierke@synyx.de
*/
public class PluginNamespaceHandler extends NamespaceHandlerSupport {
/*
* (non-Javadoc)
*
* @see org.springframework.beans.factory.xml.NamespaceHandler#init()
*/
public void init() {
registerBeanDefinitionParser("list", new PluginListDefinitionParser());
registerBeanDefinitionParser("registry",
new PluginRegistryDefinitionParser());
}
}

View File

@@ -0,0 +1,23 @@
package org.synyx.plugin.core.config;
import org.w3c.dom.Element;
/**
* @author Oliver Gierke - gierke@synyx.de
*/
public class PluginRegistryDefinitionParser extends PluginListDefinitionParser {
/*
* (non-Javadoc)
*
* @see
* org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser
* #getBeanClassName(org.w3c.dom.Element)
*/
@Override
protected String getBeanClassName(Element element) {
return "org.synyx.plugin.core.support.PluginRegistryBeanFactoryPostProcessor";
}
}

View File

@@ -0,0 +1,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
This package contains configuration support classes to ease registry configuration with
Spring namespaces.
</body>
</html>

View File

@@ -0,0 +1,8 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
This package contains the core plugin API. It allows other modules implementing
components that extend functionality defined by a plugin interface. Plugin clients
can be equipped with plugin implementations.
</body>
</html>

View File

@@ -0,0 +1,217 @@
package org.synyx.plugin.core.support;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.ListFactoryBean;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.ManagedList;
import org.synyx.plugin.core.PluginRegistry;
/**
* Simple {@link BeanFactoryPostProcessor} to autocreate lists of the configured
* {@code lists} property. The post processor will register
* {@link ListFactoryBean}s for each list named with the given key containing
* all beans of the {@link org.springframework.context.ApplicationContext}
* implementing the interface defines as lists value. E.g.:
*
* <pre>
* &lt;bean class=&quot;org.synyx.plugin.core.support.BeanListFactoryPostProcessor&quot;&gt;
* &lt;property name=&quot;lists&quot;&gt;
* &lt;map&gt;
* &lt;entry key=&quot;beanName&quot; value=&quot;org.synyx.plugin.core.Plugin&quot; /&gt;
* &lt;/map&gt;
* &lt;/property&gt;
* &lt;/bean&gt;
* </pre>
*
* This would register all Spring beans implementing the
* {@link org.synyx.plugin.core.Plugin} interface in a list bean with the name
* {@coder beanName}.
*
* @author Oliver Gierke - gierke@synyx.de
*/
public class BeanListBeanFactoryPostProcessor implements
BeanFactoryPostProcessor {
private static final Log log =
LogFactory.getLog(BeanListBeanFactoryPostProcessor.class);
private Map<String, Class<?>> lists = new HashMap<String, Class<?>>();
private boolean initFactories = false;
/**
* Setter to inject required plugin registry configuration. The map's keys
* will be used as bean ids for the resulting {@link PluginRegistry}
* instances. These registry instances will contain all beans having the
* given type.
*
* @param registryMap
*/
public void setLists(Map<String, Class<?>> lists) {
this.lists = lists;
}
/**
* Setter to activate {@link org.springframework.beans.factory.FactoryBean}
* scanning. This allows auto-registration of factory bean targets (the
* objects actually created by the factory bean} but comes with the drawback
* of having to initialize those factories eagerly. This can cause
* unpredictable behaviour if other {@link BeanFactoryPostProcessor}s would
* have been applied to these factories. Defaults to {@value #initFactories}
* .
*
* @param initFactories the allowEagerInit to set
*/
public void setInitFactories(boolean initFactories) {
this.initFactories = initFactories;
}
/*
* (non-Javadoc)
*
* @seeorg.springframework.beans.factory.config.BeanFactoryPostProcessor#
* postProcessBeanFactory
* (org.springframework.beans.factory.config.ConfigurableListableBeanFactory
* )
*/
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
if (!(beanFactory instanceof BeanDefinitionRegistry)) {
throw new IllegalArgumentException(
"Given beanFactory is not a BeanDefinitionRegistry!");
}
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
// Do for each required wrapper
for (String wrapperName : lists.keySet()) {
Class<?> requiredType = lists.get(wrapperName);
// Find implementations of the component type
List<RuntimeBeanReference> beans =
getBeanReferencesOfType(beanFactory, requiredType);
registerListBeanDefinition(wrapperName, requiredType, beans,
registry);
}
}
/**
* Registers a {@link ListFactoryBean} containing the given beans under the
* given id. Allows subclasses to modify or wrap the actually registered
* bean definition by calling
* {@link #wrapListBeanDefinition(BeanDefinition)} with the bean definition
* of the {<7B>linl {@link ListFactoryBean}.
*
* @param id
* @param beans
* @param registry
*/
protected <T> void registerListBeanDefinition(String id, Class<T> type,
List<RuntimeBeanReference> beans, BeanDefinitionRegistry registry) {
// Create ListFactory bean definition
BeanDefinitionBuilder listDefinitionBuilder =
BeanDefinitionBuilder.rootBeanDefinition(ListFactoryBean.class);
// Set the implementations as source for the wrapper
listDefinitionBuilder.addPropertyValue("sourceList", beans);
BeanDefinition beanDefinition =
wrapListBeanDefinition(getSourcedBeanDefinition(listDefinitionBuilder));
if (log.isDebugEnabled()) {
log.debug(String.format(
"Registering bean '%s' of type %s containing %s!", id,
beanDefinition.getBeanClassName(), beans.toString()));
}
// Register wrapper under defined name
registry.registerBeanDefinition(id, beanDefinition);
}
/**
* Template method to allow subclasses to wrap the list bean definition into
* another bean definition that will actually be registered. The default
* implementation simply returns the given bean definition.
*
* @param beanDefinition
* @return
*/
protected BeanDefinition wrapListBeanDefinition(
BeanDefinition beanDefinition) {
return beanDefinition;
}
/**
* Returns a {@link BeanDefinitionBuilder}s {@link BeanDefinition} setting
* the current {@link PluginRegistryBeanFactoryPostProcessor} as source.
*
* @param builder
* @return
*/
protected BeanDefinition getSourcedBeanDefinition(
BeanDefinitionBuilder builder) {
AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
beanDefinition.setSource(this);
return beanDefinition;
}
/**
* Returns bean references to beans with the given type.
*
* @param beanFactory
* @param type
* @return
*/
@SuppressWarnings("unchecked")
protected List<RuntimeBeanReference> getBeanReferencesOfType(
ConfigurableListableBeanFactory beanFactory, Class<?> type) {
// Lookup bean candidates either via Spring or manually
List<String> beanNames =
Arrays.asList(beanFactory.getBeanNamesForType(type, true,
initFactories));
List<RuntimeBeanReference> beanReferences =
new ManagedList(beanNames.size());
for (String beanName : beanNames) {
RuntimeBeanReference reference = new RuntimeBeanReference(beanName);
reference.setSource(this);
beanReferences.add(reference);
}
return (List<RuntimeBeanReference>) beanReferences;
}
}

View File

@@ -0,0 +1,43 @@
package org.synyx.plugin.core.support;
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.plugin.core.PluginRegistry;
/**
* This {@link BeanFactoryPostProcessor} automatically looksup bean instances
* from the {@link BeanFactory} hierarchy and registers {@link PluginRegistry}
* instances for them.
*
* @see org.synyx.plugin.core.support.BeanListBeanFactoryPostProcessor
* @author Oliver Gierke - gierke@synyx.de
*/
public class PluginRegistryBeanFactoryPostProcessor extends
BeanListBeanFactoryPostProcessor {
/**
* Additionally wraps the
* {@link org.springframework.beans.factory.config.ListFactoryBean}
* {@link BeanDefinition} into a {@link PluginRegistry}.
*
* @see com.synyx.minos.core.plugin.support.BeanListBeanFactoryPostProcessor#
* wrapBeanDefinition
* (org.springframework.beans.factory.config.BeanDefinition)
* @return a {@link BeanDefinition} containing a {@link PluginRegistry}
*/
@Override
protected BeanDefinition wrapListBeanDefinition(
BeanDefinition beanDefinition) {
// Create PluginRegistry bean definition to wrap actual bean definition
BeanDefinitionBuilder builder =
BeanDefinitionBuilder.rootBeanDefinition(PluginRegistry.class);
builder.addPropertyValue("plugins", beanDefinition);
return getSourcedBeanDefinition(builder);
}
}

View File

@@ -0,0 +1,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
This package contains support classes to create bean lists or plugin
registry instances out of beans implementing a certain interface.
</body>
</html>