Custom plug-in sample validator class

When a new object is created in the registry, the sample validation plug-in adds a property called "Date_and_Time" to the object, and sets its value to the current date and time.

If an object is updated, and that object already has a property called "Date_and_Time", the validator updates its value to the current date and time.

The validator creates a logger object with a component name of "pluginsample.validator". This means that if you enable WebSphere® Application Server tracing for that component, the server logs the calls of the validator methods.

See the Related link for more details on creating a validation plug-in class.

The source code for the custom plug-in sample validator class is as follows:

package pluginsample.validator;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.ibm.serviceregistry.ServiceRegistryInvalidPropertyException;
import com.ibm.serviceregistry.ServiceRegistryStatus;
import com.ibm.serviceregistry.ServiceRegistryValidator;
import com.ibm.serviceregistry.sdo.OriginalObject;
import com.ibm.serviceregistry.sdo.helper.BSRSDOHelper;

public class StoreValidationDate implements ServiceRegistryValidator {

  private static Logger logger = Logger.getLogger("pluginsample.validator");

  private static final String CLASS_NAME = StoreValidationDate.class.getName();

  // Add a property to the new object with the current date and time before it
  // is persisted to the service registry.
  public ServiceRegistryStatus create(OriginalObject newObject) {
    final String METHOD_NAME = "create";
    if (logger.isLoggable(Level.FINEST)) {
      logger.entering(CLASS_NAME, METHOD_NAME);
    }
    ServiceRegistryStatus status = new ServiceRegistryStatus();

    // Try to add the property. If it fails, add the exception to the status
    // object, which will automatically set the status return code to ERROR.
    // This will result in a validation failure, stopping the create from
    // continuing.
    try {
      BSRSDOHelper.INSTANCE.addProperty(newObject, "Date_and_Time", new Date()
          .toString());
    } catch (ServiceRegistryInvalidPropertyException sripe) {
      status.addException(sripe);
    }

    if (logger.isLoggable(Level.FINEST)) {
      logger.exiting(CLASS_NAME, METHOD_NAME);
    }
    return status;
  }

  public ServiceRegistryStatus delete(OriginalObject oldObject) {
    return new ServiceRegistryStatus();
  }

  // Update the Date_and_Time property on the new object with the current date
  // and time before the triggering update is made.
  public ServiceRegistryStatus update(OriginalObject oldObject,
      OriginalObject newObject) {
    final String METHOD_NAME = "update";
    if (logger.isLoggable(Level.FINEST)) {
      logger.entering(CLASS_NAME, METHOD_NAME);
    }
    ServiceRegistryStatus status = new ServiceRegistryStatus();

    // Try to update the property value if it exists and is set.
    // If it fails, we add the exception to the status object, which will
    // automatically set the status return code to ERROR.
    // This will result in a validation failure, stopping the update from
    // continuing.
    try {
      if (BSRSDOHelper.INSTANCE.isPropertySet(newObject, "Date_and_Time")) {
        BSRSDOHelper.INSTANCE.setPropertyValue(newObject, "Date_and_Time",
            new Date().toString());
      }
    } catch (ServiceRegistryInvalidPropertyException sripe) {
      status.addException(sripe);
    }

    if (logger.isLoggable(Level.FINEST)) {
      logger.exiting(CLASS_NAME, METHOD_NAME);
    }
    return status;
  }
}