001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.struts;
022:
023: import com.liferay.portal.kernel.util.StringUtil;
024:
025: import java.io.InputStream;
026:
027: import java.net.URL;
028:
029: import java.util.Enumeration;
030: import java.util.Map;
031: import java.util.Properties;
032:
033: import javax.servlet.ServletContext;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037: import org.apache.struts.util.MessageResourcesFactory;
038: import org.apache.struts.util.PropertyMessageResources;
039:
040: /**
041: * <a href="MultiMessageResources.java.html"><b><i>View Source</i></b></a>
042: *
043: * @author Brian Wing Shun Chan
044: *
045: */
046: public class MultiMessageResources extends PropertyMessageResources {
047:
048: public MultiMessageResources(MessageResourcesFactory factory,
049: String config) {
050:
051: super (factory, config);
052: }
053:
054: public MultiMessageResources(MessageResourcesFactory factory,
055: String config, boolean returnNull) {
056:
057: super (factory, config, returnNull);
058: }
059:
060: public Map getMessages() {
061: return messages;
062: }
063:
064: public void setServletContext(ServletContext servletContext) {
065: _servletContext = servletContext;
066: }
067:
068: protected void loadLocale(String localeKey) {
069: synchronized (locales) {
070: if (locales.get(localeKey) != null) {
071: return;
072: }
073:
074: locales.put(localeKey, localeKey);
075: }
076:
077: String[] names = StringUtil.split(config.replace('.', '/'));
078:
079: for (int i = 0; i < names.length; i++) {
080: String name = names[i];
081:
082: if (localeKey.length() > 0) {
083: name += "_" + localeKey;
084: }
085:
086: name += ".properties";
087:
088: _loadProps(name, localeKey, false);
089: }
090:
091: for (int i = 0; i < names.length; i++) {
092: String name = names[i];
093:
094: if (localeKey.length() > 0) {
095: name += "_" + localeKey;
096: }
097:
098: name += ".properties";
099:
100: _loadProps(name, localeKey, true);
101: }
102: }
103:
104: private void _loadProps(String name, String localeKey,
105: boolean useServletContext) {
106:
107: Properties props = new Properties();
108:
109: try {
110: URL url = null;
111:
112: if (useServletContext) {
113: url = _servletContext.getResource("/WEB-INF/" + name);
114: } else {
115: ClassLoader classLoader = getClass().getClassLoader();
116:
117: url = classLoader.getResource(name);
118: }
119:
120: if (_log.isInfoEnabled()) {
121: _log.info("Attempting to load " + name + " "
122: + localeKey + " " + useServletContext);
123: }
124:
125: if (url != null) {
126: InputStream is = url.openStream();
127:
128: props.load(is);
129:
130: is.close();
131:
132: if (_log.isInfoEnabled()) {
133: _log.info("Loading " + url + " with "
134: + props.size() + " values");
135: }
136: }
137: } catch (Exception e) {
138: _log.warn(e);
139: }
140:
141: if (props.size() < 1) {
142: return;
143: }
144:
145: synchronized (messages) {
146: Enumeration names = props.keys();
147:
148: while (names.hasMoreElements()) {
149: String key = (String) names.nextElement();
150:
151: messages.put(messageKey(localeKey, key), props
152: .getProperty(key));
153: }
154: }
155: }
156:
157: private static Log _log = LogFactory
158: .getLog(MultiMessageResources.class);
159:
160: private transient ServletContext _servletContext;
161:
162: }
|