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.Map; |
21 | |
22 | import org.springframework.batch.core.JobExecution; |
23 | import org.springframework.batch.core.JobExecutionListener; |
24 | import org.springframework.batch.core.annotation.AfterJob; |
25 | import org.springframework.batch.core.annotation.BeforeJob; |
26 | |
27 | /** |
28 | * Enumeration for {@link JobExecutionListener} meta data, which ties together the names |
29 | * of methods, their interfaces, annotation, and expected arguments. |
30 | * |
31 | * @author Lucas Ward |
32 | * @since 2.0 |
33 | * @see JobListenerFactoryBean |
34 | */ |
35 | public enum JobListenerMetaData implements ListenerMetaData { |
36 | |
37 | BEFORE_JOB("beforeJob", "before-job-method", BeforeJob.class), |
38 | AFTER_JOB("afterJob", "after-job-method", AfterJob.class); |
39 | |
40 | |
41 | private final String methodName; |
42 | private final String propertyName; |
43 | private final Class<? extends Annotation> annotation; |
44 | private static final Map<String, JobListenerMetaData> propertyMap; |
45 | |
46 | JobListenerMetaData(String methodName, String propertyName, Class<? extends Annotation> annotation) { |
47 | this.methodName = methodName; |
48 | this.propertyName = propertyName; |
49 | this.annotation = annotation; |
50 | } |
51 | |
52 | static{ |
53 | propertyMap = new HashMap<String, JobListenerMetaData>(); |
54 | for(JobListenerMetaData metaData : values()){ |
55 | propertyMap.put(metaData.getPropertyName(), metaData); |
56 | } |
57 | } |
58 | |
59 | public String getMethodName() { |
60 | return methodName; |
61 | } |
62 | |
63 | public Class<? extends Annotation> getAnnotation() { |
64 | return annotation; |
65 | } |
66 | |
67 | public Class<?> getListenerInterface() { |
68 | return JobExecutionListener.class; |
69 | } |
70 | |
71 | public String getPropertyName() { |
72 | return propertyName; |
73 | } |
74 | |
75 | public Class<?>[] getParamTypes() { |
76 | return new Class<?>[]{ JobExecution.class }; |
77 | } |
78 | |
79 | /** |
80 | * Return the relevant meta data for the provided property name. |
81 | * |
82 | * @param propertyName |
83 | * @return meta data with supplied property name, null if none exists. |
84 | */ |
85 | public static JobListenerMetaData fromPropertyName(String propertyName){ |
86 | return propertyMap.get(propertyName); |
87 | } |
88 | } |