private void getEagerly(BeanId id, HashMap<BeanId, Bean> found, Set<BeanId> seen) { if (seen.contains(id)) { return; } seen.add(id); Bean bean = found.get(id) == null ? storage.get(id) : found.get(id); if (bean == null) { return; } found.put(id, bean); // bean found, fetch references. for (BeanId ref : bean.getReferences()) { if (ref.getBean() != null) { continue; } Bean refBean = found.get(ref) == null ? storage.get(ref) : found.get(ref); if (refBean == null) { throw CFG301_MISSING_RUNTIME_REF(bean.getId(), ref); } ref.setBean(refBean); found.put(ref, refBean); getEagerly(ref, found, seen); } }
/** * Assemble beans and initalize references from what have been provided. */ private void connectReferences() { // ready to associate initalized beans with references for (Bean bean : beans.values()) { for (JpaRef ref : refs.get(bean.getId())) { BeanId target = ref.getTarget(); Bean targetBean = beans.get(target); target.setBean(targetBean); bean.addReference(ref.getPropertyName(), target); } } } }
beanId.setBean(ref); core.setSchema(beanId.getBean(), schemaManager.getSchemas());
cache.put(beanId, bean); beanId.setBean(bean);
private void merge(List<Bean> sources, Bean mergeBean) { HashMap<BeanId, Bean> cache = new HashMap<BeanId, Bean>(); for (Bean source : sources) { for (String name : mergeBean.getPropertyNames()) { List<String> values = mergeBean.getValues(name); if (values == null || values.size() == 0) { continue; } source.setProperty(name, values); } for (String name : mergeBean.getReferenceNames()) { List<BeanId> refs = mergeBean.getReference(name); if (refs == null || refs.size() == 0) { source.setReferences(name, refs); continue; } for (BeanId beanId : refs) { Bean bean = cache.get(beanId); if (bean == null) { bean = beanManager.getLazy(beanId); setSchema(bean, schemaManager.getSchemas()); cache.put(beanId, bean); } beanId.setBean(bean); } source.setReferences(name, refs); } } }
private void setSingletonReferences(Bean bean, Map<String, Schema> schemas) { Schema s = bean.getSchema(); for (SchemaPropertyRef ref : s.get(SchemaPropertyRef.class)) { if (ref.isSingleton()) { Schema singletonSchema = schemas.get(ref.getSchemaName()); Optional<Bean> singleton = beanManager.getSingleton(ref.getSchemaName()); if (!singleton.isPresent()) { initFile(null); Bean fileBean = FILE_CONFIG.get(BeanId.createSingleton(ref.getSchemaName())); if (fileBean != null) { singleton = Optional.of(fileBean); } } if (!singleton.isPresent()) { singleton = Optional.of(Bean.create(BeanId.createSingleton(ref.getSchemaName()))); } singleton.get().set(singletonSchema); BeanId singletonId = singleton.get().getId(); singletonId.setBean(singleton.get()); // recursive call. setSingletonReferences(singleton.get(), schemas); bean.setReference(ref.getName(), singletonId); } } }
/** * Used for setting or creating a multiple beans. * * We must consider that the operation may include beans that have * references betewen eachother. User provided beans are * prioritized and the storage is secondary for looking up references. */ private void initalizeReferences(Collection<Bean> beans) { Map<BeanId, Bean> userProvided = BeanUtils.uniqueIndex(beans); for (Bean bean : beans) { for (String name : bean.getReferenceNames()) { List<BeanId> values = bean.getReference(name); if (values == null) { continue; } for (BeanId beanId : values) { // the does not exist in storage, but may exist in the // set of beans provided by the user. Bean ref = userProvided.get(beanId); if (ref == null) { ref = beanManager.getEager(beanId); } beanId.setBean(ref); setSchema(beanId.getBean(), schemaManager.getSchemas()); } } } }
private void initReferences(Collection<Bean> beans) { Map<BeanId, Bean> indexed = BeanUtils.uniqueIndex(beans); for (Bean bean : beans) { for (String name : bean.getReferenceNames()) { List<BeanId> ids = bean.getReference(name); if (ids == null) { continue; } for (BeanId id : ids) { Bean ref = indexed.get(id); if (ref == null) { // TODO: investigate if eager is really needed Optional<Bean> optionalRef = beanManager.getEager(id); if (optionalRef.isPresent()) { core.setSchema(optionalRef.get(), schemaManager.getSchemas()); ref = optionalRef.get(); } } id.setBean(ref); } } } }
private Bean getEagerly(BeanId id, Map<BeanId, Bean> all) { Bean result = all.get(id); if (result == null) { throw CFG304_BEAN_DOESNT_EXIST(id); } // bean found, initalize references. for (BeanId ref : result.getReferences()) { if (ref.getBean() != null) { continue; } Bean refBean = all.get(ref); if (refBean == null) { throw CFG301_MISSING_RUNTIME_REF(result.getId(), ref); } ref.setBean(refBean); getEagerly(ref, all); } return result; }
/** * Used for setting or creating a single bean. */ @SuppressWarnings("unused") private void initalizeReferences(Bean bean) { for (String name : bean.getReferenceNames()) { List<BeanId> values = bean.getReference(name); if (values == null) { continue; } for (BeanId beanId : values) { Optional<Bean> ref = beanManager.getEager(beanId); if (ref.isPresent()) { beanId.setBean(ref.get()); } core.setSchema(beanId.getBean(), schemaManager.getSchemas()); } } }
private void setSingletonReferences(Bean bean, Map<String, Schema> schemas) { Schema s = bean.getSchema(); for (SchemaPropertyRef ref : s.get(SchemaPropertyRef.class)) { if (ref.isSingleton()) { Schema singletonSchema = schemas.get(ref.getSchemaName()); Optional<Bean> singleton = beanManager.getSingleton(ref.getSchemaName()); if (singleton.isPresent()) { singleton.get().set(singletonSchema); BeanId singletonId = singleton.get().getId(); singletonId.setBean(singleton.get()); // recursive call. setSingletonReferences(singleton.get(), schemas); bean.setReference(ref.getName(), singletonId); } } } }
@Override public Bean getLazy(BeanId id) throws AbortRuntimeException { Map<BeanId, Bean> all = readValuesAsMap(); Bean bean = all.get(id); if (bean == null) { throw CFG304_BEAN_DOESNT_EXIST(id); } for (BeanId ref : bean.getReferences()) { Bean refBean = all.get(ref); if (bean == null) { throw CFG301_MISSING_RUNTIME_REF(ref); } ref.setBean(refBean); } return bean; }
/** * Used for setting or creating a single bean. */ private void initalizeReferences(Bean bean) { for (String name : bean.getReferenceNames()) { List<BeanId> values = bean.getReference(name); if (values == null) { continue; } for (BeanId beanId : values) { Bean ref = beanManager.getEager(beanId); beanId.setBean(ref); setSchema(beanId.getBean(), schemaManager.getSchemas()); } } }
private void initReferences(Collection<Bean> beans) { Map<BeanId, Bean> indexed = BeanUtils.uniqueIndex(beans); for (Bean bean : beans) { for (String name : bean.getReferenceNames()) { List<BeanId> ids = bean.getReference(name); for (BeanId id : ids) { Bean ref = indexed.get(id); if (ref == null) { // TODO: investigate if eager is really needed ref = beanManager.getEager(id); setSchema(ref, schemaManager.getSchemas()); } id.setBean(ref); } } } }
@Override public Optional<Bean> getLazy(BeanId id) throws AbortRuntimeException { Bean bean = storage.get(id); if (bean == null) { return Optional.absent(); } for (BeanId ref : bean.getReferences()) { Bean refBean = storage.get(ref); if (refBean == null) { throw CFG301_MISSING_RUNTIME_REF(ref); } ref.setBean(refBean); } return Optional.of(bean); }
private void setSingletonReferences(Bean bean, Map<String, Schema> schemas) { Schema s = bean.getSchema(); for (SchemaPropertyRef ref : s.get(SchemaPropertyRef.class)) { if (ref.isSingleton()) { Schema singletonSchema = schemas.get(ref.getSchemaName()); Bean singleton = beanManager.getSingleton(ref.getSchemaName()); singleton.set(singletonSchema); BeanId singletonId = singleton.getId(); singletonId.setBean(singleton); // recursive call. setSingletonReferences(singleton, schemas); bean.setReference(ref.getName(), singletonId); } } }
private void setSingletonReferences(Bean bean, Map<String, Schema> schemas) { Schema s = bean.getSchema(); for (SchemaPropertyRef ref : s.get(SchemaPropertyRef.class)) { if (ref.isSingleton()) { Schema singletonSchema = schemas.get(ref.getSchemaName()); Bean singleton = beanManager.getSingleton(ref.getSchemaName()); singleton.set(singletonSchema); BeanId singletonId = singleton.getId(); singletonId.setBean(singleton); // recursive call. setSingletonReferences(singleton, schemas); bean.setReference(ref.getName(), singletonId); } } }
private BeanId getRecursiveBeanId(Object bean) { BeanId targetId = getBeanId(bean); Bean targetBean = conversion.convert(bean, Bean.class); targetBean.set(conversion.convert(bean.getClass(), Schema.class)); targetId.setBean(targetBean); return targetId; } }
public void put(Bean bean) { // disconnect any references that have been set for (BeanId id : bean.getReferences()) { id.setBean(null); } beans.put(bean.getId(), bean); }