org.apache.camel.impl.converter
Class BaseTypeConverterRegistry

java.lang.Object
  extended by org.apache.camel.support.ServiceSupport
      extended by org.apache.camel.impl.converter.BaseTypeConverterRegistry
All Implemented Interfaces:
Service, ShutdownableService, TypeConverterRegistry, StatefulService, SuspendableService, TypeConverter
Direct Known Subclasses:
DefaultTypeConverter, LazyLoadingTypeConverter

public abstract class BaseTypeConverterRegistry
extends ServiceSupport
implements TypeConverter, TypeConverterRegistry

Base implementation of a type converter registry used for type converters in Camel.

Version:

Nested Class Summary
protected static class BaseTypeConverterRegistry.FallbackTypeConverter
          Represents a fallback type converter
protected static class BaseTypeConverterRegistry.TypeMapping
          Represents a mapping from one type (which can be null) to another
 
Nested classes/interfaces inherited from interface org.apache.camel.spi.TypeConverterRegistry
TypeConverterRegistry.Statistics
 
Field Summary
protected  AtomicLong attemptCounter
           
protected  FactoryFinder factoryFinder
           
protected  AtomicLong failedCounter
           
protected  List<BaseTypeConverterRegistry.FallbackTypeConverter> fallbackConverters
           
protected  AtomicLong hitCounter
           
protected  Injector injector
           
protected  org.slf4j.Logger log
           
protected  AtomicLong missCounter
           
protected  LRUSoftCache<BaseTypeConverterRegistry.TypeMapping,BaseTypeConverterRegistry.TypeMapping> misses
           
protected  PackageScanClassResolver resolver
           
protected  TypeConverterRegistry.Statistics statistics
           
protected  List<TypeConverterLoader> typeConverterLoaders
           
protected  ConcurrentMap<BaseTypeConverterRegistry.TypeMapping,TypeConverter> typeMappings
           
 
Fields inherited from class org.apache.camel.support.ServiceSupport
shutdown, shuttingdown, started, starting, stopped, stopping, suspended, suspending
 
Constructor Summary
BaseTypeConverterRegistry(PackageScanClassResolver resolver, Injector injector, FactoryFinder factoryFinder)
           
 
Method Summary
 void addFallbackTypeConverter(TypeConverter typeConverter, boolean canPromote)
          Registers a new fallback type converter
 void addTypeConverter(Class<?> toType, Class<?> fromType, TypeConverter typeConverter)
          Registers a new type converter
<T> T
convertTo(Class<T> type, Exchange exchange, Object value)
          Converts the value to the specified type in the context of an exchange

Used when conversion requires extra information from the current exchange (such as encoding).

<T> T
convertTo(Class<T> type, Object value)
          Converts the value to the specified type
protected  Object doConvertTo(Class<?> type, Exchange exchange, Object value, boolean tryConvert)
           
protected  TypeConverter doLookup(Class<?> toType, Class<?> fromType, boolean isSuper)
           
protected  void doStart()
          Implementations override this method to support customized start/stop.
protected  void doStop()
          Implementations override this method to support customized start/stop.
 Set<Class<?>> getFromClassMappings()
           
 Injector getInjector()
          Gets the injector
protected
<T> TypeConverter
getOrFindTypeConverter(Class<?> toType, Object value)
           
 TypeConverterRegistry.Statistics getStatistics()
          Gets the utilization statistics of this type converter registry
 Map<Class<?>,TypeConverter> getToClassMappings(Class<?> fromClass)
           
 TypeConverter getTypeConverter(Class<?> toType, Class<?> fromType)
           
 List<TypeConverterLoader> getTypeConverterLoaders()
           
 Map<BaseTypeConverterRegistry.TypeMapping,TypeConverter> getTypeMappings()
           
 void loadCoreTypeConverters()
          Loads the core type converters which is mandatory to use Camel
protected  void loadFallbackTypeConverters()
           
protected  void loadTypeConverters()
          Checks if the registry is loaded and if not lazily load it
 TypeConverter lookup(Class<?> toType, Class<?> fromType)
          Performs a lookup for a given type converter.
<T> T
mandatoryConvertTo(Class<T> type, Exchange exchange, Object value)
          Converts the value to the specified type in the context of an exchange

Used when conversion requires extra information from the current exchange (such as encoding).

<T> T
mandatoryConvertTo(Class<T> type, Object value)
          Converts the value to the specified type
 void setInjector(Injector injector)
          Sets the injector to be used for creating new instances during type conversions.
<T> T
tryConvertTo(Class<T> type, Exchange exchange, Object value)
          Tries to convert the value to the specified type in the context of an exchange, returning null if not possible to convert.
<T> T
tryConvertTo(Class<T> type, Object value)
          Tries to convert the value to the specified type, returning null if not possible to convert.
 
Methods inherited from class org.apache.camel.support.ServiceSupport
doResume, doShutdown, doSuspend, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isSuspended, isSuspending, resume, shutdown, start, stop, suspend
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.camel.Service
start, stop
 

Field Detail

log

protected final transient org.slf4j.Logger log

typeMappings

protected final ConcurrentMap<BaseTypeConverterRegistry.TypeMapping,TypeConverter> typeMappings

misses

protected final LRUSoftCache<BaseTypeConverterRegistry.TypeMapping,BaseTypeConverterRegistry.TypeMapping> misses

typeConverterLoaders

protected final List<TypeConverterLoader> typeConverterLoaders

fallbackConverters

protected final List<BaseTypeConverterRegistry.FallbackTypeConverter> fallbackConverters

resolver

protected final PackageScanClassResolver resolver

injector

protected Injector injector

factoryFinder

protected final FactoryFinder factoryFinder

statistics

protected final TypeConverterRegistry.Statistics statistics

attemptCounter

protected final AtomicLong attemptCounter

missCounter

protected final AtomicLong missCounter

hitCounter

protected final AtomicLong hitCounter

failedCounter

protected final AtomicLong failedCounter
Constructor Detail

BaseTypeConverterRegistry

public BaseTypeConverterRegistry(PackageScanClassResolver resolver,
                                 Injector injector,
                                 FactoryFinder factoryFinder)
Method Detail

getTypeConverterLoaders

public List<TypeConverterLoader> getTypeConverterLoaders()

convertTo

public <T> T convertTo(Class<T> type,
                       Object value)
Description copied from interface: TypeConverter
Converts the value to the specified type

Specified by:
convertTo in interface TypeConverter
Parameters:
type - the requested type
value - the value to be converted
Returns:
the converted value, or null if not possible to convert

convertTo

public <T> T convertTo(Class<T> type,
                       Exchange exchange,
                       Object value)
Description copied from interface: TypeConverter
Converts the value to the specified type in the context of an exchange

Used when conversion requires extra information from the current exchange (such as encoding).

Specified by:
convertTo in interface TypeConverter
Parameters:
type - the requested type
exchange - the current exchange
value - the value to be converted
Returns:
the converted value, or null if not possible to convert

mandatoryConvertTo

public <T> T mandatoryConvertTo(Class<T> type,
                                Object value)
                     throws NoTypeConversionAvailableException
Description copied from interface: TypeConverter
Converts the value to the specified type

Specified by:
mandatoryConvertTo in interface TypeConverter
Parameters:
type - the requested type
value - the value to be converted
Returns:
the converted value, is never null
Throws:
NoTypeConversionAvailableException

mandatoryConvertTo

public <T> T mandatoryConvertTo(Class<T> type,
                                Exchange exchange,
                                Object value)
                     throws NoTypeConversionAvailableException
Description copied from interface: TypeConverter
Converts the value to the specified type in the context of an exchange

Used when conversion requires extra information from the current exchange (such as encoding).

Specified by:
mandatoryConvertTo in interface TypeConverter
Parameters:
type - the requested type
exchange - the current exchange
value - the value to be converted
Returns:
the converted value, is never null
Throws:
NoTypeConversionAvailableException

tryConvertTo

public <T> T tryConvertTo(Class<T> type,
                          Object value)
Description copied from interface: TypeConverter
Tries to convert the value to the specified type, returning null if not possible to convert.

This method will not throw an exception if an exception occurred during conversion.

Specified by:
tryConvertTo in interface TypeConverter
Parameters:
type - the requested type
value - the value to be converted
Returns:
the converted value, or null if not possible to convert

tryConvertTo

public <T> T tryConvertTo(Class<T> type,
                          Exchange exchange,
                          Object value)
Description copied from interface: TypeConverter
Tries to convert the value to the specified type in the context of an exchange, returning null if not possible to convert.

This method will not throw an exception if an exception occurred during conversion. Converts the value to the specified type in the context of an exchange

Used when conversion requires extra information from the current exchange (such as encoding).

Specified by:
tryConvertTo in interface TypeConverter
Parameters:
type - the requested type
exchange - the current exchange
value - the value to be converted
Returns:
the converted value, or null if not possible to convert

doConvertTo

protected Object doConvertTo(Class<?> type,
                             Exchange exchange,
                             Object value,
                             boolean tryConvert)

addTypeConverter

public void addTypeConverter(Class<?> toType,
                             Class<?> fromType,
                             TypeConverter typeConverter)
Description copied from interface: TypeConverterRegistry
Registers a new type converter

Specified by:
addTypeConverter in interface TypeConverterRegistry
Parameters:
toType - the type to convert to
fromType - the type to convert from
typeConverter - the type converter to use

addFallbackTypeConverter

public void addFallbackTypeConverter(TypeConverter typeConverter,
                                     boolean canPromote)
Description copied from interface: TypeConverterRegistry
Registers a new fallback type converter

Specified by:
addFallbackTypeConverter in interface TypeConverterRegistry
Parameters:
typeConverter - the type converter to use
canPromote - whether or not the fallback type converter can be promoted to a first class type converter

getTypeConverter

public TypeConverter getTypeConverter(Class<?> toType,
                                      Class<?> fromType)

getInjector

public Injector getInjector()
Description copied from interface: TypeConverterRegistry
Gets the injector

Specified by:
getInjector in interface TypeConverterRegistry
Returns:
the injector

setInjector

public void setInjector(Injector injector)
Description copied from interface: TypeConverterRegistry
Sets the injector to be used for creating new instances during type conversions.

Specified by:
setInjector in interface TypeConverterRegistry
Parameters:
injector - the injector

getFromClassMappings

public Set<Class<?>> getFromClassMappings()

getToClassMappings

public Map<Class<?>,TypeConverter> getToClassMappings(Class<?> fromClass)

getTypeMappings

public Map<BaseTypeConverterRegistry.TypeMapping,TypeConverter> getTypeMappings()

getOrFindTypeConverter

protected <T> TypeConverter getOrFindTypeConverter(Class<?> toType,
                                                   Object value)

lookup

public TypeConverter lookup(Class<?> toType,
                            Class<?> fromType)
Description copied from interface: TypeConverterRegistry
Performs a lookup for a given type converter.

Specified by:
lookup in interface TypeConverterRegistry
Parameters:
toType - the type to convert to
fromType - the type to convert from
Returns:
the type converter or null if not found.

doLookup

protected TypeConverter doLookup(Class<?> toType,
                                 Class<?> fromType,
                                 boolean isSuper)

loadCoreTypeConverters

public void loadCoreTypeConverters()
                            throws Exception
Loads the core type converters which is mandatory to use Camel

Throws:
Exception

loadTypeConverters

protected void loadTypeConverters()
                           throws Exception
Checks if the registry is loaded and if not lazily load it

Throws:
Exception

loadFallbackTypeConverters

protected void loadFallbackTypeConverters()
                                   throws IOException,
                                          ClassNotFoundException
Throws:
IOException
ClassNotFoundException

getStatistics

public TypeConverterRegistry.Statistics getStatistics()
Description copied from interface: TypeConverterRegistry
Gets the utilization statistics of this type converter registry

Specified by:
getStatistics in interface TypeConverterRegistry
Returns:
the utilization statistics

doStart

protected void doStart()
                throws Exception
Description copied from class: ServiceSupport
Implementations override this method to support customized start/stop.

Important: See ServiceSupport.doStop() for more details.

Specified by:
doStart in class ServiceSupport
Throws:
Exception
See Also:
ServiceSupport.doStop()

doStop

protected void doStop()
               throws Exception
Description copied from class: ServiceSupport
Implementations override this method to support customized start/stop.

Important: Camel will invoke this ServiceSupport.doStop() method when the service is being stopped. This method will also be invoked if the service is still in uninitialized state (eg has not been started). The method is always called to allow the service to do custom logic when the service is being stopped, such as when CamelContext is shutting down.

Specified by:
doStop in class ServiceSupport
Throws:
Exception
See Also:
ServiceSupport.doStart()


Apache CAMEL