| 1 | /* |
| 2 | * Copyright 2002-2013 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.springframework.batch.core.listener; |
| 17 | |
| 18 | import java.lang.annotation.Annotation; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import org.springframework.batch.core.ChunkListener; |
| 24 | import org.springframework.batch.core.ItemProcessListener; |
| 25 | import org.springframework.batch.core.ItemReadListener; |
| 26 | import org.springframework.batch.core.ItemWriteListener; |
| 27 | import org.springframework.batch.core.SkipListener; |
| 28 | import org.springframework.batch.core.StepExecution; |
| 29 | import org.springframework.batch.core.StepExecutionListener; |
| 30 | import org.springframework.batch.core.StepListener; |
| 31 | import org.springframework.batch.core.annotation.AfterChunk; |
| 32 | import org.springframework.batch.core.annotation.AfterChunkError; |
| 33 | import org.springframework.batch.core.annotation.AfterProcess; |
| 34 | import org.springframework.batch.core.annotation.AfterRead; |
| 35 | import org.springframework.batch.core.annotation.AfterStep; |
| 36 | import org.springframework.batch.core.annotation.AfterWrite; |
| 37 | import org.springframework.batch.core.annotation.BeforeChunk; |
| 38 | import org.springframework.batch.core.annotation.BeforeProcess; |
| 39 | import org.springframework.batch.core.annotation.BeforeRead; |
| 40 | import org.springframework.batch.core.annotation.BeforeStep; |
| 41 | import org.springframework.batch.core.annotation.BeforeWrite; |
| 42 | import org.springframework.batch.core.annotation.OnProcessError; |
| 43 | import org.springframework.batch.core.annotation.OnReadError; |
| 44 | import org.springframework.batch.core.annotation.OnSkipInProcess; |
| 45 | import org.springframework.batch.core.annotation.OnSkipInRead; |
| 46 | import org.springframework.batch.core.annotation.OnSkipInWrite; |
| 47 | import org.springframework.batch.core.annotation.OnWriteError; |
| 48 | import org.springframework.batch.core.scope.context.ChunkContext; |
| 49 | |
| 50 | /** |
| 51 | * Enumeration for {@link StepListener} meta data, which ties together the names |
| 52 | * of methods, their interfaces, annotation, and expected arguments. |
| 53 | * |
| 54 | * @author Lucas Ward |
| 55 | * @since 2.0 |
| 56 | * @see StepListenerFactoryBean |
| 57 | */ |
| 58 | public enum StepListenerMetaData implements ListenerMetaData { |
| 59 | |
| 60 | BEFORE_STEP("beforeStep", "before-step-method", BeforeStep.class, StepExecutionListener.class, StepExecution.class), |
| 61 | AFTER_STEP("afterStep", "after-step-method", AfterStep.class, StepExecutionListener.class, StepExecution.class), |
| 62 | BEFORE_CHUNK("beforeChunk", "before-chunk-method", BeforeChunk.class, ChunkListener.class, ChunkContext.class), |
| 63 | AFTER_CHUNK("afterChunk", "after-chunk-method", AfterChunk.class, ChunkListener.class, ChunkContext.class), |
| 64 | AFTER_CHUNK_ERROR("afterChunkError", "after-chunk-error-method", AfterChunkError.class, ChunkListener.class, ChunkContext.class), |
| 65 | BEFORE_READ("beforeRead", "before-read-method", BeforeRead.class, ItemReadListener.class), |
| 66 | AFTER_READ("afterRead", "after-read-method", AfterRead.class, ItemReadListener.class, Object.class), |
| 67 | ON_READ_ERROR("onReadError", "on-read-error-method", OnReadError.class, ItemReadListener.class, Exception.class), |
| 68 | BEFORE_PROCESS("beforeProcess", "before-process-method", BeforeProcess.class, ItemProcessListener.class, Object.class), |
| 69 | AFTER_PROCESS("afterProcess", "after-process-method", AfterProcess.class, ItemProcessListener.class, Object.class, Object.class), |
| 70 | ON_PROCESS_ERROR("onProcessError", "on-process-error-method", OnProcessError.class, ItemProcessListener.class, Object.class, Exception.class), |
| 71 | BEFORE_WRITE("beforeWrite", "before-write-method", BeforeWrite.class, ItemWriteListener.class, List.class), |
| 72 | AFTER_WRITE("afterWrite", "after-write-method", AfterWrite.class, ItemWriteListener.class, List.class), |
| 73 | ON_WRITE_ERROR("onWriteError", "on-write-error-method", OnWriteError.class, ItemWriteListener.class, Exception.class, List.class), |
| 74 | ON_SKIP_IN_READ("onSkipInRead", "on-skip-in-read-method", OnSkipInRead.class, SkipListener.class, Throwable.class), |
| 75 | ON_SKIP_IN_PROCESS("onSkipInProcess", "on-skip-in-process-method", OnSkipInProcess.class, SkipListener.class, Object.class, Throwable.class), |
| 76 | ON_SKIP_IN_WRITE("onSkipInWrite", "on-skip-in-write-method", OnSkipInWrite.class, SkipListener.class, Object.class, Throwable.class); |
| 77 | |
| 78 | private final String methodName; |
| 79 | private final String propertyName; |
| 80 | private final Class<? extends Annotation> annotation; |
| 81 | private final Class<? extends StepListener> listenerInterface; |
| 82 | private final Class<?>[] paramTypes; |
| 83 | private static final Map<String, StepListenerMetaData> propertyMap; |
| 84 | |
| 85 | StepListenerMetaData(String methodName, String propertyName, Class<? extends Annotation> annotation, Class<? extends StepListener> listenerInterface, Class<?>... paramTypes) { |
| 86 | this.methodName = methodName; |
| 87 | this.propertyName = propertyName; |
| 88 | this.annotation = annotation; |
| 89 | this.listenerInterface = listenerInterface; |
| 90 | this.paramTypes = paramTypes; |
| 91 | } |
| 92 | |
| 93 | static{ |
| 94 | propertyMap = new HashMap<String, StepListenerMetaData>(); |
| 95 | for(StepListenerMetaData metaData : values()){ |
| 96 | propertyMap.put(metaData.getPropertyName(), metaData); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public String getMethodName() { |
| 102 | return methodName; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Class<? extends Annotation> getAnnotation() { |
| 107 | return annotation; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Class<?> getListenerInterface() { |
| 112 | return listenerInterface; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Class<?>[] getParamTypes() { |
| 117 | return paramTypes; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public String getPropertyName() { |
| 122 | return propertyName; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Return the relevant meta data for the provided property name. |
| 127 | * |
| 128 | * @param propertyName |
| 129 | * @return meta data with supplied property name, null if none exists. |
| 130 | */ |
| 131 | public static StepListenerMetaData fromPropertyName(String propertyName){ |
| 132 | return propertyMap.get(propertyName); |
| 133 | } |
| 134 | |
| 135 | public static ListenerMetaData[] itemListenerMetaData() { |
| 136 | return new ListenerMetaData[] {BEFORE_WRITE, AFTER_WRITE, ON_WRITE_ERROR, BEFORE_PROCESS, AFTER_PROCESS, ON_PROCESS_ERROR, BEFORE_READ, AFTER_READ, ON_READ_ERROR, ON_SKIP_IN_WRITE, ON_SKIP_IN_PROCESS, ON_SKIP_IN_READ}; |
| 137 | } |
| 138 | |
| 139 | public static ListenerMetaData[] stepExecutionListenerMetaData() { |
| 140 | return new ListenerMetaData[] {BEFORE_STEP, AFTER_STEP}; |
| 141 | } |
| 142 | |
| 143 | public static ListenerMetaData[] taskletListenerMetaData() { |
| 144 | return new ListenerMetaData[] {BEFORE_CHUNK, AFTER_CHUNK, AFTER_CHUNK_ERROR}; |
| 145 | } |
| 146 | |
| 147 | } |