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.lang.reflect.Constructor;
021    import java.util.Collections;
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.support.BeanDefinitionRegistry;
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.core.SpringVersion;
032    
033    /**
034     * An XBean version of the regular Spring class to provide improved XML handling.
035     * 
036     * @author James Strachan
037     * @author Dain Sundstrom
038     * @version $Id$
039     * @since 2.0
040     */
041    public class ClassPathXmlApplicationContext extends org.springframework.context.support.ClassPathXmlApplicationContext implements SpringApplicationContext {
042        private final List xmlPreprocessors;
043    
044        /**
045         * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified location on the class
046         * path.
047         * @param configLocation the location of the configuration file on the class path
048         * @throws BeansException if a problem occurs while reading the configuration
049         */
050        public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
051            this(new String[] {configLocation}, true, null, Collections.EMPTY_LIST);
052        }
053    
054        /**
055         * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
056         * path.
057         * @param configLocations the locations of the configuration files on the class path
058         * @throws BeansException if a problem occurs while reading the configuration
059         */
060        public ClassPathXmlApplicationContext(String[] configLocations) throws BeansException {
061            this(configLocations, true, null, Collections.EMPTY_LIST);
062        }
063    
064        /**
065         * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
066         * path.
067         * @param configLocations the locations of the configuration files on the class path
068         * @param refresh if true the configurations are immedately loaded; otherwise the configurations are not loaded
069         * until refresh() is called
070         * @throws BeansException if a problem occurs while reading the configuration
071         */
072        public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {
073            this(configLocations, refresh, null, Collections.EMPTY_LIST);
074        }
075    
076        /**
077         * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
078         * path.
079         * @param configLocations the locations of the configuration files on the class path
080         * @param parent the parent of this application context
081         * @throws BeansException if a problem occurs while reading the configuration
082         */
083        public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {
084            this(configLocations, true, parent, Collections.EMPTY_LIST);
085        }
086    
087        /**
088         * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
089         * path.
090         * @param configLocations the locations of the configuration files on the class path
091         * @param refresh if true the configurations are immedately loaded; otherwise the configurations are not loaded
092         * until refresh() is called
093         * @param parent the parent of this application context
094         * @throws BeansException if a problem occurs while reading the configuration
095         */
096        public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException {
097            this(configLocations, refresh, parent, Collections.EMPTY_LIST);
098        }
099    
100        /**
101         * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified location on the class
102         * path.
103         * @param configLocation the location of the configuration file on the classpath
104         * @param xmlPreprocessors the SpringXmlPreprocessors to apply before passing the xml to Spring for processing
105         * @throws BeansException if a problem occurs while reading the configuration
106         */
107        public ClassPathXmlApplicationContext(String configLocation, List xmlPreprocessors) throws BeansException {
108            this(new String[] {configLocation}, true, null, xmlPreprocessors);
109        }
110    
111        /**
112         * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
113         * path.
114         * @param configLocations the locations of the configuration files on the class path
115         * @param xmlPreprocessors the SpringXmlPreprocessors to apply before passing the xml to Spring for processing
116         * @throws BeansException if a problem occurs while reading the configuration
117         */
118        public ClassPathXmlApplicationContext(String[] configLocations, List xmlPreprocessors) throws BeansException {
119            this(configLocations, true, null, xmlPreprocessors);
120        }
121    
122        /**
123         * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
124         * path.
125         * @param configLocations the locations of the configuration files on the class path
126         * @param refresh if true the configurations are immedately loaded; otherwise the configurations are not loaded
127         * until refresh() is called
128         * @param xmlPreprocessors the SpringXmlPreprocessors to apply before passing the xml to Spring for processing
129         * @throws BeansException if a problem occurs while reading the configuration
130         */
131        public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, List xmlPreprocessors) throws BeansException {
132            this(configLocations, refresh, null, xmlPreprocessors);
133        }
134    
135        /**
136         * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
137         * path.
138         * @param configLocations the locations of the configuration files on the class path
139         * @param parent the parent of this application context
140         * @param xmlPreprocessors the SpringXmlPreprocessors to apply before passing the xml to Spring for processing
141         * @throws BeansException if a problem occurs while reading the configuration
142         */
143        public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent, List xmlPreprocessors) throws BeansException {
144            this(configLocations, true, parent, xmlPreprocessors);
145        }
146    
147        /**
148         * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
149         * path.
150         * @param configLocations the locations of the configuration files on the class path
151         * @param refresh if true the configurations are immedately loaded; otherwise the configurations are not loaded
152         * until refresh() is called
153         * @param parent the parent of this application context
154         * @param xmlPreprocessors the SpringXmlPreprocessors to apply before passing the xml to Spring for processing
155         * @throws BeansException if a problem occurs while reading the configuration
156         */
157        public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent, List xmlPreprocessors) throws BeansException {
158            super(configLocations, false, parent);
159            this.xmlPreprocessors = xmlPreprocessors;
160            if (refresh) {
161                refresh();
162            }
163        }
164    
165        /**
166         * {@inheritDoc}
167         */
168        protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException {
169            // Create a new XmlBeanDefinitionReader for the given BeanFactory.
170            XmlBeanDefinitionReader beanDefinitionReader = XBeanHelper.createBeanDefinitionReader(this, beanFactory, xmlPreprocessors);
171    
172            // Configure the bean definition reader with this context's
173            // resource loading environment.
174            beanDefinitionReader.setResourceLoader(this);
175            beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
176    
177            // Allow a subclass to provide custom initialization of the reader,
178            // then proceed with actually loading the bean definitions.
179            initBeanDefinitionReader(beanDefinitionReader);
180            loadBeanDefinitions(beanDefinitionReader);
181        }
182        
183    }