private BeanId getBeanId(Object bean) { Config config = bean.getClass().getAnnotation(Config.class); if (config == null) { throw new IllegalArgumentException("Bean [" + bean + "] does not have a @Config annotation."); } String schemaName = config.name(); if (schemaName == null || "".equals(schemaName.trim())) { schemaName = bean.getClass().getName(); } ClassIntrospector i = new ClassIntrospector(bean.getClass()); List<FieldWrap> ids = i.getFieldList(Id.class); if (ids == null || ids.size() != 1) { // a get return BeanId.createSingleton(schemaName); } FieldWrap id = ids.get(0); BeanId targetId; Object idValue = id.getValue(bean); if (idValue == null) { throw new IllegalArgumentException("Cannot convert an objects that have empty id [" + bean + "]"); } targetId = BeanId.create(id.getValue(bean).toString(), schemaName); return targetId; }
public static boolean exists(BeanId id) { Query query = getEmOrFail().createNamedQuery(FIND_BEAN_FROM_BEANID_NAME); query.setParameter(1, id.getInstanceId()); query.setParameter(2, id.getSchemaName()); try { query.getSingleResult(); } catch (NoResultException e) { return false; } return true; }
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(Map<String, Object> properties, Schema schema, Bean bean) { bean.set(schemas.get(bean.getId().getSchemaName())); for (String propertyName : schema.getPropertyNames()) { Object value = properties.get(propertyName); Schema refSchema = schemas.get(schema.getReferenceSchemaName(propertyName)); if (refSchema.getId().isSingleton()) { BeanId id = BeanId.createSingleton(refSchema.getName()); Optional<Bean> optional = get(id); 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); 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); if (!idBean.isPresent()) { b.set(schemas.get(b.getId().getSchemaName()));
@Test 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();
cache.put(beanId, bean); beanId.setBean(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); } }
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 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 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); } }
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); } }
@Override public <T> T get(Class<T> configurable) { doLookup(); Schema schema = getSchema(configurable); BeanId singleton = getSingletonId(schema, configurable); Optional<Bean> bean = beanManager.getEager(singleton); if (!bean.isPresent()) { initFile(configurable); Bean fileBean = FILE_CONFIG.get(singleton); if (fileBean != null) { fileBean.set(schema); bean = Optional.of(fileBean); } if (bean.isPresent()) { T object = conversion.convert(bean.get(), configurable); core.cache(bean.get()); return object; } } if (!bean.isPresent()) { bean = Optional.of(Bean.create(BeanId.createSingleton(schema.getName()))); } core.setSchema(bean.get(), schemas); setSingletonReferences(bean.get(), schemas); T object = conversion.convert(bean.get(), configurable); core.cache(bean.get()); return object; }
/** * 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()); } } } }
@Override public Optional<Bean> getEager(BeanId id) { try { if (!begin()) { return Optional.absent(); } List<Bean> beans = findEager(Sets.newHashSet(id)); if (beans.size() == 0) { if (id.isSingleton()) { return Optional.of(Bean.create(id)); } return Optional.absent(); } commit(); return Optional.of(beans.get(0)); } catch (AbortRuntimeException e) { rollback(); if (id.isSingleton()) { return Optional.of(Bean.create(id)); } else { throw e; } } catch (Throwable e) { rollback(); throw new RuntimeException(e); } }
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 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); } }
public List<Bean> list(Schema schema) { List<Bean> beans = new ArrayList<>(); Map<String, Map<String, Object>> instances; try { instances = (Map) config.getAnyRef(schema.getName()); if (instances == null) { return beans; } } catch (ConfigException e) { return beans; } if (schema.getId().isSingleton()) { final Bean bean = Bean.create(BeanId.createSingleton(schema.getName())); Map<String, Object> properties = (Map) instances; beans.add(bean); constructBean(properties, schema, bean); } else { for (String instanceId : instances.keySet()) { final Map<String, Object> properties = instances.get(instanceId); final Bean bean = Bean.create(BeanId.create(instanceId, schema.getName())); bean.set(schema); beans.add(bean); constructBean(properties, schema, bean); } } return beans; }
@Override public Optional<Bean> getLazy(BeanId id) throws AbortRuntimeException { try { if (!begin()) { return Optional.absent(); } List<Bean> bean = findLazy(Sets.newHashSet(id)); if (bean.size() == 0) { if (id.isSingleton()) { return Optional.of(Bean.create(id)); } return Optional.absent(); } commit(); return Optional.of(bean.get(0)); } catch (AbortRuntimeException e) { rollback(); throw e; } catch (Throwable e) { rollback(); throw new RuntimeException(e); } }
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); } } } }