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