1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.batch.admin.integration;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20
21 import org.springframework.batch.admin.service.JobService;
22 import org.springframework.batch.admin.domain.JobInfo;
23 import org.springframework.batch.core.Job;
24 import org.springframework.batch.core.configuration.DuplicateJobException;
25 import org.springframework.batch.core.configuration.JobRegistry;
26 import org.springframework.batch.core.configuration.support.ApplicationContextFactory;
27 import org.springframework.batch.core.configuration.support.GenericApplicationContextFactory;
28 import org.springframework.batch.core.configuration.support.JobLoader;
29 import org.springframework.batch.core.launch.NoSuchJobException;
30 import org.springframework.beans.BeansException;
31 import org.springframework.context.ApplicationContext;
32 import org.springframework.context.ApplicationContextAware;
33 import org.springframework.core.io.Resource;
34 import org.springframework.integration.annotation.MessageEndpoint;
35 import org.springframework.integration.annotation.ServiceActivator;
36
37
38
39
40
41
42
43
44
45
46
47
48
49 @MessageEndpoint
50 public class JobConfigurationResourceLoader implements ApplicationContextAware {
51
52 private JobLoader jobLoader;
53
54 private JobService jobService;
55
56 private ApplicationContext parent;
57
58 public void setJobLoader(JobLoader jobLoader) {
59 this.jobLoader = jobLoader;
60 }
61
62 public void setJobService(JobService jobService) {
63 this.jobService = jobService;
64 }
65
66 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
67 this.parent = applicationContext;
68 }
69
70 @ServiceActivator
71 public Collection<JobInfo> loadJobs(Resource resource) throws DuplicateJobException {
72
73 Collection<JobInfo> result = new ArrayList<JobInfo>();
74
75 ApplicationContextFactory factory = createApplicationContextFactory(parent, resource);
76 Collection<Job> jobs = jobLoader.reload(factory);
77
78 for (Job job : jobs) {
79 String name = job.getName();
80 int count = 0;
81 try {
82 count = jobService.countJobExecutionsForJob(name);
83 }
84 catch (NoSuchJobException e) {
85
86 }
87 boolean launchable = jobService.isLaunchable(name);
88 boolean incrementable = jobService.isIncrementable(name);
89 result.add(new JobInfo(name, count, null, launchable, incrementable));
90 }
91
92 return result;
93
94 }
95
96
97
98
99
100
101
102
103
104
105
106 protected ApplicationContextFactory createApplicationContextFactory(ApplicationContext parent, Resource resource) {
107 GenericApplicationContextFactory applicationContextFactory = new GenericApplicationContextFactory(resource);
108 if (parent != null) {
109 applicationContextFactory.setApplicationContext(parent);
110 }
111 return applicationContextFactory;
112 }
113
114 }