| 1 | /* |
| 2 | * Copyright 2006-2007 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 | |
| 17 | package org.springframework.batch.core.configuration.support; |
| 18 | |
| 19 | import java.util.Arrays; |
| 20 | import java.util.Collection; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.apache.commons.logging.Log; |
| 24 | import org.apache.commons.logging.LogFactory; |
| 25 | import org.springframework.batch.core.Job; |
| 26 | import org.springframework.batch.core.configuration.DuplicateJobException; |
| 27 | import org.springframework.batch.core.configuration.JobFactory; |
| 28 | import org.springframework.batch.core.configuration.ListableJobRegistry; |
| 29 | import org.springframework.batch.core.launch.NoSuchJobException; |
| 30 | import org.springframework.beans.BeansException; |
| 31 | import org.springframework.beans.factory.InitializingBean; |
| 32 | import org.springframework.context.ApplicationContext; |
| 33 | import org.springframework.context.ApplicationContextAware; |
| 34 | import org.springframework.core.io.Resource; |
| 35 | |
| 36 | /** |
| 37 | * Implementation of the {@link ListableJobRegistry} interface that assumes all |
| 38 | * Jobs will be loaded from ClassPathXml resources. |
| 39 | * |
| 40 | * @author Lucas Ward |
| 41 | * @author Dave Syer |
| 42 | * @since 2.0 |
| 43 | */ |
| 44 | public class ClassPathXmlJobRegistry implements ListableJobRegistry, ApplicationContextAware, InitializingBean { |
| 45 | |
| 46 | private static Log logger = LogFactory.getLog(ClassPathXmlJobRegistry.class); |
| 47 | |
| 48 | private List<Resource> jobPaths; |
| 49 | |
| 50 | private ApplicationContext parent; |
| 51 | |
| 52 | private ListableJobRegistry jobRegistry = new MapJobRegistry(); |
| 53 | |
| 54 | public void setJobPaths(Resource[] jobPaths) { |
| 55 | this.jobPaths = Arrays.asList(jobPaths); |
| 56 | } |
| 57 | |
| 58 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 59 | parent = applicationContext; |
| 60 | } |
| 61 | |
| 62 | public Job getJob(String name) throws NoSuchJobException { |
| 63 | return jobRegistry.getJob(name); |
| 64 | } |
| 65 | |
| 66 | public void afterPropertiesSet() throws Exception { |
| 67 | |
| 68 | for (Resource resource : jobPaths) { |
| 69 | ClassPathXmlApplicationContextFactory applicationContextFactory = new ClassPathXmlApplicationContextFactory(); |
| 70 | applicationContextFactory.setPath(resource); |
| 71 | applicationContextFactory.setApplicationContext(parent); |
| 72 | ApplicationContext context = applicationContextFactory.createApplicationContext(); |
| 73 | String[] names = context.getBeanNamesForType(Job.class); |
| 74 | |
| 75 | for (String name : names) { |
| 76 | logger.debug("Registering job: " + name + " from context: " + resource); |
| 77 | ApplicationContextJobFactory jobFactory = new ApplicationContextJobFactory(applicationContextFactory, |
| 78 | name); |
| 79 | jobRegistry.register(jobFactory); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (jobRegistry.getJobNames().isEmpty()) { |
| 84 | throw new NoSuchJobException("Could not locate any jobs in resources provided."); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | public Collection<String> getJobNames() { |
| 90 | return jobRegistry.getJobNames(); |
| 91 | } |
| 92 | |
| 93 | public void register(JobFactory jobFactory) throws DuplicateJobException { |
| 94 | jobRegistry.register(jobFactory); |
| 95 | } |
| 96 | |
| 97 | public void unregister(String jobName) { |
| 98 | jobRegistry.unregister(jobName); |
| 99 | } |
| 100 | } |