/** * Initializes a {@link AnnotationCommandTargetResolver} as specified through this Builder. * * @return a {@link AnnotationCommandTargetResolver} as specified through this Builder */ public AnnotationCommandTargetResolver build() { return new AnnotationCommandTargetResolver(this); } }
private Long findVersion(Message<?> command) throws InvocationTargetException, IllegalAccessException { return asLong(invokeAnnotated(command, versionAnnotation)); }
/** * {@inheritDoc} */ @Override public VersionedAggregateIdentifier resolveTarget(CommandMessage<?> command) { String aggregateIdentifier; Long aggregateVersion; try { aggregateIdentifier = findIdentifier(command); aggregateVersion = findVersion(command); } catch (InvocationTargetException e) { throw new IllegalArgumentException("An exception occurred while extracting aggregate " + "information form a command", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("The current security context does not allow extraction of " + "aggregate information from the given command.", e); } catch (NumberFormatException e) { throw new IllegalArgumentException("The value provided for the version is not a number.", e); } if (aggregateIdentifier == null) { throw new IllegalArgumentException( format("Invalid command. It does not identify the target aggregate. " + "Make sure at least one of the fields or methods in the [%s] class contains the " + "@TargetAggregateIdentifier annotation and that it returns a non-null value.", command.getPayloadType().getSimpleName())); } return new VersionedAggregateIdentifier(aggregateIdentifier, aggregateVersion); }
private String findIdentifier(Message<?> command)throws InvocationTargetException, IllegalAccessException { return Optional.ofNullable(invokeAnnotated(command, identifierAnnotation)).map(Object::toString).orElse(null); }
private Long findVersion(CommandMessage<?> command) throws InvocationTargetException, IllegalAccessException { for (Method m : methodsOf(command.getPayloadType())) { if (AnnotationUtils.isAnnotationPresent(m, TargetAggregateVersion.class)) { ensureAccessible(m); return asLong(m.invoke(command.getPayload())); } } for (Field f : fieldsOf(command.getPayloadType())) { if (AnnotationUtils.isAnnotationPresent(f, TargetAggregateVersion.class)) { return asLong(getFieldValue(f, command.getPayload())); } } return null; }
@Override public VersionedAggregateIdentifier resolveTarget(CommandMessage<?> command) { String aggregateIdentifier; Long aggregateVersion; try { aggregateIdentifier = findIdentifier(command); aggregateVersion = findVersion(command); } catch (InvocationTargetException e) { throw new IllegalArgumentException("An exception occurred while extracting aggregate " + "information form a command", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("The current security context does not allow extraction of " + "aggregate information from the given command.", e); } catch (NumberFormatException e) { throw new IllegalArgumentException("The value provided for the version is not a number.", e); } if (aggregateIdentifier == null) { throw new IllegalArgumentException( format("Invalid command. It does not identify the target aggregate. " + "Make sure at least one of the fields or methods in the [%s] class contains the " + "@TargetAggregateIdentifier annotation and that it returns a non-null value.", command.getPayloadType().getSimpleName())); } return new VersionedAggregateIdentifier(aggregateIdentifier, aggregateVersion); }