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); } }
private void findReferences(BeanId reference, Bean predecessor, ArrayList<Bean> matches, ArrayList<Bean> checked) { if (checked.contains(predecessor)) { return; } checked.add(predecessor); if (reference.equals(predecessor.getId()) && !matches.contains(predecessor)) { matches.add(predecessor); } for (BeanId ref : predecessor.getReferences()) { if (ref.getBean() == null) { continue; } if (matches.contains(ref.getBean())) { continue; } if (ref.equals(reference)) { matches.add(ref.getBean()); } findReferences(reference, ref.getBean(), matches, checked); } }
private void findReferences(BeanId reference, Bean predecessor, ArrayList<Bean> matches, ArrayList<Bean> checked) { if (checked.contains(predecessor)) { return; } checked.add(predecessor); if (reference.equals(predecessor.getId()) && !matches.contains(predecessor)) { matches.add(predecessor); } for (BeanId ref : predecessor.getReferences()) { if (ref.getBean() == null) { continue; } if (matches.contains(ref.getBean())) { continue; } if (ref.equals(reference)) { matches.add(ref.getBean()); } findReferences(reference, ref.getBean(), matches, checked); } }
core.setSchema(beanId.getBean(), schemaManager.getSchemas());
assertThat(childs.size(), is(1)); BeanId childRef = grandpa.get().getReference(childs.get(0)).get(0); Bean childBean = childRef.getBean(); assertThat(childBean.getId(), is(childBean.getId())); assertEquals(childBean.getSingleValue("property1"), "prop1"); assertThat(childs.size(), is(1)); childRef = childBean.getReference(childs.get(0)).get(0); childBean = childRef.getBean();
private void convertPropertyRefList(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRefList prop : source.getSchema().get(SchemaPropertyRefList.class)) { List<BeanId> references = source.getReference(prop.getName()); if (references == null) { continue; } Collection<Object> c = newCollection(loadClass(prop.getCollectionType())); for (BeanId beanId : references) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { String type = b.getSchema().getType(); try { beanInstance = newInstance(loadClass(type)); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(b, beanInstance, new HashMap<String, Object>(), instanceCache); } c.add(beanInstance); } } values.put(prop.getFieldName(), c); } }
private void convertPropertyRef(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRef prop : source.getSchema().get(SchemaPropertyRef.class)) { BeanId id = source.getFirstReference(prop.getName()); if (id == null) { continue; } Bean ref = id.getBean(); if (ref == null) { continue; } Schema refSchema = ref.getSchema(); SchemaPropertyRef schemaRef = source.getSchema().get(SchemaPropertyRef.class, prop.getName()); Object beanInstance = instanceCache.get(id); if (beanInstance == null) { try { beanInstance = newInstance(forName(refSchema.getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(ref, beanInstance, new HashMap<String, Object>(), instanceCache); } values.put(schemaRef.getFieldName(), beanInstance); } }
private void convertPropertyRefMap(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRefMap prop : source.getSchema().get(SchemaPropertyRefMap.class)) { List<BeanId> beans = source.getReference(prop.getName()); if (beans == null) { continue; } Map<Object, Object> c = newMap(loadClass(prop.getMapType())); for (BeanId beanId : beans) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { try { beanInstance = newInstance(loadClass(b.getSchema().getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(b, beanInstance, new HashMap<String, Object>(), instanceCache); } c.put(beanId.getInstanceId(), beanInstance); } } values.put(prop.getFieldName(), c); } }
private void convertPropertyRefMap(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRefMap prop : source.getSchema().get(SchemaPropertyRefMap.class)) { List<BeanId> beans = source.getReference(prop.getName()); if (beans == null) { continue; } Map<Object, Object> c = newMap(forName(prop.getMapType())); for (BeanId beanId : beans) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { try { beanInstance = newInstance(forName(b.getSchema().getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(b, beanInstance, new HashMap<String, Object>(), instanceCache); } c.put(beanId.getInstanceId(), beanInstance); } } values.put(prop.getFieldName(), c); } }
private void convertPropertyRef(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRef prop : source.getSchema().get(SchemaPropertyRef.class)) { BeanId id = source.getFirstReference(prop.getName()); if (id == null) { continue; } Bean ref = id.getBean(); if (ref == null) { continue; } Schema refSchema = ref.getSchema(); SchemaPropertyRef schemaRef = source.getSchema().get(SchemaPropertyRef.class, prop.getName()); Object beanInstance = instanceCache.get(id); if (beanInstance == null) { try { beanInstance = newInstance(loadClass(refSchema.getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(ref, beanInstance, new HashMap<String, Object>(), instanceCache); } values.put(schemaRef.getFieldName(), beanInstance); } }
private void convertPropertyRefList(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRefList prop : source.getSchema().get(SchemaPropertyRefList.class)) { List<BeanId> references = source.getReference(prop.getName()); if (references == null) { continue; } Collection<Object> c = newCollection(forName(prop.getCollectionType())); for (BeanId beanId : references) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { String type = b.getSchema().getType(); try { beanInstance = newInstance(forName(type)); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(b, beanInstance, new HashMap<String, Object>(), instanceCache); } c.add(beanInstance); } } values.put(prop.getFieldName(), c); } }
/** * 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 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()); } } }
/** * Test that get references are automatically assigned without needing to provision them * from admin context. */ @Test public void test_singleton_references() { // provision a bean without the get reference. Bean singletonParent = toBean(sp1); admin.create(singletonParent); // asert that the get reference is set for config SingletonParent parent = config.get(singletonParent.getId().getInstanceId(), SingletonParent.class).get(); assertNotNull(parent.getSingleton()); // assert that the get reference is set for admin Bean result = admin.get(singletonParent.getId()).get(); BeanId singletonId = result.getFirstReference("get"); assertThat(singletonId, is(s1.getBeanId())); assertThat(singletonId.getBean(), is(toBean(s1))); }
/** * 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()); } } }
public static void setSchema(Bean b, Map<String, Schema> schemas) { Schema s = schemas.get(b.getId().getSchemaName()); if (s == null) { throw CFG101_SCHEMA_NOT_EXIST(b.getId().getSchemaName()); } b.set(s); for (BeanId id : b.getReferences()) { Bean ref = id.getBean(); if (ref != null && ref.getSchema() == null) { setSchema(ref, schemas); } } }
public void setSchema(Bean b, Map<String, Schema> schemas) { Schema s = schemas.get(b.getId().getSchemaName()); if (s == null) { throw CFG101_SCHEMA_NOT_EXIST(b.getId().getSchemaName()); } b.set(s); for (BeanId id : b.getReferences()) { Bean ref = id.getBean(); if (ref != null && ref.getSchema() == null) { setSchema(ref, schemas); } } }