View Javadoc
1   /*
2    * Copyright 2015 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.configuration;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.springframework.batch.core.configuration.support.ApplicationContextFactory;
23  import org.springframework.beans.factory.FactoryBean;
24  import org.springframework.beans.factory.InitializingBean;
25  import org.springframework.util.Assert;
26  import org.springframework.util.CollectionUtils;
27  
28  /**
29   * @author Michael Minella
30   */
31  public class CompositeApplicationContextFactory implements FactoryBean<ApplicationContextFactory[]>, InitializingBean {
32  
33  	private List<FactoryBean<ApplicationContextFactory[]>> factoryBeans;
34  
35  	private List<ApplicationContextFactory> delegateFactories;
36  
37  	public void setFactoryBeans(List<FactoryBean<ApplicationContextFactory[]>> factoryBeans) {
38  		this.factoryBeans = factoryBeans;
39  	}
40  
41  	public void setFactories(List<ApplicationContextFactory> delegateFactories) {
42  		this.delegateFactories = delegateFactories;
43  	}
44  
45  	@Override
46  	public ApplicationContextFactory[] getObject() throws Exception {
47  		List<ApplicationContextFactory> factories = new ArrayList<ApplicationContextFactory>();
48  
49  		if(factoryBeans != null) {
50  			for (FactoryBean<ApplicationContextFactory[]> factory : factoryBeans) {
51  				factories.addAll(Arrays.asList(factory.getObject()));
52  			}
53  		}
54  
55  		if(delegateFactories != null) {
56  			factories.addAll(delegateFactories);
57  		}
58  
59  		return factories.toArray(new ApplicationContextFactory[factories.size()]);
60  	}
61  
62  	@Override
63  	public Class<?> getObjectType() {
64  		return ApplicationContextFactory[].class;
65  	}
66  
67  	@Override
68  	public boolean isSingleton() {
69  		return true;
70  	}
71  
72  	@Override
73  	public void afterPropertiesSet() throws Exception {
74  		Assert.state(!CollectionUtils.isEmpty(factoryBeans) || !CollectionUtils.isEmpty(delegateFactories),
75  				"A factory or factoryBean is required");
76  	}
77  }