001: /**
002: * $Id: BeanTag.java,v 1.6 2005/09/21 10:50:24 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.base;
014:
015: import com.sun.portal.log.common.PortalLogger;
016:
017: import javax.servlet.jsp.*;
018: import javax.servlet.jsp.tagext.*;
019: import java.util.logging.Logger;
020: import java.util.logging.Level;
021:
022: /**
023: * BeanTag - tag that represents a bean
024: *
025: * This tag encapsulates a bean or object for
026: * use with other tags such as get and set.
027: *
028: * Attributes:
029: *
030: * id
031: * name to store the new bean in the page context
032: * name
033: * name to use to find the bean in the page context
034: *
035: * Either "id" or "name" attribute must be used.
036: * Implementations of these attributes is inheirited
037: * from the TagSupport class.
038: *
039: * Subclasses must implement the newBean() method
040: * which will be called to create a new bean.
041: * Cooperative classes use the getBean() method to
042: * get a handle on the bean when this tag is used
043: * as a parent.
044: *
045: * @author Robert O'Brien
046: * @version 1.0
047: * @see BeanGetTag
048: * @see BeanSetTag
049: */
050: public abstract class BeanTag extends BaseTagSupport implements
051: BeanHolder {
052: // Create a logger for this class
053: private static Logger debugLogger = PortalLogger
054: .getLogger(BeanTag.class);
055:
056: /**
057: * The bean object this tag represents
058: */
059: protected Object bean;
060:
061: /**
062: * Get the bean object
063: *
064: * @return the bean object
065: */
066: public Object getBean() {
067: if (bean == null) {
068: try {
069: bean = findBean();
070: } catch (Exception e) {
071: debugLogger.log(Level.WARNING, "exception thrown on "
072: + this , e);
073: bean = null;
074: }
075: }
076: return bean;
077: }
078:
079: /**
080: * Set the bean object
081: *
082: * @param bean the bean object
083: */
084: public void setBean(Object bean) {
085: this .bean = bean;
086: }
087:
088: /**
089: * Create a new bean
090: *
091: * Subclasses must implement this method to
092: * return the bean object that this tag represents.
093: *
094: * @return the newly created bean
095: */
096: public abstract Object newBean() throws Exception;
097:
098: /**
099: * Find the bean
100: *
101: * If the "name" attribute is specified the get
102: * the bean from then page context.
103: *
104: * Otherwise search for the bean elsewhere.
105: * If there is a parent CollectionTag then use
106: * it to get the current item. Otherwise call
107: * newBean() to create a new object.
108: *
109: * If the "id" attribute is specified then store
110: * the bean into the page context using "id".
111: *
112: * @return the found bean
113: */
114: public Object findBean() throws Exception {
115: Object bean;
116:
117: if (name != null) {
118: return pageContext.getAttribute(name);
119: }
120:
121: CollectionTag collection = (CollectionTag) TagSupport
122: .findAncestorWithClass(this , CollectionTag.class);
123:
124: if (collection != null) {
125: bean = collection.getItem();
126: } else {
127: bean = newBean();
128: }
129:
130: if (id != null) {
131: debugLogger
132: .log(Level.FINE, "About to set bean=" + bean
133: + ", with id=" + id + ", on context="
134: + pageContext);
135: pageContext.setAttribute(id, bean);
136: }
137:
138: return bean;
139: }
140:
141: /**
142: * Make the bean available.
143: *
144: * @return EVAL_BODY_INCLUDE
145: * @exception JspException
146: */
147: public int doStartTag() throws JspException {
148:
149: if (id != null && name != null) {
150: debugLogger.fine("PSMA_CSPWTB0011");
151: throw new JspException(
152: this .getClass().getName()
153: + ".doStartTag(): Cannot use both 'id' and 'name' attributes");
154: }
155:
156: try {
157: bean = findBean();
158: } catch (Exception e) {
159: debugLogger.log(Level.FINE, "PSMA_CSPWTB0001", e);
160: throw new JspException(this .getClass().getName()
161: + ".doStartTag(): " + e.getMessage());
162: }
163:
164: return EVAL_BODY_INCLUDE;
165: }
166:
167: /**
168: * Continue evaluating the page
169: *
170: * @return EVAL_PAGE
171: * @exception JspException
172: */
173: public int doEndTag() throws JspException {
174: return EVAL_PAGE;
175: }
176:
177: /**
178: * Cleanup
179: */
180: public void release() {
181: super.release();
182: id = null;
183: name = null;
184: bean = null;
185: }
186: }
|