public static Bean toBean(Object object) { return CONVERSION.convert(object, Bean.class); }
public static Collection<Bean> toBeans(Object... objects) { return CONVERSION.convert(Lists.newArrayList(objects), Bean.class); }
private Collection<Bean> convertToBeans(Collection<?> objects) { ArrayList<Bean> beans = new ArrayList<>(); for (Object object : objects) { if(object instanceof Bean) { beans.add((Bean) object); } else { Bean bean = conversion.convert(object, Bean.class); beans.add(bean); } } return beans; } }
/** * Validate a collection of bean instances. This method is called * when beans are provisioned from an administrative context. * <p> * Beans can correlate their respective validation constraints using * the schema name. * </p> * * @param beans to be validated. * @throws AbortRuntimeException */ @SuppressWarnings({ "rawtypes", "unchecked" }) public void validate(Collection<Bean> beans) throws AbortRuntimeException { if (validator == null) { validator = Validation.buildDefaultValidatorFactory().getValidator(); } for (Bean bean : beans) { Class genclazz = loadClass(bean.getSchema().getType()); Object beanToValidate = conversion.convert(bean, genclazz); Set<ConstraintViolation<Object>> violations = validator.validate(beanToValidate); String msg = ""; for (ConstraintViolation<Object> v : violations) { msg = msg + v.getPropertyPath() + " " + v.getMessage(); } if (!"".equals(msg.trim())) { throw Events.CFG309_VALIDATION_ERROR(msg); } } }
@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; }
@Override public <T> Optional<T> get(String id, Class<T> configurable) { doLookup(); Schema s = getSchema(configurable); Map<String, Schema> schemas = schemaManager.getSchemas(); 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(); } } core.setSchema(bean.get(), schemas); setSingletonReferences(bean.get(), schemas); T object = conversion.convert(bean.get(), configurable); core.cache(bean.get()); return Optional.of(object); }
@Override public void unregister(Class<?>... configurable) { doLookup(); for (Class<?> clazz : configurable) { Schema schema = conversion.convert(clazz, Schema.class); schemaManager.removeSchema(schema.getName()); core.removeIndex(schema); } }
for(T object : conversion.convert(beans.values(), configurable)) { objects.add(object);
private void convertProperty(Bean source, Map<String, Object> values) { for (SchemaProperty prop : source.getSchema().get(SchemaProperty.class)) { String value = source.getSingleValue(prop.getName()); String field = prop.getFieldName(); Object converted = conversion.convert(value, loadClass(prop.getType())); values.put(field, converted); } }
@Override public Bean convert(Object source, Class<? extends Bean> specificType) { ClassIntrospector i = new ClassIntrospector(source.getClass()); Bean bean = Bean.create(getBeanId(source)); for (FieldWrap field : i.getNonStaticFieldList()) { field.checkNotPublic(); Object value = field.getValue(source); if (value == null) { continue; } addProperty(value, bean, field); } Schema schema = conversion.convert(source.getClass(), Schema.class); bean.set(schema); return bean; }
private Schema getSchema(Class<?> clazz) { Schema schema = schemas.get(clazz); if(schema != null) { return schema; } ClassIntrospector introspector = new ClassIntrospector(clazz); Config config = introspector.getAnnotation(Config.class); if (config == null) { throw CFG102_NOT_CONFIGURABLE(clazz); } String schemaName = config.name(); if (schemaName == null || "".equals(schemaName)) { schemaName = clazz.getName(); } schema = conversion.convert(clazz, Schema.class); for (Class<?> cls : schema.getReferenceSchemaTypes()) { if (schemas.get(clazz) != null) { getSchema(cls); } } properties.registerSchema(schema); schemas.put(schema.getName(), schema); schemaManager.registerSchema(schema); core.putIndex(schema); return schema; } }
@Override public void registerDefault(Object... instances) { doLookup(); for (Object instance : instances) { Bean bean = conversion.convert(instance, Bean.class); bean.setDefault(); try { beanManager.create(bean); } catch (AbortRuntimeException e) { // ignore if bean already exist if (e.getEvent().getCode() != CFG303) { throw e; } } } }
private void convertPropertyList(Bean source, Map<String, Object> values) { for (SchemaPropertyList prop : source.getSchema().get(SchemaPropertyList.class)) { List<String> vals = source.getValues(prop.getName()); String field = prop.getFieldName(); if (vals == null) { continue; } Collection<Object> c = newCollection(loadClass(prop.getCollectionType())); for (String val : vals) { Object converted = conversion.convert(val, loadClass(prop.getType())); c.add(converted); } values.put(field, c); } }
@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; }
private static void validateProperties(Bean bean) { Schema schema = bean.getSchema(); for (SchemaProperty prop : schema.get(SchemaProperty.class)) { String value = validateSingle(bean, prop); if (value == null) { continue; } try { conversion.convert(value, forName(prop.getType())); } catch (Exception e) { throw Events.CFG105_WRONG_PROPERTY_TYPE(bean.getId(), prop.getName(), prop.getType(), value); } } }
private static void validatePropertyList(Bean bean) { Schema schema = bean.getSchema(); for (SchemaPropertyList prop : schema.get(SchemaPropertyList.class)) { List<String> values = bean.getValues(prop.getName()); if (values == null) { continue; } for (String value : values) { try { conversion.convert(value, forName(prop.getType())); } catch (Exception e) { throw Events.CFG105_WRONG_PROPERTY_TYPE(bean.getId(), prop.getName(), prop.getType(), value); } } } }