1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.philemonworks.selfdiagnose.check;
18
19 import java.util.Locale;
20
21 import org.xml.sax.Attributes;
22
23 import com.philemonworks.selfdiagnose.DiagnoseException;
24 import com.philemonworks.selfdiagnose.DiagnoseUtil;
25 import com.philemonworks.selfdiagnose.DiagnosticTaskResult;
26 import com.philemonworks.selfdiagnose.ExecutionContext;
27
28
29
30
31
32
33
34
35
36
37
38
39 public class CheckBeanProperty extends CheckProperty {
40 private static final long serialVersionUID = -7976546053356912993L;
41 protected static final String PARAMETER_METHOD = "method";
42 protected static final String PARAMETER_BEAN = "bean";
43
44 private String method;
45
46 private String bean;
47
48
49
50 public String getDescription(){
51 return "Check whether the value (or its String representation )of a bean property matches a pattern";
52 }
53
54 public void initializeFromAttributes(Attributes attributes) {
55 super.initializeFromAttributes(attributes);
56 this.setMethod(attributes.getValue(PARAMETER_METHOD));
57 this.setBean(attributes.getValue(PARAMETER_BEAN));
58 }
59
60 public void setUp(ExecutionContext ctx) throws DiagnoseException {
61 super.setUp(ctx);
62 DiagnoseUtil.verifyNotNull(PARAMETER_BEAN, bean, CheckBeanProperty.class);
63 }
64
65 protected void checkPropertyAccess() throws DiagnoseException {}
66
67 public void run(ExecutionContext ctx, DiagnosticTaskResult result)
68 throws DiagnoseException {
69 Object beanValue = ctx.resolveValue(this.getBean());
70 DiagnoseUtil.verifyNotNull("bean", beanValue, CheckBeanProperty.class);
71
72 Object value = beanValue;
73 if (isMessagePerformRequired())
74 value = DiagnoseUtil.perform(beanValue, this.constructGetter(), new Object[0]);
75 ctx.setValue(this.getVariableName(), value);
76 checkValueAgainstPattern(result,"Bean", "property", this.constructGetter(), value);
77 }
78
79 private boolean isMessagePerformRequired() {
80 return property != null || method != null;
81 }
82
83 public String constructGetter(){
84 if (method != null)
85 return method;
86 if (property == null)
87 return "this";
88 String getter = "get"
89 + (String.valueOf(this.getProperty().charAt(0)).toUpperCase(Locale.getDefault()))
90 + this.getProperty().substring(1);
91 return getter;
92 }
93
94
95 public String getMethod() {
96 return method;
97 }
98
99 public void setMethod(String method) {
100 this.method = method;
101 }
102
103 public void setBean(String aString){
104 bean = aString;
105 }
106 public String getBean(){
107 return bean;
108 }
109 }