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;
018
019 import java.lang.reflect.Method;
020 import java.util.ArrayList;
021 import java.util.Collections;
022 import java.util.Comparator;
023 import java.util.List;
024
025 import org.apache.camel.TypeConverter;
026 import org.apache.camel.impl.converter.AnnotationTypeConverterLoader;
027 import org.apache.camel.spi.PackageScanClassResolver;
028 import org.apache.camel.spi.TypeConverterRegistry;
029 import org.apache.camel.util.ObjectHelper;
030
031 /**
032 * Type converter loader that is capable of reporting the loaded type converters.
033 * <p/>
034 * Used by the camel-maven-plugin.
035 */
036 public class ReportingTypeConverterLoader extends AnnotationTypeConverterLoader {
037
038 private static final Comparator<TypeMapping> COMPARE_LAST_LOADED_FIRST = new Comparator<TypeMapping>() {
039 public int compare(TypeMapping t1, TypeMapping t2) {
040 if (ObjectHelper.equal(t1.fromType, t2.fromType)) {
041 return ObjectHelper.equal(t1.toType, t2.toType) ? t1.index - t2.index : ObjectHelper
042 .compare(getTypeName(t1.toType), getTypeName(t2.toType));
043 }
044 return ObjectHelper.compare(getTypeName(t1.fromType), getTypeName(t2.fromType));
045 }
046
047 };
048 private List<TypeMapping> typeMappings = new ArrayList<TypeMapping>();
049
050 public ReportingTypeConverterLoader(PackageScanClassResolver resolver) {
051 super(resolver);
052 }
053
054 public TypeMapping[] getTypeConversions() {
055 Collections.sort(typeMappings, COMPARE_LAST_LOADED_FIRST);
056 return typeMappings.toArray(new TypeMapping[typeMappings.size()]);
057 }
058
059 protected void registerTypeConverter(TypeConverterRegistry registry, Method method, Class toType,
060 Class fromType, TypeConverter typeConverter) {
061
062 TypeMapping mapping = new TypeMapping(toType, fromType, typeConverter.getClass(), method);
063 typeMappings.add(mapping);
064 }
065
066 private static String getTypeName(Class type) {
067 return type != null ? type.getName() : null;
068 }
069
070 /**
071 * Represents a mapping from one type (which can be null) to another
072 *
073 * Used by the camel-maven-plugin.
074 */
075 public static class TypeMapping {
076 private static int counter;
077 private Class toType;
078 private Class fromType;
079 private Class converterType;
080 private Method method;
081 private int index;
082
083 public TypeMapping(Class toType, Class fromType, Class converterType, Method method) {
084 this.toType = toType;
085 this.fromType = fromType;
086 this.converterType = converterType;
087 this.method = method;
088 this.index = counter++;
089 }
090
091 public Class getFromType() {
092 return fromType;
093 }
094
095 public Class getToType() {
096 return toType;
097 }
098
099 public Class getConverterType() {
100 return converterType;
101 }
102
103 public Method getMethod() {
104 return method;
105 }
106
107 public int getIndex() {
108 return index;
109 }
110
111 @Override
112 public boolean equals(Object object) {
113 if (object instanceof TypeMapping) {
114 TypeMapping that = (TypeMapping)object;
115 return this.index == that.index;
116 }
117 return false;
118 }
119
120 @Override
121 public int hashCode() {
122 int answer = toType.hashCode();
123 if (fromType != null) {
124 answer *= 37 + fromType.hashCode();
125 }
126 return answer;
127 }
128
129 @Override
130 public String toString() {
131 return "[" + fromType.getSimpleName() + "=>" + toType.getSimpleName() + "]";
132 }
133 }
134
135 }