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