@SuppressWarnings("unchecked") private Object handle(CommandMessage<?> commandMessage) throws Exception { List<AnnotatedCommandHandlerInterceptor<? super T>> interceptors = inspector.commandHandlerInterceptors() .stream() .filter(chi -> chi.canHandle(commandMessage)) .sorted((chi1, chi2) -> Integer.compare(chi2.priority(), chi1.priority())) .map(chi -> new AnnotatedCommandHandlerInterceptor<>(chi, aggregateRoot)) .collect(Collectors.toList()); MessageHandlingMember<? super T> handler = inspector.commandHandlers() .stream() .filter(mh -> mh.canHandle(commandMessage)) .findFirst() .orElseThrow(() -> new NoHandlerForCommandException(format("No handler available to handle command [%s]", commandMessage.getCommandName()))); Object result; if (interceptors.isEmpty()) { result = handler.handle(commandMessage, aggregateRoot); } else { result = new DefaultInterceptorChain<>( (UnitOfWork<CommandMessage<?>>) CurrentUnitOfWork.get(), interceptors, m -> handler.handle(commandMessage, aggregateRoot) ).proceed(); } if (aggregateRoot == null) { aggregateRoot = (T) result; return identifierAsString(); } return result; }
@SuppressWarnings("unchecked") private Object handle(CommandMessage<?> commandMessage) throws Exception { List<AnnotatedCommandHandlerInterceptor<? super T>> interceptors = inspector.commandHandlerInterceptors() .stream() .filter(chi -> chi.canHandle(commandMessage)) .sorted((chi1, chi2) -> Integer.compare(chi2.priority(), chi1.priority())) .map(chi -> new AnnotatedCommandHandlerInterceptor<>(chi, aggregateRoot)) .collect(Collectors.toList()); MessageHandlingMember<? super T> handler = inspector.commandHandlers() .stream() .filter(mh -> mh.canHandle(commandMessage)) .findFirst() .orElseThrow(() -> new NoHandlerForCommandException(format("No handler available to handle command [%s]", commandMessage.getCommandName()))); Object result; if (interceptors.isEmpty()) { result = handler.handle(commandMessage, aggregateRoot); } else { result = new DefaultInterceptorChain<>( (UnitOfWork<CommandMessage<?>>) CurrentUnitOfWork.get(), interceptors, m -> handler.handle(commandMessage, aggregateRoot) ).proceed(); } if (aggregateRoot == null) { aggregateRoot = (T) result; return identifierAsString(); } return result; }