001: /**
002: * $Id: BeansTag.java,v 1.3 2004/04/26 23:11:18 saare 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.*;
016: import javax.servlet.jsp.*;
017: import javax.servlet.jsp.tagext.*;
018: import com.sun.portal.wireless.taglibs.base.*;
019:
020: /**
021: * BeansTag - make a collection of beans available
022: *
023: * Attributes:
024: *
025: * id
026: * name of the collection to save on page context
027: * name
028: * name of the collection to retrieve from page context
029: * size
030: * number of beans to create for the collection
031: * type
032: * class type of the bean to create
033: *
034: * @author Robert O'Brien
035: * @version 1.0
036: * @see com.sun.portal.wireless.taglibs.base.CollectionTag
037: * @see com.sun.portal.wireless.taglibs.util.BeanTag
038: */
039:
040: public class BeansTag extends CollectionTag {
041:
042: /**
043: * The type or class of the bean object
044: */
045: String type;
046:
047: /**
048: * Get the class type of the bean
049: *
050: * @return the bean class
051: */
052: public String getType() {
053: return type;
054: }
055:
056: /**
057: * Set the class type of the bean
058: *
059: * @param type the bean class
060: */
061: public void setType(String type) {
062: this .type = type;
063: }
064:
065: /**
066: * The size of the bean collection
067: */
068: int size;
069:
070: /**
071: * Get the size of the bean collection
072: *
073: * @return size of the bean collection
074: */
075: public String getSize() {
076: return Integer.toString(size);
077: }
078:
079: /**
080: * Set the size of the bean collection
081: *
082: * @param type the bean class
083: */
084: public void setSize(String size) {
085: try {
086: this .size = Integer.parseInt(size);
087: } catch (NumberFormatException e) {
088: this .size = 0;
089: }
090: }
091:
092: /**
093: * Create a list of beans of the specified class type
094: *
095: * @return the bean object
096: */
097: public Collection findCollection() throws Exception {
098:
099: if (type == null) {
100: throw new Exception("BeansTag: missing 'type' attribute");
101: }
102:
103: if (size < 0) {
104: throw new Exception("BeansTag: invalid 'size' attribute");
105: }
106:
107: ArrayList list = new ArrayList(size);
108: Class beanClass = Class.forName(type);
109:
110: for (int i = 0; i < size; i++) {
111: list.add(beanClass.newInstance());
112: }
113:
114: return list;
115: }
116:
117: /**
118: * Cleanup
119: */
120: public void release() {
121: super .release();
122: type = null;
123: size = 0;
124: }
125: }
|