kalix.javasdk.eventsourced.ReflectiveEventSourcedEntityProvider Maven / Gradle / Ivy
/*
* Copyright 2021 Lightbend Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kalix.javasdk.eventsourced;
import com.google.protobuf.Descriptors;
import kalix.javasdk.common.ForwardHeadersExtractor;
import kalix.javasdk.eventsourcedentity.EventSourcedEntity;
import kalix.javasdk.eventsourcedentity.EventSourcedEntityContext;
import kalix.javasdk.eventsourcedentity.EventSourcedEntityOptions;
import kalix.javasdk.eventsourcedentity.EventSourcedEntityProvider;
import kalix.javasdk.impl.ComponentDescriptor;
import kalix.javasdk.impl.ComponentDescriptorFactory$;
import kalix.javasdk.impl.JsonMessageCodec;
import kalix.javasdk.impl.MessageCodec;
import kalix.javasdk.impl.eventsourcedentity.EventSourceEntityHandlers;
import kalix.javasdk.impl.eventsourcedentity.EventSourcedEntityRouter;
import kalix.javasdk.impl.eventsourcedentity.EventSourcedHandlersExtractor;
import kalix.javasdk.impl.eventsourcedentity.ReflectiveEventSourcedEntityRouter;
import java.util.Optional;
import java.util.function.Function;
public class ReflectiveEventSourcedEntityProvider>
implements EventSourcedEntityProvider {
private final String entityType;
private final Function factory;
private final EventSourcedEntityOptions options;
private final Descriptors.FileDescriptor fileDescriptor;
private final Descriptors.ServiceDescriptor serviceDescriptor;
private final ComponentDescriptor componentDescriptor;
private final JsonMessageCodec messageCodec;
private final EventSourceEntityHandlers eventHandlers;
public static > ReflectiveEventSourcedEntityProvider of(
Class cls,
JsonMessageCodec messageCodec,
Function factory) {
return new ReflectiveEventSourcedEntityProvider<>(
cls, messageCodec, factory, EventSourcedEntityOptions.defaults());
}
public ReflectiveEventSourcedEntityProvider(
Class entityClass,
JsonMessageCodec messageCodec,
Function factory,
EventSourcedEntityOptions options) {
String typeId = ComponentDescriptorFactory$.MODULE$.readTypeIdValue(entityClass);
if (typeId == null)
throw new IllegalArgumentException(
"Event Sourced Entity [" + entityClass.getName() + "] is missing '@TypeId' annotation");
this.eventHandlers = EventSourcedHandlersExtractor.handlersFrom(entityClass, messageCodec);
if (this.eventHandlers.errors().nonEmpty()) {
throw new IllegalArgumentException(
"Event Sourced Entity ["
+ entityClass.getName()
+ "] has event handlers configured incorrectly: "
+ this.eventHandlers.errors());
}
this.entityType = typeId;
this.factory = factory;
this.options = options.withForwardHeaders(ForwardHeadersExtractor.extractFrom(entityClass));
this.messageCodec = messageCodec;
this.componentDescriptor = ComponentDescriptor.descriptorFor(entityClass, messageCodec);
this.fileDescriptor = componentDescriptor.fileDescriptor();
this.serviceDescriptor = componentDescriptor.serviceDescriptor();
}
@Override
public EventSourcedEntityOptions options() {
return options;
}
@Override
public Descriptors.ServiceDescriptor serviceDescriptor() {
return serviceDescriptor;
}
@Override
public String entityType() {
return entityType;
}
@Override
public EventSourcedEntityRouter newRouter(EventSourcedEntityContext context) {
ES entity = factory.apply(context);
return new ReflectiveEventSourcedEntityRouter<>(
entity, componentDescriptor.commandHandlers(), eventHandlers.handlers(), messageCodec);
}
@Override
public Descriptors.FileDescriptor[] additionalDescriptors() {
return new Descriptors.FileDescriptor[] {fileDescriptor};
}
@Override
public Optional alternativeCodec() {
return Optional.of(messageCodec);
}
}