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
018 package org.apache.commons.resources.impl;
019
020 import java.io.FileNotFoundException;
021 import java.io.IOException;
022 import java.io.InputStream;
023 import java.util.Locale;
024 import java.util.Map;
025 import java.util.Properties;
026
027 import javax.servlet.ServletContext;
028
029 import org.apache.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031
032 /**
033 * <p>Concrete implementation of {@link org.apache.commons.resources.Resources} that wraps a family
034 * (one per <code>Locale</code> of properties files that share a base
035 * context-relative path for servlet context resources, and have
036 * name suffixes reflecting the <code>Locale</code> for which
037 * the document's messages apply. Resources are looked up in a hierarchy
038 * of properties files in a manner identical to that performed by
039 * <code>java.util.ResourceBundle.getBundle().</code>.</p>
040 *
041 * <p>The base resource path passed to our constructor must contain the
042 * context-relative base name of the properties file family.
043 * For example, if the base path is passed as
044 * <code>http://localhost/foo/Bar</code>, the resources for the
045 * <code>en_US</code> Locale would be stored under URL
046 * <code>http://localhost/foo/Bar_en_US.properties</code>, and the default
047 * resources would be stored in
048 * <code>http://localhost/foo/Bar.properties</code>.</p>
049 */
050 public class WebappPropertyResources extends CollectionResourcesBase {
051
052 /**
053 * <p>The <code>Log</code> instance for this class.</p>
054 */
055 private transient Log log =
056 LogFactory.getLog(WebappPropertyResources.class);
057
058 // ----------------------------------------------------------- Constructors
059
060
061 /**
062 * <p>Create a new {@link org.apache.commons.resources.Resources} instance with the specified
063 * logical name and base resource URL.</p>
064 *
065 * @param name Logical name of the new instance
066 * @param base Base URL of the family of properties files that contain
067 * the resource keys and values
068 * @param servletContext the <code>ServletContext</code> instance
069 * to use for resolving resource references
070 */
071 public WebappPropertyResources(String name, String base,
072 ServletContext servletContext) {
073
074 super(name, base);
075 this.servletContext = servletContext;
076
077 }
078
079
080 // ----------------------------------------------------- Instance Variables
081
082
083 /**
084 * <p>The <code>ServletContext</code> instance for resolving
085 * our resources references.</p>
086 */
087 private ServletContext servletContext = null;
088
089
090 // ------------------------------------------------------ Protected Methods
091
092
093 /**
094 * <p>Return a <code>Map</code> containing the name-value mappings for
095 * the specified base URL and requested <code>Locale</code>, if there
096 * are any. If there are no defined mappings for the specified
097 * <code>Locale</code>, return an empty <code>Map</code> instead.</p>
098 *
099 * <p>Concrete subclasses must override this method to perform the
100 * appropriate lookup. A typical implementation will construct an
101 * absolute URL based on the specified base URL and <code>Locale</code>,
102 * retrieve the specified resource file (if any), and parse it into
103 * a <code>Map</code> structure.</p>
104 *
105 * <p>Caching of previously retrieved <code>Map</code>s (if any) should
106 * be performed by callers of this method. Therefore, this method should
107 * always attempt to retrieve the specified resource and load it
108 * appropriately.</p>
109 *
110 * @param baseUrl Base URL of the resource files for this {@link org.apache.commons.resources.Resources}
111 * instance
112 * @param locale <code>Locale</code> for which name-value mappings
113 * are requested
114 * @return A name-value Map for the specified URL and locale.
115 */
116 protected Map getLocaleMap(String baseUrl, Locale locale) {
117
118 if (getLog().isDebugEnabled()) {
119 getLog().debug("Loading locale '" + locale + "' resources from base '" +
120 baseUrl + "'");
121 }
122
123 Properties props = new Properties();
124 String name = baseUrl.replace('.', '/');
125 name += getLocaleSuffix(locale) + ".properties";
126
127 InputStream stream = null;
128
129 try {
130
131 // Open an input stream to the URL for this locale (if any)
132 if (getLog().isTraceEnabled()) {
133 getLog().trace("Complete path is '" + name + "'");
134 }
135 try{
136 stream = servletContext.getResourceAsStream(name);
137 }
138 catch(Exception e){
139 // not found
140 // try ContextClassLoader
141 }
142 if (stream == null){
143
144 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
145 if (classLoader == null) {
146 classLoader = this.getClass().getClassLoader();
147 }
148 stream = classLoader.getResourceAsStream(name);
149 }
150
151 // Parse the input stream and populate the name-value mappings map
152 if (stream != null) {
153 if (getLog().isTraceEnabled()) {
154 getLog().trace("Parsing input resource");
155 }
156 props.load(stream);
157 }
158
159 } catch (FileNotFoundException e) {
160
161 // Log and swallow this exception
162 if (getLog().isDebugEnabled()) {
163 getLog().debug("No resources for locale '" + locale +
164 "' from base '" + baseUrl + "'");
165 }
166 props.clear();
167
168 } catch (IOException e) {
169
170 getLog().warn("IOException loading locale '" + locale +
171 "' from base '" + baseUrl + "'", e);
172 props.clear();
173
174 } finally {
175
176 // Close the input stream that was opened earlier
177 if (stream != null) {
178 try {
179 stream.close();
180 } catch (IOException e) {
181 getLog().error("Error closing stream.", e);
182 }
183 stream = null;
184 }
185
186 }
187
188 // Return the populated (or empty) properties
189 return (props);
190
191 }
192
193 /**
194 * Accessor method for Log instance.
195 *
196 * The Log instance variable is transient and
197 * accessing it through this method ensures it
198 * is re-initialized when this instance is
199 * de-serialized.
200 *
201 * @return The Log instance.
202 */
203 private Log getLog() {
204 if (log == null) {
205 log = LogFactory.getLog(WebappPropertyResources.class);
206 }
207 return log;
208 }
209
210 }