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