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