1
14
15 package com.liferay.portal.language;
16
17 import com.liferay.portal.kernel.cache.key.CacheKeyGeneratorUtil;
18 import com.liferay.portal.kernel.util.ConcurrentHashSet;
19 import com.liferay.portal.kernel.util.StringPool;
20
21 import java.util.Locale;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.concurrent.ConcurrentHashMap;
25
26 import org.apache.struts.util.MessageResources;
27
28
33 public class LanguageResources {
34
35 public static void clearCache() {
36 _instance._clearCache();
37 }
38
39 public static String getMessage(Locale locale, String key) {
40 return _instance._getMessage(locale, key);
41 }
42
43 public static void init(MessageResources messageResources) {
44 _instance._init(messageResources);
45 }
46
47 public static boolean isInitialized() {
48 if (_instance._messageResources == null) {
49 return true;
50 }
51 else {
52 return false;
53 }
54 }
55
56 private LanguageResources() {
57 _cacheValues = new ConcurrentHashMap<String, String>(10000);
58 _nullCacheKeys = new ConcurrentHashSet<String>(10000);
59 }
60
61 private void _clearCache() {
62 _cacheValues.clear();
63 }
64
65 private String _getCacheKey(Locale locale, String key) {
66 return String.valueOf(locale).concat(StringPool.POUND).concat(
67 CacheKeyGeneratorUtil.getCacheKey(getClass().getName(), key));
68 }
69
70 private String _getMessage(Locale locale, String key) {
71 String cacheKey = _getCacheKey(locale, key);
72
73 String cacheValue = _cacheValues.get(cacheKey);
74
75 if (cacheValue == null) {
76 if (_nullCacheKeys.contains(cacheKey)) {
77 return null;
78 }
79
80 cacheValue = _messageResources.getMessage(locale, key);
81
82 if (cacheValue == null) {
83 _nullCacheKeys.add(cacheKey);
84
85 return null;
86 }
87
88 _cacheValues.put(cacheKey, cacheValue);
89 }
90
91 return cacheValue;
92 }
93
94 private void _init(MessageResources messageResources) {
95 _messageResources = messageResources;
96 }
97
98 private static LanguageResources _instance = new LanguageResources();
99
100 private Map<String, String> _cacheValues;
101 private Set<String> _nullCacheKeys;
102 private MessageResources _messageResources;
103
104 }