| 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 | package org.springframework.batch.core.configuration.support; |
| 17 | |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.springframework.batch.core.Job; |
| 21 | import org.springframework.batch.core.JobExecution; |
| 22 | import org.springframework.batch.core.JobExecutionException; |
| 23 | import org.springframework.batch.core.configuration.JobFactory; |
| 24 | import org.springframework.context.ApplicationContext; |
| 25 | import org.springframework.context.ConfigurableApplicationContext; |
| 26 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 27 | |
| 28 | /** |
| 29 | * A {@link JobFactory} that creates its own {@link ApplicationContext} from a |
| 30 | * path supplied, and pulls a bean out when asked to create a {@link Job}. |
| 31 | * |
| 32 | * @author Dave Syer |
| 33 | * |
| 34 | */ |
| 35 | public class ClassPathXmlApplicationContextJobFactory implements JobFactory { |
| 36 | |
| 37 | final private String beanName; |
| 38 | |
| 39 | final private String path; |
| 40 | |
| 41 | final private ApplicationContext parent; |
| 42 | |
| 43 | /** |
| 44 | * @param beanName the id of the {@link Job} in the application context to |
| 45 | * be created |
| 46 | * @param path the path to the XML configuration containing the {@link Job} |
| 47 | * @param parent the application context to use as a parent (or null) |
| 48 | */ |
| 49 | public ClassPathXmlApplicationContextJobFactory(String beanName, String path, ApplicationContext parent) { |
| 50 | super(); |
| 51 | this.beanName = beanName; |
| 52 | this.path = path; |
| 53 | this.parent = parent; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Create a {@link ClassPathXmlApplicationContext} from the path provided |
| 58 | * and pull out a bean with the name given during initialization. |
| 59 | * |
| 60 | * @see org.springframework.batch.core.configuration.JobFactory#createJob() |
| 61 | */ |
| 62 | public Job createJob() { |
| 63 | ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { path }, false, parent); |
| 64 | context.setDisplayName("Job ApplicationContext "+beanName); |
| 65 | context.refresh(); |
| 66 | Job job = (Job) context.getBean(beanName, Job.class); |
| 67 | return new ContextClosingJob(job, context); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Return the bean name of the job in the application context. N.B. this is |
| 72 | * usually the name of the job as well, but it needn't be. The important |
| 73 | * thing is that the job can be located by this name. |
| 74 | * |
| 75 | * @see org.springframework.batch.core.configuration.JobFactory#getJobName() |
| 76 | */ |
| 77 | public String getJobName() { |
| 78 | return beanName; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @author Dave Syer |
| 83 | * |
| 84 | */ |
| 85 | private static class ContextClosingJob implements Job { |
| 86 | private Job delegate; |
| 87 | |
| 88 | private ConfigurableApplicationContext context; |
| 89 | |
| 90 | /** |
| 91 | * @param delegate |
| 92 | * @param context |
| 93 | */ |
| 94 | public ContextClosingJob(Job delegate, ConfigurableApplicationContext context) { |
| 95 | super(); |
| 96 | this.delegate = delegate; |
| 97 | this.context = context; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param execution |
| 102 | * @throws JobExecutionException |
| 103 | * @see org.springframework.batch.core.Job#execute(org.springframework.batch.core.JobExecution) |
| 104 | */ |
| 105 | public void execute(JobExecution execution) throws JobExecutionException { |
| 106 | try { |
| 107 | delegate.execute(execution); |
| 108 | } |
| 109 | finally { |
| 110 | context.close(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @see org.springframework.batch.core.Job#getName() |
| 116 | */ |
| 117 | public String getName() { |
| 118 | return delegate.getName(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @deprecated planned for removal in 2.0 |
| 123 | * @see org.springframework.batch.core.Job#getSteps() |
| 124 | */ |
| 125 | public List getSteps() { |
| 126 | return delegate.getSteps(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @see org.springframework.batch.core.Job#isRestartable() |
| 131 | */ |
| 132 | public boolean isRestartable() { |
| 133 | return delegate.isRestartable(); |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | |
| 138 | } |