001: /**
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and iPlanet
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.app.collabcommon;
013:
014: import com.sun.portal.log.common.PortalLogger;
015: import java.util.HashMap;
016: import java.util.Locale;
017: import java.util.Map;
018: import java.util.logging.Level;
019: import java.util.logging.Logger;
020: import javax.portlet.PortletContext;
021: import javax.servlet.ServletContext;
022:
023: /**
024: * Retrieves resources from the Portal Server locale .
025: *
026: * @author Alejandro Abdelnur / Nigel Jacobs
027: * @created July 5, 2005
028: *
029: */
030: public class LocalizedResourceManager {
031:
032: private static Logger logger = PortalLogger
033: .getLogger(LocalizedResourceManager.class);
034:
035: // Localized resources cache on Servlet / Porlet context
036: private static final String LOCALIZED_RESOURCES_CONTEXT_ATTR = "com.sun.portal.LocalizedResources";
037:
038: public static String getLocalizedFile(ServletContext context,
039: String filePath, Locale locale) {
040: return getLocalizedFile((Object) context, filePath, locale);
041: }
042:
043: public static String getLocalizedFile(PortletContext context,
044: String filePath, Locale locale) {
045: return getLocalizedFile((Object) context, filePath, locale);
046: }
047:
048: private static final Object GATE = new Object();
049:
050: /**
051: * Get an existing localized file.
052: *
053: * @param context PortletContext or ServletContext
054: */
055: private static String getLocalizedFile(Object context,
056: String filePath, Locale locale) {
057:
058: locale = (locale != null) ? locale : Locale.getDefault();
059: boolean found = false;
060: String key = filePath + "_" + locale.toString();
061: String realPath = null;
062: Map localizedResources;
063:
064: synchronized (GATE) {
065: if (context instanceof ServletContext) {
066: localizedResources = (Map) ((ServletContext) context)
067: .getAttribute(LOCALIZED_RESOURCES_CONTEXT_ATTR);
068: } else {
069: localizedResources = (Map) ((PortletContext) context)
070: .getAttribute(LOCALIZED_RESOURCES_CONTEXT_ATTR);
071: }
072: if (localizedResources == null) {
073: localizedResources = new HashMap();
074: if (context instanceof ServletContext) {
075: ((ServletContext) context).setAttribute(
076: LOCALIZED_RESOURCES_CONTEXT_ATTR,
077: localizedResources);
078: } else {
079: ((PortletContext) context).setAttribute(
080: LOCALIZED_RESOURCES_CONTEXT_ATTR,
081: localizedResources);
082: }
083: } else {
084: found = localizedResources.containsKey(key);
085: if (found) {
086: realPath = (String) localizedResources.get(key);
087: if (logger.isLoggable(Level.FINER)) {
088: logger.finer("cached - filePath: " + filePath
089: + ", loc: " + locale + " => "
090: + realPath);
091: }
092: }
093: }
094: }
095:
096: if (!found) {
097: realPath = getMostSpecificLocalizedFile(context, filePath,
098: locale);
099: if (logger.isLoggable(Level.FINER)) {
100: logger.finer("not cached - filePath: " + filePath
101: + ", loc: " + locale + " => " + realPath);
102: }
103: if (realPath != null) {
104: synchronized (GATE) {
105: localizedResources.put(key, realPath);
106: }
107: }
108: }
109:
110: return realPath;
111:
112: }
113:
114: /**
115: * Find the most specific existing localized file.
116: *
117: * @param context PortletContext or ServletContext
118: */
119: private static String getMostSpecificLocalizedFile(Object context,
120: String filePath, Locale locale) {
121:
122: int separator = filePath.lastIndexOf(".");
123: String filePathWoExtension = (separator > -1) ? filePath
124: .substring(0, separator) : filePath;
125: String fileExtension = (separator > -1) ? filePath
126: .substring(separator + 1) : null;
127: String language = (locale != null) ? locale.getLanguage()
128: : null;
129: String country = (locale != null) ? locale.getCountry() : null;
130: String variant = (locale != null) ? locale.getVariant() : null;
131: if ("".equals(language))
132: language = null;
133: if ("".equals(country))
134: country = null;
135: if ("".equals(variant))
136: variant = null;
137:
138: String realPath = getParticularLocalizedFile(context,
139: filePathWoExtension, fileExtension, language, country,
140: variant);
141: if (realPath != null)
142: return realPath;
143:
144: if (variant != null) {
145: realPath = getParticularLocalizedFile(context,
146: filePathWoExtension, fileExtension, language,
147: country, null);
148: if (realPath != null)
149: return realPath;
150: }
151:
152: if (country != null) {
153: realPath = getParticularLocalizedFile(context,
154: filePathWoExtension, fileExtension, language, null,
155: null);
156: if (realPath != null)
157: return realPath;
158: }
159:
160: realPath = getParticularLocalizedFile(context,
161: filePathWoExtension, fileExtension, null, null, null);
162: if (realPath != null)
163: return realPath;
164:
165: logger.warning("Can't find localized file with file path: "
166: + filePath + " with locale: " + locale);
167: return null;
168:
169: }
170:
171: /**
172: * Get a particular localized file from filePath w/o ext. / extension / lang /country / variant.
173: *
174: * Compute the localized path from the file path without extension, as follows:
175: *
176: * filePathWoExtension_language_country_variant
177: *
178: * and only return this if the resource exists, and null otherwise.
179: *
180: * @param context PortletContext or ServletContext
181: */
182: private static String getParticularLocalizedFile(Object context,
183: String filePathWoExtension, String extension,
184: String language, String country, String variant) {
185:
186: StringBuffer sb = new StringBuffer();
187: sb.append(filePathWoExtension);
188: if (language != null) {
189: sb.append("_").append(language);
190: if (country != null) {
191: sb.append("_").append(country);
192: if (variant != null) {
193: sb.append("_").append(variant);
194: }
195: }
196: }
197:
198: if (extension != null) {
199: sb.append(".").append(extension);
200: }
201:
202: String path = sb.toString();
203: String realPath = null;
204: if (context instanceof ServletContext) {
205: realPath = (((ServletContext) context)
206: .getResourceAsStream(path) != null) ? path : null;
207: } else {
208: realPath = (((PortletContext) context)
209: .getResourceAsStream(path) != null) ? path : null;
210: }
211:
212: if (logger.isLoggable(Level.FINER)) {
213: logger.finer("FP: " + filePathWoExtension + ", ext: "
214: + extension + ", lang: " + language + ", cntry: "
215: + country + ", var: " + variant + ", path: " + path
216: + " => " + realPath);
217: }
218:
219: return realPath;
220:
221: }
222:
223: }
|