001: /**
002: * $Id: BeanListTag.java,v 1.5 2005/09/21 10:50:15 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 java.util.*;
018: import java.util.logging.Logger;
019: import java.util.logging.Level;
020: import java.beans.*;
021: import java.lang.reflect.*;
022: import javax.servlet.jsp.*;
023: import javax.servlet.jsp.tagext.*;
024:
025: /**
026: * BeanListTag - list all bean properties
027: *
028: * This class lists all the properties and values of
029: * the specified bean to the page output.
030: * The bean is either specified with the "name" attribute
031: * or is found in a parent BeanTag.
032: *
033: * Attributes:
034: *
035: * name
036: * name of the bean in the page context
037: *
038: * @author Robert O'Brien
039: * @version 1.0
040: * @see BeanSupport
041: */
042: public class BeanListTag extends BeanSupport {
043:
044: // Create a logger for this class
045: private static Logger debugLogger = PortalLogger
046: .getLogger(BeanListTag.class);
047:
048: /**
049: * List the bean's properties to the page output
050: *
051: * @return SKIP_BODY
052: * @exception JspException
053: */
054: public int doStartTag() throws JspException {
055:
056: try {
057: bean = findBean();
058: } catch (Exception e) {
059: debugLogger.log(Level.FINE, "PSMA_CSPWTB0007", e);
060: throw new JspException(this .getClass().getName()
061: + ".doStartTag(): No bean found: "
062: + e.getMessage());
063: }
064:
065: try {
066: JspWriter out = pageContext.getOut();
067: HashMap properties = getBeanProperties(bean);
068: Set keys = properties.keySet();
069: Iterator iterator = keys.iterator();
070:
071: out.println("[" + bean.getClass());
072:
073: while (iterator.hasNext()) {
074: String property = iterator.next().toString();
075: PropertyDescriptor descriptor;
076: String value = null;
077:
078: if (property.equals("class"))
079: continue;
080:
081: descriptor = (PropertyDescriptor) properties
082: .get(property);
083: Method method = descriptor.getReadMethod();
084:
085: if (method != null) {
086: property = descriptor.getName();
087: try {
088: Object obj = accessProperty(bean, method, null);
089: value = (obj != null) ? obj.toString() : "null";
090: } catch (IntrospectionException e) {
091: value = e.getMessage();
092: }
093: }
094: String desc = descriptor.getPropertyType()
095: + "(r"
096: + ((descriptor.getWriteMethod() != null) ? "w)"
097: : ")");
098:
099: out.println(" " + property + "[" + desc + "] = "
100: + value);
101: }
102:
103: out.println("]");
104:
105: } catch (IntrospectionException e) {
106: debugLogger.log(Level.FINE, "PSMA_CSPWTB0008", bean
107: .getClass());
108: throw new JspException(
109: this .getClass().getName()
110: + ".doStartTag(): Couldn't get bean properties for "
111: + bean.getClass());
112:
113: } catch (java.io.IOException e) {
114: debugLogger.log(Level.INFO, "PSMA_CSPWTB0001", e);
115: throw new JspException(this .getClass().getName()
116: + ".doStartTag(): IO Exception: " + e.getMessage());
117: }
118: return SKIP_BODY;
119: }
120: }
|