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     */
017    package org.apache.camel.impl.converter;
018    
019    
020    import java.io.BufferedReader;
021    import java.io.IOException;
022    import java.io.InputStreamReader;
023    
024    import java.lang.reflect.Method;
025    
026    import java.net.URL;
027    import java.util.Enumeration;
028    import java.util.HashSet;
029    import java.util.Set;
030    import java.util.StringTokenizer;
031    import static java.lang.reflect.Modifier.isAbstract;
032    import static java.lang.reflect.Modifier.isPublic;
033    import static java.lang.reflect.Modifier.isStatic;
034    
035    import org.apache.camel.Converter;
036    import org.apache.camel.TypeConverter;
037    import org.apache.camel.impl.CachingInjector;
038    import org.apache.camel.util.ObjectHelper;
039    import org.apache.camel.util.ResolverUtil;
040    import org.apache.commons.logging.Log;
041    import org.apache.commons.logging.LogFactory;
042    
043    
044    /**
045     * A class which will auto-discover converter objects and methods to pre-load
046     * the registry of converters on startup
047     *
048     * @version $Revision: 642753 $
049     */
050    public class AnnotationTypeConverterLoader implements TypeConverterLoader {
051        public static final String META_INF_SERVICES = "META-INF/services/org/apache/camel/TypeConverter";
052        private static final transient Log LOG = LogFactory.getLog(AnnotationTypeConverterLoader.class);
053        private ResolverUtil resolver = new ResolverUtil();
054        private Set<Class> visitedClasses = new HashSet<Class>();
055    
056        public void load(TypeConverterRegistry registry) throws Exception {
057            String[] packageNames = findPackageNames();
058            resolver.findAnnotated(Converter.class, packageNames);
059            Set<Class> classes = resolver.getClasses();
060            for (Class type : classes) {
061                if (LOG.isDebugEnabled()) {
062                    LOG.debug("Loading converter class: " + ObjectHelper.name(type));
063                }
064                loadConverterMethods(registry, type);
065            }
066        }
067    
068        /**
069         * Finds the names of the packages to search for on the classpath looking
070         * for text files on the classpath at the {@link #META_INF_SERVICES} location.
071         *
072         * @return a collection of packages to search for
073         * @throws IOException is thrown for IO relatede errors
074         */
075        protected String[] findPackageNames() throws IOException {
076            Set<String> packages = new HashSet<String>();
077            findPackages(packages, Thread.currentThread().getContextClassLoader());
078            findPackages(packages, getClass().getClassLoader());
079            return packages.toArray(new String[packages.size()]);
080        }
081    
082        protected void findPackages(Set<String> packages, ClassLoader classLoader) throws IOException {
083            Enumeration<URL> resources = classLoader.getResources(META_INF_SERVICES);
084            while (resources.hasMoreElements()) {
085                URL url = resources.nextElement();
086                if (url != null) {
087                    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
088                    try {
089                        while (true) {
090                            String line = reader.readLine();
091                            if (line == null) {
092                                break;
093                            }
094                            line = line.trim();
095                            if (line.startsWith("#") || line.length() == 0) {
096                                continue;
097                            }
098                            tokenize(packages, line);
099                        }
100                    } finally {
101                        try {
102                            reader.close();
103                        } catch (IOException e) {
104                            LOG.warn("Caught exception closing stream: " + e, e);
105                        }
106                    }
107                }
108            }
109        }
110    
111        /**
112         * Tokenizes the line from the META-IN/services file using commas and
113         * ignoring whitespace between packages
114         */
115        protected void tokenize(Set<String> packages, String line) {
116            StringTokenizer iter = new StringTokenizer(line, ",");
117            while (iter.hasMoreTokens()) {
118                String name = iter.nextToken().trim();
119                if (name.length() > 0) {
120                    packages.add(name);
121                }
122            }
123        }
124    
125        /**
126         * Loads all of the converter methods for the given type
127         */
128        protected void loadConverterMethods(TypeConverterRegistry registry, Class type) {
129            if (visitedClasses.contains(type)) {
130                return;
131            }
132            visitedClasses.add(type);
133            try {
134                Method[] methods = type.getDeclaredMethods();
135                CachingInjector injector = null;
136    
137                for (Method method : methods) {
138                    Converter annotation = method.getAnnotation(Converter.class);
139                    if (annotation != null) {
140                        Class<?>[] parameterTypes = method.getParameterTypes();
141                        if (parameterTypes == null || parameterTypes.length != 1) {
142                            LOG.warn("Ignoring bad converter on type: " + type.getName() + " method: " + method
143                                    + " as a converter method should have one parameter");
144                        } else {
145                            int modifiers = method.getModifiers();
146                            if (isAbstract(modifiers) || !isPublic(modifiers)) {
147                                LOG.warn("Ignoring bad converter on type: " + type.getName() + " method: " + method
148                                        + " as a converter method is not a public and concrete method");
149                            } else {
150                                Class toType = method.getReturnType();
151                                if (toType.equals(Void.class)) {
152                                    LOG.warn("Ignoring bad converter on type: " + type.getName() + " method: "
153                                            + method + " as a converter method returns a void method");
154                                } else {
155                                    Class fromType = parameterTypes[0];
156                                    if (isStatic(modifiers)) {
157                                        registerTypeConverter(registry, method, toType, fromType,
158                                                              new StaticMethodTypeConverter(method));
159                                    } else {
160                                        if (injector == null) {
161                                            injector = new CachingInjector(registry, type);
162                                        }
163                                        registerTypeConverter(registry, method, toType, fromType,
164                                                new InstanceMethodTypeConverter(injector, method));
165                                    }
166                                }
167                            }
168                        }
169                    }
170                }
171                Class superclass = type.getSuperclass();
172                if (superclass != null && !superclass.equals(Object.class)) {
173                    loadConverterMethods(registry, superclass);
174                }
175            } catch (NoClassDefFoundError e) {
176                LOG.debug("Ignoring converter type: " + type.getName() + " as a dependent class could not be found: " + e, e);
177            }
178        }
179    
180        protected void registerTypeConverter(TypeConverterRegistry registry, Method method,
181                                             Class toType, Class fromType, TypeConverter typeConverter) {
182    
183            registry.addTypeConverter(toType, fromType, typeConverter);
184        }
185    }