001/* 002 * Copyright 2002-2013 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 org.springframework.batch.core.JobExecutionListener; 019 020/** 021 * This {@link AbstractListenerFactoryBean} implementation is used to create a 022 * {@link JobExecutionListener}. 023 * 024 * @author Lucas Ward 025 * @author Dan Garrette 026 * @since 2.0 027 * @see AbstractListenerFactoryBean 028 * @see JobListenerMetaData 029 */ 030public class JobListenerFactoryBean extends AbstractListenerFactoryBean<JobExecutionListener> { 031 032 @Override 033 protected ListenerMetaData getMetaDataFromPropertyName(String propertyName) { 034 return JobListenerMetaData.fromPropertyName(propertyName); 035 } 036 037 @Override 038 protected ListenerMetaData[] getMetaDataValues() { 039 return JobListenerMetaData.values(); 040 } 041 042 @Override 043 protected Class<?> getDefaultListenerClass() { 044 return JobExecutionListener.class; 045 } 046 047 @Override 048 public Class<?> getObjectType() { 049 return JobExecutionListener.class; 050 } 051 052 /** 053 * Convenience method to wrap any object and expose the appropriate 054 * {@link JobExecutionListener} interfaces. 055 * 056 * @param delegate a delegate object 057 * @return a JobListener instance constructed from the delegate 058 */ 059 public static JobExecutionListener getListener(Object delegate) { 060 JobListenerFactoryBean factory = new JobListenerFactoryBean(); 061 factory.setDelegate(delegate); 062 return (JobExecutionListener) factory.getObject(); 063 } 064 065 /** 066 * Convenience method to check whether the given object is or can be made 067 * into a {@link JobExecutionListener}. 068 * 069 * @param delegate the object to check 070 * @return true if the delegate is an instance of 071 * {@link JobExecutionListener}, or contains the marker annotations 072 */ 073 public static boolean isListener(Object delegate) { 074 return AbstractListenerFactoryBean.isListener(delegate, JobExecutionListener.class, JobListenerMetaData 075 .values()); 076 } 077}