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    
023    import java.lang.annotation.Annotation;
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.Exchange;
037    import org.apache.camel.TypeConverter;
038    import org.apache.camel.util.ObjectHelper;
039    import org.apache.camel.util.ResolverUtil;
040    import org.apache.camel.util.WebSphereResolverUtil;
041    import org.apache.commons.logging.Log;
042    import org.apache.commons.logging.LogFactory;
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: 65385 $
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;
054        private Set<Class> visitedClasses = new HashSet<Class>();
055        
056        public AnnotationTypeConverterLoader() {
057            // use WebSphere specific resolver if running on WebSphere
058            if (WebSphereResolverUtil.isWebSphereClassLoader(this.getClass().getClassLoader())) {
059                LOG.info("Using WebSphere specific ResolverUtil");
060                resolver = new WebSphereResolverUtil(META_INF_SERVICES);
061            } else {
062                resolver = new ResolverUtil();
063            }
064        }
065        
066        public AnnotationTypeConverterLoader(ResolverUtil resolverUtil) {
067            this.resolver = resolverUtil;
068        }
069    
070        public void load(TypeConverterRegistry registry) throws Exception {
071            String[] packageNames = findPackageNames();
072            resolver.findAnnotated(Converter.class, packageNames);
073            Set<Class> classes = resolver.getClasses();
074            for (Class type : classes) {
075                if (LOG.isDebugEnabled()) {
076                    LOG.debug("Loading converter class: " + ObjectHelper.name(type));
077                }
078                loadConverterMethods(registry, type);
079            }
080        }
081    
082        /**
083         * Finds the names of the packages to search for on the classpath looking
084         * for text files on the classpath at the {@link #META_INF_SERVICES} location.
085         *
086         * @return a collection of packages to search for
087         * @throws IOException is thrown for IO related errors
088         */
089        protected String[] findPackageNames() throws IOException {
090            Set<String> packages = new HashSet<String>();
091            findPackages(packages, Thread.currentThread().getContextClassLoader());
092            findPackages(packages, getClass().getClassLoader());
093            return packages.toArray(new String[packages.size()]);
094        }
095    
096        protected void findPackages(Set<String> packages, ClassLoader classLoader) throws IOException {
097            Enumeration<URL> resources = classLoader.getResources(META_INF_SERVICES);
098            while (resources.hasMoreElements()) {
099                URL url = resources.nextElement();
100                if (url != null) {
101                    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
102                    try {
103                        while (true) {
104                            String line = reader.readLine();
105                            if (line == null) {
106                                break;
107                            }
108                            line = line.trim();
109                            if (line.startsWith("#") || line.length() == 0) {
110                                continue;
111                            }
112                            tokenize(packages, line);
113                        }
114                    } finally {
115                        ObjectHelper.close(reader, null, LOG);
116                    }
117                }
118            }
119        }
120    
121        /**
122         * Tokenizes the line from the META-IN/services file using commas and
123         * ignoring whitespace between packages
124         */
125        protected void tokenize(Set<String> packages, String line) {
126            StringTokenizer iter = new StringTokenizer(line, ",");
127            while (iter.hasMoreTokens()) {
128                String name = iter.nextToken().trim();
129                if (name.length() > 0) {
130                    packages.add(name);
131                }
132            }
133        }
134    
135        /**
136         * Loads all of the converter methods for the given type
137         */
138        protected void loadConverterMethods(TypeConverterRegistry registry, Class type) {
139            if (visitedClasses.contains(type)) {
140                return;
141            }
142            visitedClasses.add(type);
143            try {
144                Method[] methods = type.getDeclaredMethods();
145                CachingInjector injector = null;
146    
147                for (Method method : methods) {
148                    // this may be prone to ClassLoader or packaging problems when the same class is defined
149                    // in two different jars (as is the case sometimes with specs).
150                    if (ObjectHelper.hasAnnotation(method, Converter.class, true)) {
151                        if (isValidConverterMethod(method)) {
152                            int modifiers = method.getModifiers();
153                            if (isAbstract(modifiers) || !isPublic(modifiers)) {
154                                LOG.warn("Ignoring bad converter on type: " + type.getName() + " method: " + method
155                                        + " as a converter method is not a public and concrete method");
156                            } else {
157                                Class<?> toType = method.getReturnType();
158                                if (toType.equals(Void.class)) {
159                                    LOG.warn("Ignoring bad converter on type: " + type.getName() + " method: "
160                                            + method + " as a converter method returns a void method");
161                                } else {
162                                    Class<?> fromType = method.getParameterTypes()[0];
163                                    if (isStatic(modifiers)) {
164                                        registerTypeConverter(registry, method, toType, fromType,
165                                                new StaticMethodTypeConverter(method));
166                                    } else {
167                                        if (injector == null) {
168                                            injector = new CachingInjector(registry, type);
169                                        }
170                                        registerTypeConverter(registry, method, toType, fromType,
171                                                new InstanceMethodTypeConverter(injector, method));
172                                    }
173                                }
174                            }
175                        } else {
176                            LOG.warn("Ignoring bad converter on type: " + type.getName() + " method: " + method
177                                    + " as a converter method should have one parameter");
178                        }
179                    }
180                }
181    
182                Class superclass = type.getSuperclass();
183                if (superclass != null && !superclass.equals(Object.class)) {
184                    loadConverterMethods(registry, superclass);
185                }
186            } catch (NoClassDefFoundError e) {
187                LOG.warn("Ignoring converter type: " + type.getName() + " as a dependent class could not be found: " + e, e);
188            }
189        }
190    
191        protected void registerTypeConverter(TypeConverterRegistry registry,
192                                             Method method, Class toType, Class fromType, TypeConverter typeConverter) {
193    
194            registry.addTypeConverter(toType, fromType, typeConverter);
195        }
196    
197        protected boolean isValidConverterMethod(Method method) {
198            Class<?>[] parameterTypes = method.getParameterTypes();
199            return (parameterTypes != null) && (parameterTypes.length == 1
200                || (parameterTypes.length == 2 && Exchange.class.isAssignableFrom(parameterTypes[1])));
201        }
202    }