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.launch.support; |
17 | |
18 | import java.util.HashMap; |
19 | import java.util.Iterator; |
20 | import java.util.Map; |
21 | import java.util.Properties; |
22 | import java.util.Map.Entry; |
23 | |
24 | import org.springframework.batch.core.Job; |
25 | import org.springframework.batch.core.JobExecution; |
26 | import org.springframework.batch.core.JobExecutionException; |
27 | import org.springframework.batch.core.JobParameters; |
28 | import org.springframework.batch.core.StepExecution; |
29 | import org.springframework.batch.core.configuration.JobLocator; |
30 | import org.springframework.batch.core.converter.DefaultJobParametersConverter; |
31 | import org.springframework.batch.core.converter.JobParametersConverter; |
32 | import org.springframework.batch.core.launch.JobLauncher; |
33 | import org.springframework.batch.core.repository.NoSuchJobException; |
34 | import org.springframework.batch.item.ExecutionContext; |
35 | import org.springframework.batch.support.PropertiesConverter; |
36 | import org.springframework.beans.factory.InitializingBean; |
37 | import org.springframework.util.Assert; |
38 | |
39 | /** |
40 | * @author Dave Syer |
41 | * |
42 | */ |
43 | public class SimpleExportedJobLauncher implements ExportedJobLauncher, InitializingBean { |
44 | |
45 | private JobLauncher launcher; |
46 | |
47 | private JobLocator jobLocator; |
48 | |
49 | private Map registry = new HashMap(); |
50 | |
51 | private JobParametersConverter jobParametersConverter = new DefaultJobParametersConverter(); |
52 | |
53 | /* (non-Javadoc) |
54 | * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() |
55 | */ |
56 | public void afterPropertiesSet() throws Exception { |
57 | Assert.notNull(launcher, "JobLauncher must be provided."); |
58 | Assert.notNull(jobLocator, "JobLocator must be provided."); |
59 | } |
60 | |
61 | /** |
62 | * Public setter for the {@link JobLauncher}. |
63 | * @param launcher the launcher to set |
64 | */ |
65 | public void setLauncher(JobLauncher launcher) { |
66 | this.launcher = launcher; |
67 | } |
68 | |
69 | /** |
70 | * Public setter for the JobLocator. |
71 | * @param jobLocator the jobLocator to set |
72 | */ |
73 | public void setJobLocator(JobLocator jobLocator) { |
74 | this.jobLocator = jobLocator; |
75 | } |
76 | |
77 | /** |
78 | * Public setter for the JobParametersFactory. |
79 | * @param jobParametersConverter the jobParametersFactory to set |
80 | */ |
81 | public void setJobParametersFactory(JobParametersConverter jobParametersConverter) { |
82 | this.jobParametersConverter = jobParametersConverter; |
83 | } |
84 | |
85 | /* |
86 | * (non-Javadoc) |
87 | * @see org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher#getStatistics() |
88 | */ |
89 | public Properties getStatistics() { |
90 | Properties result = new Properties(); |
91 | int i = 0; |
92 | for (Iterator iterator = registry.keySet().iterator(); iterator.hasNext();) { |
93 | String key = (String) iterator.next(); |
94 | JobExecution execution = (JobExecution) registry.get(key); |
95 | addStatistics(result, execution, "job" + i + "."); |
96 | i++; |
97 | } |
98 | return result; |
99 | } |
100 | |
101 | /** |
102 | * @param result |
103 | * @param execution |
104 | */ |
105 | private void addStatistics(Properties result, JobExecution execution, String prefix) { |
106 | int i = 0; |
107 | for (Iterator iterator = execution.getStepExecutions().iterator(); iterator.hasNext();) { |
108 | StepExecution stepExecution = (StepExecution) iterator.next(); |
109 | ExecutionContext statistics = stepExecution.getExecutionContext(); |
110 | for (Iterator iter = statistics.entrySet().iterator(); iter.hasNext();) { |
111 | Entry entry = (Entry) iter.next(); |
112 | result.setProperty(prefix + "step" + i + "." + entry.getKey(), ""+entry.getValue()); |
113 | } |
114 | } |
115 | } |
116 | |
117 | /* |
118 | * (non-Javadoc) |
119 | * @see org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher#isRunning() |
120 | */ |
121 | public boolean isRunning() { |
122 | for (Iterator iterator = registry.keySet().iterator(); iterator.hasNext();) { |
123 | String key = (String) iterator.next(); |
124 | JobExecution execution = (JobExecution) registry.get(key); |
125 | if (execution.isRunning()) { |
126 | return true; |
127 | } |
128 | } |
129 | return false; |
130 | } |
131 | |
132 | /* |
133 | * (non-Javadoc) |
134 | * @see org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher#run(java.lang.String) |
135 | */ |
136 | public String run(String name) { |
137 | return run(name, null); |
138 | } |
139 | |
140 | /* |
141 | * (non-Javadoc) |
142 | * @see org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher#run(java.lang.String, |
143 | * java.lang.String) |
144 | */ |
145 | public String run(String name, String params) { |
146 | |
147 | Job job; |
148 | try { |
149 | job = jobLocator.getJob(name); |
150 | } |
151 | catch (NoSuchJobException e) { |
152 | return e.getClass().getName() + ": " + e.getMessage(); |
153 | } |
154 | |
155 | JobParameters jobParameters = new JobParameters(); |
156 | if (params != null) { |
157 | jobParameters = jobParametersConverter.getJobParameters(PropertiesConverter.stringToProperties(params)); |
158 | } |
159 | |
160 | JobExecution execution; |
161 | try { |
162 | execution = launcher.run(job, jobParameters); |
163 | } |
164 | catch (JobExecutionException e) { |
165 | return e.getClass().getName() + ": " + e.getMessage(); |
166 | } |
167 | registry.put(name + params, execution); |
168 | |
169 | return execution.toString(); |
170 | |
171 | } |
172 | |
173 | /* |
174 | * (non-Javadoc) |
175 | * @see org.springframework.batch.execution.bootstrap.support.ExportedJobLauncher#stop() |
176 | */ |
177 | public void stop() { |
178 | for (Iterator iterator = registry.keySet().iterator(); iterator.hasNext();) { |
179 | String key = (String) iterator.next(); |
180 | JobExecution execution = (JobExecution) registry.get(key); |
181 | execution.stop(); |
182 | } |
183 | registry.clear(); |
184 | } |
185 | |
186 | } |