INT-1060 Added @Filter annotation.

This commit is contained in:
Mark Fisher
2010-04-12 20:03:23 +00:00
parent 1f7e12810a
commit 7e6591e219
7 changed files with 343 additions and 12 deletions

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2002-2010 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.integration.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that a method is capable of playing the role of a Message Filter.
* <p>
* A method annotated with @Filter may accept a parameter of type
* {@link org.springframework.integration.core.Message} or of the expected
* Message payload's type. Any type conversion supported by default or any
* Converters registered with the "integrationConversionService" bean will be
* applied to the Message payload if necessary. Header values can also be passed
* as Message parameters by using the {@link Header @Header} parameter annotation.
* <p>
* The return type of the annotated method must be a boolean (or Boolean).
*
* @author Mark Fisher
* @since 2.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Filter {
String inputChannel() default "";
String outputChannel() default "";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@@ -53,15 +53,17 @@ public class FilterFactoryBean extends AbstractMessageHandlerFactoryBean {
@Override
MessageHandler createMethodInvokingHandler(Object targetObject, String targetMethodName) {
MessageSelector selector = null;
if (targetObject instanceof MessageSelector) {
return this.createFilter((MessageSelector) targetObject);
selector = (MessageSelector) targetObject;
}
if (StringUtils.hasText(targetMethodName)) {
MessageSelector selector = new MethodInvokingSelector(targetObject, targetMethodName);
return this.createFilter(selector);
else if (StringUtils.hasText(targetMethodName)) {
selector = new MethodInvokingSelector(targetObject, targetMethodName);
}
throw new IllegalArgumentException("Filter must provide a target method" +
" name if the 'ref' does not point to a MessageSelector instance.");
else {
selector = new MethodInvokingSelector(targetObject);
}
return this.createFilter(selector);
}
@Override

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2002-2010 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.integration.config.annotation;
import java.lang.reflect.Method;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.integration.annotation.Filter;
import org.springframework.integration.filter.MessageFilter;
import org.springframework.integration.filter.MethodInvokingSelector;
import org.springframework.integration.message.MessageHandler;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Post-processor for Methods annotated with {@link Filter @Filter}.
*
* @author Mark Fisher
* @since 2.0
*/
public class FilterAnnotationPostProcessor extends AbstractMethodAnnotationPostProcessor<Filter> {
public FilterAnnotationPostProcessor(ListableBeanFactory beanFactory) {
super(beanFactory);
}
@Override
protected MessageHandler createHandler(Object bean, Method method, Filter annotation) {
Assert.isTrue(boolean.class.equals(method.getReturnType()) || Boolean.class.equals(method.getReturnType()),
"The Filter annotation may only be applied to methods with a boolean return type.");
MethodInvokingSelector selector = new MethodInvokingSelector(bean, method);
MessageFilter filter = new MessageFilter(selector);
String outputChannelName = annotation.outputChannel();
if (StringUtils.hasText(outputChannelName)) {
filter.setOutputChannel(this.channelResolver.resolveChannelName(outputChannelName));
}
return filter;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@@ -44,6 +44,7 @@ import org.springframework.context.Lifecycle;
import org.springframework.context.SmartLifecycle;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.integration.annotation.Aggregator;
import org.springframework.integration.annotation.Filter;
import org.springframework.integration.annotation.Router;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Splitter;
@@ -57,7 +58,7 @@ import org.springframework.util.StringUtils;
/**
* A {@link BeanPostProcessor} implementation that processes method-level
* messaging annotations such as @Transformer, @Splitter, and @Router.
* messaging annotations such as @Transformer, @Splitter, @Router, and @Filter.
*
* @author Mark Fisher
* @author Marius Bogoevici
@@ -87,11 +88,12 @@ public class MessagingAnnotationPostProcessor implements BeanPostProcessor, Bean
public void afterPropertiesSet() {
Assert.notNull(this.beanFactory, "BeanFactory must not be null");
postProcessors.put(Aggregator.class, new AggregatorAnnotationPostProcessor(this.beanFactory));
postProcessors.put(Filter.class, new FilterAnnotationPostProcessor(this.beanFactory));
postProcessors.put(Router.class, new RouterAnnotationPostProcessor(this.beanFactory));
postProcessors.put(Transformer.class, new TransformerAnnotationPostProcessor(this.beanFactory));
postProcessors.put(ServiceActivator.class, new ServiceActivatorAnnotationPostProcessor(this.beanFactory));
postProcessors.put(Splitter.class, new SplitterAnnotationPostProcessor(this.beanFactory));
postProcessors.put(Transformer.class, new TransformerAnnotationPostProcessor(this.beanFactory));
postProcessors.put(Aggregator.class, new AggregatorAnnotationPostProcessor(this.beanFactory));
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@@ -18,6 +18,7 @@ package org.springframework.integration.filter;
import java.lang.reflect.Method;
import org.springframework.integration.annotation.Filter;
import org.springframework.integration.handler.MethodInvokingMessageProcessor;
import org.springframework.integration.selector.MessageSelector;
import org.springframework.util.Assert;
@@ -41,4 +42,8 @@ public class MethodInvokingSelector extends AbstractMessageProcessingSelector {
super(new MethodInvokingMessageProcessor(object, methodName));
}
public MethodInvokingSelector(Object object) {
super(new MethodInvokingMessageProcessor(object, Filter.class));
}
}