1   /**
2    * Copyright (c) 2000-2007 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.util.StringUtil;
26  
27  import java.io.InputStream;
28  
29  import java.net.URL;
30  
31  import java.util.Enumeration;
32  import java.util.Map;
33  import java.util.Properties;
34  
35  import javax.servlet.ServletContext;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  import org.apache.struts.util.MessageResourcesFactory;
40  import org.apache.struts.util.PropertyMessageResources;
41  
42  /**
43   * <a href="MultiMessageResources.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class MultiMessageResources extends PropertyMessageResources {
49  
50      public MultiMessageResources(MessageResourcesFactory factory,
51                                   String config) {
52  
53          super(factory, config);
54      }
55  
56      public MultiMessageResources(MessageResourcesFactory factory,
57                                   String config, boolean returnNull) {
58  
59          super(factory, config, returnNull);
60      }
61  
62      public Map getMessages() {
63          return messages;
64      }
65  
66      public void setServletContext(ServletContext servletContext) {
67          _servletContext = servletContext;
68      }
69  
70      protected void loadLocale(String localeKey) {
71          synchronized (locales) {
72              if (locales.get(localeKey) != null) {
73                  return;
74              }
75  
76              locales.put(localeKey, localeKey);
77          }
78  
79          String[] names = StringUtil.split(config.replace('.', '/'));
80  
81          for (int i = 0; i < names.length; i++) {
82              String name = names[i];
83  
84              if (localeKey.length() > 0) {
85                  name += "_" + localeKey;
86              }
87  
88              name += ".properties";
89  
90              _loadProps(name, localeKey, false);
91          }
92  
93          for (int i = 0; i < names.length; i++) {
94              String name = names[i];
95  
96              if (localeKey.length() > 0) {
97                  name += "_" + localeKey;
98              }
99  
100             name += ".properties";
101 
102             _loadProps(name, localeKey, true);
103         }
104     }
105 
106     private void _loadProps(
107         String name, String localeKey, boolean useServletContext) {
108 
109         Properties props = new Properties();
110 
111         try {
112             URL url = null;
113 
114             if (useServletContext) {
115                 url = _servletContext.getResource("/WEB-INF/" + name);
116             }
117             else {
118                 ClassLoader classLoader = getClass().getClassLoader();
119 
120                 url = classLoader.getResource(name);
121             }
122 
123             if (_log.isInfoEnabled()) {
124                 _log.info(
125                     "Attempting to load " + name + " " + localeKey + " " +
126                         useServletContext);
127             }
128 
129             if (url != null) {
130                 InputStream is = url.openStream();
131 
132                 props.load(is);
133 
134                 is.close();
135 
136                 if (_log.isInfoEnabled()) {
137                     _log.info(
138                         "Loading " + url + " with " + props.size() + " values");
139                 }
140             }
141         }
142         catch (Exception e) {
143             _log.warn(e);
144         }
145 
146         if (props.size() < 1) {
147             return;
148         }
149 
150         synchronized (messages) {
151             Enumeration names = props.keys();
152 
153             while (names.hasMoreElements()) {
154                 String key = (String)names.nextElement();
155 
156                 messages.put(
157                     messageKey(localeKey, key), props.getProperty(key));
158             }
159         }
160     }
161 
162     private static Log _log = LogFactory.getLog(MultiMessageResources.class);
163 
164     private transient ServletContext _servletContext;
165 
166 }