EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.core.configuration.support]

COVERAGE SUMMARY FOR SOURCE FILE [AutomaticJobRegistrar.java]

nameclass, %method, %block, %line, %
AutomaticJobRegistrar.java100% (1/1)100% (11/11)90%  (147/164)92%  (38.8/42)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AutomaticJobRegistrar100% (1/1)100% (11/11)90%  (147/164)92%  (38.8/42)
isRunning (): boolean 100% (1/1)67%  (10/15)67%  (2/3)
stop (): void 100% (1/1)75%  (15/20)95%  (4.7/5)
start (): void 100% (1/1)87%  (41/47)83%  (9.1/11)
afterPropertiesSet (): void 100% (1/1)89%  (8/9)94%  (1.9/2)
AutomaticJobRegistrar (): void 100% (1/1)100% (19/19)100% (5/5)
getOrder (): int 100% (1/1)100% (3/3)100% (1/1)
onApplicationEvent (ApplicationEvent): void 100% (1/1)100% (17/17)100% (6/6)
setApplicationContext (ApplicationContext): void 100% (1/1)100% (4/4)100% (2/2)
setApplicationContextFactories (ApplicationContextFactory []): void 100% (1/1)100% (22/22)100% (3/3)
setJobLoader (JobLoader): void 100% (1/1)100% (4/4)100% (2/2)
setOrder (int): void 100% (1/1)100% (4/4)100% (2/2)

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 
17package org.springframework.batch.core.configuration.support;
18 
19import java.util.ArrayList;
20import java.util.Collection;
21 
22import org.springframework.batch.core.Job;
23import org.springframework.batch.core.configuration.DuplicateJobException;
24import org.springframework.batch.core.configuration.JobRegistry;
25import org.springframework.beans.factory.InitializingBean;
26import org.springframework.context.ApplicationContext;
27import org.springframework.context.ApplicationContextAware;
28import org.springframework.context.ApplicationEvent;
29import org.springframework.context.ApplicationListener;
30import org.springframework.context.Lifecycle;
31import org.springframework.context.event.ContextClosedEvent;
32import org.springframework.context.event.ContextRefreshedEvent;
33import org.springframework.core.Ordered;
34import 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 */
48public 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}

[all classes][org.springframework.batch.core.configuration.support]
EMMA 2.0.5312 (C) Vladimir Roubtsov