Custom service discovery sample configuration validator class

The configuration validator class implements a validate method to parse the configuration, and throws a ContentValidationException if the configuration is invalid.

An Element object is passed to the validate method. This object contains the DOM tree for the configuration.

Each instance, of the custom target environment, that is defined in the configuration is checked to make sure it has at least one WSDL URL. The configuration instance class is used to obtain the WSDL URL values.

See the Related link for more details on creating a configuration validator class.

The source code for the custom service discovery sample configuration validator class is as follows:

package com.ibm.sr.servicediscovery.http;

import java.net.URI;
import java.util.List;

import org.w3c.dom.Element;

import com.ibm.serviceregistry.exception.admin.ContentValidationException;
import com.ibm.serviceregistry.servicediscovery.DiscoveryConfigValidator;
import com.ibm.serviceregistry.servicediscovery.DiscoveryInstanceConfig;
import com.ibm.serviceregistry.servicediscovery.DiscoveryTaskConfig;

public class HTTPConfigurationValidator extends DiscoveryConfigValidator {
  /**
  * validate the configuration file format and content
  * 
  * @param configuration
  */
  public void validate(Element configuration) throws ContentValidationException {

    System.out.println("======== Validate configuration format =======");

    // Check we can parse the config
    HTTPParser configParser = new HTTPParser();
    DiscoveryTaskConfig disConfig = null;
    try {
      disConfig = configParser.parseConfig(configuration);
    } catch (Exception e) {
      System.out.println("HTTPDiscoverer: stack: ");
      e.printStackTrace();
      // if we cannot parse the config, throw a ContentValidationException to 
      // prevent the user saving it
      throw new ContentValidationException("Failed to parse configuration" +
        "for HTTPServiceDiscoverer", null, e);
    }
		
    // there might be more than one instance in the configuration
    // loop over them checking they are valid
    List instances = disConfig.getwsrtanceConfig();
    int len = instances.size();
    System.out.println("======== validate the instance content =======");
    for (int i = 0; i < len; i++) {
      DiscoveryInstanceConfig instance = (DiscoveryInstanceConfig)instances.get(i);
      // Cast it to our http instance type
      if (instance instanceof HTTPInstanceConfig) {
        HTTPInstanceConfig HTTPinstance = (HTTPInstanceConfig)instance;
        List<URI> wsdls = HTTPinstance.getWsdls();
        if (wsdls == null || wsdls.size() == 0) {
          throw new ContentValidationException
            ("Must specify at least one URL to discover", null);
        }
      }
    }
  }
}