| 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 org.springframework.beans.BeansException; |
| 20 | import org.springframework.context.ApplicationContext; |
| 21 | import org.springframework.context.ApplicationContextAware; |
| 22 | import org.springframework.context.ConfigurableApplicationContext; |
| 23 | import org.springframework.context.support.AbstractXmlApplicationContext; |
| 24 | import org.springframework.core.io.Resource; |
| 25 | import org.springframework.util.Assert; |
| 26 | |
| 27 | /** |
| 28 | * {@link ApplicationContextFactory} implementation that takes a parent context and a path |
| 29 | * to the context to create. Each time the createApplicationContext method is called, a new |
| 30 | * {@link ApplicationContext} will be returned. It should be noted that if a path isn't |
| 31 | * set, the parent will always be returned. |
| 32 | * |
| 33 | */ |
| 34 | public class ClassPathXmlApplicationContextFactory implements ApplicationContextFactory, ApplicationContextAware { |
| 35 | |
| 36 | private ConfigurableApplicationContext parent; |
| 37 | |
| 38 | private Resource path; |
| 39 | |
| 40 | /** |
| 41 | * Setter for the path to the xml to load to create an |
| 42 | * {@link ApplicationContext}. Use imports to centralise the configuration in |
| 43 | * one file. |
| 44 | * |
| 45 | * @param path the resource path to the xml to load for the child context. |
| 46 | */ |
| 47 | public void setPath(Resource path) { |
| 48 | this.path = path; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Setter for the parent application context. |
| 53 | * |
| 54 | * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) |
| 55 | */ |
| 56 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 57 | Assert.isInstanceOf(ConfigurableApplicationContext.class, applicationContext); |
| 58 | parent = (ConfigurableApplicationContext) applicationContext; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Creates an {@link ApplicationContext} from the provided path. |
| 63 | * |
| 64 | * @see ApplicationContextFactory#createApplicationContext() |
| 65 | */ |
| 66 | public ConfigurableApplicationContext createApplicationContext() { |
| 67 | if (path == null) { |
| 68 | return parent; |
| 69 | } |
| 70 | return new ResourceXmlApplicationContext(parent); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @author Dave Syer |
| 75 | * |
| 76 | */ |
| 77 | private final class ResourceXmlApplicationContext extends AbstractXmlApplicationContext { |
| 78 | /** |
| 79 | * @param parent |
| 80 | */ |
| 81 | private ResourceXmlApplicationContext(ApplicationContext parent) { |
| 82 | super(parent); |
| 83 | refresh(); |
| 84 | } |
| 85 | |
| 86 | protected Resource[] getConfigResources() { |
| 87 | return new Resource[] {path}; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | } |