1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.appstatus.core.check;
18
19 import java.text.MessageFormat;
20 import java.util.Locale;
21 import java.util.ResourceBundle;
22
23 import net.sf.appstatus.core.check.impl.StatusResultImpl;
24
25 public class CheckResultBuilder {
26
27 int code;
28 private Object[] descriptionArgs;
29 boolean fatal = false;
30 private Locale locale;
31 String name, group, description, resolutionSteps, bundle = null;
32 private Object[] resolutionStepArgs;
33
34 public CheckResultBuilder() {
35 }
36
37 public ICheckResult build() {
38
39 ResourceBundle resBundle = null;
40 if (bundle != null) {
41 resBundle = ResourceBundle.getBundle(bundle, locale);
42 }
43
44 StatusResultImpl result = new StatusResultImpl();
45
46 result.setGroup(resBundle != null ? resBundle.getString(group) : group);
47 result.setProbeName(resBundle != null ? resBundle.getString(name) : name);
48 result.setCode(code);
49 result.setFatal(code == ICheckResult.OK ? false : fatal);
50
51 if ((resolutionStepArgs != null || descriptionArgs != null) && bundle == null) {
52 throw new IllegalStateException("messageBundle() must be set when using messages with args.");
53 }
54
55 if (bundle != null) {
56
57 if (description != null) {
58 result.setDescription(MessageFormat.format(resBundle.getString(description), descriptionArgs));
59 }
60
61 if (resolutionSteps != null) {
62 result.setResolutionSteps(MessageFormat.format(resBundle.getString(resolutionSteps), resolutionStepArgs));
63 }
64 } else {
65 if (description != null) {
66 result.setDescription(description);
67 }
68
69 if (resolutionSteps != null) {
70 result.setResolutionSteps(resolutionSteps);
71 }
72
73 }
74 return result;
75 }
76
77 public CheckResultBuilder code(int code) {
78 this.code = code;
79 return this;
80 }
81
82 public CheckResultBuilder description(String description) {
83 this.description = description;
84 return this;
85 }
86
87 public CheckResultBuilder description(String description, Object... args) {
88 this.description = description;
89 this.descriptionArgs = args;
90 return this;
91 }
92
93 public CheckResultBuilder fatal() {
94 this.fatal = true;
95 return this;
96 }
97
98 public CheckResultBuilder from(ICheck check) {
99 this.name = check.getName();
100 this.group = check.getGroup();
101 return this;
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 public CheckResultBuilder messageBundle(String bundle, Locale locale) {
118 this.bundle = bundle;
119 this.locale = locale;
120 return this;
121 }
122
123 public CheckResultBuilder resolutionSteps(String resolutionSteps) {
124 this.resolutionSteps = resolutionSteps;
125 return this;
126 }
127
128 public CheckResultBuilder resolutionSteps(String resolutionSteps, Object... args) {
129 this.resolutionSteps = resolutionSteps;
130 this.resolutionStepArgs = args;
131 return this;
132 }
133
134 }