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