KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > support > FileSystemXmlApplicationContext


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.context.support;
18
19 import org.springframework.beans.BeansException;
20 import org.springframework.context.ApplicationContext;
21 import org.springframework.core.io.FileSystemResource;
22 import org.springframework.core.io.Resource;
23
24 /**
25  * Standalone XML application context, taking the context definition files
26  * from the file system or from URLs, interpreting plain paths as relative
27  * file system locations (e.g. "mydir/myfile.txt"). Useful for test harnesses
28  * as well as for standalone environments.
29  *
30  * <p><b>NOTE:</b> Plain paths will always be interpreted as relative
31  * to the current VM working directory, even if they start with a slash.
32  * (This is consistent with the semantics in a Servlet container.)
33  * <b>Use an explicit "file:" prefix to enforce an absolute file path.</b>
34  *
35  * <p>The config location defaults can be overridden via {@link #getConfigLocations},
36  * Config locations can either denote concrete files like "/myfiles/context.xml"
37  * or Ant-style patterns like "/myfiles/*-context.xml" (see the
38  * {@link org.springframework.util.AntPathMatcher} javadoc for pattern details).
39  *
40  * <p>Note: In case of multiple config locations, later bean definitions will
41  * override ones defined in earlier loaded files. This can be leveraged to
42  * deliberately override certain bean definitions via an extra XML file.
43  *
44  * <p><b>This is a simple, one-stop shop convenience ApplicationContext.
45  * Consider using the {@link GenericApplicationContext} class in combination
46  * with an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}
47  * for more flexible context setup.</b>
48  *
49  * @author Rod Johnson
50  * @author Juergen Hoeller
51  * @see #getResource
52  * @see #getResourceByPath
53  * @see GenericApplicationContext
54  */

55 public class FileSystemXmlApplicationContext extends AbstractXmlApplicationContext {
56
57     private String JavaDoc[] configLocations;
58
59
60     /**
61      * Create a new FileSystemXmlApplicationContext, loading the definitions
62      * from the given XML file and automatically refreshing the context.
63      * @param configLocation file path
64      * @throws BeansException if context creation failed
65      */

66     public FileSystemXmlApplicationContext(String JavaDoc configLocation) throws BeansException {
67         this(new String JavaDoc[] {configLocation});
68     }
69
70     /**
71      * Create a new FileSystemXmlApplicationContext, loading the definitions
72      * from the given XML files and automatically refreshing the context.
73      * @param configLocations array of file paths
74      * @throws BeansException if context creation failed
75      */

76     public FileSystemXmlApplicationContext(String JavaDoc[] configLocations) throws BeansException {
77         this(configLocations, null);
78     }
79
80     /**
81      * Create a new FileSystemXmlApplicationContext with the given parent,
82      * loading the definitions from the given XML files and automatically
83      * refreshing the context.
84      * @param configLocations array of file paths
85      * @param parent the parent context
86      * @throws BeansException if context creation failed
87      */

88     public FileSystemXmlApplicationContext(String JavaDoc[] configLocations, ApplicationContext parent)
89             throws BeansException {
90
91         super(parent);
92         this.configLocations = configLocations;
93         refresh();
94     }
95
96
97     /**
98      * Create a new FileSystemXmlApplicationContext, loading the definitions
99      * from the given XML files.
100      * @param configLocations array of file paths
101      * @param refresh whether to automatically refresh the context,
102      * loading all bean definitions and creating all singletons.
103      * Alternatively, call refresh manually after further configuring the context.
104      * @throws BeansException if context creation failed
105      * @see #refresh()
106      */

107     public FileSystemXmlApplicationContext(String JavaDoc[] configLocations, 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 JavaDoc[] configLocations, boolean refresh, ApplicationContext parent)
123             throws BeansException {
124
125         super(parent);
126         this.configLocations = configLocations;
127         if (refresh) {
128             refresh();
129         }
130     }
131
132
133     protected String JavaDoc[] getConfigLocations() {
134         return this.configLocations;
135     }
136
137     /**
138      * Resolve resource paths as file system paths.
139      * <p>Note: Even if a given path starts with a slash, it will get
140      * interpreted as relative to the current VM working directory.
141      * This is consistent with the semantics in a Servlet container.
142      * @param path path to the resource
143      * @return Resource handle
144      * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath
145      */

146     protected Resource getResourceByPath(String JavaDoc path) {
147         if (path != null && path.startsWith("/")) {
148             path = path.substring(1);
149         }
150         return new FileSystemResource(path);
151     }
152
153 }
154
Popular Tags