/
AbstractReceiveStrategy.java
100 lines (83 loc) · 3.65 KB
/
AbstractReceiveStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.utils.persistentactors.commands;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.model.base.entity.Entity;
import org.eclipse.ditto.services.utils.akka.LogUtil;
import org.eclipse.ditto.services.utils.persistentactors.results.Result;
import org.eclipse.ditto.services.utils.persistentactors.results.ResultFactory;
import org.eclipse.ditto.signals.commands.base.Command;
import akka.event.DiagnosticLoggingAdapter;
/**
* This <em>Singleton</em> delegates a {@code Command} to a dedicated strategy - if one is available - to be handled.
*/
@Immutable
public abstract class AbstractReceiveStrategy<S extends Entity, I, E>
extends AbstractCommandStrategy<Command, S, I, E> {
protected final Map<Class<? extends Command>, CommandStrategy<? extends Command, S, I, E>> strategies =
new HashMap<>();
/**
* Constructs a new {@code AbstractCommandStrategy} object.
*
* @param theMatchingClass the class
* @throws NullPointerException if {@code theMatchingClass} is {@code null}.
*/
protected AbstractReceiveStrategy(
final Class<Command> theMatchingClass) {
super(theMatchingClass);
}
/**
* Add a command strategy. Call in constructor only.
*
* @param strategy the strategy.
*/
protected void addStrategy(final CommandStrategy<? extends Command, S, I, E> strategy) {
final Class<? extends Command> matchingClass = strategy.getMatchingClass();
strategies.put(matchingClass, strategy);
}
@Override
public Result<E> unhandled(final Context<I> context, @Nullable final S entity, final long nextRevision,
final Command command) {
final DiagnosticLoggingAdapter log = context.getLog();
LogUtil.enhanceLogWithCorrelationId(log, command);
log.info("Command of type <{}> cannot be handled by this strategy.", command.getClass().getName());
return ResultFactory.emptyResult();
}
@Override
public boolean isDefined(final Command command) {
return strategies.containsKey(command.getClass());
}
@Override
public boolean isDefined(final Context<I> context, @Nullable final S entity, final Command command) {
return isDefined(command);
}
@Override
protected Result<E> doApply(final Context<I> context, @Nullable final S entity, final long nextRevision,
final Command command) {
final CommandStrategy<Command, S, I, E> commandStrategy = getAppropriateStrategy(command.getClass());
final DiagnosticLoggingAdapter log = context.getLog();
LogUtil.enhanceLogWithCorrelationId(log, command);
LogUtil.enhanceLogWithCorrelationId(log, command.getDittoHeaders().getCorrelationId());
if (log.isDebugEnabled()) {
log.debug("Applying command <{}>: {}", command.getType(), command.toJsonString());
}
return commandStrategy.apply(context, entity, nextRevision, command);
}
@SuppressWarnings("unchecked")
private CommandStrategy<Command, S, I, E> getAppropriateStrategy(final Class commandClass) {
return (CommandStrategy<Command, S, I, E>) strategies.get(commandClass);
}
}