View Javadoc
1   /*
2    * Copyright 2009-2013 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.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   * Load XML from a Spring {@link Resource} and create an
39   * {@link ApplicationContext}, registering any {@link Job} instances defined in
40   * a {@link JobRegistry}. If this component is itself configured with Spring,
41   * then its own ApplicationContext will be the parent for the new one, and the
42   * child will inherit AOP configuration, as well as property placeholder and
43   * custom editor configuration from the parent.
44   * 
45   * @author Dave Syer
46   * @author Michael Minella
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  				// shouldn't happen
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  	 * Create an application context from the resource provided. Extension point
98  	 * for subclasses if they need to customize the context in any way. The
99  	 * default uses a {@link GenericApplicationContextFactory}.
100 	 * 
101 	 * @param parent the parent application context (or null if there is none)
102 	 * @param resource the location of the XML configuration
103 	 * 
104 	 * @return an application context containing jobs
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 }