1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.struts;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.StringUtil;
25  
26  import java.io.InputStream;
27  
28  import java.net.URL;
29  
30  import java.util.Enumeration;
31  import java.util.Map;
32  import java.util.Properties;
33  
34  import javax.servlet.ServletContext;
35  
36  import org.apache.struts.util.MessageResourcesFactory;
37  import org.apache.struts.util.PropertyMessageResources;
38  
39  /**
40   * <a href="MultiMessageResources.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class MultiMessageResources extends PropertyMessageResources {
46  
47      public MultiMessageResources(
48          MessageResourcesFactory factory, String config) {
49  
50          super(factory, config);
51      }
52  
53      public MultiMessageResources(
54          MessageResourcesFactory factory, String config, boolean returnNull) {
55  
56          super(factory, config, returnNull);
57      }
58  
59      public Map<String, String> getMessages() {
60          return messages;
61      }
62  
63      public void setServletContext(ServletContext servletContext) {
64          _servletContext = servletContext;
65      }
66  
67      protected void loadLocale(String localeKey) {
68          synchronized (locales) {
69              if (locales.get(localeKey) != null) {
70                  return;
71              }
72  
73              locales.put(localeKey, localeKey);
74          }
75  
76          String[] names = StringUtil.split(config.replace('.', '/'));
77  
78          for (int i = 0; i < names.length; i++) {
79              String name = names[i];
80  
81              if (localeKey.length() > 0) {
82                  name += "_" + localeKey;
83              }
84  
85              name += ".properties";
86  
87              _loadProps(name, localeKey, false);
88          }
89  
90          for (int i = 0; i < names.length; i++) {
91              String name = names[i];
92  
93              if (localeKey.length() > 0) {
94                  name += "_" + localeKey;
95              }
96  
97              name += ".properties";
98  
99              _loadProps(name, localeKey, true);
100         }
101     }
102 
103     private void _loadProps(
104         String name, String localeKey, boolean useServletContext) {
105 
106         Properties props = new Properties();
107 
108         try {
109             URL url = null;
110 
111             if (useServletContext) {
112                 url = _servletContext.getResource("/WEB-INF/" + name);
113             }
114             else {
115                 ClassLoader classLoader = getClass().getClassLoader();
116 
117                 url = classLoader.getResource(name);
118             }
119 
120             if (_log.isInfoEnabled()) {
121                 _log.info(
122                     "Attempting to load " + name + " " + localeKey + " " +
123                         useServletContext);
124             }
125 
126             if (url != null) {
127                 InputStream is = url.openStream();
128 
129                 props.load(is);
130 
131                 is.close();
132 
133                 if (_log.isInfoEnabled()) {
134                     _log.info(
135                         "Loading " + url + " with " + props.size() + " values");
136                 }
137             }
138         }
139         catch (Exception e) {
140             _log.warn(e);
141         }
142 
143         if (props.size() < 1) {
144             return;
145         }
146 
147         synchronized (messages) {
148             Enumeration<Object> names = props.keys();
149 
150             while (names.hasMoreElements()) {
151                 String key = (String)names.nextElement();
152 
153                 messages.put(
154                     messageKey(localeKey, key), props.getProperty(key));
155             }
156         }
157     }
158 
159     private static Log _log =
160          LogFactoryUtil.getLog(MultiMessageResources.class);
161 
162     private transient ServletContext _servletContext;
163 
164 }