001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.reifier.validator;
018
019import java.util.HashMap;
020import java.util.Map;
021import java.util.function.BiFunction;
022
023import org.apache.camel.CamelContext;
024import org.apache.camel.model.validator.CustomValidatorDefinition;
025import org.apache.camel.model.validator.EndpointValidatorDefinition;
026import org.apache.camel.model.validator.PredicateValidatorDefinition;
027import org.apache.camel.model.validator.ValidatorDefinition;
028import org.apache.camel.reifier.AbstractReifier;
029import org.apache.camel.spi.ReifierStrategy;
030import org.apache.camel.spi.Validator;
031
032public abstract class ValidatorReifier<T> extends AbstractReifier  {
033
034    private static final Map<Class<?>, BiFunction<CamelContext, ValidatorDefinition, ValidatorReifier<? extends ValidatorDefinition>>> VALIDATORS;
035    static {
036        Map<Class<?>, BiFunction<CamelContext, ValidatorDefinition, ValidatorReifier<? extends ValidatorDefinition>>> map = new HashMap<>();
037        map.put(CustomValidatorDefinition.class, CustomValidatorReifier::new);
038        map.put(EndpointValidatorDefinition.class, EndpointValidatorReifier::new);
039        map.put(PredicateValidatorDefinition.class, PredicateValidatorReifier::new);
040        VALIDATORS = map;
041        ReifierStrategy.addReifierClearer(ValidatorReifier::clearReifiers);
042    }
043
044    protected final T definition;
045
046    public ValidatorReifier(CamelContext camelContext, T definition) {
047        super(camelContext);
048        this.definition = definition;
049    }
050
051    public static void registerReifier(Class<?> processorClass, BiFunction<CamelContext, ValidatorDefinition, ValidatorReifier<? extends ValidatorDefinition>> creator) {
052        VALIDATORS.put(processorClass, creator);
053    }
054
055    public static ValidatorReifier<? extends ValidatorDefinition> reifier(CamelContext camelContext, ValidatorDefinition definition) {
056        BiFunction<CamelContext, ValidatorDefinition, ValidatorReifier<? extends ValidatorDefinition>> reifier = VALIDATORS.get(definition.getClass());
057        if (reifier != null) {
058            return reifier.apply(camelContext, definition);
059        }
060        throw new IllegalStateException("Unsupported definition: " + definition);
061    }
062
063    public static void clearReifiers() {
064        VALIDATORS.clear();
065    }
066
067    public Validator createValidator() {
068        return doCreateValidator();
069    }
070
071    protected abstract Validator doCreateValidator();
072
073}