|
18 | 18 | */
|
19 | 19 | package org.apache.polaris.persistence.cdi.persistence;
|
20 | 20 |
|
| 21 | +import static com.google.common.base.Preconditions.checkState; |
21 | 22 | import static org.apache.polaris.realms.id.RealmId.newRealmId;
|
22 | 23 |
|
23 |
| -import jakarta.enterprise.context.Dependent; |
24 |
| -import jakarta.enterprise.inject.spi.InjectionPoint; |
| 24 | +import jakarta.annotation.Nonnull; |
| 25 | +import jakarta.enterprise.context.ApplicationScoped; |
25 | 26 | import jakarta.inject.Inject;
|
26 | 27 | import org.apache.polaris.ids.api.IdGenerator;
|
27 | 28 | import org.apache.polaris.ids.api.MonotonicClock;
|
28 | 29 | import org.apache.polaris.persistence.api.Persistence;
|
29 | 30 | import org.apache.polaris.persistence.api.PersistenceParams;
|
30 |
| -import org.apache.polaris.persistence.api.RealmPersistence; |
| 31 | +import org.apache.polaris.persistence.api.RealmPersistenceFactory; |
31 | 32 | import org.apache.polaris.persistence.api.backend.Backend;
|
| 33 | +import org.apache.polaris.realms.id.RealmId; |
32 | 34 |
|
33 |
| -@RealmPersistence |
34 |
| -@Dependent |
35 |
| -public class ObservingRealmPersistence extends ObservingPersistence { |
36 |
| - private final Persistence delegate; |
| 35 | +@ApplicationScoped |
| 36 | +class ObservingRealmPersistence implements RealmPersistenceFactory { |
| 37 | + private final PersistenceParams persistenceConfig; |
| 38 | + private final Backend backend; |
| 39 | + private final IdGenerator idGenerator; |
| 40 | + private final MonotonicClock monotonicClock; |
| 41 | + private final PersistenceDecorators persistenceDecorators; |
37 | 42 |
|
38 | 43 | @Inject
|
39 | 44 | ObservingRealmPersistence(
|
40 | 45 | PersistenceParams persistenceConfig,
|
41 | 46 | Backend backend,
|
42 | 47 | IdGenerator idGenerator,
|
43 | 48 | MonotonicClock monotonicClock,
|
44 |
| - PersistenceDecorators persistenceDecorators, |
45 |
| - InjectionPoint injectionPoint) { |
46 |
| - for (var qualifier : injectionPoint.getQualifiers()) { |
47 |
| - if (qualifier instanceof RealmPersistence realmPersistence) { |
48 |
| - var id = realmPersistence.realmId(); |
49 |
| - var realmId = newRealmId(id); |
50 |
| - var persistence = |
51 |
| - backend.newPersistence(persistenceConfig, realmId, monotonicClock, idGenerator); |
52 |
| - this.delegate = persistenceDecorators.decorate(persistence); |
53 |
| - return; |
54 |
| - } |
55 |
| - } |
56 |
| - throw new IllegalStateException("Not a @RealmPersistence injection point: " + injectionPoint); |
| 49 | + PersistenceDecorators persistenceDecorators) { |
| 50 | + this.persistenceConfig = persistenceConfig; |
| 51 | + this.backend = backend; |
| 52 | + this.idGenerator = idGenerator; |
| 53 | + this.monotonicClock = monotonicClock; |
| 54 | + this.persistenceDecorators = persistenceDecorators; |
57 | 55 | }
|
58 | 56 |
|
59 | 57 | @Override
|
60 |
| - Persistence delegate() { |
61 |
| - return delegate; |
| 58 | + public RealmPersistenceBuilder newBuilder() { |
| 59 | + return new RealmPersistenceBuilder() { |
| 60 | + private RealmId realmId; |
| 61 | + private boolean consumed; |
| 62 | + |
| 63 | + @Override |
| 64 | + public RealmPersistenceBuilder realmId(@Nonnull RealmId realmId) { |
| 65 | + checkState(this.realmId == null, "RealmPersistenceBuilder can only be used once"); |
| 66 | + this.realmId = realmId; |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public RealmPersistenceBuilder realmId(@Nonnull String realmId) { |
| 72 | + return realmId(newRealmId(realmId)); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Persistence build() { |
| 77 | + checkState(!consumed, "RealmPersistenceBuilder can only be used once"); |
| 78 | + checkState(realmId != null, "Must call RealmPersistenceBuilder.setRealmId() before .build"); |
| 79 | + consumed = true; |
| 80 | + |
| 81 | + var persistence = |
| 82 | + backend.newPersistence(persistenceConfig, realmId, monotonicClock, idGenerator); |
| 83 | + var notObserved = persistenceDecorators.decorate(persistence); |
| 84 | + return new ObservingPersistence() { |
| 85 | + @Override |
| 86 | + Persistence delegate() { |
| 87 | + return notObserved; |
| 88 | + } |
| 89 | + }; |
| 90 | + } |
| 91 | + }; |
62 | 92 | }
|
63 | 93 | }
|
0 commit comments