OPEN - issue BATCH-673: Add new Java 5.0 features
http://jira.springframework.org/browse/BATCH-673 Removed some unnecessary classes and added javadocs.
This commit is contained in:
@@ -61,17 +61,34 @@ public class MethodInvokerUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 candidate, Class<?>... params){
|
||||
Object object, Class<?>... paramTypes){
|
||||
|
||||
if(iFace.isAssignableFrom(candidate.getClass())){
|
||||
return MethodInvokerUtils.createMethodInvokerByName(candidate, methodName, true, params);
|
||||
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");
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.batch.core.job;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobExecutionListener;
|
||||
import org.springframework.batch.core.annotation.AfterJob;
|
||||
import org.springframework.batch.core.annotation.BeforeJob;
|
||||
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
|
||||
|
||||
/**
|
||||
* {@link BeanPostProcessor} that registers any declared beans to the provided job
|
||||
* that have any methods annotated with {@link BeforeJob} or {@link AfterJob}
|
||||
*
|
||||
* @author Lucas Ward
|
||||
* @since 2.0
|
||||
* @see SimpleJob
|
||||
*/
|
||||
public class JobListenerAnnotationBeanPostProcessor implements BeanPostProcessor {
|
||||
|
||||
private AbstractJob job;
|
||||
|
||||
public JobListenerAnnotationBeanPostProcessor(AbstractJob job) {
|
||||
this.job = job;
|
||||
}
|
||||
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
|
||||
for(Method method : bean.getClass().getMethods()){
|
||||
BeforeJob beforeJob = AnnotationUtils.findAnnotation(method, BeforeJob.class);
|
||||
if(beforeJob != null){
|
||||
JobExecutionListener listener = new BeforeJobProxy(method, bean, supportsJobExecutionPassing(method));
|
||||
job.registerJobExecutionListener(listener);
|
||||
}
|
||||
AfterJob afterJob = AnnotationUtils.findAnnotation(method, AfterJob.class);
|
||||
if(afterJob != null){
|
||||
JobExecutionListener listener = new AfterJobProxy(method, bean, supportsJobExecutionPassing(method));
|
||||
job.registerJobExecutionListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static class BeforeJobProxy extends JobExecutionListenerSupport{
|
||||
|
||||
Method method;
|
||||
Object bean;
|
||||
boolean passThroughExecution;
|
||||
|
||||
public BeforeJobProxy(Method method, Object bean, boolean passThroughExecution) {
|
||||
this.method = method;
|
||||
this.bean = bean;
|
||||
this.passThroughExecution = passThroughExecution;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeJob(JobExecution jobExecution) {
|
||||
try{
|
||||
if(passThroughExecution){
|
||||
method.invoke(bean, jobExecution);
|
||||
}
|
||||
else{
|
||||
method.invoke(bean);
|
||||
}
|
||||
}
|
||||
catch(Exception ex){
|
||||
throw new IllegalStateException("Unable to invoke annotated method: [" + method, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class AfterJobProxy extends JobExecutionListenerSupport{
|
||||
|
||||
Method method;
|
||||
Object bean;
|
||||
boolean passThroughExecution;
|
||||
|
||||
public AfterJobProxy(Method method, Object bean, boolean passThroughExecution) {
|
||||
this.method = method;
|
||||
this.bean = bean;
|
||||
this.passThroughExecution = passThroughExecution;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterJob(JobExecution jobExecution) {
|
||||
try{
|
||||
if(passThroughExecution){
|
||||
method.invoke(bean, jobExecution);
|
||||
}
|
||||
else{
|
||||
method.invoke(bean);
|
||||
}
|
||||
}
|
||||
catch(Exception ex){
|
||||
throw new IllegalStateException("Unable to invoke annotated method: [" + method, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean supportsJobExecutionPassing(Method method){
|
||||
Class<?>[] parameters = method.getParameterTypes();
|
||||
if(parameters.length == 0 || parameters.length > 1){
|
||||
return false;
|
||||
}
|
||||
|
||||
Class<?> parameter = parameters[0];
|
||||
if(parameter == JobExecution.class){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,130 +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.listener;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.batch.core.ChunkListener;
|
||||
import org.springframework.batch.core.ExitStatus;
|
||||
import org.springframework.batch.core.ItemProcessListener;
|
||||
import org.springframework.batch.core.ItemReadListener;
|
||||
import org.springframework.batch.core.ItemWriteListener;
|
||||
import org.springframework.batch.core.SkipListener;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.StepExecutionListener;
|
||||
import org.springframework.batch.core.configuration.util.MethodInvoker;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class StepExecutionListenerAdapter<T, S> implements StepExecutionListener, ChunkListener,
|
||||
ItemReadListener<T>, ItemWriteListener<S>, ItemProcessListener<T, S>, SkipListener<T, S>{
|
||||
|
||||
private Set<MethodInvoker> beforeStepMethodInvokers = new HashSet<MethodInvoker>();
|
||||
private Set<MethodInvoker> afterMethodInvokers = new HashSet<MethodInvoker>();
|
||||
|
||||
public void setAfterMethodInvokers(Set<MethodInvoker> afterMethodInvokers) {
|
||||
this.afterMethodInvokers = afterMethodInvokers;
|
||||
}
|
||||
|
||||
public void setBeforeMethodInvokers(Set<MethodInvoker> beforeMethodInvokers) {
|
||||
this.beforeStepMethodInvokers = beforeMethodInvokers;
|
||||
}
|
||||
|
||||
public ExitStatus afterStep(StepExecution stepExecution) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void beforeStep(StepExecution stepExecution) {
|
||||
for(MethodInvoker invoker : beforeStepMethodInvokers){
|
||||
invoker.invokeMethod(stepExecution);
|
||||
}
|
||||
}
|
||||
|
||||
public void afterChunk() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void beforeChunk() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void afterRead(T item) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void beforeRead() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void onReadError(Exception ex) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void afterWrite(List<? extends S> items) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void beforeWrite(List<? extends S> items) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void onWriteError(Exception exception, List<? extends S> items) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void afterProcess(T item, S result) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void beforeProcess(T item) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void onProcessError(T item, Exception e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void onSkipInProcess(T item, Throwable t) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void onSkipInRead(Throwable t) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void onSkipInWrite(S item, Throwable t) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.batch.core.job;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobExecutionException;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.annotation.AfterJob;
|
||||
import org.springframework.batch.core.annotation.BeforeJob;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class JobListenerAnnotationBeanPostProcessorTests {
|
||||
|
||||
JobListenerAnnotationBeanPostProcessor postProcessor;
|
||||
AnnotatedClassStub annotatedClass;
|
||||
StubJob job;
|
||||
JobExecution jobExecution;
|
||||
|
||||
@Before
|
||||
public void init(){
|
||||
job = new StubJob();
|
||||
annotatedClass = new AnnotatedClassStub();
|
||||
postProcessor = new JobListenerAnnotationBeanPostProcessor(job);
|
||||
jobExecution = new JobExecution(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeforeJob(){
|
||||
|
||||
postProcessor.postProcessBeforeInitialization(annotatedClass, "test");
|
||||
job.execute(jobExecution);
|
||||
assertTrue(annotatedClass.beforeJobCalled);
|
||||
assertTrue(annotatedClass.beforeJobWithExecutionCalled);
|
||||
assertTrue(annotatedClass.afterJobCalled);
|
||||
assertTrue(annotatedClass.afterJobWithExecutionCalled);
|
||||
}
|
||||
|
||||
private class AnnotatedClassStub{
|
||||
|
||||
boolean beforeJobCalled = false;
|
||||
boolean beforeJobWithExecutionCalled = false;
|
||||
|
||||
boolean afterJobCalled = false;
|
||||
boolean afterJobWithExecutionCalled = false;
|
||||
|
||||
@BeforeJob
|
||||
public void beforeJobMethod(){
|
||||
beforeJobCalled = true;
|
||||
};
|
||||
|
||||
@BeforeJob
|
||||
public void beforeJobMethodWithExecution(JobExecution jobExecution){
|
||||
beforeJobWithExecutionCalled = true;
|
||||
}
|
||||
|
||||
@AfterJob
|
||||
public void afterJobMethod(){
|
||||
afterJobCalled = true;
|
||||
}
|
||||
|
||||
@AfterJob
|
||||
public void afterJobMethodWithExecution(JobExecution jobExecution){
|
||||
afterJobWithExecutionCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
private class StubJob extends AbstractJob{
|
||||
|
||||
public StubJob() {
|
||||
super.setJobRepository(createMock(JobRepository.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StepExecution doExecute(JobExecution execution)
|
||||
throws JobExecutionException {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user