Search in sources :

Example 1 with Injector

use of com.yahoo.elide.core.dictionary.Injector in project elide by yahoo.

the class MultiplexManagerTest method subordinateEntityDictionaryInheritsInjector.

@Test
public void subordinateEntityDictionaryInheritsInjector() {
    final Injector injector = entity -> {
    // NOOP
    };
    final QueryDictionaryDataStore ds1 = new QueryDictionaryDataStore();
    final MultiplexManager multiplexManager = new MultiplexManager(ds1);
    multiplexManager.populateEntityDictionary(EntityDictionary.builder().injector(injector).build());
    assertEquals(ds1.getDictionary().getInjector(), injector);
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) HashMapDataStore(com.yahoo.elide.core.datastore.inmemory.HashMapDataStore) TransactionException(com.yahoo.elide.core.exceptions.TransactionException) ClassType(com.yahoo.elide.core.type.ClassType) ArrayList(java.util.ArrayList) IterableUtils(org.apache.commons.collections4.IterableUtils) Lists(com.google.common.collect.Lists) TestInstance(org.junit.jupiter.api.TestInstance) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) ClassScanner(com.yahoo.elide.core.utils.ClassScanner) Injector(com.yahoo.elide.core.dictionary.Injector) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) DefaultClassScanner(com.yahoo.elide.core.utils.DefaultClassScanner) ComplexAttribute(com.yahoo.elide.example.beans.ComplexAttribute) EntityProjection(com.yahoo.elide.core.request.EntityProjection) FirstBean(com.yahoo.elide.example.beans.FirstBean) IOException(java.io.IOException) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Test(org.junit.jupiter.api.Test) Slf4j(lombok.extern.slf4j.Slf4j) OtherBean(com.yahoo.elide.example.other.OtherBean) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) DataStore(com.yahoo.elide.core.datastore.DataStore) Injector(com.yahoo.elide.core.dictionary.Injector) Test(org.junit.jupiter.api.Test)

Example 2 with Injector

use of com.yahoo.elide.core.dictionary.Injector in project elide by yahoo.

the class Elide method registerCustomSerde.

protected void registerCustomSerde() {
    Injector injector = elideSettings.getDictionary().getInjector();
    Set<Class<?>> classes = registerCustomSerdeScan();
    for (Class<?> clazz : classes) {
        if (!Serde.class.isAssignableFrom(clazz)) {
            log.warn("Skipping Serde registration (not a Serde!): {}", clazz);
            continue;
        }
        Serde serde = (Serde) injector.instantiate(clazz);
        injector.inject(serde);
        ElideTypeConverter converter = clazz.getAnnotation(ElideTypeConverter.class);
        Class baseType = converter.type();
        registerCustomSerde(baseType, serde, converter.name());
        for (Class type : converter.subTypes()) {
            if (!baseType.isAssignableFrom(type)) {
                throw new IllegalArgumentException("Mentioned type " + type + " not subtype of " + baseType);
            }
            registerCustomSerde(type, serde, converter.name());
        }
    }
}
Also used : Serde(com.yahoo.elide.core.utils.coerce.converters.Serde) Injector(com.yahoo.elide.core.dictionary.Injector) ElideTypeConverter(com.yahoo.elide.core.utils.coerce.converters.ElideTypeConverter)

Example 3 with Injector

use of com.yahoo.elide.core.dictionary.Injector in project elide by yahoo.

the class DataStoreTransaction method createNewObject.

/**
 * Create a new instance of an object.
 *
 * @param entityClass the class
 * @param <T> the model type to create
 * @return a new instance of type T
 */
default <T> T createNewObject(Type<T> entityClass, RequestScope scope) {
    Injector injector = scope.getDictionary().getInjector();
    T obj;
    if (entityClass.getUnderlyingClass().isPresent()) {
        obj = injector.instantiate(entityClass.getUnderlyingClass().get());
    } else {
        try {
            obj = entityClass.newInstance();
        } catch (java.lang.InstantiationException | IllegalAccessException e) {
            obj = null;
        }
    }
    return obj;
}
Also used : Injector(com.yahoo.elide.core.dictionary.Injector)

Example 4 with Injector

use of com.yahoo.elide.core.dictionary.Injector in project elide by yahoo.

the class ElideStandaloneSettings method getEntityDictionary.

/**
 * Gets the EntityDictionary for elide.
 * @param injector Service locator for web service for dependency injection.
 * @param dynamicConfiguration optional dynamic config object.
 * @param entitiesToExclude set of Entities to exclude from binding.
 * @return EntityDictionary object initialized.
 */
default EntityDictionary getEntityDictionary(ServiceLocator injector, ClassScanner scanner, Optional<DynamicConfiguration> dynamicConfiguration, Set<Type<?>> entitiesToExclude) {
    Map<String, Class<? extends Check>> checks = new HashMap<>();
    if (getAnalyticProperties().enableDynamicModelConfigAPI()) {
        checks.put(ConfigChecks.CAN_CREATE_CONFIG, ConfigChecks.CanNotCreate.class);
        checks.put(ConfigChecks.CAN_READ_CONFIG, ConfigChecks.CanNotRead.class);
        checks.put(ConfigChecks.CAN_DELETE_CONFIG, ConfigChecks.CanNotDelete.class);
        checks.put(ConfigChecks.CAN_UPDATE_CONFIG, ConfigChecks.CanNotUpdate.class);
    }
    EntityDictionary dictionary = new EntityDictionary(// Checks
    new HashMap<>(), // Role Checks
    new HashMap<>(), new Injector() {

        @Override
        public void inject(Object entity) {
            injector.inject(entity);
        }

        @Override
        public <T> T instantiate(Class<T> cls) {
            return injector.create(cls);
        }
    }, // Serde Lookup
    CoerceUtil::lookup, entitiesToExclude, scanner);
    dynamicConfiguration.map(DynamicConfiguration::getRoles).orElseGet(Collections::emptySet).forEach(role -> dictionary.addRoleCheck(role, new Role.RoleMemberCheck(role)));
    return dictionary;
}
Also used : HashMap(java.util.HashMap) Check(com.yahoo.elide.core.security.checks.Check) CoerceUtil(com.yahoo.elide.core.utils.coerce.CoerceUtil) DynamicConfiguration(com.yahoo.elide.modelconfig.DynamicConfiguration) Injector(com.yahoo.elide.core.dictionary.Injector) ConfigChecks(com.yahoo.elide.modelconfig.store.models.ConfigChecks) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 5 with Injector

use of com.yahoo.elide.core.dictionary.Injector in project elide by yahoo.

the class ElideAutoConfiguration method buildDictionary.

/**
 * Creates the entity dictionary for Elide which contains static metadata about Elide models.
 * Override to load check classes or life cycle hooks.
 * @param beanFactory Injector to inject Elide models.
 * @param dynamicConfig An instance of DynamicConfiguration.
 * @param settings Elide configuration settings.
 * @param entitiesToExclude set of Entities to exclude from binding.
 * @return a newly configured EntityDictionary.
 */
@Bean
@ConditionalOnMissingBean
@Scope(SCOPE_PROTOTYPE)
public EntityDictionary buildDictionary(AutowireCapableBeanFactory beanFactory, ClassScanner scanner, @Autowired(required = false) DynamicConfiguration dynamicConfig, ElideConfigProperties settings, @Qualifier("entitiesToExclude") Set<Type<?>> entitiesToExclude) {
    Map<String, Class<? extends Check>> checks = new HashMap<>();
    if (settings.getDynamicConfig().isConfigApiEnabled()) {
        checks.put(ConfigChecks.CAN_CREATE_CONFIG, ConfigChecks.CanNotCreate.class);
        checks.put(ConfigChecks.CAN_READ_CONFIG, ConfigChecks.CanNotRead.class);
        checks.put(ConfigChecks.CAN_DELETE_CONFIG, ConfigChecks.CanNotDelete.class);
        checks.put(ConfigChecks.CAN_UPDATE_CONFIG, ConfigChecks.CanNotUpdate.class);
    }
    EntityDictionary dictionary = new EntityDictionary(// Checks
    checks, // Role Checks
    new HashMap<>(), new Injector() {

        @Override
        public void inject(Object entity) {
            beanFactory.autowireBean(entity);
        }

        @Override
        public <T> T instantiate(Class<T> cls) {
            return beanFactory.createBean(cls);
        }
    }, // Serde Lookup
    CoerceUtil::lookup, entitiesToExclude, scanner);
    if (isAggregationStoreEnabled(settings) && isDynamicConfigEnabled(settings)) {
        dynamicConfig.getRoles().forEach(role -> {
            dictionary.addRoleCheck(role, new Role.RoleMemberCheck(role));
        });
    }
    return dictionary;
}
Also used : HashMap(java.util.HashMap) Check(com.yahoo.elide.core.security.checks.Check) Role(com.yahoo.elide.core.security.checks.prefab.Role) CoerceUtil(com.yahoo.elide.core.utils.coerce.CoerceUtil) Injector(com.yahoo.elide.core.dictionary.Injector) ConfigChecks(com.yahoo.elide.modelconfig.store.models.ConfigChecks) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) Scope(org.springframework.context.annotation.Scope) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

Injector (com.yahoo.elide.core.dictionary.Injector)7 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)5 Check (com.yahoo.elide.core.security.checks.Check)4 CoerceUtil (com.yahoo.elide.core.utils.coerce.CoerceUtil)4 ConfigChecks (com.yahoo.elide.modelconfig.store.models.ConfigChecks)4 HashMap (java.util.HashMap)4 Role (com.yahoo.elide.core.security.checks.prefab.Role)2 ClassScanner (com.yahoo.elide.core.utils.ClassScanner)2 DynamicConfiguration (com.yahoo.elide.modelconfig.DynamicConfiguration)2 Bean (org.springframework.context.annotation.Bean)2 Lists (com.google.common.collect.Lists)1 JSONAPI_CONTENT_TYPE (com.yahoo.elide.Elide.JSONAPI_CONTENT_TYPE)1 DataStore (com.yahoo.elide.core.datastore.DataStore)1 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1 HashMapDataStore (com.yahoo.elide.core.datastore.inmemory.HashMapDataStore)1 HttpStatus (com.yahoo.elide.core.exceptions.HttpStatus)1 TransactionException (com.yahoo.elide.core.exceptions.TransactionException)1 EntityProjection (com.yahoo.elide.core.request.EntityProjection)1 ClassType (com.yahoo.elide.core.type.ClassType)1 Type (com.yahoo.elide.core.type.Type)1