Optional<Bean> get(BeanId beanId, Map<String, Schema> schemas) { Schema schema = schemas.get(beanId.getSchemaName()); if (schema == null) { throw Events.CFG101_SCHEMA_NOT_EXIST(beanId.getSchemaName()); Bean bean; if (schema.getId().isSingleton()) { bean = Bean.create(BeanId.createSingleton(schema.getName())); constructBean(schemaConfig, schema, bean, schemas); return Optional.of(bean); try { for (Object instanceId : schemaConfig.root().keySet()) { if (instanceId.equals(beanId.getInstanceId())) { Config instance = schemaConfig.getConfig(instanceId.toString()); bean = Bean.create(BeanId.create(instanceId.toString(), schema.getName())); constructBean(instance, schema, bean, schemas); return Optional.of(bean);
/** * Create a bean identification. * * @param instanceId of this bean. * @param schemaName The bean schema name. * @return AdminBeanId */ public static BeanId create(final String instanceId, final String schemaName) { if (instanceId == null || "".equals(instanceId)) { throw CFG107_MISSING_ID(); } return new BeanId(instanceId, schemaName); }
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 constructBean(Config properties, Schema schema, Bean bean, Map<String, Schema> schemas) { bean.set(schemas.get(bean.getId().getSchemaName())); for (String propertyName : schema.getPropertyNames()) { Object value; BeanId id = BeanId.createSingleton(refSchema.getName()); Optional<Bean> optional = get(id, schemas); if (!optional.isPresent()) { b.set(schemas.get(b.getId().getSchemaName())); id.setBean(b); bean.addReference(propertyName, id); List<BeanId> beanIds = new ArrayList<>(); for (String id : stringValues) { BeanId beanId = BeanId.create(id, refSchema.getName()); Optional<Bean> ref = get(beanId, schemas); if (!ref.isPresent()) { b.set(schemas.get(b.getId().getSchemaName())); beanId.setBean(b); beanIds.add(beanId); BeanId id = BeanId.create(value.toString(), refSchema.getName()); Optional<Bean> idBean = get(id, schemas); if (!idBean.isPresent()) { b.set(schemas.get(b.getId().getSchemaName())); id.setBean(b); bean.addReference(propertyName, id);
g2.setProp21(g2_merged.getProp21()); listAndAssert(g1.getBeanId().getSchemaName(), g1, g2);
bean = Optional.of(Bean.create(BeanId.createSingleton(schema.getName())));
public void test_get_eagerly() { Bean child = Bean.create(BeanId.create("child", "java.lang.String")); child.addProperty("property1", "true"); child.addProperty("property2", "false"); Bean parent = Bean.create(BeanId.create("parent", "java.lang.String")); parent.addReference("refName", BeanId.create("child", "java.lang.String")); parent.addProperty("property1", "prop1"); parent.addProperty("property2", "prop2"); BeanId grandParentId = BeanId.create("grandparent", "java.lang.String"); Bean grandparent = Bean.create(grandParentId); grandparent.addReference("refName", BeanId.create("parent", "java.lang.String")); manager.create(grandparent); 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();
public Bean toBean(Schema schema) { final BeanId id; if (schema.getId().isSingleton()) { id = BeanId.createSingleton(schema.getName()); } else { id = BeanId.create(getId(), getSchemaName()); bean.addReference(name, BeanId.create(value, schemaName));
if (bean.getId().getSchemaName().equals(s.getName())) { beans.put(bean.getId(), bean);
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); } }
cache.put(beanId, bean); beanId.setBean(bean);
/** * Initialize references that lack a bean instance eagerly. * * @param beans to be initialized */ public final void initializeReferences(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) { Optional<Bean> optionalRef = getEager(id); if (optionalRef.isPresent()) { ref = optionalRef.get(); } } id.setBean(ref); } } } }
private void setSingletonReferences(Bean bean) { Schema s = bean.getSchema(); for (SchemaPropertyRef ref : s.get(SchemaPropertyRef.class)) { if (ref.isSingleton()) { Schema singletonSchema = schemaManager.getSchema(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()); bean.setReference(ref.getName(), singletonId); } } }
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(); if (refSchema == null) { throw Events.CFG101_SCHEMA_NOT_EXIST(ref.getId().getSchemaName()); } 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); } }
@Override public <T> Optional<T> get(String id, Class<T> configurable) { doLookup(); Schema s = schemaManager.getSchema(configurable); BeanId beanId = BeanId.create(id, s.getName()); Optional<Bean> bean = beanManager.getEager(beanId); if (!bean.isPresent()) { initFile(configurable); Bean fileBean = FILE_CONFIG.get(beanId); if (fileBean != null) { bean = Optional.of(fileBean); } else { return Optional.absent(); } } schemaManager.setSchema(Arrays.asList(bean.get())); setSingletonReferences(bean.get()); T object = (T) schemaManager.convertBean(bean.get()); if (cacheManager.isPresent()) { cacheManager.get().put(bean.get()); } return Optional.of(object); }
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 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); } }
public static Bean copy(Bean bean) { if (bean == null) { return null; } Bean copy = Bean.create(bean.getId()); for (String property : bean.getPropertyNames()) { Collection<String> values = bean.getValues(property); if (values == null) { continue; } copy.setProperty(property, bean.getValues(property)); } for (String property : bean.getReferenceNames()) { List<BeanId> ids = bean.getReference(property); if (ids == null) { continue; } for (BeanId id : ids) { copy.addReference(property, BeanId.create(id.getInstanceId(), id.getSchemaName())); } } return copy; }