OPEN - issue BATCH-1108: Add composite ItemWriter/Processor based on Classifier

First draft of classifier features added.  Also refactored MethodInvoker abstraction into infrastructure.
This commit is contained in:
dsyer
2009-03-03 11:55:03 +00:00
parent b8208dcd8a
commit 998f6f592e
25 changed files with 819 additions and 132 deletions

View File

@@ -1,104 +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.springframework.batch.core.configuration.util;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicReference;
import org.springframework.aop.support.AopUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
/**
* MethodResolver implementation that finds a <em>single</em> Method on the
* given Class that contains the specified annotation type.
*
* @author Mark Fisher
*/
public class AnnotationMethodResolver implements MethodResolver {
private Class<? extends Annotation> annotationType;
/**
* Create a MethodResolver for the specified Method-level annotation type
*/
public AnnotationMethodResolver(Class<? extends Annotation> annotationType) {
Assert.notNull(annotationType, "annotationType must not be null");
Assert.isTrue(ObjectUtils.containsElement(
annotationType.getAnnotation(Target.class).value(), ElementType.METHOD),
"Annotation [" + annotationType + "] is not a Method-level annotation.");
this.annotationType = annotationType;
}
/**
* Find a <em>single</em> Method on the Class of the given candidate object
* that contains the annotation type for which this resolver is searching.
*
* @param candidate the instance whose Class will be checked for the
* annotation
*
* @return a single matching Method instance or <code>null</code> if the
* candidate's Class contains no Methods with the specified annotation
*
* @throws IllegalArgumentException if more than one Method has the
* specified annotation
*/
public Method findMethod(Object candidate) {
Assert.notNull(candidate, "candidate object must not be null");
Class<?> targetClass = AopUtils.getTargetClass(candidate);
if (targetClass == null) {
targetClass = candidate.getClass();
}
return this.findMethod(targetClass);
}
/**
* Find a <em>single</em> Method on the given Class that contains the
* annotation type for which this resolver is searching.
*
* @param clazz the Class instance to check for the annotation
*
* @return a single matching Method instance or <code>null</code> if the
* Class contains no Methods with the specified annotation
*
* @throws IllegalArgumentException if more than one Method has the
* specified annotation
*/
public Method findMethod(final Class<?> clazz) {
Assert.notNull(clazz, "class must not be null");
final AtomicReference<Method> annotatedMethod = new AtomicReference<Method>();
ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType);
if (annotation != null) {
Assert.isNull(annotatedMethod.get(), "found more than one method on target class ["
+ clazz + "] with the annotation type [" + annotationType + "]");
annotatedMethod.set(method);
}
}
});
return annotatedMethod.get();
}
}

View File

@@ -1,29 +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.springframework.batch.core.configuration.util;
/**
* A strategy interface for invoking a method.
* Typically used by adapters.
*
* @author Mark Fisher
*/
public interface MethodInvoker {
Object invokeMethod(Object ... args);
}

View File

@@ -1,117 +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.springframework.batch.core.configuration.util;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicReference;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
/**
* Utility methods for create MethodInvoker instances.
*
* @author Lucas Ward
* @since 2.0
*/
public class MethodInvokerUtils {
/**
* Create a {@link MethodInvoker} using the provided method name to search.
*
* @param object to be invoked
* @param methodName of the method to be invoked
* @param paramsRequired boolean indicating whether the parameters are required, if false, a no args version of the
* method will be searched for.
* @param paramTypes - parameter types of the method to search for.
* @return MethodInvoker if the method is found, null if it is not.
*/
public static MethodInvoker createMethodInvokerByName(Object object, String methodName, boolean paramsRequired, Class<?>... paramTypes){
Assert.notNull(object, "Object to invoke must not be null");
Method method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName, paramTypes);
//if no method was found for the given parameters, and the parameters aren't required
if(method == null && !paramsRequired){
//try with no params
method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName, new Class[]{});
}
if(method == null){
return null;
}
else{
return new SimpleMethodInvoker(object, method);
}
}
/**
* Create a {@link MethodInvoker} using the provided interface, and string methodname from that interface.
*
* @param object to be invoked
* @param methodName of the method to be invoked
* @param paramTypes - parameter types of the method to search for.
* @return MethodInvoker if the method is found, null if it is not.
*/
public static MethodInvoker getMethodInvokerForInterface(Class<?> iFace, String methodName,
Object object, Class<?>... paramTypes){
if(iFace.isAssignableFrom(object.getClass())){
return MethodInvokerUtils.createMethodInvokerByName(object, methodName, true, paramTypes);
}
else{
return null;
}
}
/**
* Create {@link MethodInvoker} for the method with the provided annotation on the provided object. It
* should be noted that annotations that cannot be applied to methods (i.e. that aren't annotated with an
* element type of METHOD) will cause an exception to be thrown.
*
* @param annotationType to be searched for
* @param candidate to be invoked
* @return MethodInvoker for the provided annotation, null if none is found.
*/
public static MethodInvoker getMethodInvokerByAnnotation(final Class<? extends Annotation> annotationType, final Object candidate){
Assert.notNull(candidate, "class must not be null");
Assert.notNull(annotationType, "annotationType must not be null");
Assert.isTrue(ObjectUtils.containsElement(
annotationType.getAnnotation(Target.class).value(), ElementType.METHOD),
"Annotation [" + annotationType + "] is not a Method-level annotation.");
final AtomicReference<Method> annotatedMethod = new AtomicReference<Method>();
ReflectionUtils.doWithMethods(candidate.getClass(), new ReflectionUtils.MethodCallback() {
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType);
if (annotation != null) {
Assert.isNull(annotatedMethod.get(), "found more than one method on target class ["
+ candidate + "] with the annotation type [" + candidate + "]");
annotatedMethod.set(method);
}
}
});
Method method = annotatedMethod.get();
if(method == null){
return null;
}
else{
return new SimpleMethodInvoker(candidate, annotatedMethod.get());
}
}
}

View File

@@ -1,57 +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.springframework.batch.core.configuration.util;
import java.lang.reflect.Method;
/**
* Strategy interface for detecting a single Method on a Class.
*
* @author Mark Fisher
*/
public interface MethodResolver {
/**
* Find a single Method on the provided Object that matches this resolver's
* criteria.
*
* @param candidate the candidate Object whose Class should be searched for
* a Method
*
* @return a single Method or <code>null</code> if no Method matching this
* resolver's criteria can be found.
*
* @throws IllegalArgumentException if more than one Method defined on the
* given candidate's Class matches this resolver's criteria
*/
Method findMethod(Object candidate) throws IllegalArgumentException;
/**
* Find a <em>single</em> Method on the given Class that matches this
* resolver's criteria.
*
* @param clazz the Class instance on which to search for a Method
*
* @return a single Method or <code>null</code> if no Method matching this
* resolver's criteria can be found.
*
* @throws IllegalArgumentException if more than one Method defined on the
* given Class matches this resolver's criteria
*/
Method findMethod(Class<?> clazz);
}

View File

@@ -1,119 +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.
*/
/*
* 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.springframework.batch.core.configuration.util;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Simple implementation of the {@link MethodInvoker} interface that invokes a method on an
* object. If the method has no arguments, but arguments are provided, they are ignored and the method
* is invoked anyway. If there are more arguments than there are provided, then an exception is thrown.
*
* @author Lucas Ward
* @since 2.0
*/
public class SimpleMethodInvoker implements MethodInvoker {
private final Object object;
private Method method;
public SimpleMethodInvoker(Object object, Method method) {
Assert.notNull(object, "Object to invoke must not be null");
Assert.notNull(method, "Method to invoke must not be null");
this.object = object;
this.method = method;
}
public SimpleMethodInvoker(Object object, String methodName, Class<?>... paramTypes){
Assert.notNull(object, "Object to invoke must not be null");
this.object = object;
this.method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName, paramTypes);
if(this.method == null){
//try with no params
this.method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName, new Class[]{});
}
if(this.method == null){
throw new IllegalArgumentException("No methods found for name: [" + methodName + "] in class: [" +
object.getClass() + "] with arguments of type: [" + Arrays.toString(paramTypes) + "]");
}
}
/* (non-Javadoc)
* @see org.springframework.batch.core.configuration.util.MethodInvoker#invokeMethod(java.lang.Object[])
*/
public Object invokeMethod(Object... args) {
Class<?>[] parameterTypes = method.getParameterTypes();
Object[] invokeArgs;
if(parameterTypes.length == 0){
invokeArgs = new Object[]{};
}
else if(parameterTypes.length > args.length){
throw new IllegalArgumentException("Wrong number of arguments, expected no more than: [" + parameterTypes.length + "]");
}
else{
invokeArgs = args;
}
method.setAccessible(true);
try {
return method.invoke(object, invokeArgs);
} catch (Exception e) {
throw new IllegalArgumentException("Unable to invoke method: [" + method + "] on object: [" +
object + "] with arguments: [" + Arrays.toString(args) + "]", e);
}
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof SimpleMethodInvoker)){
return false;
}
if(obj == this){
return true;
}
SimpleMethodInvoker rhs = (SimpleMethodInvoker) obj;
return (rhs.method.equals(this.method)) && (rhs.object.equals(this.object));
}
@Override
public int hashCode() {
return new HashCodeBuilder(25, 37).append(object.hashCode()).append(method.hashCode()).toHashCode();
}
}

View File

@@ -15,8 +15,8 @@
*/
package org.springframework.batch.core.listener;
import static org.springframework.batch.core.configuration.util.MethodInvokerUtils.getMethodInvokerByAnnotation;
import static org.springframework.batch.core.configuration.util.MethodInvokerUtils.getMethodInvokerForInterface;
import static org.springframework.batch.support.MethodInvokerUtils.getMethodInvokerByAnnotation;
import static org.springframework.batch.support.MethodInvokerUtils.getMethodInvokerForInterface;
import java.util.HashMap;
import java.util.HashSet;
@@ -28,8 +28,8 @@ import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.configuration.util.MethodInvoker;
import org.springframework.batch.core.configuration.util.MethodInvokerUtils;
import org.springframework.batch.support.MethodInvoker;
import org.springframework.batch.support.MethodInvokerUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

View File

@@ -22,7 +22,7 @@ import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.configuration.util.MethodInvoker;
import org.springframework.batch.support.MethodInvoker;
/**
* {@link MethodInterceptor} that, given a map of method names and

View File

@@ -15,8 +15,8 @@
*/
package org.springframework.batch.core.listener;
import static org.springframework.batch.core.configuration.util.MethodInvokerUtils.getMethodInvokerByAnnotation;
import static org.springframework.batch.core.configuration.util.MethodInvokerUtils.getMethodInvokerForInterface;
import static org.springframework.batch.support.MethodInvokerUtils.getMethodInvokerByAnnotation;
import static org.springframework.batch.support.MethodInvokerUtils.getMethodInvokerForInterface;
import java.util.HashMap;
import java.util.HashSet;
@@ -27,8 +27,8 @@ import java.util.Map.Entry;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.batch.core.StepListener;
import org.springframework.batch.core.configuration.util.MethodInvoker;
import org.springframework.batch.core.configuration.util.MethodInvokerUtils;
import org.springframework.batch.support.MethodInvoker;
import org.springframework.batch.support.MethodInvokerUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

View File

@@ -136,7 +136,7 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
* Validate StepExecution. At a minimum, JobId, StartTime, and Status cannot
* be null. EndTime can be null for an unfinished job.
*
* @param jobExecution
* @param value
* @throws IllegalArgumentException
*/
private void validateStepExecution(StepExecution stepExecution) {

View File

@@ -1,144 +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.
*/
/*
* 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.springframework.batch.core.configuration.util;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.util.Assert;
/**
* @author Lucas Ward
*
*/
public class SimpleMethodInvokerTests {
TestClass testClass;
JobExecution jobExecution = new JobExecution(11L);
@Before
public void setUp(){
testClass = new TestClass();
}
@Test
public void testMethod() throws Exception{
Method method = TestClass.class.getMethod("beforeJob");
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, method);
methodInvoker.invokeMethod(jobExecution);
assertTrue(testClass.beforeJobCalled);
}
@Test
public void testMethodByName() throws Exception{
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, "beforeJob", JobExecutionListener.class);
methodInvoker.invokeMethod(jobExecution);
assertTrue(testClass.beforeJobCalled);
}
@Test
public void testMethodWithExecution() throws Exception{
Method method = TestClass.class.getMethod("beforeJobWithExecution", JobExecution.class);
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, method);
methodInvoker.invokeMethod(jobExecution);
assertTrue(testClass.beforeJobCalled);
}
@Test
public void testMethodByNameWithExecution() throws Exception{
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, "beforeJobWithExecution", JobExecution.class);
methodInvoker.invokeMethod(jobExecution);
assertTrue(testClass.beforeJobCalled);
}
@Test(expected=IllegalArgumentException.class)
public void testMethodWithTooManyArguments() throws Exception{
Method method = TestClass.class.getMethod("beforeJobWithTooManyArguments", JobExecution.class, int.class);
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, method);
methodInvoker.invokeMethod(jobExecution);
assertFalse(testClass.beforeJobCalled);
}
@Test(expected=IllegalArgumentException.class)
public void testMethodByNameWithTooManyArguments() throws Exception{
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, "beforeJobWithTooManyArguments", JobExecution.class);
methodInvoker.invokeMethod(jobExecution);
assertFalse(testClass.beforeJobCalled);
}
@Test
public void testMethodWithArgument() throws Exception{
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, "argumentTest", Object.class);
methodInvoker.invokeMethod(new Object());
assertTrue(testClass.argumentTestCalled);
}
@Test
public void testEquals() throws Exception{
Method method = TestClass.class.getMethod("beforeJobWithExecution", JobExecution.class);
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, method);
method = TestClass.class.getMethod("beforeJobWithExecution", JobExecution.class);
MethodInvoker methodInvoker2 = new SimpleMethodInvoker(testClass, method);
assertEquals(methodInvoker, methodInvoker2);
}
private class TestClass{
boolean beforeJobCalled = false;
boolean argumentTestCalled = false;
public void beforeJob(){
beforeJobCalled = true;
}
public void beforeJobWithExecution(JobExecution jobExecution){
beforeJobCalled = true;
}
public void beforeJobWithTooManyArguments(JobExecution jobExecution, int someInt){
beforeJobCalled = true;
}
public void argumentTest(Object object){
Assert.notNull(object);
argumentTestCalled = true;
}
}
}

View File

@@ -26,7 +26,7 @@ import java.lang.annotation.Target;
import java.lang.reflect.Method;
import org.junit.Test;
import org.springframework.batch.core.configuration.util.AnnotationMethodResolver;
import org.springframework.batch.support.AnnotationMethodResolver;
/**
* @author Mark Fisher

View File

@@ -13,9 +13,9 @@ import org.aopalliance.intercept.MethodInvocation;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.configuration.util.MethodInvoker;
import org.springframework.batch.core.configuration.util.MethodInvokerUtils;
import org.springframework.batch.core.configuration.util.SimpleMethodInvoker;
import org.springframework.batch.support.MethodInvoker;
import org.springframework.batch.support.MethodInvokerUtils;
import org.springframework.batch.support.SimpleMethodInvoker;
public class StepListenerMethodInterceptorTests {