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