1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.struts;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.LocaleUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  
30  import java.io.InputStream;
31  
32  import java.net.URL;
33  
34  import java.util.Enumeration;
35  import java.util.Map;
36  import java.util.Properties;
37  
38  import javax.servlet.ServletContext;
39  
40  import org.apache.struts.util.MessageResourcesFactory;
41  import org.apache.struts.util.PropertyMessageResources;
42  
43  /**
44   * <a href="MultiMessageResources.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Bruno Farache
48   *
49   */
50  public class MultiMessageResources extends PropertyMessageResources {
51  
52      public MultiMessageResources(
53          MessageResourcesFactory factory, String config) {
54  
55          super(factory, config);
56      }
57  
58      public MultiMessageResources(
59          MessageResourcesFactory factory, String config, boolean returnNull) {
60  
61          super(factory, config, returnNull);
62      }
63  
64      public Map<String, String> getMessages() {
65          return messages;
66      }
67  
68      public void putLocale(String localeKey) {
69          synchronized (locales) {
70              locales.put(localeKey, localeKey);
71          }
72      }
73  
74      public Properties putMessages(Properties props, String localeKey) {
75          Properties oldProps = new Properties();
76  
77          if (props.size() < 1) {
78              return oldProps;
79          }
80  
81          synchronized (messages) {
82              Enumeration<Object> names = props.keys();
83  
84              while (names.hasMoreElements()) {
85                  String key = (String)names.nextElement();
86  
87                  String message = getMessage(
88                      LocaleUtil.fromLanguageId(localeKey), key);
89  
90                  if (message != null) {
91                      oldProps.put(key, message);
92                  }
93  
94                  messages.put(
95                      messageKey(localeKey, key), props.getProperty(key));
96              }
97          }
98  
99          return oldProps;
100     }
101 
102     public void setServletContext(ServletContext servletContext) {
103         _servletContext = servletContext;
104     }
105 
106     public void loadLocale(String localeKey) {
107         synchronized (locales) {
108             if (locales.get(localeKey) != null) {
109                 return;
110             }
111 
112             putLocale(localeKey);
113         }
114 
115         String[] names = StringUtil.split(config.replace('.', '/'));
116 
117         for (int i = 0; i < names.length; i++) {
118             String name = names[i];
119 
120             if (localeKey.length() > 0) {
121                 name += "_" + localeKey;
122             }
123 
124             name += ".properties";
125 
126             _loadProps(name, localeKey, false);
127         }
128 
129         for (int i = 0; i < names.length; i++) {
130             String name = names[i];
131 
132             if (localeKey.length() > 0) {
133                 name += "_" + localeKey;
134             }
135 
136             name += ".properties";
137 
138             _loadProps(name, localeKey, true);
139         }
140     }
141 
142     private void _loadProps(
143         String name, String localeKey, boolean useServletContext) {
144 
145         Properties props = new Properties();
146 
147         try {
148             URL url = null;
149 
150             if (useServletContext) {
151                 url = _servletContext.getResource("/WEB-INF/" + name);
152             }
153             else {
154                 ClassLoader classLoader = getClass().getClassLoader();
155 
156                 url = classLoader.getResource(name);
157             }
158 
159             if (_log.isInfoEnabled()) {
160                 _log.info(
161                     "Attempting to load " + name + " " + localeKey + " " +
162                         useServletContext);
163             }
164 
165             if (url != null) {
166                 InputStream is = url.openStream();
167 
168                 props.load(is);
169 
170                 is.close();
171 
172                 if (_log.isInfoEnabled()) {
173                     _log.info(
174                         "Loading " + url + " with " + props.size() + " values");
175                 }
176             }
177         }
178         catch (Exception e) {
179             _log.warn(e);
180         }
181 
182         putMessages(props, localeKey);
183     }
184 
185     private static Log _log =
186          LogFactoryUtil.getLog(MultiMessageResources.class);
187 
188     private transient ServletContext _servletContext;
189 
190 }