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.List;
020
021import org.apache.camel.CamelContextAware;
022import org.apache.camel.LoggingLevel;
023import org.apache.camel.StaticService;
024import org.apache.camel.TypeConverter;
025import org.apache.camel.TypeConverterExists;
026import org.apache.camel.TypeConverters;
027
028/**
029 * Registry for type converters.
030 * <p/>
031 * The utilization {@link Statistics} is by default disabled, as it has a slight performance impact under very high
032 * concurrent load. The statistics can be enabled using {@link Statistics#setStatisticsEnabled(boolean)} method.
033 */
034public interface TypeConverterRegistry extends StaticService, CamelContextAware {
035
036    /**
037     * Utilization statistics of the this registry.
038     */
039    interface Statistics {
040
041        /**
042         * Number of noop attempts (no type conversion was needed)
043         */
044        long getNoopCounter();
045
046        /**
047         * Number of type conversion attempts
048         */
049        long getAttemptCounter();
050
051        /**
052         * Number of successful conversions
053         */
054        long getHitCounter();
055
056        /**
057         * Number of attempts which cannot be converted as no suitable type converter exists
058         */
059        long getMissCounter();
060
061        /**
062         * Number of failed attempts during type conversion
063         */
064        long getFailedCounter();
065
066        /**
067         * Reset the counters
068         */
069        void reset();
070
071        /**
072         * Whether statistics is enabled.
073         */
074        boolean isStatisticsEnabled();
075
076        /**
077         * Sets whether statistics is enabled.
078         *
079         * @param statisticsEnabled <tt>true</tt> to enable
080         */
081        void setStatisticsEnabled(boolean statisticsEnabled);
082    }
083
084    /**
085     * Registers a new type converter.
086     * <p/>
087     * This method may throw {@link org.apache.camel.TypeConverterExistsException} if configured to fail if an existing
088     * type converter already exists
089     *
090     * @param toType        the type to convert to
091     * @param fromType      the type to convert from
092     * @param typeConverter the type converter to use
093     */
094    void addTypeConverter(Class<?> toType, Class<?> fromType, TypeConverter typeConverter);
095
096    /**
097     * Removes the type converter
098     *
099     * @param toType        the type to convert to
100     * @param fromType      the type to convert from
101     * @return <tt>true</tt> if removed, <tt>false</tt> if the type converter didn't exist
102     */
103    boolean removeTypeConverter(Class<?> toType, Class<?> fromType);
104
105    /**
106     * Registers all the type converters from the class, each converter must be implemented as a method and annotated with {@link org.apache.camel.Converter}.
107     *
108     * @param typeConverters class which implements the type converters
109     */
110    void addTypeConverters(TypeConverters typeConverters);
111
112    /**
113     * Registers a new fallback type converter
114     *
115     * @param typeConverter the type converter to use
116     * @param canPromote  whether or not the fallback type converter can be promoted to a first class type converter
117     */
118    void addFallbackTypeConverter(TypeConverter typeConverter, boolean canPromote);
119
120    /**
121     * Performs a lookup for a given type converter.
122     *
123     * @param toType        the type to convert to
124     * @param fromType      the type to convert from
125     * @return the type converter or <tt>null</tt> if not found.
126     */
127    TypeConverter lookup(Class<?> toType, Class<?> fromType);
128
129    /**
130     * Gets a read-only list of the type converter from / to classes
131     *
132     * @return a list containing fromType/toType class names
133     */
134    List<Class<?>[]> listAllTypeConvertersFromTo();
135
136    /**
137     * Sets the injector to be used for creating new instances during type conversions.
138     *
139     * @param injector the injector
140     */
141    void setInjector(Injector injector);
142
143    /**
144     * Gets the injector
145     *
146     * @return the injector
147     */
148    Injector getInjector();
149
150    /**
151     * Gets the utilization statistics of this type converter registry
152     *
153     * @return the utilization statistics
154     */
155    Statistics getStatistics();
156
157    /**
158     * Number of type converters in the registry.
159     *
160     * @return number of type converters in the registry.
161     */
162    int size();
163
164    /**
165     * The logging level to use when logging that a type converter already exists when attempting to add a duplicate type converter.
166     * <p/>
167     * The default logging level is <tt>WARN</tt>
168     */
169    LoggingLevel getTypeConverterExistsLoggingLevel();
170
171    /**
172     * The logging level to use when logging that a type converter already exists when attempting to add a duplicate type converter.
173     * <p/>
174     * The default logging level is <tt>WARN</tt>
175     */
176    void setTypeConverterExistsLoggingLevel(LoggingLevel typeConverterExistsLoggingLevel);
177
178    /**
179     * What should happen when attempting to add a duplicate type converter.
180     * <p/>
181     * The default behavior is to override the existing.
182     */
183    TypeConverterExists getTypeConverterExists();
184
185    /**
186     * What should happen when attempting to add a duplicate type converter.
187     * <p/>
188     * The default behavior is to override the existing.
189     */
190    void setTypeConverterExists(TypeConverterExists typeConverterExists);
191
192}