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