/** * This is the fully qualified classname of the configurable class that this schema originates from. * * @return Class of this schema. */ public Class<?> getClassType() { try { return Class.forName(getType()); } catch (ClassNotFoundException e) { throw new IllegalStateException(e); } }
public String toString() { return Objects.toStringHelper(Schema.class).add("id", id).add("name", getName()) .add("type", getType()).add("desc", getDesc()).add("properties", properties) .toString(); }
@Override public int hashCode() { return Objects.hashCode(properties, getType(), getDesc(), getName()); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void validate(Collection<Bean> beans) throws AbortRuntimeException { if (validator == null) { validator = Validation.buildDefaultValidatorFactory().getValidator(); } ClassRepository repos = new ClassRepository(); ClassLoader cl = repos.getClassLoader(); ClassLoader org = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(cl); for (Bean bean : beans) { Class genclazz = forName(bean.getSchema().getType()); Object beanToValidate = conversion.convert(bean, genclazz); logger.debug("Validating {}", beanToValidate); Set<ConstraintViolation<Object>> violations = validator.validate(beanToValidate); String msg = ""; for (ConstraintViolation<Object> v : violations) { msg = msg + v.getPropertyPath() + " " + v.getMessage(); } if (msg != null && !"".equals(msg.trim())) { throw CFG309_VALIDATION_ERROR(msg); } } } finally { Thread.currentThread().setContextClassLoader(org); } }
/** * 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); } } }
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); } }
public XmlSchema(Schema bean) { this.id = new XmlSchemaId(bean.getId()); this.name = bean.getName(); this.type = bean.getType(); this.desc = bean.getDesc(); for (SchemaProperty p : bean.get(SchemaProperty.class)) { properties.add(new XmlSchemaProperty(p)); } for (SchemaPropertyList p : bean.get(SchemaPropertyList.class)) { collection.add(new XmlSchemaCollection(p)); } for (SchemaPropertyRef p : bean.get(SchemaPropertyRef.class)) { ref.add(new XmlSchemaRef(p)); } for (SchemaPropertyRefList p : bean.get(SchemaPropertyRefList.class)) { refCollection.add(new XmlSchemaRefCollection(p)); } for (SchemaPropertyRefMap p : bean.get(SchemaPropertyRefMap.class)) { refMap.add(new XmlSchemaRefMap(p)); } } }
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); } }
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 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 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); } }
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); } }