In the event that a sensor's properties must have a certain value in order for the sensor to function properly, a validation class can be written to ensure that the value entered by the user is correct. Example 21.8, “The ValidateVimProperties class” is an example of a class extending PropertyValidation. The validation class must be placed into the same package as the extended SensorInstaller class.
Example 21.8. The ValidateVimProperties class
public class ValidateVimProperties extends PropertyValidation {
/**
* Adds key-value pairs to the methodNames HashMap. This must be done in order for
* the method reflection to execute correctly when calling the validation methods in this class
*/
public ValidateVimProperties() {
this.methodNames.put("HACKYSTAT_VIM_SENSOR_DATA_FILE", "VimDataFilePath");
}
/**
* Validates the HACKYSTAT_VIM_SENSOR_DATA_FILE Property.
*
* @param dataFile the datafile location.
* @return true if the url is valid, false if not.
*/
public boolean validateVimDataFilePath (String dataFile) {
File file = new File(dataFile);
if (new File(file.getAbsolutePath()).exists()) {
return true;
}
else { // file path doesn't exist
Control.getInstance()
.notifyModelListeners(new PropertyEvent("Vim data filepath is invalid.", true));
return false;
}
}
}