001: package com.opensymphony.webwork.config_browser;
002:
003: import com.opensymphony.xwork.util.OgnlUtil;
004: import com.opensymphony.xwork.validator.Validator;
005: import ognl.Ognl;
006: import ognl.OgnlException;
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009:
010: import java.beans.BeanInfo;
011: import java.beans.IntrospectionException;
012: import java.beans.Introspector;
013: import java.beans.PropertyDescriptor;
014: import java.util.Collections;
015: import java.util.Map;
016: import java.util.Set;
017: import java.util.TreeSet;
018:
019: /**
020: * ShowValidatorAction
021: *
022: * @author Jason Carreira
023: * Date: Jun 1, 2004 9:01:02 PM
024: */
025: public class ShowValidatorAction extends ListValidatorsAction {
026: private static Log log = LogFactory
027: .getLog(ShowValidatorAction.class);
028:
029: Set properties = Collections.EMPTY_SET;
030: int selected = 0;
031:
032: public int getSelected() {
033: return selected;
034: }
035:
036: public void setSelected(int selected) {
037: this .selected = selected;
038: }
039:
040: public Set getProperties() {
041: return properties;
042: }
043:
044: public Validator getSelectedValidator() {
045: return (Validator) validators.get(selected);
046: }
047:
048: public String execute() throws Exception {
049: loadValidators();
050: Validator validator = getSelectedValidator();
051: properties = new TreeSet();
052: try {
053: Map context = Ognl.createDefaultContext(validator);
054: BeanInfo beanInfoFrom = null;
055: try {
056: beanInfoFrom = Introspector.getBeanInfo(validator
057: .getClass(), Object.class);
058: } catch (IntrospectionException e) {
059: log.error("An error occurred", e);
060: addActionError("An error occurred while introspecting a validator of type "
061: + validator.getClass().getName());
062: return ERROR;
063: }
064:
065: PropertyDescriptor[] pds = beanInfoFrom
066: .getPropertyDescriptors();
067:
068: for (int i = 0; i < pds.length; i++) {
069: PropertyDescriptor pd = pds[i];
070: String name = pd.getName();
071: Object value = null;
072: if (pd.getReadMethod() == null) {
073: value = "No read method for property";
074: } else {
075: try {
076: Object expr = OgnlUtil.compile(name);
077: value = Ognl.getValue(expr, context, validator);
078: } catch (OgnlException e) {
079: addActionError("Caught OGNL exception while getting property value for '"
080: + name
081: + "' on validator of type "
082: + validator.getClass().getName());
083: }
084: }
085: properties.add(new PropertyInfo(name, pd
086: .getPropertyType(), value));
087: }
088: } catch (Exception e) {
089: log.warn("Unable to retrieve properties.", e);
090: addActionError("Unable to retrieve properties: "
091: + e.toString());
092: }
093:
094: if (hasErrors())
095: return ERROR;
096: else
097: return SUCCESS;
098: }
099:
100: public static class PropertyInfo implements Comparable {
101: private String name;
102: private Class type;
103: private Object value;
104:
105: public PropertyInfo(String name, Class type, Object value) {
106: if (name == null) {
107: throw new IllegalArgumentException(
108: "Name must not be null");
109: }
110: if (type == null) {
111: throw new IllegalArgumentException(
112: "Type must not be null");
113: }
114: this .name = name;
115: this .type = type;
116: this .value = value;
117: }
118:
119: public Class getType() {
120: return type;
121: }
122:
123: public void setType(Class type) {
124: this .type = type;
125: }
126:
127: public Object getValue() {
128: return value;
129: }
130:
131: public void setValue(Object value) {
132: this .value = value;
133: }
134:
135: public String getName() {
136: return name;
137: }
138:
139: public void setName(String name) {
140: this .name = name;
141: }
142:
143: public boolean equals(Object o) {
144: if (this == o)
145: return true;
146: if (!(o instanceof PropertyInfo))
147: return false;
148:
149: final PropertyInfo propertyInfo = (PropertyInfo) o;
150:
151: if (!name.equals(propertyInfo.name))
152: return false;
153: if (!type.equals(propertyInfo.type))
154: return false;
155: if (value != null ? !value.equals(propertyInfo.value)
156: : propertyInfo.value != null)
157: return false;
158:
159: return true;
160: }
161:
162: public int hashCode() {
163: int result;
164: result = name.hashCode();
165: result = 29 * result + type.hashCode();
166: result = 29 * result
167: + (value != null ? value.hashCode() : 0);
168: return result;
169: }
170:
171: public int compareTo(Object o) {
172: PropertyInfo other = (PropertyInfo) o;
173: return this.name.compareTo(other.name);
174: }
175: }
176: }
|