001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.xbean.spring.context;
018
019 import java.io.IOException;
020 import java.util.Collections;
021 import java.util.Iterator;
022 import java.util.List;
023
024 import org.apache.xbean.spring.context.impl.XBeanHelper;
025 import org.springframework.beans.BeansException;
026 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
027 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
028 import org.springframework.beans.factory.xml.ResourceEntityResolver;
029 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
030 import org.springframework.context.ApplicationContext;
031 import org.springframework.context.support.AbstractXmlApplicationContext;
032 import org.springframework.core.io.Resource;
033
034 /**
035 * An XBean version of a regular Spring ApplicationContext which takes a
036 * {@link Resource} as a parameter to load the application context
037 *
038 * @author James Strachan
039 * @author Dain Sundstrom
040 * @version $Id$
041 * @since 2.0
042 */
043 public class ResourceXmlApplicationContext extends AbstractXmlApplicationContext implements SpringApplicationContext {
044 private final List xmlPreprocessors;
045 private final Resource resource;
046
047 /**
048 * Creates a ResourceXmlApplicationContext which loads the configuration from the specified Resource.
049 * @param resource the resource from which the configuration is loaded
050 */
051 public ResourceXmlApplicationContext(Resource resource) {
052 this(resource, Collections.EMPTY_LIST);
053 }
054
055 /**
056 * Creates a ResourceXmlApplicationContext which loads the configuration from the specified Resource.
057 * @param resource the resource from which the configuration is loaded
058 * @param xmlPreprocessors the SpringXmlPreprocessors to apply before passing the xml to Spring for processing
059 */
060 public ResourceXmlApplicationContext(Resource resource, List xmlPreprocessors) {
061 super();
062 this.xmlPreprocessors = xmlPreprocessors;
063 this.resource = resource;
064 refresh();
065 }
066
067 public ResourceXmlApplicationContext(Resource resource, ApplicationContext parent) {
068 this(resource, Collections.EMPTY_LIST, parent);
069 }
070
071 public ResourceXmlApplicationContext(Resource resource, List xmlPreprocessors, ApplicationContext parent) {
072 this(resource, xmlPreprocessors, parent, Collections.EMPTY_LIST);
073 }
074
075 public ResourceXmlApplicationContext(Resource resource, List xmlPreprocessors, ApplicationContext parent, List beanPostProcessors) {
076 this(resource, xmlPreprocessors, parent, beanPostProcessors, true);
077 }
078
079 public ResourceXmlApplicationContext(Resource resource, List xmlPreprocessors, ApplicationContext parent, List beanPostProcessors, boolean refresh) {
080 super(parent);
081 this.xmlPreprocessors = xmlPreprocessors;
082 this.resource = resource;
083 for (Iterator iter = beanPostProcessors.iterator(); iter.hasNext();) {
084 BeanFactoryPostProcessor processor = (BeanFactoryPostProcessor) iter.next();
085 addBeanFactoryPostProcessor(processor);
086 }
087 if (refresh) {
088 refresh();
089 }
090 }
091
092 protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException {
093 // Create a new XmlBeanDefinitionReader for the given BeanFactory.
094 XmlBeanDefinitionReader beanDefinitionReader = XBeanHelper.createBeanDefinitionReader(this, beanFactory, xmlPreprocessors);
095
096 // Configure the bean definition reader with this context's
097 // resource loading environment.
098 beanDefinitionReader.setResourceLoader(this);
099 beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
100
101 // Allow a subclass to provide custom initialization of the reader,
102 // then proceed with actually loading the bean definitions.
103 initBeanDefinitionReader(beanDefinitionReader);
104 loadBeanDefinitions(beanDefinitionReader);
105 }
106
107 /**
108 * {@inheritDoc}
109 */
110 protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
111 reader.loadBeanDefinitions(resource);
112 }
113
114 /**
115 * {@inheritDoc}
116 */
117 protected String[] getConfigLocations() {
118 return null;
119 }
120 }