INT-721 PayloadTypeRouter now considers the weight of the match so that an exact match is no longer required. Now interface and superclass types can also be valid candidates.

This commit is contained in:
Mark Fisher
2009-08-31 19:36:04 +00:00
parent c2d77a8c87
commit 50fc478102
5 changed files with 237 additions and 55 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,8 +27,8 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.integration.core.Message;
import org.springframework.integration.util.ClassUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* An implementation of {@link HandlerMethodResolver} that matches the payload
@@ -95,7 +95,7 @@ public class PayloadTypeMatchingHandlerMethodResolver implements HandlerMethodRe
if (parameterType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) parameterType;
Type rawType = extractRawTypeIfGeneric(parameterizedType.getRawType());
if (rawType instanceof Class) {
if (rawType instanceof Class<?>) {
Class<?> rawTypeClass = (Class<?>) rawType;
if (Message.class.isAssignableFrom(rawTypeClass)) {
expectedType = this.determineExpectedTypeFromParameterizedMessageType(parameterizedType);
@@ -105,7 +105,7 @@ public class PayloadTypeMatchingHandlerMethodResolver implements HandlerMethodRe
}
}
}
else if (parameterType instanceof Class) {
else if (parameterType instanceof Class<?>) {
expectedType = (Class<?>) parameterType;
}
Assert.notNull(expectedType, "Failed to determine expected type for parameter ["
@@ -124,18 +124,14 @@ public class PayloadTypeMatchingHandlerMethodResolver implements HandlerMethodRe
private Method findClosestMatch(Class<?> payloadType) {
Set<Class<?>> expectedTypes = this.methodMap.keySet();
int minTypeDiffWeight = Integer.MAX_VALUE;
Class<?> match = ClassUtils.findClosestMatch(payloadType, expectedTypes, true);
Method matchingMethod = null;
for (Class<?> expectedType : expectedTypes) {
int typeDiffWeight = getTypeDifferenceWeight(expectedType, payloadType);
if (typeDiffWeight < minTypeDiffWeight) {
minTypeDiffWeight = typeDiffWeight;
matchingMethod = this.methodMap.get(expectedType);
if (match != null) {
matchingMethod = this.methodMap.get(match);
if (matchingMethod != null) {
this.methodMap.put(payloadType, matchingMethod);
}
}
if (matchingMethod != null) {
this.methodMap.put(payloadType, matchingMethod);
}
return matchingMethod;
}
@@ -146,41 +142,16 @@ public class PayloadTypeMatchingHandlerMethodResolver implements HandlerMethodRe
WildcardType wildcardType = (WildcardType) actualType;
if (wildcardType.getUpperBounds().length == 1) {
Type upperBound = wildcardType.getUpperBounds()[0];
if (upperBound instanceof Class) {
if (upperBound instanceof Class<?>) {
expectedType = (Class<?>) upperBound;
}
}
}
else if (actualType instanceof Class) {
else if (actualType instanceof Class<?>) {
expectedType = (Class<?>) actualType;
}
return expectedType;
}
private int getTypeDifferenceWeight(Class<?> expectedType, Class<?> payloadType) {
int result = 0;
if (!ClassUtils.isAssignable(expectedType, payloadType)) {
return Integer.MAX_VALUE;
}
Class<?> superClass = payloadType.getSuperclass();
while (superClass != null) {
if (expectedType.equals(superClass)) {
result = result + 2;
superClass = null;
}
else if (ClassUtils.isAssignable(expectedType, superClass)) {
result = result + 2;
superClass = superClass.getSuperclass();
}
else {
superClass = null;
}
}
if (expectedType.isInterface()) {
result = result + 1;
}
return result;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.util.ClassUtils;
import org.springframework.util.Assert;
/**
@@ -42,7 +43,12 @@ public class PayloadTypeRouter extends AbstractSingleChannelRouter {
@Override
protected MessageChannel determineTargetChannel(Message<?> message) {
return this.payloadTypeChannelMap.get(message.getPayload().getClass());
Class<?> closestMatch = ClassUtils.findClosestMatch(
message.getPayload().getClass(), this.payloadTypeChannelMap.keySet(), true);
if (closestMatch != null) {
return this.payloadTypeChannelMap.get(closestMatch);
}
return null;
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.util;
import java.util.Set;
/**
* @author Mark Fisher
* @since 2.0
*/
public abstract class ClassUtils {
public static Class<?> findClosestMatch(Class<?> type, Set<Class<?>> candidates, boolean failOnTie) {
int minTypeDiffWeight = Integer.MAX_VALUE;
Class<?> closestMatch = null;
for (Class<?> candidate : candidates) {
int typeDiffWeight = getTypeDifferenceWeight(candidate, type);
if (typeDiffWeight < minTypeDiffWeight) {
minTypeDiffWeight = typeDiffWeight;
closestMatch = candidate;
}
else if (failOnTie && typeDiffWeight < Integer.MAX_VALUE && (typeDiffWeight == minTypeDiffWeight)) {
throw new IllegalStateException("Unresolvable ambiguity while attempting to find closest match for [" +
type.getName() + "]. Candidate types [" + closestMatch.getName() + "] and [" + candidate.getName() +
"] have equal weight.");
}
}
return closestMatch;
}
private static int getTypeDifferenceWeight(Class<?> candidate, Class<?> type) {
int result = 0;
if (!org.springframework.util.ClassUtils.isAssignable(candidate, type)) {
return Integer.MAX_VALUE;
}
Class<?> superClass = type.getSuperclass();
while (superClass != null) {
if (type.equals(superClass)) {
result = result + 2;
superClass = null;
}
else if (org.springframework.util.ClassUtils.isAssignable(candidate, superClass)) {
result = result + 2;
superClass = superClass.getSuperclass();
}
else {
superClass = null;
}
}
if (candidate.isInterface()) {
result = result + 1;
}
return result;
}
}