001: /**
002: * $Id: SessionTag.java,v 1.6 2005/09/21 10:53:39 dg154973 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.taglibs.util;
014:
015: import java.util.logging.Level;
016: import java.util.logging.Logger;
017:
018: import javax.servlet.jsp.*;
019: import javax.servlet.jsp.tagext.*;
020:
021: import com.sun.portal.log.common.PortalLogger;
022: import com.sun.portal.wireless.taglibs.base.*;
023:
024: /**
025: * SessionTag -
026: *
027: * Attributes:
028: *
029: * id
030: * restore attribute from session to page context
031: * name
032: * save attribute from page context to session
033: * attribute
034: * name of session attribute to operate on
035: *
036: * @author Robert O'Brien
037: *
038: * @version 1.0
039: */
040: public class SessionTag extends BaseTagSupport {
041: /**
042: * The attribute name
043: */
044: String attribute;
045:
046: private static Logger logger = PortalLogger
047: .getLogger(SessionTag.class);
048:
049: /**
050: * If 'name' attribute then save from page to session.
051: * If 'id' attribute then restore from session to page.
052: * If neither then remove attribute from session.
053: *
054: * @return SKIP_BODY
055: * @exception JspException
056: */
057: public int doStartTag() throws JspException {
058: UtilContext uc;
059: Object bean;
060:
061: if (attribute == null) {
062: logger.warning("PSMA_CSPWTU0006");
063: throw new JspException("Missing 'attribute' attribute");
064: }
065:
066: try {
067: uc = UtilContext.getContext(pageContext);
068: } catch (Exception e) {
069: logger.warning("PSMA_CSPWTU0007");
070: throw new JspException("Couldn't get context");
071: }
072:
073: if (name != null) {
074: bean = pageContext.getAttribute(name);
075: if (bean == null)
076: throw new JspException("Couldn't get the bean by name="
077: + name);
078: if (logger.isLoggable(Level.FINE)) {
079: String[] param = { attribute, bean.toString() };
080: logger.log(Level.FINE, "PSMA_CSPWTU0007", param);
081: }
082: uc.setAttribute(attribute, bean);
083:
084: } else if (id != null) {
085: bean = uc.getAttribute(attribute);
086: pageContext.setAttribute(id, bean);
087:
088: } else {
089: uc.removeAttribute(attribute);
090: }
091:
092: return SKIP_BODY;
093: }
094:
095: /**
096: * Set the attribute name
097: *
098: * @param value the attribute name
099: */
100: public void setAttribute(String attribute) {
101: this .attribute = evalAttribute(attribute);
102: }
103:
104: /**
105: * Cleanup
106: */
107: public void release() {
108: super.release();
109: attribute = null;
110: }
111: }
|