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.IOException;
20  import java.io.InputStream;
21  import java.net.URL;
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.DiagnosticTask;
28  import com.philemonworks.selfdiagnose.DiagnosticTaskResult;
29  import com.philemonworks.selfdiagnose.ExecutionContext;
30  
31  /**
32   * CheckResourceAccessible is a DiagnosticTask that verifies the availability of a resource by name on the classpath.
33   * <p/>
34   * <pre>
35  &lt;checkresourceaccessible name="application.properties" /&gt;
36   * </pre>
37   * Stores the URL (java.net.URL) of the resource into the (optional) specified variable. 
38   * @author emicklei
39   */
40  public class CheckResourceAccessible extends DiagnosticTask {
41  	private static final String PARAMETER_NAME = "name";
42  	private String name = null;
43  	/*
44  	 * (non-Javadoc)
45  	 * 
46  	 * @see com.philemonworks.selfdiagnose.DiagnosticTask#initializeFromAttributes(Attributes)
47  	 */
48  	public void initializeFromAttributes(Attributes attributes){
49  		super.initializeFromAttributes(attributes);
50  		this.setName(attributes.getValue(PARAMETER_NAME));
51  	}
52  	public void setUp(ExecutionContext ctx) throws DiagnoseException {
53  		super.setUp(ctx);
54  		DiagnoseUtil.verifyNonEmptyString(PARAMETER_NAME, name, CheckResourceAccessible.class);
55  	}
56  	/*
57  	 * (non-Javadoc)
58  	 * 
59  	 * @see com.philemonworks.selfdiagnose.DiagnosticTask#run()
60  	 */
61  	public void run(ExecutionContext ctx, DiagnosticTaskResult result) throws DiagnoseException {
62  		URL url = DiagnoseUtil.findResource(name,false);
63  		if (url == null)
64  			throw new DiagnoseException("Unable to find:" + name);	
65  		ctx.setValue(this.getVariableName(), url);
66  		try {
67  			InputStream is = url.openStream();
68  			if (is.available() == 0)
69  				result.setFailedMessage("No data available from [" + name + "] located at [" + url + "]");			
70  		} catch (IOException ex) {
71  			throw new DiagnoseException(ex);
72  		}
73  		result.setPassedMessage("Resource [" + name + "] is located at [" + url + "]");
74  	}
75  	/**
76  	 * @return String resourceName
77  	 */
78  	public String getName() {
79  		return name;
80  	}
81  	/**
82  	 * @param string
83  	 */
84  	public void setName(String string) {
85  		name = string;
86  	}
87  	/**
88  	 * Return the description of this task.
89  	 */	
90  	public String getDescription() {
91  		return "Check that this resource can be found and is accessible";
92  	}
93  }