| 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 | |
| 17 | package org.springframework.batch.core.configuration.support; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.Collection; |
| 21 | |
| 22 | import org.springframework.batch.core.Job; |
| 23 | import org.springframework.batch.core.configuration.DuplicateJobException; |
| 24 | import org.springframework.batch.core.configuration.JobRegistry; |
| 25 | import org.springframework.beans.factory.InitializingBean; |
| 26 | import org.springframework.context.ApplicationContext; |
| 27 | import org.springframework.context.ApplicationContextAware; |
| 28 | import org.springframework.context.ApplicationEvent; |
| 29 | import org.springframework.context.ApplicationListener; |
| 30 | import org.springframework.context.Lifecycle; |
| 31 | import org.springframework.context.event.ContextClosedEvent; |
| 32 | import org.springframework.context.event.ContextRefreshedEvent; |
| 33 | import org.springframework.core.Ordered; |
| 34 | import org.springframework.util.Assert; |
| 35 | |
| 36 | /** |
| 37 | * Loads and unloads {@link Job Jobs} when the application context is created |
| 38 | * and destroyed. Each resource provided is loaded as an application context |
| 39 | * with the current context as its parent, and then all the jobs from the child |
| 40 | * context are registered under their bean names. A {@link JobRegistry} is |
| 41 | * required. |
| 42 | * |
| 43 | * @author Lucas Ward |
| 44 | * @author Dave Syer |
| 45 | * |
| 46 | * @since 2.1 |
| 47 | */ |
| 48 | public class AutomaticJobRegistrar implements Ordered, Lifecycle, ApplicationListener, ApplicationContextAware, InitializingBean { |
| 49 | |
| 50 | private Collection<ApplicationContextFactory> applicationContextFactories = new ArrayList<ApplicationContextFactory>(); |
| 51 | |
| 52 | private JobLoader jobLoader; |
| 53 | |
| 54 | private ApplicationContext applicationContext; |
| 55 | |
| 56 | private volatile boolean running = false; |
| 57 | |
| 58 | private Object lifecycleMonitor = new Object(); |
| 59 | |
| 60 | private int order = Ordered.LOWEST_PRECEDENCE; |
| 61 | |
| 62 | /** |
| 63 | * The enclosing application context, which can be used to check if |
| 64 | * {@link ApplicationEvent events} come from the expected source. |
| 65 | * |
| 66 | * @param applicationContext the enclosing application context if there is |
| 67 | * one |
| 68 | * @see ApplicationContextAware#setApplicationContext(ApplicationContext) |
| 69 | */ |
| 70 | public void setApplicationContext(ApplicationContext applicationContext) { |
| 71 | this.applicationContext = applicationContext; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Add some factories to the set that will be used to load contexts and jobs. |
| 76 | * |
| 77 | * @param applicationContextFactories the {@link ApplicationContextFactory} |
| 78 | * values to use |
| 79 | */ |
| 80 | public void setApplicationContextFactories(ApplicationContextFactory[] applicationContextFactories) { |
| 81 | for (ApplicationContextFactory applicationContextFactory : applicationContextFactories) { |
| 82 | this.applicationContextFactories.add(applicationContextFactory); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * The job loader that will be used to load and manage jobs. |
| 88 | * |
| 89 | * @param jobLoader the {@link JobLoader} to set |
| 90 | */ |
| 91 | public void setJobLoader(JobLoader jobLoader) { |
| 92 | this.jobLoader = jobLoader; |
| 93 | } |
| 94 | |
| 95 | public int getOrder() { |
| 96 | return order; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * The order to start up and shutdown. |
| 101 | * @param order the order (default {@link Ordered#LOWEST_PRECEDENCE}). |
| 102 | * @see Ordered |
| 103 | */ |
| 104 | public void setOrder(int order) { |
| 105 | this.order = order; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @throws Exception |
| 110 | */ |
| 111 | public void afterPropertiesSet() { |
| 112 | |
| 113 | Assert.state(jobLoader != null, "A JobLoader must be provided"); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Creates all the application contexts required and set up job registry |
| 119 | * entries with all the instances of {@link Job} found therein. Also closes |
| 120 | * the contexts when the enclosing context is closed. |
| 121 | * |
| 122 | * @see InitializingBean#afterPropertiesSet() |
| 123 | */ |
| 124 | public final void onApplicationEvent(ApplicationEvent event) { |
| 125 | // TODO: With Spring 3 a SmartLifecycle is started automatically |
| 126 | if (event.getSource() == applicationContext) { |
| 127 | if (event instanceof ContextRefreshedEvent) { |
| 128 | start(); |
| 129 | } |
| 130 | else if (event instanceof ContextClosedEvent) { |
| 131 | stop(); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Delegates to {@link JobLoader#clear()}. |
| 138 | * |
| 139 | * @see Lifecycle#stop() |
| 140 | */ |
| 141 | public void stop() { |
| 142 | synchronized (this.lifecycleMonitor) { |
| 143 | jobLoader.clear(); |
| 144 | running = false; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Take all the contexts from the factories provided and pass them to teh |
| 150 | * {@link JobLoader}. |
| 151 | * |
| 152 | * @see Lifecycle#start() |
| 153 | */ |
| 154 | public void start() { |
| 155 | synchronized (this.lifecycleMonitor) { |
| 156 | if (running) { |
| 157 | return; |
| 158 | } |
| 159 | for (ApplicationContextFactory factory : applicationContextFactories) { |
| 160 | try { |
| 161 | jobLoader.load(factory); |
| 162 | } |
| 163 | catch (DuplicateJobException e) { |
| 164 | throw new IllegalStateException(e); |
| 165 | } |
| 166 | } |
| 167 | running = true; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Check if this component has been started. |
| 173 | * |
| 174 | * @return true if started successfully and not stopped |
| 175 | * @see Lifecycle#isRunning() |
| 176 | */ |
| 177 | public boolean isRunning() { |
| 178 | synchronized (this.lifecycleMonitor) { |
| 179 | return running; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | } |