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: import org.springframework.util.StringUtils;
024:
025: /**
026: * Standalone XML application context, taking the context definition files
027: * from the file system or from URLs, interpreting plain paths as relative
028: * file system locations (e.g. "mydir/myfile.txt"). Useful for test harnesses
029: * as well as for standalone environments.
030: *
031: * <p><b>NOTE:</b> Plain paths will always be interpreted as relative
032: * to the current VM working directory, even if they start with a slash.
033: * (This is consistent with the semantics in a Servlet container.)
034: * <b>Use an explicit "file:" prefix to enforce an absolute file path.</b>
035: *
036: * <p>The config location defaults can be overridden via {@link #getConfigLocations},
037: * Config locations can either denote concrete files like "/myfiles/context.xml"
038: * or Ant-style patterns like "/myfiles/*-context.xml" (see the
039: * {@link org.springframework.util.AntPathMatcher} javadoc for pattern details).
040: *
041: * <p>Note: In case of multiple config locations, later bean definitions will
042: * override ones defined in earlier loaded files. This can be leveraged to
043: * deliberately override certain bean definitions via an extra XML file.
044: *
045: * <p><b>This is a simple, one-stop shop convenience ApplicationContext.
046: * Consider using the {@link GenericApplicationContext} class in combination
047: * with an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}
048: * for more flexible context setup.</b>
049: *
050: * @author Rod Johnson
051: * @author Juergen Hoeller
052: * @see #getResource
053: * @see #getResourceByPath
054: * @see GenericApplicationContext
055: */
056: public class FileSystemXmlApplicationContext extends
057: AbstractXmlApplicationContext {
058:
059: private String[] configLocations;
060:
061: /**
062: * Create a new FileSystemXmlApplicationContext, loading the definitions
063: * from the given XML file and automatically refreshing the context.
064: * @param configLocation file path
065: * @throws BeansException if context creation failed
066: */
067: public FileSystemXmlApplicationContext(String configLocation)
068: throws BeansException {
069: this (new String[] { configLocation }, true, null);
070: }
071:
072: /**
073: * Create a new FileSystemXmlApplicationContext, loading the definitions
074: * from the given XML files and automatically refreshing the context.
075: * @param configLocations array of file paths
076: * @throws BeansException if context creation failed
077: */
078: public FileSystemXmlApplicationContext(String[] configLocations)
079: throws BeansException {
080: this (configLocations, true, null);
081: }
082:
083: /**
084: * Create a new FileSystemXmlApplicationContext with the given parent,
085: * loading the definitions from the given XML files and automatically
086: * refreshing the context.
087: * @param configLocations array of file paths
088: * @param parent the parent context
089: * @throws BeansException if context creation failed
090: */
091: public FileSystemXmlApplicationContext(String[] configLocations,
092: ApplicationContext parent) throws BeansException {
093: this (configLocations, true, parent);
094: }
095:
096: /**
097: * Create a new FileSystemXmlApplicationContext, loading the definitions
098: * from the given XML files.
099: * @param configLocations array of file paths
100: * @param refresh whether to automatically refresh the context,
101: * loading all bean definitions and creating all singletons.
102: * Alternatively, call refresh manually after further configuring the context.
103: * @throws BeansException if context creation failed
104: * @see #refresh()
105: */
106: public FileSystemXmlApplicationContext(String[] configLocations,
107: boolean refresh) throws BeansException {
108: this (configLocations, refresh, null);
109: }
110:
111: /**
112: * Create a new FileSystemXmlApplicationContext with the given parent,
113: * loading the definitions from the given XML files.
114: * @param configLocations array of file paths
115: * @param refresh whether to automatically refresh the context,
116: * loading all bean definitions and creating all singletons.
117: * Alternatively, call refresh manually after further configuring the context.
118: * @param parent the parent context
119: * @throws BeansException if context creation failed
120: * @see #refresh()
121: */
122: public FileSystemXmlApplicationContext(String[] configLocations,
123: boolean refresh, ApplicationContext parent)
124: throws BeansException {
125:
126: super (parent);
127: this .configLocations = StringUtils
128: .trimArrayElements(configLocations);
129: if (refresh) {
130: refresh();
131: }
132: }
133:
134: protected String[] getConfigLocations() {
135: return this .configLocations;
136: }
137:
138: /**
139: * Resolve resource paths as file system paths.
140: * <p>Note: Even if a given path starts with a slash, it will get
141: * interpreted as relative to the current VM working directory.
142: * This is consistent with the semantics in a Servlet container.
143: * @param path path to the resource
144: * @return Resource handle
145: * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath
146: */
147: protected Resource getResourceByPath(String path) {
148: if (path != null && path.startsWith("/")) {
149: path = path.substring(1);
150: }
151: return new FileSystemResource(path);
152: }
153:
154: }
|