Skip to content

Commit

Permalink
Avoid wrapping in plain RuntimeException in favor of IllegalStateExce…
Browse files Browse the repository at this point in the history
…ption

(cherry picked from commit e5122d0)
  • Loading branch information
jhoeller committed Jul 2, 2016
1 parent cd37873 commit fa624cd
Showing 1 changed file with 49 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
Expand Down Expand Up @@ -83,17 +83,13 @@ public abstract class AbstractMethodMessageHandler<T>

protected final Log logger = LogFactory.getLog(getClass());

private Collection<String> destinationPrefixes = new ArrayList<String>();

private final List<HandlerMethodArgumentResolver> customArgumentResolvers = new ArrayList<HandlerMethodArgumentResolver>(4);

private final List<HandlerMethodReturnValueHandler> customReturnValueHandlers = new ArrayList<HandlerMethodReturnValueHandler>(4);

private HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite();
private final HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite();

private HandlerMethodReturnValueHandlerComposite returnValueHandlers =new HandlerMethodReturnValueHandlerComposite();

private ApplicationContext applicationContext;
private final HandlerMethodReturnValueHandlerComposite returnValueHandlers =new HandlerMethodReturnValueHandlerComposite();

private final Map<T, HandlerMethod> handlerMethods = new LinkedHashMap<T, HandlerMethod>();

Expand All @@ -105,6 +101,16 @@ public abstract class AbstractMethodMessageHandler<T>
private final Map<MessagingAdviceBean, AbstractExceptionHandlerMethodResolver> exceptionHandlerAdviceCache =
new LinkedHashMap<MessagingAdviceBean, AbstractExceptionHandlerMethodResolver>(64);

private Collection<String> destinationPrefixes = new ArrayList<String>();

private ApplicationContext applicationContext;

/**
* Return the configured destination prefixes.
*/
public Collection<String> getDestinationPrefixes() {
return this.destinationPrefixes;
}

/**
* When this property is configured only messages to destinations matching
Expand All @@ -125,10 +131,10 @@ public void setDestinationPrefixes(Collection<String> prefixes) {
}

/**
* Return the configured destination prefixes.
* Return the configured custom argument resolvers, if any.
*/
public Collection<String> getDestinationPrefixes() {
return this.destinationPrefixes;
public List<HandlerMethodArgumentResolver> getCustomArgumentResolvers() {
return this.customArgumentResolvers;
}

/**
Expand All @@ -144,10 +150,10 @@ public void setCustomArgumentResolvers(List<HandlerMethodArgumentResolver> custo
}

/**
* Return the configured custom argument resolvers, if any.
* Return the configured custom return value handlers, if any.
*/
public List<HandlerMethodArgumentResolver> getCustomArgumentResolvers() {
return this.customArgumentResolvers;
public List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers() {
return this.customReturnValueHandlers;
}

/**
Expand All @@ -163,10 +169,10 @@ public void setCustomReturnValueHandlers(List<HandlerMethodReturnValueHandler> c
}

/**
* Return the configured custom return value handlers, if any.
* Return the configured argument resolvers, if any.
*/
public List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers() {
return this.customReturnValueHandlers;
public List<HandlerMethodArgumentResolver> getArgumentResolvers() {
return this.argumentResolvers.getResolvers();
}

/**
Expand All @@ -182,8 +188,11 @@ public void setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentRes
this.argumentResolvers.addResolvers(argumentResolvers);
}

public List<HandlerMethodArgumentResolver> getArgumentResolvers() {
return this.argumentResolvers.getResolvers();
/**
* Return the configured return value handlers, if any.
*/
public List<HandlerMethodReturnValueHandler> getReturnValueHandlers() {
return this.returnValueHandlers.getReturnValueHandlers();
}

/**
Expand All @@ -199,27 +208,15 @@ public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnV
this.returnValueHandlers.addHandlers(returnValueHandlers);
}

public List<HandlerMethodReturnValueHandler> getReturnValueHandlers() {
return this.returnValueHandlers.getReturnValueHandlers();
}

/**
* Return a map with all handler methods and their mappings.
*/
public Map<T, HandlerMethod> getHandlerMethods() {
return Collections.unmodifiableMap(this.handlerMethods);
public ApplicationContext getApplicationContext() {
return this.applicationContext;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}

public ApplicationContext getApplicationContext() {
return this.applicationContext;
}


@Override
public void afterPropertiesSet() {
if (this.argumentResolvers.getResolvers().isEmpty()) {
Expand Down Expand Up @@ -366,6 +363,13 @@ protected void registerExceptionHandlerAdvice(MessagingAdviceBean bean, Abstract
this.exceptionHandlerAdviceCache.put(bean, resolver);
}

/**
* Return a map with all handler methods and their mappings.
*/
public Map<T, HandlerMethod> getHandlerMethods() {
return Collections.unmodifiableMap(this.handlerMethods);
}


@Override
public void handleMessage(Message<?> message) throws MessagingException {
Expand Down Expand Up @@ -427,14 +431,14 @@ protected void handleMessageInternal(Message<?> message, String lookupDestinatio
addMatchesToCollection(allMappings, message, matches);
}
if (matches.isEmpty()) {
handleNoMatch(handlerMethods.keySet(), lookupDestination, message);
handleNoMatch(this.handlerMethods.keySet(), lookupDestination, message);
return;
}
Comparator<Match> comparator = new MatchComparator(getMappingComparator(message));
Collections.sort(matches, comparator);

if (logger.isTraceEnabled()) {
logger.trace("Found " + matches.size() + " methods: " + matches);
logger.trace("Found " + matches.size() + " handler methods: " + matches);
}

Match bestMatch = matches.get(0);
Expand All @@ -451,7 +455,6 @@ protected void handleMessageInternal(Message<?> message, String lookupDestinatio
handleMatch(bestMatch.mapping, bestMatch.handlerMethod, lookupDestination, message);
}


private void addMatchesToCollection(Collection<T> mappingsToCheck, Message<?> message, List<Match> matches) {
for (T mapping : mappingsToCheck) {
T match = getMatchingMapping(mapping, message);
Expand All @@ -470,6 +473,10 @@ private void addMatchesToCollection(Collection<T> mappingsToCheck, Message<?> me
*/
protected abstract T getMatchingMapping(T mapping, Message<?> message);

protected void handleNoMatch(Set<T> ts, String lookupDestination, Message<?> message) {
logger.debug("No matching message handler methods.");
}

/**
* Return a comparator for sorting matching mappings.
* The returned comparator should sort 'better' matches higher.
Expand All @@ -478,7 +485,6 @@ private void addMatchesToCollection(Collection<T> mappingsToCheck, Message<?> me
*/
protected abstract Comparator<T> getMappingComparator(Message<?> message);


protected void handleMatch(T mapping, HandlerMethod handlerMethod, String lookupDestination, Message<?> message) {
if (logger.isDebugEnabled()) {
logger.debug("Invoking " + handlerMethod.getShortLogMessage());
Expand Down Expand Up @@ -515,7 +521,7 @@ protected void handleMatch(T mapping, HandlerMethod handlerMethod, String lookup
protected void processHandlerMethodException(HandlerMethod handlerMethod, Exception ex, Message<?> message) {
InvocableHandlerMethod invocable = getExceptionHandlerMethod(handlerMethod, ex);
if (invocable == null) {
logger.error("Unhandled exception", ex);
logger.error("Unhandled exception from message handler method", ex);
return;
}
invocable.setMessageMethodArgumentResolvers(this.argumentResolvers);
Expand All @@ -535,8 +541,6 @@ protected void processHandlerMethodException(HandlerMethod handlerMethod, Except
}
}

protected abstract AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor(Class<?> beanType);

/**
* Find an {@code @MessageExceptionHandler} method for the given exception.
* The default implementation searches methods in the class hierarchy of the
Expand Down Expand Up @@ -575,9 +579,9 @@ protected InvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handler
return null;
}

protected void handleNoMatch(Set<T> ts, String lookupDestination, Message<?> message) {
logger.debug("No matching methods.");
}
protected abstract AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor(
Class<?> beanType);


@Override
public String toString() {
Expand All @@ -596,7 +600,7 @@ private class Match {

private final HandlerMethod handlerMethod;

private Match(T mapping, HandlerMethod handlerMethod) {
public Match(T mapping, HandlerMethod handlerMethod) {
this.mapping = mapping;
this.handlerMethod = handlerMethod;
}
Expand All @@ -622,13 +626,13 @@ public int compare(Match match1, Match match2) {
}
}


private class ReturnValueListenableFutureCallback implements ListenableFutureCallback<Object> {

private final InvocableHandlerMethod handlerMethod;

private final Message<?> message;


public ReturnValueListenableFutureCallback(InvocableHandlerMethod handlerMethod, Message<?> message) {
this.handlerMethod = handlerMethod;
this.message = message;
Expand All @@ -651,7 +655,7 @@ public void onFailure(Throwable ex) {
}

private void handleFailure(Throwable ex) {
Exception cause = (ex instanceof Exception ? (Exception) ex : new RuntimeException(ex));
Exception cause = (ex instanceof Exception ? (Exception) ex : new IllegalStateException(ex));
processHandlerMethodException(this.handlerMethod, cause, this.message);
}
}
Expand Down

0 comments on commit fa624cd

Please sign in to comment.