Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.deephacks.tools4j.config.internal.core.runtime;
 
 
 import java.util.List;
 import java.util.Map;
 
 import static org.deephacks.tools4j.config.model.Events.CFG102_NOT_CONFIGURABLE;
 import static org.deephacks.tools4j.config.model.Events.CFG303;

RuntimeCoreContext is responsible for separating the admin, runtime and spi context so that no dependencies (compile nor runtime) exist between them.
 
 public final class RuntimeCoreContext extends RuntimeContext {
     private Conversion conversion;
     private SchemaManager schemaManager;
     private BeanManager beanManager;
     private Optional<CacheManagercacheManager;
     private ConcurrentHashMap<Class<?>, Schemaschemas = new ConcurrentHashMap<>();
 
     @Inject
     private ConfigCore core;
 
     @Override
     public void register(Class<?>... configurable) {
         doLookup();
         for (Class<?> clazz : configurable) {
             Schema schema = .convert(clazzSchema.class);
             .regsiterSchema(schema);
             .putIndex(schema);
         }
     }
 
     @Override
     public void unregister(Class<?>... configurable) {
         doLookup();
         for (Class<?> clazz : configurable) {
             Schema schema = .convert(clazzSchema.class);
             .removeSchema(schema.getName());
             .removeIndex(schema);
         }
     }
 
     @Override
     public void registerDefault(Object... instances) {
         doLookup();
         for (Object instance : instances) {
             Bean bean = .convert(instanceBean.class);
             bean.setDefault();
             try {
                 .create(bean);
             } catch (AbortRuntimeException e) {
                 // ignore if bean already exist
                 if (e.getEvent().getCode() != ) {
                     throw e;
                 }
             }
         }
     }
 
     @Override
     public <T> T singleton(Class<T> configurable) {
         doLookup();
         Schema schema = .convert(configurableSchema.class);
         BeanId singleton = getSingletonId(schemaconfigurable);
        Map<StringSchemaschemas = .getSchemas();
        Bean bean = .getEager(singleton);
        .setSchema(beanschemas);
        setSingletonReferences(beanschemas);
        T object = .convert(beanconfigurable);
        .cache(bean);
        return object;
    }
    @Override
    public <T> List<T> all(Class<T> clazz) {
        doLookup();
        Schema s = getSchema(clazz);
        Map<StringSchemaschemas = .getSchemas();
        Map<BeanIdBeanbeans = .list(s.getName());
        .setSchema(schemasbeans);
        for (Bean bean : beans.values()) {
            setSingletonReferences(beanschemas);
            .cache(bean);
        }
        ArrayList<T> objects = new ArrayList<>();
        for(T object : .convert(beans.values(), clazz)) {
            objects.add(object);
        }
        return objects;
    }
    @Override
    public <T> T get(String idClass<T> clazz) {
        doLookup();
        Schema s = getSchema(clazz);
        Map<StringSchemaschemas = .getSchemas();
        BeanId beanId = BeanId.create(ids.getName());
        Bean bean = .getEager(beanId);
        if (bean == null) {
            throw Events.CFG304_BEAN_DOESNT_EXIST(beanId);
        }
        .setSchema(beanschemas);
        setSingletonReferences(beanschemas);
        T object = .convert(beanclazz);
        .cache(bean);
        return object;
    }
    @Override
    public <T> org.deephacks.tools4j.config.query.ConfigQuery newQuery(Class<T> configurable) {
        doLookup();
        Schema schema = getSchema(configurable);
        ConfigIndexedCollection collection = .get(schema);
        if(collection == null) {
            throw Events.CFG101_SCHEMA_NOT_EXIST(configurable.getName());
        }
        if(!.isPresent()) {
            throw new IllegalStateException("Queries are not possible without a cache manager.");
        }
        return new ConfigQuery<T>(collection.get());
    }
    private BeanId getSingletonId(Schema sClass<?> configurable) {
        return BeanId.createSingleton(s.getName());
    }
    private void setSingletonReferences(Bean beanMap<StringSchemaschemas) {
        Schema s = bean.getSchema();
        for (SchemaPropertyRef ref : s.get(SchemaPropertyRef.class)) {
            if (ref.isSingleton()) {
                Schema singletonSchema = schemas.get(ref.getSchemaName());
                Bean singleton = .getSingleton(ref.getSchemaName());
                singleton.set(singletonSchema);
                BeanId singletonId = singleton.getId();
                singletonId.setBean(singleton);
                // recursive call.
                setSingletonReferences(singletonschemas);
                bean.setReference(ref.getName(), singletonId);
            }
        }
    }
    public void doLookup() {
        // core would already be injected in a cdi environment
        if ( == null) {
             = new ConfigCore();
        }
        if ( == null) {
             = Conversion.get();
            .register(new ObjectToBeanConverter());
            .register(new ClassToSchemaConverter());
            .register(new FieldToSchemaPropertyConverter());
            .register(new BeanToObjectConverter());
        }
        if ( == null) {
             = .lookupBeanManager();
        }
        if ( == null) {
             = .lookupSchemaManager();
        }
        if ( == null) {
             = .lookupCacheManager();
        }
    }
    private Schema getSchema(Class<?> clazz) {
        Schema schema = .get(clazz);
        if(schema != null) {
            return schema;
        }
        ClassIntrospector introspector = new ClassIntrospector(clazz);
        Config config = introspector.getAnnotation(Config.class);
        if (config == null) {
            throw CFG102_NOT_CONFIGURABLE(clazz);
        }
        String schemaName = config.name();
        if (schemaName == null || "".equals(schemaName)) {
            schemaName = clazz.getName();
        }
        schema = .getSchema(schemaName);
        .put(clazzschema);
        return schema;
    }
New to GrepCode? Check out our FAQ X