Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions teaql-core/src/main/java/io/teaql/core/meta/EntityFactory.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -31,19 +32,19 @@ public interface EntityFactory {
*/
<T extends Entity> 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 extends Entity> T createEntity(Class<T> 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();
}
Expand All @@ -53,7 +54,7 @@ public <T extends Entity> T createEntity(Class<T> 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);
}
Expand All @@ -66,4 +67,18 @@ public <T extends Entity> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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 <T extends BaseEntity> SimpleEntityMetaFactory metadata(
Class<T> type, java.util.function.Supplier<T> supplier) {
EntityDescriptor descriptor = new EntityDescriptor();
descriptor.setType("Shared");
descriptor.setTargetType(type);
descriptor.setEntitySupplier(supplier);
SimpleEntityMetaFactory metadata = new SimpleEntityMetaFactory();
metadata.register(descriptor);
return metadata;
}
}
Loading