001/*
002 * Copyright 2006-2007 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 */
016
017package org.springframework.batch.core.configuration.support;
018
019import java.util.Map;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.springframework.batch.core.configuration.JobFactory;
024import org.springframework.batch.core.configuration.JobRegistry;
025
026/**
027 * Generic service that can bind and unbind a {@link JobFactory} in a
028 * {@link JobRegistry}.
029 * 
030 * @author Dave Syer
031 * 
032 */
033public class JobFactoryRegistrationListener {
034
035        private Log logger = LogFactory.getLog(getClass());
036
037        private JobRegistry jobRegistry;
038
039        /**
040         * Public setter for a {@link JobRegistry} to use for all the bind and
041         * unbind events.
042         * 
043         * @param jobRegistry {@link JobRegistry}
044         */
045        public void setJobRegistry(JobRegistry jobRegistry) {
046                this.jobRegistry = jobRegistry;
047        }
048
049        /**
050         * Take the {@link JobFactory} provided and register it with the
051         * {@link JobRegistry}.
052         * @param jobFactory a {@link JobFactory}
053         * @param params not needed by this listener.
054         * @throws Exception if there is a problem
055         */
056        public void bind(JobFactory jobFactory, Map<String, ?> params) throws Exception {
057                logger.info("Binding JobFactory: " + jobFactory.getJobName());
058                jobRegistry.register(jobFactory);
059        }
060
061        /**
062         * Take the {@link JobFactory} provided and unregister it with the
063         * {@link JobRegistry}.
064         * @param jobFactory a {@link JobFactory}
065         * @param params not needed by this listener.
066         * @throws Exception if there is a problem
067         */
068        public void unbind(JobFactory jobFactory, Map<String, ?> params) throws Exception {
069                logger.info("Unbinding JobFactory: " + jobFactory.getJobName());
070                jobRegistry.unregister(jobFactory.getJobName());
071        }
072
073}