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 transformers in memory.
025 * <p/>
026 * The registry contains two caches:
027 * <ul>
028 *     <li>static - which keeps all the transformers in the cache for the entire lifecycle</li>
029 *     <li>dynamic - which keeps the transformers in a {@link org.apache.camel.support.LRUCache} and may evict transformers which hasn't been requested recently</li>
030 * </ul>
031 * The static cache stores all the transformers 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 transformers that are created and used ad-hoc, such as from custom Java code that creates new transformers etc.
035 * The dynamic cache has an upper limit, that by default is 1000 entries.
036 *
037 * @param <K> transformer key
038 */
039public interface TransformerRegistry<K> extends Map<K, Transformer>, StaticService {
040
041    /**
042     * Lookup a {@link Transformer} in the registry which supports the transformation for
043     * the data types represented by the key.
044     * @param key a key represents the from/to data types to transform
045     * @return {@link Transformer} if matched, otherwise null
046     */
047    Transformer resolveTransformer(K key);
048
049    /**
050     * Number of transformers in the static registry.
051     */
052    int staticSize();
053
054    /**
055     * Number of transformers 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 transformers from the dynamic cache)
066     */
067    void purge();
068
069    /**
070     * Whether the given transformer is stored in the static cache
071     *
072     * @param scheme the scheme supported by this transformer
073     * @return <tt>true</tt> if in static cache, <tt>false</tt> if not
074     */
075    boolean isStatic(String scheme);
076
077    /**
078     * Whether the given transformer is stored in the static cache
079     *
080     * @param from  'from' data type
081     * @param to 'to' data type
082     * @return <tt>true</tt> if in static cache, <tt>false</tt> if not
083     */
084    boolean isStatic(DataType from, DataType to);
085
086    /**
087     * Whether the given transformer is stored in the dynamic cache
088     *
089     * @param scheme the scheme supported by this transformer
090     * @return <tt>true</tt> if in dynamic cache, <tt>false</tt> if not
091     */
092    boolean isDynamic(String scheme);
093
094    /**
095     * Whether the given {@link Transformer} is stored in the dynamic cache
096     *
097     * @param from 'from' data type
098     * @param to 'to' data type
099     * @return <tt>true</tt> if in dynamic cache, <tt>false</tt> if not
100     */
101    boolean isDynamic(DataType from, DataType to);
102
103    /**
104     * Cleanup the cache (purging stale entries)
105     */
106    void cleanUp();
107
108}