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.IOException;
20 import java.io.InputStream;
21 import java.net.URL;
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.DiagnosticTask;
28 import com.philemonworks.selfdiagnose.DiagnosticTaskResult;
29 import com.philemonworks.selfdiagnose.ExecutionContext;
30
31
32
33
34
35
36
37
38
39
40 public class CheckResourceAccessible extends DiagnosticTask {
41 private static final String PARAMETER_NAME = "name";
42 private String name = null;
43
44
45
46
47
48 public void initializeFromAttributes(Attributes attributes){
49 super.initializeFromAttributes(attributes);
50 this.setName(attributes.getValue(PARAMETER_NAME));
51 }
52 public void setUp(ExecutionContext ctx) throws DiagnoseException {
53 super.setUp(ctx);
54 DiagnoseUtil.verifyNonEmptyString(PARAMETER_NAME, name, CheckResourceAccessible.class);
55 }
56
57
58
59
60
61 public void run(ExecutionContext ctx, DiagnosticTaskResult result) throws DiagnoseException {
62 URL url = DiagnoseUtil.findResource(name,false);
63 if (url == null)
64 throw new DiagnoseException("Unable to find:" + name);
65 ctx.setValue(this.getVariableName(), url);
66 try {
67 InputStream is = url.openStream();
68 if (is.available() == 0)
69 result.setFailedMessage("No data available from [" + name + "] located at [" + url + "]");
70 } catch (IOException ex) {
71 throw new DiagnoseException(ex);
72 }
73 result.setPassedMessage("Resource [" + name + "] is located at [" + url + "]");
74 }
75
76
77
78 public String getName() {
79 return name;
80 }
81
82
83
84 public void setName(String string) {
85 name = string;
86 }
87
88
89
90 public String getDescription() {
91 return "Check that this resource can be found and is accessible";
92 }
93 }