-
Notifications
You must be signed in to change notification settings - Fork 7
/
SmartSequenceStrategy.java
86 lines (71 loc) · 2.55 KB
/
SmartSequenceStrategy.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
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.biz.sequences;
import sirius.biz.sequences.jdbc.SQLSequenceStrategy;
import sirius.biz.sequences.mongo.MongoSequenceStrategy;
import sirius.db.jdbc.schema.Schema;
import sirius.db.mixing.Mixing;
import sirius.db.mongo.Mongo;
import sirius.kernel.di.GlobalContext;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Register;
import javax.annotation.Nonnull;
import java.util.function.Consumer;
/**
* Picks either the {@link SQLSequenceStrategy} or {@link MongoSequenceStrategy} based on the available configuration.
* <p>
* This is the standard strategy which determines if a JDBC database is present or a MongoDB and pick the effective
* strategy accordingly.
*/
@Register(framework = Sequences.FRAMEWORK_SEQUENCES)
public class SmartSequenceStrategy implements SequenceStrategy {
private SequenceStrategy delegate;
@Part
private Schema schema;
@Part
private Mongo mongo;
@Part
private GlobalContext ctx;
@Nonnull
@Override
public String getName() {
return "smart";
}
private SequenceStrategy getDelegate() {
if (delegate == null) {
if (schema.isConfigured(Mixing.DEFAULT_REALM)) {
Sequences.LOG.INFO("Using SQL based sequences...");
delegate = ctx.getPart(SQLSequenceStrategy.TYPE, SequenceStrategy.class);
} else {
Sequences.LOG.INFO("Using MongoDB for sequences...");
delegate = ctx.getPart(MongoSequenceStrategy.TYPE, SequenceStrategy.class);
if (!mongo.isConfigured()) {
Sequences.LOG.SEVERE(
"Sequences cannot determine a valid strategy! Neigher JDBC nor MongoDB are available!");
}
}
}
return delegate;
}
@Override
public Long tryGenerateId(String sequence) throws Exception {
return getDelegate().tryGenerateId(sequence);
}
@Override
public long peekNextValue(String sequence) throws Exception {
return getDelegate().peekNextValue(sequence);
}
@Override
public void setNextValue(String sequence, long nextValue, boolean force) throws Exception {
getDelegate().setNextValue(sequence, nextValue, force);
}
@Override
public void collectKnownSequences(Consumer<String> nameConsumer) throws Exception {
getDelegate().collectKnownSequences(nameConsumer);
}
}