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.io.InputStream;
20 import java.net.URL;
21 import java.util.Properties;
22
23 import org.xml.sax.Attributes;
24
25 import com.philemonworks.selfdiagnose.DiagnoseException;
26 import com.philemonworks.selfdiagnose.DiagnoseUtil;
27 import com.philemonworks.selfdiagnose.DiagnosticTaskResult;
28 import com.philemonworks.selfdiagnose.ExecutionContext;
29
30
31
32
33
34 public class CheckResourceProperty extends CheckProperty {
35 private static final long serialVersionUID = -5176698762404603297L;
36 protected static final String PARAMETER_NAME = "name";
37 protected static final String PARAMETER_URL = "url";
38
39 protected String name;
40 protected String url;
41
42
43
44
45
46
47 public String getDescription() {
48 return "Check that the value of a property matches this pattern. The properties resource must be on the classpath.";
49 }
50
51
52
53
54
55 public void initializeFromAttributes(Attributes attributes) {
56
57 super.initializeFromAttributes(attributes);
58 this.setName(attributes.getValue(PARAMETER_NAME));
59 }
60 public void setUp(ExecutionContext ctx) throws DiagnoseException {
61 super.setUp(ctx);
62 if (url == null)
63 DiagnoseUtil.verifyNonEmptyString(PARAMETER_NAME, name, CheckProperty.class);
64 if (name == null)
65 DiagnoseUtil.verifyNonEmptyString(PARAMETER_URL, url, CheckProperty.class);
66 }
67
68
69
70
71
72 public void run(ExecutionContext ctx, DiagnosticTaskResult result) throws DiagnoseException {
73 String value = null;
74 Properties properties = this.getProperties(ctx);
75 value = properties.getProperty(property);
76
77 ctx.setValue(this.getVariableName(), value);
78 if (value == null) {
79 result.setFailedMessage("No such property available from resource [" + url + "]");
80 return;
81 }
82 if (pattern != null) {
83 if (value.matches(pattern))
84 result.setFailedMessage("Property value [" + value + "] does not match pattern [" + pattern + "] from resource [" + url + "]");
85 return;
86 }
87 String msg = "Property [" + property + "] with value ["+value+"] exists in resource [" + url + "]";
88 if (pattern != null) msg += " and matches [" + pattern + "]";
89 result.setPassedMessage(msg);
90 }
91
92 protected Properties getProperties(ExecutionContext ctx) throws DiagnoseException {
93 try {
94 URL theUrl = DiagnoseUtil.retrieveURL(ctx,name,url);
95 if (theUrl == null)
96 throw new DiagnoseException("Unable to find resource [" + (name==null?url:name) + "]");
97 Properties properties = new Properties();
98 InputStream input = theUrl.openStream();
99 properties.load(input);
100 return properties;
101 } catch (Exception ex) {
102 throw new DiagnoseException(ex);
103 }
104 }
105
106 public String getName() {
107 return name;
108 }
109 public void setName(String name) {
110 this.name = name;
111 }
112 public String getUrl() {
113 return url;
114 }
115 public void setUrl(String url){
116 this.url = url;
117 }
118 }