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.impl.converter;
018
019import java.lang.annotation.Annotation;
020import java.util.Collections;
021import java.util.LinkedHashSet;
022import java.util.Set;
023
024import org.apache.camel.spi.PackageScanClassResolver;
025import org.apache.camel.spi.PackageScanFilter;
026
027/**
028 * A {@link org.apache.camel.spi.ClassResolver} which loads type converters
029 * from an instance that implements {@link org.apache.camel.TypeConverters}.
030 * <p/>
031 * This is used when adding converters manually using the
032 * {@link org.apache.camel.impl.converter.BaseTypeConverterRegistry#addTypeConverters(org.apache.camel.TypeConverters)} method.
033 */
034public class TypeConvertersPackageScanClassResolver implements PackageScanClassResolver {
035
036    private final Set<ClassLoader> classLoaders = new LinkedHashSet<>();
037    private final Set<Class<?>> converters = new LinkedHashSet<>();
038
039    public TypeConvertersPackageScanClassResolver(Class<?> clazz) {
040        converters.add(clazz);
041        // use the classloader that loaded the class
042        classLoaders.add(clazz.getClassLoader());
043    }
044
045    @Override
046    public void setClassLoaders(Set<ClassLoader> classLoaders) {
047        // add all the class loaders
048        this.classLoaders.addAll(classLoaders);
049    }
050
051    @Override
052    public Set<ClassLoader> getClassLoaders() {
053        // return a new set to avoid any concurrency issues in other runtimes such as OSGi
054        return Collections.unmodifiableSet(new LinkedHashSet<>(classLoaders));
055    }
056
057    @Override
058    public void addClassLoader(ClassLoader classLoader) {
059        classLoaders.add(classLoader);
060    }
061
062    @Override
063    public Set<Class<?>> findAnnotated(Class<? extends Annotation> annotation, String... packageNames) {
064        return converters;
065    }
066
067    @Override
068    public Set<Class<?>> findAnnotated(Set<Class<? extends Annotation>> annotations, String... packageNames) {
069        return converters;
070    }
071
072    @Override
073    public Set<Class<?>> findImplementations(Class<?> parent, String... packageNames) {
074        // noop
075        return null;
076    }
077
078    @Override
079    public Set<Class<?>> findByFilter(PackageScanFilter filter, String... packageNames) {
080        // noop
081        return null;
082    }
083
084    @Override
085    public void addFilter(PackageScanFilter filter) {
086        // noop
087    }
088
089    @Override
090    public void removeFilter(PackageScanFilter filter) {
091        // noop
092    }
093}