001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.core.listener;
017
018import java.lang.annotation.Annotation;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.springframework.batch.core.JobExecution;
023import org.springframework.batch.core.JobExecutionListener;
024import org.springframework.batch.core.annotation.AfterJob;
025import org.springframework.batch.core.annotation.BeforeJob;
026import org.springframework.lang.Nullable;
027
028/**
029 * Enumeration for {@link JobExecutionListener} meta data, which ties together the names
030 * of methods, their interfaces, annotation, and expected arguments.
031 *
032 * @author Lucas Ward
033 * @author Mahmoud Ben Hassine
034 * @since 2.0
035 * @see JobListenerFactoryBean
036 */
037public enum JobListenerMetaData implements ListenerMetaData {
038
039        BEFORE_JOB("beforeJob", "before-job-method", BeforeJob.class),
040        AFTER_JOB("afterJob", "after-job-method", AfterJob.class);
041
042
043        private final String methodName;
044        private final String propertyName;
045        private final Class<? extends Annotation> annotation;
046        private static final Map<String, JobListenerMetaData> propertyMap;
047
048        JobListenerMetaData(String methodName, String propertyName, Class<? extends Annotation> annotation) {
049                this.methodName = methodName;
050                this.propertyName = propertyName;
051                this.annotation = annotation;
052        }
053
054        static{
055                propertyMap = new HashMap<String, JobListenerMetaData>();
056                for(JobListenerMetaData metaData : values()){
057                        propertyMap.put(metaData.getPropertyName(), metaData);
058                }
059        }
060
061        @Override
062        public String getMethodName() {
063                return methodName;
064        }
065
066        @Override
067        public Class<? extends Annotation> getAnnotation() {
068                return annotation;
069        }
070
071        @Override
072        public Class<?> getListenerInterface() {
073                return JobExecutionListener.class;
074        }
075
076        @Override
077        public String getPropertyName() {
078                return propertyName;
079        }
080
081        @Override
082        public Class<?>[] getParamTypes() {
083                return new Class<?>[]{ JobExecution.class };
084        }
085
086        /**
087         * Return the relevant meta data for the provided property name.
088         *
089         * @param propertyName name of the property to retrieve.
090         * @return meta data with supplied property name, {@code null} if none exists.
091         */
092        @Nullable
093        public static JobListenerMetaData fromPropertyName(String propertyName){
094                return propertyMap.get(propertyName);
095        }
096}