From b2397d07bdcef78e48cc92f25b73a7170ad16db0 Mon Sep 17 00:00:00 2001 From: Philip Z Date: Wed, 23 Sep 2026 08:16:49 +0800 Subject: [PATCH] feat: bind entity factory to invoking context metadata --- .../io/teaql/core/meta/EntityFactory.java | 37 +++++++++---- .../runtime/EntityFactoryContextTest.java | 52 +++++++++++++++++++ 2 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 teaql-runtime/src/test/java/io/teaql/runtime/EntityFactoryContextTest.java diff --git a/teaql-core/src/main/java/io/teaql/core/meta/EntityFactory.java b/teaql-core/src/main/java/io/teaql/core/meta/EntityFactory.java index d24fab48f..c4a89f827 100644 --- a/teaql-core/src/main/java/io/teaql/core/meta/EntityFactory.java +++ b/teaql-core/src/main/java/io/teaql/core/meta/EntityFactory.java @@ -1,7 +1,8 @@ package io.teaql.core.meta; -import io.teaql.core.BaseEntity; import io.teaql.core.Entity; +import io.teaql.core.UserContext; +import java.util.Objects; /** * Centralized factory for entity creation. @@ -31,19 +32,19 @@ public interface EntityFactory { */ T createEntity(EntityDescriptor descriptor); - /** - * Default implementation that delegates to EntityDescriptor.createEntity() - */ + /** Creates an entity factory bound to the invoking runtime's metadata. */ + static EntityFactory forContext(UserContext context) { + return forMetadata(EntityMetaFactory.requireFrom(context)); + } + + /** Creates an entity factory bound to an explicit metadata snapshot. */ @SuppressWarnings("unchecked") - static EntityFactory defaultFactory() { + static EntityFactory forMetadata(EntityMetaFactory metadata) { + Objects.requireNonNull(metadata, "metadata must not be null"); return new EntityFactory() { @Override public T createEntity(Class entityType) { - EntityMetaFactory metaFactory = EntityMetaFactory.get(); - if (metaFactory == null) { - throw new IllegalStateException("EntityMetaFactory not initialized"); - } - for (EntityDescriptor descriptor : metaFactory.allEntityDescriptors()) { + for (EntityDescriptor descriptor : metadata.allEntityDescriptors()) { if (descriptor.getTargetType() == entityType) { return (T) descriptor.createEntity(); } @@ -53,7 +54,7 @@ public T createEntity(Class entityType) { @Override public Entity createEntity(String typeName) { - EntityDescriptor descriptor = EntityMetaFactory.get().resolveEntityDescriptor(typeName); + EntityDescriptor descriptor = metadata.resolveEntityDescriptor(typeName); if (descriptor == null) { throw new IllegalArgumentException("No entity descriptor registered for " + typeName); } @@ -66,4 +67,18 @@ public T createEntity(EntityDescriptor descriptor) { } }; } + + /** + * Legacy process-global factory. Prefer {@link #forContext(UserContext)} so + * two runtimes in one process cannot silently share entity metadata. + */ + @Deprecated + static EntityFactory defaultFactory() { + EntityMetaFactory metadata = EntityMetaFactory.get(); + if (metadata == null) { + throw new IllegalStateException("Global EntityMetaFactory is not initialized; " + + "use EntityFactory.forContext(context)"); + } + return forMetadata(metadata); + } } diff --git a/teaql-runtime/src/test/java/io/teaql/runtime/EntityFactoryContextTest.java b/teaql-runtime/src/test/java/io/teaql/runtime/EntityFactoryContextTest.java new file mode 100644 index 000000000..c88f39897 --- /dev/null +++ b/teaql-runtime/src/test/java/io/teaql/runtime/EntityFactoryContextTest.java @@ -0,0 +1,52 @@ +package io.teaql.runtime; + +import io.teaql.core.BaseEntity; +import io.teaql.core.meta.EntityDescriptor; +import io.teaql.core.meta.EntityFactory; +import io.teaql.core.meta.EntityMetaFactory; +import io.teaql.core.meta.SimpleEntityMetaFactory; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class EntityFactoryContextTest { + public static class FirstEntity extends BaseEntity { + @Override public String typeName() { return "Shared"; } + } + + public static class SecondEntity extends BaseEntity { + @Override public String typeName() { return "Shared"; } + } + + @Test + public void sameTypeNameUsesEachInvokingContextEvenWithPoisonedGlobalMetadata() { + SimpleEntityMetaFactory firstMetadata = metadata(FirstEntity.class, FirstEntity::new); + SimpleEntityMetaFactory secondMetadata = metadata(SecondEntity.class, SecondEntity::new); + EntityFactory firstFactory = EntityFactory.forContext(new DefaultUserContext( + TeaQLRuntime.builder().metadata(firstMetadata).build())); + EntityFactory secondFactory = EntityFactory.forContext(new DefaultUserContext( + TeaQLRuntime.builder().metadata(secondMetadata).build())); + + EntityMetaFactory previousGlobal = EntityMetaFactory.get(); + EntityMetaFactory.registerGlobal(firstMetadata); + try { + assertTrue(firstFactory.createEntity("Shared") instanceof FirstEntity); + assertTrue(firstFactory.createEntity(FirstEntity.class) instanceof FirstEntity); + assertTrue(secondFactory.createEntity("Shared") instanceof SecondEntity); + assertTrue(secondFactory.createEntity(SecondEntity.class) instanceof SecondEntity); + } finally { + EntityMetaFactory.registerGlobal(previousGlobal); + } + } + + private static SimpleEntityMetaFactory metadata( + Class type, java.util.function.Supplier supplier) { + EntityDescriptor descriptor = new EntityDescriptor(); + descriptor.setType("Shared"); + descriptor.setTargetType(type); + descriptor.setEntitySupplier(supplier); + SimpleEntityMetaFactory metadata = new SimpleEntityMetaFactory(); + metadata.register(descriptor); + return metadata; + } +}