Source Code Cross Referenced for LocalizedResourceManager.java in  » Portal » Open-Portal » com » sun » portal » app » calendarcommon » common » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Open Portal » com.sun.portal.app.calendarcommon.common 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.calendarcommon.common;
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.