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;
18  
19  
20  /**
21   * CustomDiagnosticTask is a wrapper on a custom defined DiagnosticTask.
22   * If the custom task could be created and initialized at configuration time then all messages are delegated to the task.
23   * If the custom task could not be created then this class provides the error message with the reason every it is asked to run.
24   * 
25   * @author Ernest Micklei
26   */
27  public class CustomDiagnosticTask extends DiagnosticTask {
28  	private static final long serialVersionUID = 3770101714847787671L;
29  	
30  	DiagnosticTask task;
31  	String errorMessage;
32  	
33  	public String getDescription() {
34  		if (task == null) // failed to create custom task
35  			return "No description available because task instance could not be created";
36  		else
37  			return task.getDescription();
38  	}
39  	public DiagnosticTaskResult createResult() {
40          if (task == null) // failed to create custom task
41              return super.createResult();
42          else 
43              return task.createResult();	    
44  	}
45  	public void run(ExecutionContext ctx, DiagnosticTaskResult result) throws DiagnoseException {
46  		if (task == null) { // failed to create custom task
47  			result.setErrorMessage(errorMessage);
48  			return;
49  		}
50  		task.run(ctx,result);
51  	}
52  	public void setUp(ExecutionContext ctx) throws DiagnoseException {
53  		if (task == null) // failed to create custom task
54  			throw new DiagnoseException(this.errorMessage);
55  		task.setUp(ctx);
56  	}
57  	public String getErrorMessage() {
58  		return errorMessage;
59  	}
60  	public void setErrorMessage(String errorMessage) {
61  		this.errorMessage = errorMessage;
62  	}
63  
64  	public DiagnosticTask getTask() {
65  		return task;
66  	}
67  
68  	public void setTask(DiagnosticTask task) {
69  		this.task = task;
70  	}	
71  	
72  	public String getComment() {
73  	    return task == null ? super.getComment() : task.getComment();
74  	}
75  }