View Javadoc
1   /*
2    * Copyright 2009-2010 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.admin.service;
17  
18  import java.util.Collection;
19  import java.util.HashSet;
20  
21  import org.springframework.batch.core.Job;
22  import org.springframework.batch.core.Step;
23  import org.springframework.batch.core.configuration.JobLocator;
24  import org.springframework.batch.core.configuration.ListableJobLocator;
25  import org.springframework.batch.core.launch.NoSuchJobException;
26  import org.springframework.batch.core.step.NoSuchStepException;
27  import org.springframework.batch.core.step.StepLocator;
28  
29  /**
30   * @author Dave Syer
31   * 
32   */
33  public class JobLocatorStepLocator implements StepLocator {
34  
35  	private ListableJobLocator jobLocator;
36  
37  	/**
38  	 * Create an instance from this {@link JobLocator}.
39  	 * 
40  	 * @param jobLocator a {@link JobLocator}
41  	 */
42  	public JobLocatorStepLocator(ListableJobLocator jobLocator) {
43  		super();
44  		this.jobLocator = jobLocator;
45  	}
46  
47  	/**
48  	 * Convenience constructor for declarative configuration.
49  	 */
50  	public JobLocatorStepLocator() {
51  		super();
52  	}
53  
54  	/**
55  	 * @param jobLocator the jobLocator to set
56  	 */
57  	public void setJobLocator(ListableJobLocator jobLocator) {
58  		this.jobLocator = jobLocator;
59  	}
60  
61  	/**
62  	 * Locate a step by referencing it through its parent job with a separator,
63  	 * e.g. <code>job-name/step-name</code>. The separator defaults to a forward
64  	 * slash.
65  	 * 
66  	 * @see StepLocator#getStep(String)
67  	 */
68  	public Step getStep(String path) throws NoSuchStepException {
69  		String jobName = path.substring(0, path.indexOf("/"));
70  		String stepName = path.substring(jobName.length() + 1);
71  		Job job;
72  		try {
73  			job = jobLocator.getJob(jobName);
74  		}
75  		catch (NoSuchJobException e) {
76  			throw new NoSuchStepException("No step could be located because no job was found with name=" + jobName);
77  		}
78  		String prefix = jobName+".";
79  		if (job instanceof StepLocator) {
80  			if (((StepLocator) job).getStepNames().contains(stepName)) {
81  				return ((StepLocator) job).getStep(stepName);
82  			}
83  			// TODO: remove this workaround for BATCH-1507
84  			if (((StepLocator) job).getStepNames().contains(prefix + stepName)) {
85  				return ((StepLocator) job).getStep(prefix + stepName);
86  			}
87  			throw new NoSuchStepException("No step could be located: "+path);
88  		}
89  		throw new NoSuchStepException("No step could be located because the job was not a StepLocator.");
90  	}
91  
92  	/**
93  	 * Loop through all the jobs and pull out their step names. The result is in
94  	 * the form that would be appropriate for {@link #getStep(String)} (i.e.
95  	 * with a separator).
96  	 * 
97  	 * @see StepLocator#getStepNames()
98  	 */
99  	public Collection<String> getStepNames() {
100 		Collection<String> result = new HashSet<String>();
101 		for (String jobName : jobLocator.getJobNames()) {
102 			Job job;
103 			try {
104 				job = jobLocator.getJob(jobName);
105 			}
106 			catch (NoSuchJobException e) {
107 				throw new IllegalStateException("Job not found although it was listed with name=" + jobName);
108 			}
109 			String prefix = jobName + ".";
110 			if (job instanceof StepLocator) {
111 				for (String stepName : ((StepLocator) job).getStepNames()) {
112 					if (stepName.startsWith(prefix)) {
113 						stepName = stepName.substring(prefix.length());
114 					}
115 					result.add(jobName + "/" + stepName);
116 				}
117 			}
118 		}
119 		return result;
120 	}
121 
122 }