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.spi;
018
019import java.util.Map;
020
021import org.apache.camel.StaticService;
022
023/**
024 * Registry to cache validators in memory.
025 * <p/>
026 * The registry contains two caches:
027 * <ul>
028 *     <li>static - which keeps all the validators in the cache for the entire lifecycle</li>
029 *     <li>dynamic - which keeps the validators in a {@link org.apache.camel.util.LRUCache} and may evict validators which hasn't been requested recently</li>
030 * </ul>
031 * The static cache stores all the validators that are created as part of setting up and starting routes.
032 * The static cache has no upper limit.
033 * <p/>
034 * The dynamic cache stores the validators that are created and used ad-hoc, such as from custom Java code that creates new validators etc.
035 * The dynamic cache has an upper limit, that by default is 1000 entries.
036 *
037 * @param <K> validator key
038 */
039public interface ValidatorRegistry<K> extends Map<K, Validator>, StaticService {
040
041    /**
042     * Lookup a {@link Validator} in the registry which supports the validation for
043     * the data type represented by the key.
044     * @param key a key represents the data type
045     * @return {@link Validator} if matched, otherwise null
046     */
047    Validator resolveValidator(K key);
048
049    /**
050     * Number of validators in the static registry.
051     */
052    int staticSize();
053
054    /**
055     * Number of validators in the dynamic registry
056     */
057    int dynamicSize();
058
059    /**
060     * Maximum number of entries to store in the dynamic registry
061     */
062    int getMaximumCacheSize();
063
064    /**
065     * Purges the cache (removes validators from the dynamic cache)
066     */
067    void purge();
068
069    /**
070     * Whether the given {@link Validator} is stored in the static cache
071     *
072     * @param type  the data type
073     * @return <tt>true</tt> if in static cache, <tt>false</tt> if not
074     */
075    boolean isStatic(DataType type);
076
077    /**
078     * Whether the given {@link Validator} is stored in the dynamic cache
079     *
080     * @param type the data type
081     * @return <tt>true</tt> if in dynamic cache, <tt>false</tt> if not
082     */
083    boolean isDynamic(DataType type);
084
085    /**
086     * Cleanup the cache (purging stale entries)
087     */
088    void cleanUp();
089
090}