Skip to content

Commit e2109fd

Browse files
authored
Merge pull request #146 from teaql/fix/entity-factory-context-134
Bind EntityFactory to invoking UserContext metadata
2 parents 2b150f0 + 1c14f98 commit e2109fd

2 files changed

Lines changed: 78 additions & 11 deletions

File tree

‎teaql-core/src/main/java/io/teaql/core/meta/EntityFactory.java‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package io.teaql.core.meta;
22

3-
import io.teaql.core.BaseEntity;
43
import io.teaql.core.Entity;
4+
import io.teaql.core.UserContext;
5+
import java.util.Objects;
56

67
/**
78
* Centralized factory for entity creation.
@@ -31,19 +32,19 @@ public interface EntityFactory {
3132
*/
3233
<T extends Entity> T createEntity(EntityDescriptor descriptor);
3334

34-
/**
35-
* Default implementation that delegates to EntityDescriptor.createEntity()
36-
*/
35+
/** Creates an entity factory bound to the invoking runtime's metadata. */
36+
static EntityFactory forContext(UserContext context) {
37+
return forMetadata(EntityMetaFactory.requireFrom(context));
38+
}
39+
40+
/** Creates an entity factory bound to an explicit metadata snapshot. */
3741
@SuppressWarnings("unchecked")
38-
static EntityFactory defaultFactory() {
42+
static EntityFactory forMetadata(EntityMetaFactory metadata) {
43+
Objects.requireNonNull(metadata, "metadata must not be null");
3944
return new EntityFactory() {
4045
@Override
4146
public <T extends Entity> T createEntity(Class<T> entityType) {
42-
EntityMetaFactory metaFactory = EntityMetaFactory.get();
43-
if (metaFactory == null) {
44-
throw new IllegalStateException("EntityMetaFactory not initialized");
45-
}
46-
for (EntityDescriptor descriptor : metaFactory.allEntityDescriptors()) {
47+
for (EntityDescriptor descriptor : metadata.allEntityDescriptors()) {
4748
if (descriptor.getTargetType() == entityType) {
4849
return (T) descriptor.createEntity();
4950
}
@@ -53,7 +54,7 @@ public <T extends Entity> T createEntity(Class<T> entityType) {
5354

5455
@Override
5556
public Entity createEntity(String typeName) {
56-
EntityDescriptor descriptor = EntityMetaFactory.get().resolveEntityDescriptor(typeName);
57+
EntityDescriptor descriptor = metadata.resolveEntityDescriptor(typeName);
5758
if (descriptor == null) {
5859
throw new IllegalArgumentException("No entity descriptor registered for " + typeName);
5960
}
@@ -66,4 +67,18 @@ public <T extends Entity> T createEntity(EntityDescriptor descriptor) {
6667
}
6768
};
6869
}
70+
71+
/**
72+
* Legacy process-global factory. Prefer {@link #forContext(UserContext)} so
73+
* two runtimes in one process cannot silently share entity metadata.
74+
*/
75+
@Deprecated
76+
static EntityFactory defaultFactory() {
77+
EntityMetaFactory metadata = EntityMetaFactory.get();
78+
if (metadata == null) {
79+
throw new IllegalStateException("Global EntityMetaFactory is not initialized; "
80+
+ "use EntityFactory.forContext(context)");
81+
}
82+
return forMetadata(metadata);
83+
}
6984
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.teaql.runtime;
2+
3+
import io.teaql.core.BaseEntity;
4+
import io.teaql.core.meta.EntityDescriptor;
5+
import io.teaql.core.meta.EntityFactory;
6+
import io.teaql.core.meta.EntityMetaFactory;
7+
import io.teaql.core.meta.SimpleEntityMetaFactory;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertTrue;
11+
12+
public class EntityFactoryContextTest {
13+
public static class FirstEntity extends BaseEntity {
14+
@Override public String typeName() { return "Shared"; }
15+
}
16+
17+
public static class SecondEntity extends BaseEntity {
18+
@Override public String typeName() { return "Shared"; }
19+
}
20+
21+
@Test
22+
public void sameTypeNameUsesEachInvokingContextEvenWithPoisonedGlobalMetadata() {
23+
SimpleEntityMetaFactory firstMetadata = metadata(FirstEntity.class, FirstEntity::new);
24+
SimpleEntityMetaFactory secondMetadata = metadata(SecondEntity.class, SecondEntity::new);
25+
EntityFactory firstFactory = EntityFactory.forContext(new DefaultUserContext(
26+
TeaQLRuntime.builder().metadata(firstMetadata).build()));
27+
EntityFactory secondFactory = EntityFactory.forContext(new DefaultUserContext(
28+
TeaQLRuntime.builder().metadata(secondMetadata).build()));
29+
30+
EntityMetaFactory previousGlobal = EntityMetaFactory.get();
31+
EntityMetaFactory.registerGlobal(firstMetadata);
32+
try {
33+
assertTrue(firstFactory.createEntity("Shared") instanceof FirstEntity);
34+
assertTrue(firstFactory.createEntity(FirstEntity.class) instanceof FirstEntity);
35+
assertTrue(secondFactory.createEntity("Shared") instanceof SecondEntity);
36+
assertTrue(secondFactory.createEntity(SecondEntity.class) instanceof SecondEntity);
37+
} finally {
38+
EntityMetaFactory.registerGlobal(previousGlobal);
39+
}
40+
}
41+
42+
private static <T extends BaseEntity> SimpleEntityMetaFactory metadata(
43+
Class<T> type, java.util.function.Supplier<T> supplier) {
44+
EntityDescriptor descriptor = new EntityDescriptor();
45+
descriptor.setType("Shared");
46+
descriptor.setTargetType(type);
47+
descriptor.setEntitySupplier(supplier);
48+
SimpleEntityMetaFactory metadata = new SimpleEntityMetaFactory();
49+
metadata.register(descriptor);
50+
return metadata;
51+
}
52+
}

0 commit comments

Comments
 (0)