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.ResourceBundle;
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.DiagnosticTask;
26 import com.philemonworks.selfdiagnose.DiagnosticTaskResult;
27 import com.philemonworks.selfdiagnose.ExecutionContext;
28
29
30
31
32
33
34
35
36 public class CheckResourceBundleKey extends DiagnosticTask {
37 private static final String PARAMETER_KEY = "key";
38 private static final String PARAMETER_NAME = "name";
39 private String name;
40 private String key;
41
42
43
44
45
46
47 public void initializeFromAttributes(Attributes attributes) {
48 super.initializeFromAttributes(attributes);
49 this.setName(attributes.getValue(PARAMETER_NAME));
50 this.setKey(attributes.getValue(PARAMETER_KEY));
51 }
52 public String getDescription() {
53 return "Check whether this Resource Bundle is found on the classpath and that it has a value for this key";
54 }
55 public void setUp(ExecutionContext ctx) throws DiagnoseException {
56 super.setUp(ctx);
57 DiagnoseUtil.verifyNonEmptyString(PARAMETER_NAME, name, CheckResourceBundleKey.class);
58 DiagnoseUtil.verifyNonEmptyString(PARAMETER_KEY, key, CheckResourceBundleKey.class);
59 }
60 public void run(ExecutionContext ctx, DiagnosticTaskResult result) throws DiagnoseException {
61 ResourceBundle bundle;
62 try {
63 bundle = ResourceBundle.getBundle(name);
64 } catch (java.util.MissingResourceException ex) {
65 throw new DiagnoseException(ex);
66 }
67 try {
68 Object value = bundle.getObject(key);
69 ctx.setValue(this.getVariableName(),value);
70 } catch (java.util.MissingResourceException ex) {
71 throw new DiagnoseException("No such key [" + key + "] in bundle [" + name + "]", ex);
72 }
73 result.setPassedMessage("Key [" + key + "] is available in resource bundle [" + name + "]");
74 }
75 public String getKey() {
76 return key;
77 }
78 public void setKey(String key) {
79 this.key = key;
80 }
81 public String getTaskName() {
82 return name;
83 }
84 public void setName(String name) {
85 this.name = name;
86 }
87 }