Search in sources :

Example 1 with ModelValidator

use of org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.ModelValidator in project scheduling by ow2-proactive.

the class ModelFromURLParserValidator method createValidator.

@Override
protected Validator<String> createValidator(String model, Converter<String> converter) throws ModelSyntaxException {
    String modelFromUrlRegexp = "^" + MODEL_FROM_URL_TYPE_REGEXP + LEFT_DELIMITER_REGEXP + "(.+)" + RIGHT_DELIMITER_REGEXP + "$";
    String urlString = parseAndGetOneGroup(model, modelFromUrlRegexp);
    URL url = null;
    try {
        url = new URL(urlString);
        List<String> lines = IOUtils.readLines(url.openStream(), Charset.defaultCharset());
        String modelReceivedFromURL = null;
        modelReceivedFromURL = findFirstNonEmptyLineTrimmed(lines);
        if (Strings.isNullOrEmpty(modelReceivedFromURL)) {
            throw new ModelSyntaxException("In " + getType() + " expression, model received from defined url '" + urlString + "' is empty.");
        }
        if (modelReceivedFromURL.startsWith(MODEL_FROM_URL_TYPE)) {
            throw new ModelSyntaxException("In " + getType() + " expression, model received from defined url '" + urlString + "' is recursive.");
        }
        return new ModelValidator(modelReceivedFromURL);
    } catch (MalformedURLException e) {
        throw new ModelSyntaxException("In " + getType() + " expression, defined url '" + urlString + "' is invalid: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new ModelSyntaxException("In " + getType() + " expression, defined url '" + urlString + "' could not be reached: " + e.getMessage(), e);
    }
}
Also used : ModelSyntaxException(org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException) MalformedURLException(java.net.MalformedURLException) ModelValidator(org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.ModelValidator) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ModelSyntaxException (org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException)1 ModelValidator (org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.ModelValidator)1