Class AnnotationTypeConverterLoader

java.lang.Object
org.apache.camel.impl.converter.AnnotationTypeConverterLoader
All Implemented Interfaces:
org.apache.camel.spi.TypeConverterLoader
Direct Known Subclasses:
TypeConvertersLoader

public class AnnotationTypeConverterLoader extends Object implements org.apache.camel.spi.TypeConverterLoader
A class which will auto-discover Converter objects and methods to pre-load the TypeConverterRegistry of converters on startup.

This implementation supports scanning for type converters in JAR files. The META_INF_SERVICES contains a list of packages or FQN class names for Converter classes. The FQN class names is loaded first and directly by the class loader.

The PackageScanClassResolver is being used to scan packages for Converter classes and this procedure is slower than loading the Converter classes directly by its FQN class name. Therefore its recommended to specify FQN class names in the META_INF_SERVICES file. Likewise the procedure for scanning using PackageScanClassResolver may require custom implementations to work in various containers such as JBoss, OSGi, etc.

  • Field Details

    • META_INF_SERVICES

      public static final String META_INF_SERVICES
      See Also:
    • resolver

      protected final org.apache.camel.spi.PackageScanClassResolver resolver
    • visitedClasses

      protected final Set<Class<?>> visitedClasses
    • visitedURIs

      protected final Set<String> visitedURIs
  • Constructor Details

    • AnnotationTypeConverterLoader

      public AnnotationTypeConverterLoader(org.apache.camel.spi.PackageScanClassResolver resolver)
    • AnnotationTypeConverterLoader

      public AnnotationTypeConverterLoader(org.apache.camel.spi.PackageScanClassResolver resolver, String basePackage)
  • Method Details

    • load

      public void load(org.apache.camel.spi.TypeConverterRegistry registry) throws org.apache.camel.TypeConverterLoaderException
      Specified by:
      load in interface org.apache.camel.spi.TypeConverterLoader
      Throws:
      org.apache.camel.TypeConverterLoaderException
    • filterPackageNamesOnly

      protected String[] filterPackageNamesOnly(org.apache.camel.spi.PackageScanClassResolver resolver, String[] packageNames, Set<Class<?>> classes)
      Filters the given list of packages and returns an array of only package names.

      This implementation will check the given list of packages, and if it contains a class name, that class will be loaded directly and added to the list of classes. This optimizes the type converter to avoid excessive file scanning for .class files.

      Parameters:
      resolver - the class resolver
      packageNames - the package names
      classes - to add loaded @Converter classes
      Returns:
      the filtered package names
    • findPackageNames

      protected String[] findPackageNames() throws IOException
      Finds the names of the packages to search for on the classpath looking for text files on the classpath at the META_INF_SERVICES location.
      Returns:
      a collection of packages to search for
      Throws:
      IOException - is thrown for IO related errors
    • findPackages

      protected void findPackages(Set<String> packages, ClassLoader classLoader) throws IOException
      Throws:
      IOException
    • loadConverterMethods

      protected void loadConverterMethods(org.apache.camel.spi.TypeConverterRegistry registry, Class<?> type)
      Loads all of the converter methods for the given type
    • acceptClass

      protected boolean acceptClass(Class<?> clazz)
    • registerTypeConverter

      protected void registerTypeConverter(org.apache.camel.spi.TypeConverterRegistry registry, Method method, Class<?> toType, Class<?> fromType, org.apache.camel.TypeConverter typeConverter)
    • isValidConverterMethod

      protected boolean isValidConverterMethod(Method method)
    • registerFallbackTypeConverter

      protected void registerFallbackTypeConverter(org.apache.camel.spi.TypeConverterRegistry registry, org.apache.camel.TypeConverter typeConverter, Method method)
    • isValidFallbackConverterMethod

      protected boolean isValidFallbackConverterMethod(Method method)
    • filterUnwantedPackage

      protected static String[] filterUnwantedPackage(String name, String[] packageNames)
      Filters the given list of packages
      Parameters:
      name - the name to filter out
      packageNames - the packages
      Returns:
      he packages without the given name