1 | /* |
2 | * Copyright 2002-2008 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.AfterProcess; |
33 | import org.springframework.batch.core.annotation.AfterRead; |
34 | import org.springframework.batch.core.annotation.AfterStep; |
35 | import org.springframework.batch.core.annotation.AfterWrite; |
36 | import org.springframework.batch.core.annotation.BeforeChunk; |
37 | import org.springframework.batch.core.annotation.BeforeProcess; |
38 | import org.springframework.batch.core.annotation.BeforeRead; |
39 | import org.springframework.batch.core.annotation.BeforeStep; |
40 | import org.springframework.batch.core.annotation.BeforeWrite; |
41 | import org.springframework.batch.core.annotation.OnProcessError; |
42 | import org.springframework.batch.core.annotation.OnReadError; |
43 | import org.springframework.batch.core.annotation.OnSkipInProcess; |
44 | import org.springframework.batch.core.annotation.OnSkipInRead; |
45 | import org.springframework.batch.core.annotation.OnSkipInWrite; |
46 | import org.springframework.batch.core.annotation.OnWriteError; |
47 | |
48 | /** |
49 | * Enumeration for {@link StepListener} meta data, which ties together the names |
50 | * of methods, their interfaces, annotation, and expected arguments. |
51 | * |
52 | * @author Lucas Ward |
53 | * @since 2.0 |
54 | * @see StepListenerFactoryBean |
55 | */ |
56 | public enum StepListenerMetaData implements ListenerMetaData { |
57 | |
58 | BEFORE_STEP("beforeStep", "before-step-method", BeforeStep.class, StepExecutionListener.class, StepExecution.class), |
59 | AFTER_STEP("afterStep", "after-step-method", AfterStep.class, StepExecutionListener.class, StepExecution.class), |
60 | BEFORE_CHUNK("beforeChunk", "before-chunk-method", BeforeChunk.class, ChunkListener.class), |
61 | AFTER_CHUNK("afterChunk", "after-chunk-method", AfterChunk.class, ChunkListener.class), |
62 | BEFORE_READ("beforeRead", "before-read-method", BeforeRead.class, ItemReadListener.class), |
63 | AFTER_READ("afterRead", "after-read-method", AfterRead.class, ItemReadListener.class, Object.class), |
64 | ON_READ_ERROR("onReadError", "on-read-error-method", OnReadError.class, ItemReadListener.class, Exception.class), |
65 | BEFORE_PROCESS("beforeProcess", "before-process-method", BeforeProcess.class, ItemProcessListener.class, Object.class), |
66 | AFTER_PROCESS("afterProcess", "after-process-method", AfterProcess.class, ItemProcessListener.class, Object.class, Object.class), |
67 | ON_PROCESS_ERROR("onProcessError", "on-process-error-method", OnProcessError.class, ItemProcessListener.class, Object.class, Exception.class), |
68 | BEFORE_WRITE("beforeWrite", "before-write-method", BeforeWrite.class, ItemWriteListener.class, List.class), |
69 | AFTER_WRITE("afterWrite", "after-write-method", AfterWrite.class, ItemWriteListener.class, List.class), |
70 | ON_WRITE_ERROR("onWriteError", "on-write-error-method", OnWriteError.class, ItemWriteListener.class, Exception.class, List.class), |
71 | ON_SKIP_IN_READ("onSkipInRead", "on-skip-in-read-method", OnSkipInRead.class, SkipListener.class, Throwable.class), |
72 | ON_SKIP_IN_PROCESS("onSkipInProcess", "on-skip-in-process-method", OnSkipInProcess.class, SkipListener.class, Object.class, Throwable.class), |
73 | ON_SKIP_IN_WRITE("onSkipInWrite", "on-skip-in-write-method", OnSkipInWrite.class, SkipListener.class, Object.class, Throwable.class); |
74 | |
75 | private final String methodName; |
76 | private final String propertyName; |
77 | private final Class<? extends Annotation> annotation; |
78 | private final Class<? extends StepListener> listenerInterface; |
79 | private final Class<?>[] paramTypes; |
80 | private static final Map<String, StepListenerMetaData> propertyMap; |
81 | |
82 | StepListenerMetaData(String methodName, String propertyName, Class<? extends Annotation> annotation, |
83 | Class<? extends StepListener> listenerInterface, Class<?>... paramTypes) { |
84 | this.methodName = methodName; |
85 | this.propertyName = propertyName; |
86 | this.annotation = annotation; |
87 | this.listenerInterface = listenerInterface; |
88 | this.paramTypes = paramTypes; |
89 | } |
90 | |
91 | static{ |
92 | propertyMap = new HashMap<String, StepListenerMetaData>(); |
93 | for(StepListenerMetaData metaData : values()){ |
94 | propertyMap.put(metaData.getPropertyName(), metaData); |
95 | } |
96 | } |
97 | |
98 | public String getMethodName() { |
99 | return methodName; |
100 | } |
101 | |
102 | public Class<? extends Annotation> getAnnotation() { |
103 | return annotation; |
104 | } |
105 | |
106 | public Class<?> getListenerInterface() { |
107 | return listenerInterface; |
108 | } |
109 | |
110 | public Class<?>[] getParamTypes() { |
111 | return paramTypes; |
112 | } |
113 | |
114 | public String getPropertyName() { |
115 | return propertyName; |
116 | } |
117 | |
118 | /** |
119 | * Return the relevant meta data for the provided property name. |
120 | * |
121 | * @param propertyName |
122 | * @return meta data with supplied property name, null if none exists. |
123 | */ |
124 | public static StepListenerMetaData fromPropertyName(String propertyName){ |
125 | return propertyMap.get(propertyName); |
126 | } |
127 | } |