private static void validateReferences(Bean bean) { Schema schema = bean.getSchema(); Set<String> schemaReferenceNames = schema.getReferenceNames(); for (String name : bean.getReferenceNames()) { if (!schemaReferenceNames.contains(name)) { throw Events.CFG111_REF_NOT_EXIST_IN_SCHEMA(name); } } }
/** * 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); } } } }
/** * 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 static void checkReferencesExist(final Bean bean, Collection<Bean> additional) { HashMap<BeanId, Bean> additionalMap = new HashMap<>(); for (Bean b : additional) { additionalMap.put(b.getId(), b); } ArrayList<BeanId> allRefs = new ArrayList<>(); for (String name : bean.getReferenceNames()) { if (bean.getReference(name) == null) { // the reference is about to be removed. continue; } for (BeanId beanId : bean.getReference(name)) { allRefs.add(beanId); } } Collection<BeanId> missingReferences = new ArrayList<>(); for (BeanId beanId : allRefs) { if (beanId.getInstanceId() == null) { continue; } if (storage.get(beanId) == null && additionalMap.get(beanId) == null) { missingReferences.add(beanId); } } if (missingReferences.size() > 0) { throw CFG301_MISSING_RUNTIME_REF(bean.getId(), missingReferences); } }
/** * Returns the a list of property names of the target bean that have * references to the bean id. */ private static boolean hasReferences(Bean target, BeanId reference) { for (String name : target.getReferenceNames()) { List<BeanId> refs = target.getReference(name); if (refs != null) { for (BeanId ref : target.getReference(name)) { if (ref.equals(reference)) { return true; } } } } return false; }
public JaxrsBean(Bean bean) { this.schemaName = bean.getId().getSchemaName(); this.id = bean.getId().getInstanceId(); for (String name : bean.getPropertyNames()) { List<String> values = bean.getValues(name); if (values == null || values.isEmpty()) { continue; } properties.put(name, values); } for (String name : bean.getReferenceNames()) { List<BeanId> refs = bean.getReferences(); if (refs == null || refs.isEmpty()) { continue; } List<String> values = new ArrayList<>(); for (BeanId id : refs) { values.add(id.getInstanceId()); } properties.put(name, values); } }
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) {
for (String name : mergeBean.getReferenceNames()) { List<BeanId> refs = mergeBean.getReference(name); if (refs == null || refs.size() == 0) {
private void replace(Bean target, Bean replace) { if (target == null) { // bean did not exist in storage, create it. target = replace; } checkReferencesExist(replace, new ArrayList<Bean>()); for (String name : replace.getPropertyNames()) { List<String> values = replace.getValues(name); if (values == null || values.size() == 0) { // null/empty indicates a remove/reset-to-default op target.remove(name); } else { target.setProperty(name, replace.getValues(name)); } } for (String name : replace.getReferenceNames()) { List<BeanId> values = replace.getReference(name); if (values == null || values.size() == 0) { // null/empty indicates a remove/reset-to-default op target.remove(name); } else { target.setReferences(name, values); } } }
List<String> childs = grandpa.get().getReferenceNames(); assertThat(childs.size(), is(1)); BeanId childRef = grandpa.get().getReference(childs.get(0)).get(0); childs = childBean.getReferenceNames(); assertThat(childs.size(), is(1)); childRef = childBean.getReference(childs.get(0)).get(0);
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; }
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; }