Sun GlassFish Enterprise Server v3 Add-On Component Development Guide

Validating Configuration Data

Validating configuration data ensures that attribute values that are being set or updated do not violate any constraints that you impose on the data. For example, you might require that an attribute that represents a name is not null, or an integer that represents a port number is within the range of available port numbers. Any attempt to set or update an attribute value that fails validation fails. Any validations that you specify for an attribute are performed when the attribute is initialized and every time the attribute is changed.

To standardize the validation of configuration data, Enterprise Server uses JSR 303: Bean Validation for validating configuration data. JSR 303 defines a metadata model and API for the validation of JavaBeans components.

To validate an attribute of an element, annotate the attribute's getter method with the annotation in the javax.validation.constraints package that performs the validation that you require. The following table lists commonly used annotations for validating Enterprise Server configuration data. For the complete list of annotations, see the javax.validation.constraints package summary.

Table 6–1 Commonly Used Annotations for Validating Enterprise Server Configuration Data

Validation 

Annotation 

Not null 

javax.validation.constraints.NotNull

Null 

javax.validation.constraints.Null

Minimum value 

javax.validation.constraints.Min

Set the value element of this annotation to the minimum allowed value.

Maximum value 

javax.validation.constraints.Max

Set the value element of this annotation to the maximum allowed value.

Regular expression matching

javax.validation.constraints.Pattern

Set the regexp element of this annotation to the regular expression that is to be matched.


Example 6–5 Specifying a Range of Valid Values for an Integer

This example specifies that the attribute rotation-interval-in-minutes must be a positive integer.

...
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
...
@Min(value=1) 
@Max(value=Integer.MAX_VALUE)
String getRotationIntervalInMinutes();
...


Example 6–6 Specifying Regular Expression Matching

This example specifies that the attribute classname must contain only non-whitespace characters.

import javax.validation.constraints.Pattern;
...
@Pattern(regexp="^[\\S]*$")
String getClassname();
...