Search in sources :

Example 1 with Validator

use of org.apache.camel.spi.Validator in project camel by apache.

the class DefaultValidatorRegistry method put.

@Override
public Validator put(ValidatorKey key, Validator validator) {
    // at first we must see if the key already exists and then replace it back, so it stays the same spot
    Validator answer = staticMap.remove(key);
    if (answer != null) {
        // replace existing
        staticMap.put(key, validator);
        return answer;
    }
    answer = super.remove(key);
    if (answer != null) {
        // replace existing
        super.put(key, validator);
        return answer;
    }
    // we want validators to be static if they are part of setting up or starting routes
    if (context.isSetupRoutes() || context.isStartingRoutes()) {
        answer = staticMap.put(key, validator);
    } else {
        answer = super.put(key, validator);
    }
    return answer;
}
Also used : Validator(org.apache.camel.spi.Validator)

Example 2 with Validator

use of org.apache.camel.spi.Validator in project camel by apache.

the class ManagedValidatorRegistry method listValidators.

@SuppressWarnings("unchecked")
public TabularData listValidators() {
    try {
        TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listValidatorsTabularType());
        Collection<Validator> validators = validatorRegistry.values();
        for (Validator validator : validators) {
            CompositeType ct = CamelOpenMBeanTypes.listValidatorsCompositeType();
            DataType type = validator.getType();
            String desc = validator.toString();
            boolean isStatic = validatorRegistry.isStatic(type);
            boolean isDynamic = validatorRegistry.isDynamic(type);
            CompositeData data = new CompositeDataSupport(ct, new String[] { "type", "static", "dynamic", "description" }, new Object[] { type.toString(), isStatic, isDynamic, desc });
            answer.put(data);
        }
        return answer;
    } catch (Exception e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
    }
}
Also used : TabularDataSupport(javax.management.openmbean.TabularDataSupport) CompositeData(javax.management.openmbean.CompositeData) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) DataType(org.apache.camel.spi.DataType) Validator(org.apache.camel.spi.Validator) TabularData(javax.management.openmbean.TabularData) CompositeType(javax.management.openmbean.CompositeType)

Example 3 with Validator

use of org.apache.camel.spi.Validator in project camel by apache.

the class AbstractLocalCamelController method getValidators.

@Override
public List<Map<String, String>> getValidators(String camelContextName) throws Exception {
    List<Map<String, String>> answer = new ArrayList<Map<String, String>>();
    if (camelContextName != null) {
        CamelContext context = this.getLocalCamelContext(camelContextName);
        if (context != null) {
            List<Validator> validators = new ArrayList<Validator>(context.getValidatorRegistry().values());
            for (Validator validator : validators) {
                Map<String, String> row = new LinkedHashMap<String, String>();
                row.put("camelContextName", context.getName());
                row.put("type", validator.getType().toString());
                row.put("state", validator.getStatus().toString());
                row.put("description", validator.toString());
                answer.add(row);
            }
        }
    }
    return answer;
}
Also used : CamelContext(org.apache.camel.CamelContext) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Validator(org.apache.camel.spi.Validator) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with Validator

use of org.apache.camel.spi.Validator in project camel by apache.

the class CustomValidatorDefinition method doCreateValidator.

@Override
protected Validator doCreateValidator(CamelContext context) throws Exception {
    if (ref == null && className == null) {
        throw new IllegalArgumentException("'ref' or 'type' must be specified for customValidator");
    }
    Validator validator;
    if (ref != null) {
        validator = context.getRegistry().lookupByNameAndType(ref, Validator.class);
        if (validator == null) {
            throw new IllegalArgumentException("Cannot find validator with ref:" + ref);
        }
        if (validator.getType() != null) {
            throw new IllegalArgumentException(String.format("Validator '%s' is already in use. Please check if duplicate validator exists.", ref));
        }
    } else {
        Class<Validator> validatorClass = context.getClassResolver().resolveMandatoryClass(className, Validator.class);
        if (validatorClass == null) {
            throw new IllegalArgumentException("Cannot find validator class: " + className);
        }
        validator = context.getInjector().newInstance(validatorClass);
    }
    validator.setCamelContext(context);
    return validator.setType(getType());
}
Also used : Validator(org.apache.camel.spi.Validator)

Example 5 with Validator

use of org.apache.camel.spi.Validator in project camel by apache.

the class ContractAdvice method doValidate.

private void doValidate(Message message, DataType type) throws ValidationException {
    Validator validator = message.getExchange().getContext().resolveValidator(type);
    if (validator != null) {
        LOG.debug("Applying validator: type='{}', validator='{}'", type, validator);
        validator.validate(message, type);
    } else {
        throw new ValidationException(message.getExchange(), String.format("No Validator found for '%s'", type));
    }
}
Also used : ValidationException(org.apache.camel.ValidationException) Validator(org.apache.camel.spi.Validator)

Aggregations

Validator (org.apache.camel.spi.Validator)5 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 CompositeData (javax.management.openmbean.CompositeData)1 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)1 CompositeType (javax.management.openmbean.CompositeType)1 TabularData (javax.management.openmbean.TabularData)1 TabularDataSupport (javax.management.openmbean.TabularDataSupport)1 CamelContext (org.apache.camel.CamelContext)1 ValidationException (org.apache.camel.ValidationException)1 DataType (org.apache.camel.spi.DataType)1