1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.language;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  
19  import java.util.Locale;
20  import java.util.Map;
21  import java.util.concurrent.ConcurrentHashMap;
22  
23  import org.apache.struts.util.MessageResources;
24  
25  /**
26   * <a href="LanguageResources.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class LanguageResources {
31  
32      public static void clearCache() {
33          _instance._clearCache();
34      }
35  
36      public static String getMessage(Locale locale, String key) {
37          return _instance._getMessage(locale, key);
38      }
39  
40      public static void init(MessageResources messageResources) {
41          _instance._init(messageResources);
42      }
43  
44      public static boolean isInitialized() {
45          if (_instance._messageResources == null) {
46              return true;
47          }
48          else {
49              return false;
50          }
51      }
52  
53      private LanguageResources() {
54          _cache = new ConcurrentHashMap<String, ResourceValue>(10000);
55      }
56  
57      private void _clearCache() {
58          _cache.clear();
59      }
60  
61      private String _getCacheKey(Locale locale, String key) {
62          return String.valueOf(locale).concat(StringPool.POUND).concat(key);
63      }
64  
65      private String _getMessage(Locale locale, String key) {
66          String cacheKey = _getCacheKey(locale, key);
67  
68          ResourceValue resourceValue = _cache.get(cacheKey);
69  
70          if (resourceValue == null) {
71              String value = _messageResources.getMessage(locale, key);
72  
73              resourceValue = new ResourceValue(value);
74  
75              _cache.put(cacheKey, resourceValue);
76          }
77  
78          return resourceValue.getValue();
79      }
80  
81      private void _init(MessageResources messageResources) {
82          _messageResources = messageResources;
83      }
84  
85      private class ResourceValue {
86  
87          private ResourceValue(String value) {
88              _value = value;
89          }
90  
91          public String getValue() {
92              return _value;
93          }
94  
95          private String _value;
96  
97      }
98  
99      private static LanguageResources _instance = new LanguageResources();
100 
101     private Map<String, ResourceValue> _cache;
102     private MessageResources _messageResources;
103 
104 }