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