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