1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.config_browser;
22
23 import java.beans.BeanInfo;
24 import java.beans.IntrospectionException;
25 import java.beans.Introspector;
26 import java.beans.PropertyDescriptor;
27 import java.util.Collections;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.TreeSet;
31
32 import ognl.Ognl;
33 import ognl.OgnlException;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 import com.opensymphony.xwork2.util.OgnlUtil;
39 import com.opensymphony.xwork2.validator.Validator;
40
41 /***
42 * ShowValidatorAction
43 *
44 */
45 public class ShowValidatorAction extends ListValidatorsAction {
46 private static final long serialVersionUID = 4061534149317835177L;
47
48 private static Log log = LogFactory.getLog(ShowValidatorAction.class);
49
50 Set properties = Collections.EMPTY_SET;
51 int selected = 0;
52
53 public int getSelected() {
54 return selected;
55 }
56
57 public void setSelected(int selected) {
58 this.selected = selected;
59 }
60
61 public Set getProperties() {
62 return properties;
63 }
64
65 public Validator getSelectedValidator() {
66 return (Validator) validators.get(selected);
67 }
68
69 public String execute() throws Exception {
70 loadValidators();
71 Validator validator = getSelectedValidator();
72 properties = new TreeSet();
73 try {
74 Map context = Ognl.createDefaultContext(validator);
75 BeanInfo beanInfoFrom = null;
76 try {
77 beanInfoFrom = Introspector.getBeanInfo(validator.getClass(), Object.class);
78 } catch (IntrospectionException e) {
79 log.error("An error occurred", e);
80 addActionError("An error occurred while introspecting a validator of type " + validator.getClass().getName());
81 return ERROR;
82 }
83
84 PropertyDescriptor[] pds = beanInfoFrom.getPropertyDescriptors();
85
86 for (int i = 0; i < pds.length; i++) {
87 PropertyDescriptor pd = pds[i];
88 String name = pd.getName();
89 Object value = null;
90 if (pd.getReadMethod() == null) {
91 value = "No read method for property";
92 } else {
93 try {
94 Object expr = OgnlUtil.compile(name);
95 value = Ognl.getValue(expr, context, validator);
96 } catch (OgnlException e) {
97 addActionError("Caught OGNL exception while getting property value for '" + name + "' on validator of type " + validator.getClass().getName());
98 }
99 }
100 properties.add(new PropertyInfo(name, pd.getPropertyType(), value));
101 }
102 } catch (Exception e) {
103 log.warn("Unable to retrieve properties.", e);
104 addActionError("Unable to retrieve properties: " + e.toString());
105 }
106
107 if (hasErrors())
108 return ERROR;
109 else
110 return SUCCESS;
111 }
112
113 public static class PropertyInfo implements Comparable {
114 private String name;
115 private Class type;
116 private Object value;
117
118 public PropertyInfo(String name, Class type, Object value) {
119 if (name == null) {
120 throw new IllegalArgumentException("Name must not be null");
121 }
122 if (type == null) {
123 throw new IllegalArgumentException("Type must not be null");
124 }
125 this.name = name;
126 this.type = type;
127 this.value = value;
128 }
129
130 public Class getType() {
131 return type;
132 }
133
134 public void setType(Class type) {
135 this.type = type;
136 }
137
138 public Object getValue() {
139 return value;
140 }
141
142 public void setValue(Object value) {
143 this.value = value;
144 }
145
146 public String getName() {
147 return name;
148 }
149
150 public void setName(String name) {
151 this.name = name;
152 }
153
154 public boolean equals(Object o) {
155 if (this == o) return true;
156 if (!(o instanceof PropertyInfo)) return false;
157
158 final PropertyInfo propertyInfo = (PropertyInfo) o;
159
160 if (!name.equals(propertyInfo.name)) return false;
161 if (!type.equals(propertyInfo.type)) return false;
162 if (value != null ? !value.equals(propertyInfo.value) : propertyInfo.value != null) return false;
163
164 return true;
165 }
166
167 public int hashCode() {
168 int result;
169 result = name.hashCode();
170 result = 29 * result + type.hashCode();
171 result = 29 * result + (value != null ? value.hashCode() : 0);
172 return result;
173 }
174
175 public int compareTo(Object o) {
176 PropertyInfo other = (PropertyInfo) o;
177 return this.name.compareTo(other.name);
178 }
179 }
180 }