diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ClassPathXmlJobRegistry.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ClassPathXmlJobRegistry.java new file mode 100644 index 000000000..ec2b73bc0 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/ClassPathXmlJobRegistry.java @@ -0,0 +1,79 @@ +/** + * + */ +package org.springframework.batch.core.configuration.support; + +import java.util.Collection; +import java.util.List; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.configuration.DuplicateJobException; +import org.springframework.batch.core.configuration.JobFactory; +import org.springframework.batch.core.configuration.ListableJobRegistry; +import org.springframework.batch.core.launch.NoSuchJobException; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.core.io.Resource; + +/** + * Implementation of the {@link ListableJobRegistry} interface that assumes all Jobs will be loaded from + * ClassPathXml resources. + * + * @author Lucas Ward + * @since 2.0 + */ +public class ClassPathXmlJobRegistry implements ListableJobRegistry, ApplicationContextAware, InitializingBean { + + List jobPaths; + ApplicationContext parent; + ListableJobRegistry jobRegistry; + + public ClassPathXmlJobRegistry(List jobPaths) { + this.jobPaths = jobPaths; + } + + public Job getJob(String name) throws NoSuchJobException { + return jobRegistry.getJob(name); + } + + public void setApplicationContext(ApplicationContext applicationContext) + throws BeansException { + parent = applicationContext; + } + + public void afterPropertiesSet() throws Exception { + + jobRegistry = new MapJobRegistry(); + for(Resource resource:jobPaths){ + ClassPathXmlApplicationContextFactory applicationContextFactory = new ClassPathXmlApplicationContextFactory(); + applicationContextFactory.setPath(new Resource[]{resource}); + applicationContextFactory.setApplicationContext(parent); + ApplicationContext context = applicationContextFactory.createApplicationContext(); + String[] names = context.getBeanNamesForType(Job.class); + + if(names.length > 1){ + throw new DuplicateJobException("More than one Job found for resource: [" + resource + "]"); + } + else if(names.length == 0){ + throw new NoSuchJobException("No Jobs found in resource: [" + resource + "]"); + } + + ApplicationContextJobFactory jobFactory = new ApplicationContextJobFactory(applicationContextFactory, names[0]); + jobRegistry.register(jobFactory); + } + } + + public Collection getJobNames() { + return jobRegistry.getJobNames(); + } + + public void register(JobFactory jobFactory) throws DuplicateJobException { + jobRegistry.register(jobFactory); + } + + public void unregister(String jobName) { + jobRegistry.unregister(jobName); + } +} diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/ClassPathXmlJobRegistryTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/ClassPathXmlJobRegistryTests.java new file mode 100644 index 000000000..69e2c214c --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/ClassPathXmlJobRegistryTests.java @@ -0,0 +1,69 @@ +package org.springframework.batch.core.configuration.support; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.junit.Test; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.configuration.DuplicateJobException; +import org.springframework.batch.core.launch.NoSuchJobException; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; + + +public class ClassPathXmlJobRegistryTests { + + ClassPathXmlJobRegistry registry; + + @Test + public void testLocateJob() throws Exception{ + + List jobPaths = new ArrayList(); + jobPaths.add(new ClassPathResource("org/springframework/batch/core/launch/support/job.xml")); + jobPaths.add(new ClassPathResource("org/springframework/batch/core/launch/support/job2.xml")); + + registry = new ClassPathXmlJobRegistry(jobPaths); + GenericApplicationContext applicationContext = new GenericApplicationContext(); + applicationContext.refresh(); + registry.setApplicationContext(applicationContext); + registry.afterPropertiesSet(); + + Collection names = registry.getJobNames(); + assertEquals(2, names.size()); + assertTrue(names.contains("test-job")); + assertTrue(names.contains("test-job2")); + + Job job = registry.getJob("test-job"); + assertEquals("test-job", job.getName()); + job = registry.getJob("test-job2"); + assertEquals("test-job2", job.getName()); + } + + @Test(expected=NoSuchJobException.class) + public void testNoJobFound() throws Exception{ + + List jobPaths = new ArrayList(); + jobPaths.add(new ClassPathResource("org/springframework/batch/core/launch/support/test-environment.xml")); + registry = new ClassPathXmlJobRegistry(jobPaths); + GenericApplicationContext applicationContext = new GenericApplicationContext(); + applicationContext.refresh(); + registry.setApplicationContext(applicationContext); + registry.afterPropertiesSet(); + } + + @Test(expected=DuplicateJobException.class) + public void testDuplicateJobsInFile() throws Exception{ + + List jobPaths = new ArrayList(); + jobPaths.add(new ClassPathResource("org/springframework/batch/core/launch/support/2jobs.xml")); + registry = new ClassPathXmlJobRegistry(jobPaths); + GenericApplicationContext applicationContext = new GenericApplicationContext(); + applicationContext.refresh(); + registry.setApplicationContext(applicationContext); + registry.afterPropertiesSet(); + } +} diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/launch/support/2jobs.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/launch/support/2jobs.xml new file mode 100644 index 000000000..a53a7cc6f --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/launch/support/2jobs.xml @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/launch/support/job2.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/launch/support/job2.xml new file mode 100644 index 000000000..e0964973d --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/launch/support/job2.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +