001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.context.support;
018:
019: import org.springframework.beans.BeansException;
020: import org.springframework.context.ApplicationContext;
021: import org.springframework.core.io.FileSystemResource;
022: import org.springframework.core.io.Resource;
023:
024: /**
025: * Standalone XML application context, taking the context definition files
026: * from the file system or from URLs, interpreting plain paths as relative
027: * file system locations (e.g. "mydir/myfile.txt"). Useful for test harnesses
028: * as well as for standalone environments.
029: *
030: * <p><b>NOTE:</b> Plain paths will always be interpreted as relative
031: * to the current VM working directory, even if they start with a slash.
032: * (This is consistent with the semantics in a Servlet container.)
033: * <b>Use an explicit "file:" prefix to enforce an absolute file path.</b>
034: *
035: * <p>The config location defaults can be overridden via {@link #getConfigLocations},
036: * Config locations can either denote concrete files like "/myfiles/context.xml"
037: * or Ant-style patterns like "/myfiles/*-context.xml" (see the
038: * {@link org.springframework.util.AntPathMatcher} javadoc for pattern details).
039: *
040: * <p>Note: In case of multiple config locations, later bean definitions will
041: * override ones defined in earlier loaded files. This can be leveraged to
042: * deliberately override certain bean definitions via an extra XML file.
043: *
044: * <p><b>This is a simple, one-stop shop convenience ApplicationContext.
045: * Consider using the {@link GenericApplicationContext} class in combination
046: * with an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}
047: * for more flexible context setup.</b>
048: *
049: * @author Rod Johnson
050: * @author Juergen Hoeller
051: * @see #getResource
052: * @see #getResourceByPath
053: * @see GenericApplicationContext
054: */
055: public class FileSystemXmlApplicationContext extends
056: AbstractXmlApplicationContext {
057:
058: private String[] configLocations;
059:
060: /**
061: * Create a new FileSystemXmlApplicationContext, loading the definitions
062: * from the given XML file and automatically refreshing the context.
063: * @param configLocation file path
064: * @throws BeansException if context creation failed
065: */
066: public FileSystemXmlApplicationContext(String configLocation)
067: throws BeansException {
068: this (new String[] { configLocation });
069: }
070:
071: /**
072: * Create a new FileSystemXmlApplicationContext, loading the definitions
073: * from the given XML files and automatically refreshing the context.
074: * @param configLocations array of file paths
075: * @throws BeansException if context creation failed
076: */
077: public FileSystemXmlApplicationContext(String[] configLocations)
078: throws BeansException {
079: this (configLocations, null);
080: }
081:
082: /**
083: * Create a new FileSystemXmlApplicationContext with the given parent,
084: * loading the definitions from the given XML files and automatically
085: * refreshing the context.
086: * @param configLocations array of file paths
087: * @param parent the parent context
088: * @throws BeansException if context creation failed
089: */
090: public FileSystemXmlApplicationContext(String[] configLocations,
091: ApplicationContext parent) throws BeansException {
092:
093: super (parent);
094: this .configLocations = configLocations;
095: refresh();
096: }
097:
098: /**
099: * Create a new FileSystemXmlApplicationContext, loading the definitions
100: * from the given XML files.
101: * @param configLocations array of file paths
102: * @param refresh whether to automatically refresh the context,
103: * loading all bean definitions and creating all singletons.
104: * Alternatively, call refresh manually after further configuring the context.
105: * @throws BeansException if context creation failed
106: * @see #refresh()
107: */
108: public FileSystemXmlApplicationContext(String[] configLocations,
109: boolean refresh) throws BeansException {
110: this (configLocations, refresh, null);
111: }
112:
113: /**
114: * Create a new FileSystemXmlApplicationContext with the given parent,
115: * loading the definitions from the given XML files.
116: * @param configLocations array of file paths
117: * @param refresh whether to automatically refresh the context,
118: * loading all bean definitions and creating all singletons.
119: * Alternatively, call refresh manually after further configuring the context.
120: * @param parent the parent context
121: * @throws BeansException if context creation failed
122: * @see #refresh()
123: */
124: public FileSystemXmlApplicationContext(String[] configLocations,
125: boolean refresh, ApplicationContext parent)
126: throws BeansException {
127:
128: super (parent);
129: this .configLocations = configLocations;
130: if (refresh) {
131: refresh();
132: }
133: }
134:
135: protected String[] getConfigLocations() {
136: return this .configLocations;
137: }
138:
139: /**
140: * Resolve resource paths as file system paths.
141: * <p>Note: Even if a given path starts with a slash, it will get
142: * interpreted as relative to the current VM working directory.
143: * This is consistent with the semantics in a Servlet container.
144: * @param path path to the resource
145: * @return Resource handle
146: * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath
147: */
148: protected Resource getResourceByPath(String path) {
149: if (path != null && path.startsWith("/")) {
150: path = path.substring(1);
151: }
152: return new FileSystemResource(path);
153: }
154:
155: }
|