View Javadoc

1   /*
2    * Copyright 2010 Capgemini and Contributors
3    *
4    * Licensed under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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 	 * Uses a message bundle to look for strings.
106 	 * <ul>
107 	 * <li>check name</li>
108 	 * <li>check group</li>
109 	 * <li>description</li>
110 	 * <li>resolutionSteps</li>
111 	 * </ul>
112 	 *
113 	 * @param bundle
114 	 * @param locale
115 	 * @return
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 }