Datasheet

package org.opensourceportals.validator;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.portlet.PortletPreferences;
import javax.portlet.PreferencesValidator;
import javax.portlet.ValidatorException;
/**
* @author Clay Richardson
*/
public class LuceneValidator implements PreferencesValidator {
/*
* In order to create a validator, we implement the
* javax.portlet.PreferencesValidator interface.
*
* If it fails validation, we throw a ValidatorException.
*
*/
We declare our LuceneValidator by extending PreferencesValidator.
public void validate(PortletPreferences preferences)
throws ValidatorException {
List problems = new ArrayList();
String indexPath = preferences.getValue(“indexPath”, null);
//Does the preference even exist?
if (indexPath == null) {
problems.add(“indexPath”);
throw new ValidatorException(
“indexPath preference doesn’t exist”,
problems);
} else {
//Let’s check to see if the index is actually there
//The segments file is a good file to key off of...
File index = new File(indexPath, “segments”);
if (!index.exists()) {
problems.add(“indexPath”);
throw new ValidatorException(
“The index doesn’t exist”,
problems);
}
}
}
}
The LuceneValidator checks first to see if the indexPath preference is actually set to some value.
If it is set, then it determines whether the specified directory contains a “segments” file, which would
indicate the presence of a Lucene search engine index. This is a perfect example of how you would
36
Chapter 1
04 469513 Ch01.qxd 1/16/04 11:04 AM Page 36