/** * Initialize the AggregateFactory for creating instances of the given {@code aggregateType}. * * @param aggregateType The type of aggregate this factory creates instances of. * @throws IncompatibleAggregateException if the aggregate constructor throws an exception, or if the JVM security * settings prevent the GenericAggregateFactory from calling the * constructor. */ public GenericAggregateFactory(Class<T> aggregateType) { super(aggregateType); Assert.isFalse(Modifier.isAbstract(aggregateType.getModifiers()), () -> "Given aggregateType may not be abstract"); try { this.constructor = ensureAccessible(aggregateType.getDeclaredConstructor()); } catch (NoSuchMethodException e) { throw new IncompatibleAggregateException(format("The aggregate [%s] doesn't provide a no-arg constructor.", aggregateType.getSimpleName()), e); } }
/** * {@inheritDoc} * <p> * * @throws IncompatibleAggregateException if the aggregate constructor throws an exception, or if the JVM security * settings prevent the GenericAggregateFactory from calling the * constructor. */ @SuppressWarnings({"unchecked"}) @Override protected T doCreateAggregate(String aggregateIdentifier, DomainEventMessage firstEvent) { try { return constructor.newInstance(); } catch (InstantiationException e) { throw new IncompatibleAggregateException(format( "The aggregate [%s] does not have a suitable no-arg constructor.", getAggregateType().getSimpleName()), e); } catch (IllegalAccessException e) { throw new IncompatibleAggregateException(format( "The aggregate no-arg constructor of the aggregate [%s] is not accessible. Please ensure that " + "the constructor is public or that the Security Manager allows access through " + "reflection.", getAggregateType().getSimpleName()), e); } catch (InvocationTargetException e) { throw new IncompatibleAggregateException(format( "The no-arg constructor of [%s] threw an exception on invocation.", getAggregateType().getSimpleName()), e); } } }
@SuppressWarnings("unchecked") @Override public void afterPropertiesSet() { if (!applicationContext.isPrototype(prototypeBeanName)) { throw new IncompatibleAggregateException( format("Cannot initialize repository '%s'. " + "The bean with name '%s' does not have the 'prototype' scope.", beanName, prototypeBeanName)); } this.delegate = new AbstractAggregateFactory<T>(getAggregateType()) { @Override protected T doCreateAggregate(String aggregateIdentifier, DomainEventMessage firstEvent) { return (T) applicationContext.getBean(prototypeBeanName); } @Override protected T postProcessInstance(T aggregate) { applicationContext.getAutowireCapableBeanFactory().configureBean(aggregate, prototypeBeanName); return aggregate; } }; } }
@Override protected void publish(EventMessage<?> msg) { super.publish(msg); snapshotTrigger.eventHandled(msg); if (identifierAsString() == null) { throw new IncompatibleAggregateException("Aggregate identifier must be non-null after applying an event. " + "Make sure the aggregate identifier is initialized at " + "the latest when handling the creation event."); } }
/** * {@inheritDoc} * <p> * * @throws IncompatibleAggregateException if the aggregate constructor throws an exception, or if the JVM security * settings prevent the GenericAggregateFactory from calling the * constructor. */ @SuppressWarnings({"unchecked"}) @Override protected T doCreateAggregate(String aggregateIdentifier, DomainEventMessage firstEvent) { try { return constructor.newInstance(); } catch (InstantiationException e) { throw new IncompatibleAggregateException(format( "The aggregate [%s] does not have a suitable no-arg constructor.", getAggregateType().getSimpleName()), e); } catch (IllegalAccessException e) { throw new IncompatibleAggregateException(format( "The aggregate no-arg constructor of the aggregate [%s] is not accessible. Please ensure that " + "the constructor is public or that the Security Manager allows access through " + "reflection.", getAggregateType().getSimpleName()), e); } catch (InvocationTargetException e) { throw new IncompatibleAggregateException(format( "The no-arg constructor of [%s] threw an exception on invocation.", getAggregateType().getSimpleName()), e); } } }
/** * Initialize the AggregateFactory for creating instances of the given {@code aggregateType}. * * @param aggregateType The type of aggregate this factory creates instances of. * @throws IncompatibleAggregateException if the aggregate constructor throws an exception, or if the JVM security * settings prevent the GenericAggregateFactory from calling the * constructor. */ public GenericAggregateFactory(Class<T> aggregateType) { super(aggregateType); Assert.isFalse(Modifier.isAbstract(aggregateType.getModifiers()), () -> "Given aggregateType may not be abstract"); try { this.constructor = ensureAccessible(aggregateType.getDeclaredConstructor()); } catch (NoSuchMethodException e) { throw new IncompatibleAggregateException(format("The aggregate [%s] doesn't provide a no-arg constructor.", aggregateType.getSimpleName()), e); } }
/** * Initialize the AggregateFactory for creating instances of the given {@code aggregateType}. * * @param aggregateType The type of aggregate this factory creates instances of. * @throws IncompatibleAggregateException if the aggregate constructor throws an exception, or if the JVM security * settings prevent the GenericAggregateFactory from calling the * constructor. */ public GenericAggregateFactory(Class<T> aggregateType) { super(aggregateType); Assert.isFalse(Modifier.isAbstract(aggregateType.getModifiers()), () -> "Given aggregateType may not be abstract"); try { this.constructor = ensureAccessible(aggregateType.getDeclaredConstructor()); } catch (NoSuchMethodException e) { throw new IncompatibleAggregateException(format("The aggregate [%s] doesn't provide a no-arg constructor.", aggregateType.getSimpleName()), e); } }
/** * {@inheritDoc} * <p> * * @throws IncompatibleAggregateException if the aggregate constructor throws an exception, or if the JVM security * settings prevent the GenericAggregateFactory from calling the * constructor. */ @SuppressWarnings({"unchecked"}) @Override protected T doCreateAggregate(String aggregateIdentifier, DomainEventMessage firstEvent) { try { return constructor.newInstance(); } catch (InstantiationException e) { throw new IncompatibleAggregateException(format( "The aggregate [%s] does not have a suitable no-arg constructor.", getAggregateType().getSimpleName()), e); } catch (IllegalAccessException e) { throw new IncompatibleAggregateException(format( "The aggregate no-arg constructor of the aggregate [%s] is not accessible. Please ensure that " + "the constructor is public or that the Security Manager allows access through " + "reflection.", getAggregateType().getSimpleName()), e); } catch (InvocationTargetException e) { throw new IncompatibleAggregateException(format( "The no-arg constructor of [%s] threw an exception on invocation.", getAggregateType().getSimpleName()), e); } } }
@Override protected void publish(EventMessage<?> msg) { super.publish(msg); snapshotTrigger.eventHandled(msg); if (identifierAsString() == null) { throw new IncompatibleAggregateException("Aggregate identifier must be non-null after applying an event. " + "Make sure the aggregate identifier is initialized at " + "the latest when handling the creation event."); } }
@Override protected void publish(EventMessage<?> msg) { super.publish(msg); snapshotTrigger.eventHandled(msg); if (identifierAsString() == null) { throw new IncompatibleAggregateException("Aggregate identifier must be non-null after applying an event. " + "Make sure the aggregate identifier is initialized at " + "the latest when handling the creation event."); } }