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  import java.io.Serializable;
20  import java.util.Locale;
21  
22  import org.apache.log4j.Logger;
23  import org.xml.sax.Attributes;
24  
25  /**
26   * DiagnosticTask is the abstract class for all tasks that can be registered with SelfDiagnose. Typically tasks are
27   * parameterized in order to run. Tasks may want to re-implement the setUp method in which parameter verification should
28   * be done.
29   * 
30   * @author emicklei
31   */
32  public abstract class DiagnosticTask implements Serializable {
33  	private static final long serialVersionUID = -1320140869670492598L;
34  	public static final String PARAMETER_VARIABLE = "var";
35      public static final String PARAMETER_COMMENT= "comment";
36      public static final String PARAMETER_REPORT= "report";
37    
38  	protected String requestor = "{unknown}";
39  	/**
40  	 * Name of the reference that refers to the value of the property.
41  	 */
42  	public String variableName;	
43  	/**
44  	 * Used to explain the task in both the log and the configuration.
45  	 */
46  	public String comment;
47  	/**
48  	 * Indication for whether the receiver will report the result.
49  	 */
50  	public boolean reportResults = true;	
51  	/**
52  	 * Return an object to store the results of running the receiver.
53  	 * @return DiagnosticTaskResult
54  	 */
55  	public DiagnosticTaskResult createResult(){
56  		return new DiagnosticTaskResult(this);
57  	}
58  	/**
59  	 * @return String the description
60  	 */
61  	public abstract String getDescription();
62  	/**
63  	 * On default, the task is the unqualified name for the class in lowercase.
64  	 * This name must be equal to that of the complexType as defined in the XSD.
65  	 * 
66  	 * @return String the name for this task
67  	 */
68  	public String getTaskName() {
69  		Class theClass= this.getClass();
70  		String taskName = theClass.getName();
71  		return (taskName.substring(taskName.lastIndexOf('.') + 1)).toLowerCase(Locale.ENGLISH);
72  	}
73  	/**
74  	 * Return the identifier for the object that created and will register this task.
75  	 * @return String
76  	 */
77  	public String getRequestor() {
78  		return requestor;
79  	}
80  	/**
81  	 * Override this method to verify that task parameters are initialized/set correctly.
82  	 * @param ctx ExecutionContext
83  	 * 
84  	 * @throws DiagnoseException
85  	 */
86  	public void setUp(ExecutionContext ctx) throws DiagnoseException {
87  	}
88  	/**
89  	 * Run the task and answer the result.
90  	 * @return DiagnosticTaskResult
91  	 */
92  	public DiagnosticTaskResult run(){
93  		return this.run(new ExecutionContext());
94  	}
95  	/**
96  	 * Run the task and answer the result.
97  	 * @param ctx : ExecutionContext
98  	 * @return DiagnosticTaskResult
99  	 */
100 	public DiagnosticTaskResult run(ExecutionContext ctx){
101 		DiagnosticTaskResult result = this.createResult();
102 		long begin = System.currentTimeMillis();
103 		long end = begin;
104 		try {
105 			this.setUp(ctx);
106 			this.run(ctx, result);
107 			end = System.currentTimeMillis();
108 		} catch (DiagnoseException e) {
109 			result.setFailedMessage(e.getMessage());
110 		} catch (Exception ex) {
111 			String why = ex.getMessage();
112 			if (ex.getCause() != null) why = ex.getCause().getMessage();
113 			if (why == null) why = ex.toString();
114 			result.setErrorMessage("Unexpected error occurred while running this task because: " + why);
115 			Logger.getLogger(SelfDiagnose.class).error(ex);
116 		}		
117 		result.setExecutionTime(end-begin);
118 		return result;		
119 	}
120 	/**
121 	 * Run the task. If an error is detected then raise a DiagnoseException. 
122 	 * Otherwise use the result object to the report any messages when a run is completed.
123 	 * @param ctx ExecutionContext
124 	 * @param result DiagnosticTaskResult
125 	 * 
126 	 * @throws DiagnoseException
127 	 */
128 	public abstract void run(ExecutionContext ctx, DiagnosticTaskResult result) throws DiagnoseException;
129 	/**
130 	 * Set the identifier for the object that created and registered this task.
131 	 * @param identifier
132 	 */
133 	public void setRequestor(String identifier) {
134 		requestor = identifier;
135 	}
136 	/**
137 	 * This method is sent from the SelfDiagnoseHandler when a configuration is being processed. Use the passed
138 	 * attributes to initialize the receiver.
139 	 * If a variable parameter is passed then store it.
140 	 * 
141 	 * @param attributes
142 	 *        org.xml.sax.Attributes
143 	 */
144 	public void initializeFromAttributes(Attributes attributes){
145 	  this.setComment(attributes.getValue(PARAMETER_COMMENT));
146 	  this.setVariableName(attributes.getValue(PARAMETER_VARIABLE));
147 	  this.setReportResults(!"false".equals(attributes.getValue(PARAMETER_REPORT)));
148 	}
149 	
150 	/**
151 	 * User-defined Diagnostic Task subclasses should redefine this method to provide reporttemplates for both a successful and failed run.
152 	 * This is only needed if the task uses the method DiagnoseUtil.report(DiagnosticTaskResult result, boolean isSucces, ...). 
153 	 * @param isSuccess result of running the task
154 	 * @return String containing one or more template arguments such as {0}
155 	 */
156 	public String getDefaultReportTemplate(boolean isSuccess){
157 		return this.getClass().getName() + (isSuccess ? " passed " : " failed ") +
158 			"[missing getDefaultReportTemplate()]";
159 	}
160 	/** 
161 	 * @return String
162 	 */
163 	public String getVariableName() {
164 		return variableName;
165 	}	
166 	/** 
167 	 * @param varName : String
168 	 */
169 	public void setVariableName(String varName) {
170 		variableName = varName;
171 	}
172 	  public boolean hasComment() {
173 	      return comment != null && comment.length() > 0;
174 	  }
175 	 
176     public String getComment() {
177         return comment;
178     }
179     public void setComment(String comment) {
180         this.comment = comment;
181     }
182 	public boolean isReportResults() {
183 		return reportResults;
184 	}
185 	public void setReportResults(boolean reportResults) {
186 		this.reportResults = reportResults;
187 	}
188 }