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.io.InputStream;
20  import java.net.URL;
21  import java.util.Properties;
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.DiagnosticTaskResult;
28  import com.philemonworks.selfdiagnose.ExecutionContext;
29  /**
30   * CheckResourceProperty is a DiagnosticTask that verifies the availability of a property in a resource.
31   * 
32   * @author Ernest Micklei
33   */
34  public class CheckResourceProperty extends CheckProperty {
35      private static final long serialVersionUID = -5176698762404603297L;
36      protected static final String PARAMETER_NAME = "name";
37  	protected static final String PARAMETER_URL = "url";
38  	// parameters
39  	protected String name;		
40  	protected String url;	
41  
42  	/*
43  	 * (non-Javadoc)
44  	 * 
45  	 * @see com.philemonworks.selfdiagnose.DiagnosticTask#getDescription()
46  	 */
47  	public String getDescription() {
48  		return "Check that the value of a property matches this pattern. The properties resource must be on the classpath.";
49  	}	
50  	/*
51  	 * (non-Javadoc)
52  	 * 
53  	 * @see com.philemonworks.selfdiagnose.DiagnosticTask#initializeFromAttributes(Attributes)
54  	 */
55  	public void initializeFromAttributes(Attributes attributes) {
56  		// store variable if specified
57  		super.initializeFromAttributes(attributes);
58  		this.setName(attributes.getValue(PARAMETER_NAME));
59  	}
60  	public void setUp(ExecutionContext ctx) throws DiagnoseException {
61  		super.setUp(ctx);
62  		if (url == null)
63  			DiagnoseUtil.verifyNonEmptyString(PARAMETER_NAME, name, CheckProperty.class);
64  		if (name == null)
65  			DiagnoseUtil.verifyNonEmptyString(PARAMETER_URL, url, CheckProperty.class);
66  	}
67  	/**
68  	 * (non-Javadoc)
69  	 * 
70  	 * @see com.philemonworks.selfdiagnose.DiagnosticTask#run(ExecutionContext)
71  	 */
72  	public void run(ExecutionContext ctx, DiagnosticTaskResult result) throws DiagnoseException {
73  		String value = null;
74  		Properties properties = this.getProperties(ctx);
75  		value = properties.getProperty(property);
76          // make available in the context if needed (even if null)
77          ctx.setValue(this.getVariableName(), value);
78  		if (value == null) {
79  		    result.setFailedMessage("No such property available from resource [" + url + "]");
80  		    return;
81  		}
82  		if (pattern != null) {
83  			if (value.matches(pattern))
84  				result.setFailedMessage("Property value [" + value + "] does not match pattern [" + pattern + "] from resource [" + url + "]");
85  				return;
86  		}
87  		String msg = "Property [" + property + "] with value ["+value+"] exists in resource [" + url + "]";
88  		if (pattern != null) msg += " and matches [" + pattern + "]";
89  		result.setPassedMessage(msg);
90  	}
91  	
92  	protected Properties getProperties(ExecutionContext ctx) throws DiagnoseException {
93  		try {
94  			URL theUrl = DiagnoseUtil.retrieveURL(ctx,name,url);
95  			if (theUrl == null)
96  				throw new DiagnoseException("Unable to find resource [" + (name==null?url:name) + "]");
97  			Properties properties = new Properties();
98  			InputStream input = theUrl.openStream();		
99  			properties.load(input);
100 			return properties;
101 		} catch (Exception ex) {
102 			throw new DiagnoseException(ex);
103 		}		
104 	}
105 	
106 	public String getName() {
107 		return name;
108 	}
109 	public void setName(String name) {
110 		this.name = name;
111 	}
112 	public String getUrl() {
113 		return url;
114 	}
115 	public void setUrl(String url){ 
116 		this.url = url;
117 	}	
118 }