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; }
private BeanId getSingletonId(Schema s, Class<?> configurable) { try { ClassIntrospector introspector = new ClassIntrospector(configurable); FieldWrap<Id> id = introspector.getFieldList(Id.class).get(0); String instanceId = id.getStaticValue().toString(); return BeanId.createSingleton(instanceId, s.getName()); } catch (Exception e) { throw new IllegalArgumentException("Configurable class [" + configurable + "] is not a singleton.", e); } }
@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; }
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()) {
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; }
private Optional<Bean> get(BeanId beanId) { Schema schema = schemas.get(beanId.getSchemaName()); if (schema == null) { throw Events.CFG101_SCHEMA_NOT_EXIST(beanId.getSchemaName()); } Map<String, Map<String, Object>> instances = (Map) config.getAnyRef(schema.getName()); try { final Bean bean; if (schema.getId().isSingleton()) { bean = Bean.create(BeanId.createSingleton(schema.getName())); constructBean((Map) instances, schema, bean); } else { if (instances == null) { return Optional.absent(); } bean = Bean.create(beanId); Map<String, Object> properties = instances.get(beanId.getInstanceId()); constructBean(properties, schema, bean); } return Optional.of(bean); } catch (ConfigException e) { return Optional.absent(); } }
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); } } }
public List<Bean> getBeans() { ArrayList<Bean> result = new ArrayList<Bean>(); for (XmlBean b : beans) { Bean bean = null; if (b.singleton) { bean = Bean.create(BeanId.createSingleton(b.id, b.name)); } else { bean = Bean.create(BeanId.create(b.id, b.name)); } for (XmlBeanPropertyList p : b.properties) { if (p.values != null) bean.addProperty(p.name, p.values); } for (XmlBeanPropertyRefList p : b.references) { if (p.refs != null) bean.addReference(p.name, p.getReferences()); } result.add(bean); } return result; }
private BeanId getSingletonId(Schema s, Class<?> configurable) { return BeanId.createSingleton(s.getName()); }
@Override public Optional<Bean> getSingleton(String schemaName) throws IllegalArgumentException { for (Bean bean : storage.all()) { if (bean.getId().getSchemaName().equals(schemaName)) { if (!bean.getId().isSingleton()) { throw new IllegalArgumentException("Schema [" + schemaName + "] is not a get."); } BeanId singletonId = bean.getId(); return getEagerly(singletonId); } } return Optional.of(Bean.create(BeanId.createSingleton(schemaName))); }
public BeanId getBeanId() { return BeanId.createSingleton(SINGLETON_SCHEMA_NAME); }
@Test public void test_context_injection() { CdiSingletonConfig config = new CdiSingletonConfig("newvalue"); Bean bean = toBean(config); admin.create(bean); admin.set(bean); BeanId id = BeanId.createSingleton(CdiSingletonConfig.class.getSimpleName()); bean = admin.get(id).get(); assertThat(bean.getValues("value").get(0), is("newvalue")); config = this.config.get(CdiSingletonConfig.class); assertThat(config.getValue(), is("newvalue")); } }