class JdbcConnectionUrlValidator implements ConstraintValidator<ValidJdbcConnectionUrl, String> {

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        context.disableDefaultConstraintViolation();
        if (value == null) {
            return true;
        }
        if (value.isEmpty()) {
            ValidatorUtil.addConstraintViolation(context, "JDBC connection URL may not be empty");
            return false;
        }

        try {
            DatabaseCommon.JdbcConnectionUrlFields fields = new DatabaseCommon().parseJdbcConnectionUrl(value);
            if (databaseExpected != fields.getDatabase().isPresent()) {
                ValidatorUtil.addConstraintViolation(context, "JDBC connection URL must " + (databaseExpected ? "" : "not ") + "have a database name");
                return false;
            }
        } catch (IllegalArgumentException e) {
            ValidatorUtil.addConstraintViolation(context, "JDBC connection URL is not valid");
            return false;
        }

        return true;
    }

}