use of org.springframework.messaging.handler.HandlerMethod in project spring-framework by spring-projects.
the class AbstractMethodMessageHandler method getHandlerMethod.
@Nullable
private Match<T> getHandlerMethod(Message<?> message) {
List<Match<T>> matches = new ArrayList<>();
RouteMatcher.Route destination = getDestination(message);
List<T> mappingsByUrl = (destination != null ? this.destinationLookup.get(destination.value()) : null);
if (mappingsByUrl != null) {
addMatchesToCollection(mappingsByUrl, message, matches);
}
if (matches.isEmpty()) {
// No direct hits, go through all mappings
Set<T> allMappings = this.handlerMethods.keySet();
addMatchesToCollection(allMappings, message, matches);
}
if (matches.isEmpty()) {
handleNoMatch(destination, message);
return null;
}
Comparator<Match<T>> comparator = new MatchComparator(getMappingComparator(message));
matches.sort(comparator);
if (logger.isTraceEnabled()) {
logger.trace("Found " + matches.size() + " handler methods: " + matches);
}
Match<T> bestMatch = matches.get(0);
if (matches.size() > 1) {
Match<T> secondBestMatch = matches.get(1);
if (comparator.compare(bestMatch, secondBestMatch) == 0) {
HandlerMethod m1 = bestMatch.handlerMethod;
HandlerMethod m2 = secondBestMatch.handlerMethod;
throw new IllegalStateException("Ambiguous handler methods mapped for destination '" + (destination != null ? destination.value() : "") + "': {" + m1.getShortLogMessage() + ", " + m2.getShortLogMessage() + "}");
}
}
return bestMatch;
}
use of org.springframework.messaging.handler.HandlerMethod in project spring-framework by spring-projects.
the class AbstractMethodMessageHandler method createHandlerMethod.
/**
* Create a HandlerMethod instance from an Object handler that is either a handler
* instance or a String-based bean name.
*/
private HandlerMethod createHandlerMethod(Object handler, Method method) {
HandlerMethod handlerMethod;
if (handler instanceof String) {
ApplicationContext context = getApplicationContext();
Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
String beanName = (String) handler;
handlerMethod = new HandlerMethod(beanName, context.getAutowireCapableBeanFactory(), method);
} else {
handlerMethod = new HandlerMethod(handler, method);
}
return handlerMethod;
}
use of org.springframework.messaging.handler.HandlerMethod in project spring-framework by spring-projects.
the class InvocableHelper method initExceptionHandlerMethod.
/**
* Find an exception handling method for the given exception.
* <p>The default implementation searches methods in the class hierarchy of
* the HandlerMethod first and if not found, it continues searching for
* additional handling methods registered via
* {@link #registerExceptionHandlerAdvice}.
* @param handlerMethod the method where the exception was raised
* @param ex the exception raised or signaled
* @return a method to handle the exception, or {@code null}
*/
@Nullable
public InvocableHandlerMethod initExceptionHandlerMethod(HandlerMethod handlerMethod, Throwable ex) {
if (logger.isDebugEnabled()) {
logger.debug("Searching for methods to handle " + ex.getClass().getSimpleName());
}
Class<?> beanType = handlerMethod.getBeanType();
AbstractExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(beanType);
if (resolver == null) {
resolver = this.exceptionMethodResolverFactory.apply(beanType);
this.exceptionHandlerCache.put(beanType, resolver);
}
InvocableHandlerMethod exceptionHandlerMethod = null;
Method method = resolver.resolveMethod(ex);
if (method != null) {
exceptionHandlerMethod = new InvocableHandlerMethod(handlerMethod.getBean(), method);
} else {
for (Map.Entry<MessagingAdviceBean, AbstractExceptionHandlerMethodResolver> entry : this.exceptionHandlerAdviceCache.entrySet()) {
MessagingAdviceBean advice = entry.getKey();
if (advice.isApplicableToBeanType(beanType)) {
resolver = entry.getValue();
method = resolver.resolveMethod(ex);
if (method != null) {
exceptionHandlerMethod = new InvocableHandlerMethod(advice.resolveBean(), method);
break;
}
}
}
}
if (exceptionHandlerMethod != null) {
logger.debug("Found exception handler " + exceptionHandlerMethod.getShortLogMessage());
exceptionHandlerMethod.setArgumentResolvers(this.argumentResolvers.getResolvers());
} else {
logger.error("No exception handling method", ex);
}
return exceptionHandlerMethod;
}
use of org.springframework.messaging.handler.HandlerMethod in project spring-framework by spring-projects.
the class AbstractMethodMessageHandler method createHandlerMethod.
/**
* Create a HandlerMethod instance from an Object handler that is either a handler
* instance or a String-based bean name.
*/
protected HandlerMethod createHandlerMethod(Object handler, Method method) {
HandlerMethod handlerMethod;
if (handler instanceof String) {
ApplicationContext context = getApplicationContext();
Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
String beanName = (String) handler;
handlerMethod = new HandlerMethod(beanName, context.getAutowireCapableBeanFactory(), method);
} else {
handlerMethod = new HandlerMethod(handler, method);
}
return handlerMethod;
}
use of org.springframework.messaging.handler.HandlerMethod in project spring-framework by spring-projects.
the class AbstractMethodMessageHandler method registerHandlerMethod.
/**
* Register a handler method and its unique mapping.
* @param handler the bean name of the handler or the handler instance
* @param method the method to register
* @param mapping the mapping conditions associated with the handler method
* @throws IllegalStateException if another method was already registered
* under the same mapping
*/
protected void registerHandlerMethod(Object handler, Method method, T mapping) {
Assert.notNull(mapping, "Mapping must not be null");
HandlerMethod newHandlerMethod = createHandlerMethod(handler, method);
HandlerMethod oldHandlerMethod = this.handlerMethods.get(mapping);
if (oldHandlerMethod != null && !oldHandlerMethod.equals(newHandlerMethod)) {
throw new IllegalStateException("Ambiguous mapping found. Cannot map '" + newHandlerMethod.getBean() + "' bean method \n" + newHandlerMethod + "\nto " + mapping + ": There is already '" + oldHandlerMethod.getBean() + "' bean method\n" + oldHandlerMethod + " mapped.");
}
this.handlerMethods.put(mapping, newHandlerMethod);
for (String pattern : getDirectLookupDestinations(mapping)) {
this.destinationLookup.add(pattern, mapping);
}
}
Aggregations