001: /**
002: * $Id: BeanCopyTag.java,v 1.5 2005/09/21 10:50:08 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.beans.*;
018: import java.util.logging.Logger;
019: import java.util.logging.Level;
020: import javax.servlet.jsp.*;
021: import javax.servlet.jsp.tagext.*;
022:
023: /**
024: * BeanCopyTag - copy a bean property
025: *
026: * This class reads the specified bean property
027: * from the specified source bean and writes the
028: * value onto the same property of target bean.
029: *
030: * Attributes:
031: *
032: * property
033: * name of the bean property to copy
034: * name
035: * name to use to retrieve the bean from the page context
036: * src
037: * name of the bean to copy the property from
038: *
039: * @author Robert O'Brien
040: * @version 1.0
041: * @see BeanGetTag
042: * @see BeanSupport
043: */
044: public class BeanCopyTag extends BeanSupport {
045:
046: // Create a logger for this class
047: private static Logger debugLogger = PortalLogger
048: .getLogger(BeanCopyTag.class);
049:
050: /**
051: * Name of the source bean to copy from
052: */
053: String source;
054:
055: /**
056: * Return the source bean to copy from
057: *
058: * @return the source bean
059: */
060: public String getSource() {
061: return source;
062: }
063:
064: /**
065: * Set the source bean to copy from
066: *
067: * @param source the source bean
068: */
069: public void setSource(String source) {
070: this .source = source;
071: }
072:
073: /**
074: * Copy the bean property value from another bean
075: *
076: * @return SKIP_BODY
077: * @exception JspException
078: */
079: public int doStartTag() throws JspException {
080:
081: try {
082: bean = findBean();
083: } catch (Exception e) {
084: debugLogger.log(Level.FINE, "PSMA_CSPWTB0001", e);
085: throw new JspException(this .getClass().getName()
086: + ".doStartTag() failed: " + e.getMessage());
087: }
088:
089: if (property == null) {
090: debugLogger.fine("PSMA_CSPWTB0002");
091: throw new JspException(this .getClass().getName()
092: + ".doStartTag(): property attribute is required");
093: }
094:
095: if (source == null) {
096: debugLogger.fine("PSMA_CSPWTB0003");
097: throw new JspException(this .getClass().getName()
098: + ".doStartTag(): source attribute is required");
099: }
100:
101: Object src = pageContext.findAttribute(source);
102: Object value;
103:
104: if (src.getClass() != bean.getClass()) {
105: debugLogger.fine("PSMA_CSPWTB0004");
106: throw new JspException(
107: this .getClass().getName()
108: + ".doStartTag(): source and bean are different classes");
109: }
110:
111: try {
112: value = readProperty(src);
113: } catch (IntrospectionException e) {
114: debugLogger.log(Level.FINE, "PSMA_CSPWTB0005",
115: getProperty());
116: throw new JspException(this .getClass().getName()
117: + ".doStartTag(): Couldn't read " + getProperty()
118: + " property");
119: }
120:
121: try {
122: writeProperty(bean, value);
123: } catch (IntrospectionException e) {
124: debugLogger.log(Level.FINE, "PSMA_CSPWTB0005",
125: getProperty());
126: throw new JspException(this .getClass().getName()
127: + ".doStartTag(): Couldn't write " + getProperty()
128: + " property");
129: }
130:
131: return SKIP_BODY;
132: }
133: }
|