001: /* Copyright 2005 Sun Microsystems, Inc. All
002: * rights reserved. Use of this product is subject
003: * to license terms. Federal Acquisitions:
004: * Commercial Software -- Government Users
005: * Subject to Standard License Terms and
006: * Conditions.
007: *
008: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
009: * are trademarks or registered trademarks of Sun Microsystems,
010: * Inc. in the United States and other countries.
011: */
012: package com.sun.portal.taglibs.i18n;
013:
014: import java.util.logging.Level;
015: import java.util.logging.Logger;
016: import java.util.Locale;
017: import java.util.ResourceBundle;
018: import java.util.Properties;
019: import java.io.IOException;
020:
021: import javax.servlet.jsp.*;
022: import javax.servlet.jsp.PageContext;
023: import javax.servlet.jsp.tagext.*;
024:
025: import com.sun.portal.log.common.PortalLogger;
026: import com.sun.portal.providers.context.ProviderContext;
027: import com.sun.portal.providers.context.ProviderContextException;
028: import com.sun.portal.desktop.taglib.providerContext.BaseProviderContextTagSupport;
029: import com.sun.portal.desktop.context.ProviderClassLoader;
030: import com.sun.portal.desktop.context.DesktopContext;
031: import com.sun.portal.desktop.context.DesktopContextThreadLocalizer;
032:
033: /**
034: * MessageCatalog - Loads a resource bundle for a given locale
035: *
036: * @author Shanmugam Senthil
037: * @author Robert O'Brien
038: * @version 1.0
039: */
040: public class MessageCatalogTag extends BaseProviderContextTagSupport {
041:
042: public MessageCatalogTag() {
043: super ();
044: }
045:
046: /**
047: * Make the message catalog available
048: *
049: * @return EVAL_BODY_INCLUDE
050: * @exception JspException
051: */
052: public int doStartTag() throws JspException {
053: if (baseName != null) {
054: catalog = (ResourceBundle) pageContext
055: .findAttribute(baseName);
056: if (catalog != null) {
057: return EVAL_BODY_INCLUDE;
058: }
059: } else {
060: logger.log(Level.WARNING, "PSTL_CSPTI0002");
061: throw new JspException(
062: "In setBundle Tag 'baseName' attribute is missing");
063: }
064:
065: catalog = getRB();
066: String id = getVar();
067: if (id != null) {
068: pageContext.setAttribute(id, catalog);
069: }
070: return EVAL_BODY_INCLUDE;
071: }
072:
073: public void setBaseName(String baseName) {
074: this .baseName = baseName;
075: }
076:
077: public String getBaseName() {
078: return baseName;
079: }
080:
081: public void setVar(String var) {
082: this .var = var;
083: }
084:
085: public String getVar() {
086: return var;
087: }
088:
089: /**
090: * Get the ResourceBundle
091: *
092: * @return
093: */
094: private ResourceBundle getRB() {
095: ResourceBundle catlg = null;
096: ProviderContext pc = null;
097:
098: try {
099: if (getCurrentObjType() != UNDEFINED) {
100: pc = getCurrentProviderContext();
101: }
102:
103: if ((pc != null) && providerClassDir == null) {
104: DesktopContext dc = DesktopContextThreadLocalizer.get();
105: providerClassDir = dc.getProviderClassBaseDir();
106: logger.log(Level.FINEST, "PSTL_CSPTI0011",
107: providerClassDir);
108: }
109: } catch (com.sun.portal.desktop.taglib.DesktopTaglibException e) {
110: logger.log(Level.INFO, "PSTL_CSPTI0007", e);
111: pc = null;
112: } catch (Exception e) {
113: logger.log(Level.INFO, "PSTL_CSPTI0007", e);
114: pc = null;
115: }
116: Locale loc = getLocale(pc);
117: if ((pc != null) && (providerClassDir != null)) {
118: catlg = ResourceBundle.getBundle(baseName, loc,
119: ProviderClassLoader.getInstance(providerClassDir));
120: } else {
121: catlg = ResourceBundle.getBundle(baseName, loc);
122: }
123: return catlg;
124: }
125:
126: /**
127: * Get the user's locale
128: *
129: * @return
130: */
131: private Locale getLocale(ProviderContext pc) {
132: Locale loc = null;
133:
134: if (pc != null) {
135: loc = pc.getLocale();
136: logger.log(Level.FINE, "PSTL_CSPTI0008", loc);
137: } else {
138: logger.log(Level.FINE, "Getting the locale");
139: /* Get Locale form HTTPServletRequest */
140: loc = pageContext.getRequest().getLocale();
141: logger.log(Level.FINE, "PSTL_CSPTI0008", loc);
142: }
143:
144: if (loc == null) {
145: loc = Locale.getDefault();
146: logger.log(Level.WARNING, "PSTL_CSPTI0009", loc);
147: }
148: return loc;
149:
150: }
151:
152: /**
153: * Cleanup
154: */
155: public void release() {
156: super .release();
157: baseName = null;
158: catalog = null;
159: var = null;
160: }
161:
162: public ResourceBundle getResourceBundle() {
163: return catalog;
164: }
165:
166: private String baseName;
167: private ResourceBundle catalog;
168: private String var;
169: private static String providerClassDir = null;
170:
171: private static Logger logger = PortalLogger
172: .getLogger(MessageCatalogTag.class);
173:
174: }
|