String name = "configName" + i; String desc = "configDesc" + i; SchemaId id = SchemaId.create("configId" + i, "configDesc" + i, false); Schema schema = Schema.create(id, type, name, desc); for (int j = 0; j < numProps; j++) {
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);
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 Schema convert(Class<?> source, Class<? extends Schema> specificType) { ClassIntrospector introspector = new ClassIntrospector(source); Config config = introspector.getAnnotation(Config.class); if (config == null) { throw CFG102_NOT_CONFIGURABLE(source); } SchemaId schemaId = getId(introspector); if (schemaId == null) { // get instance does not have @Id annotations so we create // it from the @Config annotation schemaId = SchemaId.create(config.name(), config.desc(), true); } String schemaName = config.name(); if (schemaName == null || "".equals(schemaName)) { schemaName = source.getName(); } Schema schema = Schema.create(schemaId, introspector.getName(), schemaName, config.desc()); Collection<Object> fields = new ArrayList<>(); fields.addAll(introspector.getNonStaticFieldList()); Collection<AbstractSchemaProperty> props = conversion.convert(fields, AbstractSchemaProperty.class); for (AbstractSchemaProperty abstractProp : props) { schema.add(abstractProp); } return schema; }
public Map<String, Schema> getSchemas() { Map<String, Schema> result = new HashMap<String, Schema>(); for (XmlSchema b : schemas) { Schema schema = Schema.create( SchemaId.create(b.id.name, b.id.desc, b.id.singleton), b.type, b.name, b.desc); for (XmlSchemaProperty p : b.properties) { schema.add(SchemaProperty.create(p.name, p.fieldName, p.type, p.desc, p.isImmutable, p.isEnum, p.defaultValue)); } for (XmlSchemaCollection p : b.collection) { schema.add(SchemaPropertyList.create(p.name, p.fieldName, p.parameterizedType, p.desc, p.isImmutable, p.isEnum, p.defaultValues, p.collectionType)); } for (XmlSchemaRef p : b.ref) { schema.add(SchemaPropertyRef.create(p.name, p.fieldName, p.schemaName, p.desc, p.isImmutable, p.isSingleton)); } for (XmlSchemaRefCollection p : b.refCollection) { schema.add(SchemaPropertyRefList.create(p.name, p.fieldName, p.schemaName, p.desc, p.isImmutable, p.collectionType)); } for (XmlSchemaRefMap p : b.refMap) { schema.add(SchemaPropertyRefMap.create(p.name, p.fieldName, p.schemaName, p.desc, p.isImmutable, p.mapType)); } result.put(schema.getName(), schema); } return result; }
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 SchemaId getId(ClassIntrospector introspector) { List<FieldWrap<Id>> ids = introspector.getFieldList(Id.class); if (ids == null || ids.size() == 0) { throw CFG103_NO_ID(introspector.getTarget()); } FieldWrap<Id> id = ids.get(0); if ((id.isStatic() && !id.isFinal()) || (id.isFinal() && !id.isStatic())) { throw CFG108_ILLEGAL_MODIFIERS(id.getAnnotation().name()); } boolean isSingleton = false; if (id.isFinal()) { isSingleton = true; } String name = id.getAnnotation().name(); if (name == null || "".equals(name)) { name = id.getFieldName(); } SchemaId schemaId = SchemaId.create(name, id.getAnnotation().desc(), isSingleton); return schemaId; } }
private Object convert(Bean source, Object instance, Map<String, Object> valuesToInject, Map<BeanId, Object> instanceCache) { instanceCache.put(source.getId(), instance); convertProperty(source, valuesToInject); convertPropertyList(source, valuesToInject); convertPropertyRef(source, valuesToInject, instanceCache); convertPropertyRefList(source, valuesToInject, instanceCache); convertPropertyRefMap(source, valuesToInject, instanceCache); Schema schema = source.getSchema(); if (!schema.getId().isSingleton()) { // do not try to inject singleton id: the field is static final valuesToInject.put(getIdField(instance.getClass()), source.getId().getInstanceId()); } inject(instance, valuesToInject); return instance; }
private SchemaId getId(ClassIntrospector introspector) { List<FieldWrap> ids = introspector.getFieldList(Id.class); boolean isSingleton = false; if (ids == null || ids.size() == 0) { return null; } else { FieldWrap id = ids.get(0); Id anno = (Id) id.getAnnotation().get(); if ((id.isStatic() && !id.isFinal()) || (id.isFinal() && !id.isStatic())) { throw CFG108_ILLEGAL_MODIFIERS(id.getField()); } String name = anno.name(); if (name == null || "".equals(name)) { name = id.getFieldName(); } return SchemaId.create(name, anno.desc(), isSingleton); } }
private Object convert(Bean source, Object instance, Map<String, Object> valuesToInject, Map<BeanId, Object> instanceCache) { instanceCache.put(source.getId(), instance); convertProperty(source, valuesToInject); convertPropertyList(source, valuesToInject); convertPropertyRef(source, valuesToInject, instanceCache); convertPropertyRefList(source, valuesToInject, instanceCache); convertPropertyRefMap(source, valuesToInject, instanceCache); Schema schema = source.getSchema(); if (!schema.getId().isSingleton()) { // do not try to inject get id: the field is static final valuesToInject.put(getIdField(instance.getClass()), source.getId().getInstanceId()); } inject(instance, valuesToInject); return instance; }
@Override public void register(Class<?>... configurable) { doLookup(); for (Class<?> clazz : configurable) { Schema schema = conversion.convert(clazz, Schema.class); schemaManager.regsiterSchema(schema); if (schema.getId().isSingleton()) { beanManager.createSingleton(getSingletonId(schema, clazz)); } // ok to not have validation manager available if (validationManager != null) { validationManager.register(schema.getName(), clazz); } } }
public static SchemaId create(final String name, final String desc, final boolean isSingleton) { return new SchemaId(name, desc, isSingleton); }
public XmlSchemaId(SchemaId id) { this.name = id.getName(); this.desc = id.getDesc(); this.singleton = id.isSingleton(); }