View Javadoc

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.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  	@Override
60  	public String getMethodName() {
61  		return methodName;
62  	}
63  
64  	@Override
65  	public Class<? extends Annotation> getAnnotation() {
66  		return annotation;
67  	}
68  
69  	@Override
70  	public Class<?> getListenerInterface() {
71  		return JobExecutionListener.class;
72  	}
73  
74  	@Override
75  	public String getPropertyName() {
76  		return propertyName;
77  	}
78  
79  	@Override
80  	public Class<?>[] getParamTypes() {
81  		return new Class<?>[]{ JobExecution.class };
82  	}
83  
84  	/**
85  	 * Return the relevant meta data for the provided property name.
86  	 *
87  	 * @param propertyName
88  	 * @return meta data with supplied property name, null if none exists.
89  	 */
90  	public static JobListenerMetaData fromPropertyName(String propertyName){
91  		return propertyMap.get(propertyName);
92  	}
93  }