EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[all classes][org.springframework.batch.core.configuration.annotation]

COVERAGE SUMMARY FOR SOURCE FILE [SimpleBatchConfiguration.java]

nameclass, %method, %block, %line, %
SimpleBatchConfiguration.java75%  (3/4)100% (12/12)100% (151/151)100% (33/33)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SimpleBatchConfiguration100% (1/1)100% (7/7)100% (122/122)100% (26/26)
SimpleBatchConfiguration (): void 100% (1/1)100% (26/26)100% (7/7)
createLazyProxy (AtomicReference, Class): Object 100% (1/1)100% (31/31)100% (6/6)
initialize (): void 100% (1/1)100% (37/37)100% (9/9)
jobLauncher (): JobLauncher 100% (1/1)100% (7/7)100% (1/1)
jobRegistry (): JobRegistry 100% (1/1)100% (7/7)100% (1/1)
jobRepository (): JobRepository 100% (1/1)100% (7/7)100% (1/1)
transactionManager (): PlatformTransactionManager 100% (1/1)100% (7/7)100% (1/1)
     
class SimpleBatchConfiguration$10%   (0/1)100% (0/0)100% (0/0)100% (0/0)
     
class SimpleBatchConfiguration$PassthruAdvice100% (1/1)100% (3/3)100% (13/13)100% (2/2)
SimpleBatchConfiguration$PassthruAdvice (SimpleBatchConfiguration): void 100% (1/1)100% (6/6)100% (1/1)
SimpleBatchConfiguration$PassthruAdvice (SimpleBatchConfiguration, SimpleBatc... 100% (1/1)100% (4/4)100% (1/1)
invoke (MethodInvocation): Object 100% (1/1)100% (3/3)100% (1/1)
     
class SimpleBatchConfiguration$ReferenceTargetSource100% (1/1)100% (2/2)100% (16/16)100% (5/5)
SimpleBatchConfiguration$ReferenceTargetSource (SimpleBatchConfiguration, Ato... 100% (1/1)100% (9/9)100% (3/3)
createObject (): Object 100% (1/1)100% (7/7)100% (2/2)

1/*
2 * Copyright 2012-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 */
16package org.springframework.batch.core.configuration.annotation;
17 
18import java.util.concurrent.atomic.AtomicReference;
19 
20import org.aopalliance.intercept.MethodInterceptor;
21import org.aopalliance.intercept.MethodInvocation;
22import org.springframework.aop.framework.ProxyFactory;
23import org.springframework.aop.target.AbstractLazyCreationTargetSource;
24import org.springframework.batch.core.configuration.JobRegistry;
25import org.springframework.batch.core.configuration.support.MapJobRegistry;
26import org.springframework.batch.core.launch.JobLauncher;
27import org.springframework.batch.core.repository.JobRepository;
28import org.springframework.beans.factory.annotation.Autowired;
29import org.springframework.context.ApplicationContext;
30import org.springframework.context.annotation.Bean;
31import org.springframework.context.annotation.Configuration;
32import org.springframework.transaction.PlatformTransactionManager;
33 
34/**
35 * Base {@code Configuration} class providing common structure for enabling and using Spring Batch. Customization is
36 * available by implementing the {@link BatchConfigurer} interface. The main components are created as lazy proxies that
37 * only initialize when a method is called. This is to prevent (as much as possible) configuration cycles from
38 * developing when these components are needed in a configuration resource that itself provides a
39 * {@link BatchConfigurer}.
40 *
41 * @author Dave Syer
42 * @since 2.2
43 * @see EnableBatchProcessing
44 */
45@Configuration
46public class SimpleBatchConfiguration extends AbstractBatchConfiguration {
47 
48        @Autowired
49        private ApplicationContext context;
50 
51        private boolean initialized = false;
52 
53        private AtomicReference<JobRepository> jobRepository = new AtomicReference<JobRepository>();
54 
55        private AtomicReference<JobLauncher> jobLauncher = new AtomicReference<JobLauncher>();
56 
57        private AtomicReference<JobRegistry> jobRegistry = new AtomicReference<JobRegistry>();
58 
59        private AtomicReference<PlatformTransactionManager> transactionManager = new AtomicReference<PlatformTransactionManager>();
60 
61        @Override
62        @Bean
63        public JobRepository jobRepository() throws Exception {
64                return createLazyProxy(jobRepository, JobRepository.class);
65        }
66 
67        @Override
68        @Bean
69        public JobLauncher jobLauncher() throws Exception {
70                return createLazyProxy(jobLauncher, JobLauncher.class);
71        }
72 
73        @Override
74        @Bean
75        public JobRegistry jobRegistry() throws Exception {
76                return createLazyProxy(jobRegistry, JobRegistry.class);
77        }
78 
79        @Override
80        @Bean
81        public PlatformTransactionManager transactionManager() throws Exception {
82                return createLazyProxy(transactionManager, PlatformTransactionManager.class);
83        }
84 
85        private <T> T createLazyProxy(AtomicReference<T> reference, Class<T> type) {
86                ProxyFactory factory = new ProxyFactory();
87                factory.setTargetSource(new ReferenceTargetSource<T>(reference));
88                factory.addAdvice(new PassthruAdvice());
89                factory.setInterfaces(new Class<?>[] { type });
90                @SuppressWarnings("unchecked")
91                T proxy = (T) factory.getProxy();
92                return proxy;
93        }
94 
95        /**
96         * Sets up the basic components by extracting them from the {@link BatchConfigurer configurer}, defaulting to some
97         * sensible values as long as a unique DataSource is available.
98         *
99         * @throws Exception if there is a problem in the configurer
100         */
101        protected void initialize() throws Exception {
102                if (initialized) {
103                        return;
104                }
105                BatchConfigurer configurer = getConfigurer(context.getBeansOfType(BatchConfigurer.class).values());
106                jobRepository.set(configurer.getJobRepository());
107                jobLauncher.set(configurer.getJobLauncher());
108                transactionManager.set(configurer.getTransactionManager());
109                jobRegistry.set(new MapJobRegistry());
110                initialized = true;
111        }
112 
113        private class PassthruAdvice implements MethodInterceptor {
114 
115                @Override
116                public Object invoke(MethodInvocation invocation) throws Throwable {
117                        return invocation.proceed();
118                }
119 
120        }
121 
122        private class ReferenceTargetSource<T> extends AbstractLazyCreationTargetSource {
123 
124                private AtomicReference<T> reference;
125 
126                public ReferenceTargetSource(AtomicReference<T> reference) {
127                        this.reference = reference;
128                }
129 
130                @Override
131                protected Object createObject() throws Exception {
132                        initialize();
133                        return reference.get();
134                }
135        }
136 
137}

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