001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.providers.userinfo;
007:
008: import com.iplanet.sso.SSOToken;
009: import com.iplanet.sso.SSOException;
010: import com.iplanet.sso.SSOTokenManager;
011:
012: import com.sun.portal.providers.userinfo.tag.ReadTag;
013: import com.sun.portal.providers.userinfo.tag.TagException;
014: import com.sun.portal.providers.userinfo.tag.TagWrapperException;
015: import com.sun.portal.providers.userinfo.tag.UnhandledTagException;
016: import com.sun.portal.providers.context.ProviderContext;
017: import com.sun.portal.providers.context.ProviderContextException;
018: import com.sun.portal.desktop.util.I18n;
019: import com.sun.portal.log.common.PortalLogger;
020:
021: import javax.servlet.http.HttpServletRequest;
022:
023: import java.util.*;
024: import java.util.logging.Logger;
025: import java.util.logging.Level;
026:
027: /**
028: * This class provides read access to the following keys:<br>
029: * currentDate = the current date, "August 21, 2000 4:53:42 PM PDT"<br>
030: * timeLeft = time left in the current session, in minutes; "34"<br>
031: * maxIdleTime = max idle time for the session, in minutes; "10"<br>
032: * This class does not implement WriteTag and does not support writing tags.
033: */
034:
035: public class DateTimeTag implements ReadTag {
036: private static Logger logger = PortalLogger
037: .getLogger(DateTimeTag.class);
038:
039: private static String TIMEZONE_ATTR_KEY = "preferredtimezone";
040:
041: private Locale locale = null;
042: private ProviderContext context = null;
043: private SSOToken token = null;
044: private ResourceBundle bundle = null;
045: private String channel = null;
046:
047: public void init(String channel, ProviderContext context,
048: HttpServletRequest req) throws TagException {
049: this .channel = channel;
050: if (context == null) {
051: throw new TagException(
052: "DateTimeTag.init(): provider context was null");
053: }
054: // set provider context
055: this .context = context;
056:
057: // get locale
058: locale = context.getLocale();
059:
060: // get resource bundle
061: try {
062: bundle = ResourceBundle.getBundle(context
063: .getProviderName(channel), locale);
064: } catch (ProviderContextException pce) {
065: throw new TagException(
066: "DateTime.init(): Failed to get resource bundle for "
067: + channel + " with locale " + locale);
068: }
069:
070: //
071: // get SSOToken for getTimeLeft and getMaxIdleTime
072: //
073: SSOTokenManager tokenManager = null;
074: try {
075: tokenManager = SSOTokenManager.getInstance();
076:
077: if (tokenManager == null) {
078: throw new TagException("DateTime.init(): "
079: + "Failed to getSSOTokenmanager.");
080: }
081:
082: token = tokenManager.createSSOToken(req);
083:
084: } catch (SSOException ssoe) {
085: throw new TagWrapperException("DateTimeTag.init():", ssoe);
086: }
087:
088: }
089:
090: public String get(String key) throws TagException {
091: if (key.equals("currentDate")) {
092: return getCurrentDate();
093: } else if (key.equals("timeLeft")) {
094: return getTimeLeft();
095: } else if (key.equals("maxIdle")) {
096: return getMaxIdle();
097: } else {
098: throw new UnhandledTagException("unknown key=" + key);
099: }
100: }
101:
102: private String getCurrentDate() throws TagException {
103: Date d = new Date();
104: String current = bundle.getString("timeWasFormat");
105: String tz = null;
106: TimeZone timezone = null;
107:
108: tz = context.getStringAttribute(TIMEZONE_ATTR_KEY);
109:
110: if (tz != null && tz.length() > 0) {
111: timezone = TimeZone.getTimeZone(tz);
112: } else {
113: logger.log(Level.FINE, "PSCR_CSPPUI0001");
114: timezone = TimeZone.getDefault();
115: }
116: return I18n.format(current, d, timezone, locale);
117: }
118:
119: private String getTimeLeft() throws TagException {
120: Long tl = null;
121: String minsLeft = null;
122:
123: try {
124: tl = new Long(token.getTimeLeft() / 60);
125: } catch (NumberFormatException nfe) {
126: throw new TagWrapperException(
127: "time left is not an integer", nfe);
128: } catch (SSOException ssoe) {
129: throw new TagWrapperException("error getting time left",
130: ssoe);
131: }
132:
133: minsLeft = bundle.getString("minsLeftFormat");
134: return I18n.format(minsLeft, tl, locale);
135: }
136:
137: private String getMaxIdle() throws TagException {
138: Long mi = null;
139: try {
140: mi = new Long(token.getMaxIdleTime());
141: } catch (NumberFormatException nfe) {
142: throw new TagWrapperException(
143: "max idle time is not an integer", nfe);
144: } catch (SSOException ssoe) {
145: throw new TagWrapperException(
146: "error getting max idle time", ssoe);
147: }
148:
149: String minsIdle = null;
150:
151: minsIdle = bundle.getString("minsIdleFormat");
152: return I18n.format(minsIdle, mi, locale);
153: }
154:
155: }
|