View Javadoc

1   /*
2       Copyright 2006 Ernest Micklei @ PhilemonWorks.com
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      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,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
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   * CheckResourceBundleKey is a DiagnosticTask that verifies the existence of a given key for a given resource 
30   *
31   * <pre>
32  &lt;checkresourcebundlekey name="some.properties" key="welcom" /&gt;
33   * </pre>
34   * @author E.M.Micklei
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  	 * (non-Javadoc)
44  	 * 
45  	 * @see com.philemonworks.selfdiagnose.DiagnosticTask#initializeFromAttributes(Attributes)
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  }