1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33 public class JobLocatorStepLocator implements StepLocator {
34
35 private ListableJobLocator jobLocator;
36
37
38
39
40
41
42 public JobLocatorStepLocator(ListableJobLocator jobLocator) {
43 super();
44 this.jobLocator = jobLocator;
45 }
46
47
48
49
50 public JobLocatorStepLocator() {
51 super();
52 }
53
54
55
56
57 public void setJobLocator(ListableJobLocator jobLocator) {
58 this.jobLocator = jobLocator;
59 }
60
61
62
63
64
65
66
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
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
94
95
96
97
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 }