/** * Create a bean identification. * * @param instanceId of this bean. * @param schemaName The bean schema name. * @return AdminBeanId */ public static BeanId create(final String instanceId, final String schemaName) { if (instanceId == null || "".equals(instanceId)) { throw CFG107_MISSING_ID(); } return new BeanId(instanceId, schemaName); }
public static void translateMerge(BeanId id, Throwable e) { LOG.debug("", e); Throwable cause = e; while ((cause = cause.getCause()) != null) { if (CONSTRAINT_VIOLATION.contains(cause.getClass().getName())) { throw CFG301_MISSING_RUNTIME_REF(id); } } throw new RuntimeException(e); }
public static void translateDelete(Collection<BeanId> ids, Throwable e) { LOG.debug("", e); Throwable cause = e; while ((cause = cause.getCause()) != null) { System.out.println(cause); if (CONSTRAINT_VIOLATION.contains(cause.getClass().getName())) { throw CFG302_CANNOT_DELETE_BEAN(ids); } } throw new RuntimeException(e); }
@Override public Bean getLazy(BeanId id) throws AbortRuntimeException { Map<BeanId, Bean> all = readValuesAsMap(); Bean bean = all.get(id); if (bean == null) { throw CFG304_BEAN_DOESNT_EXIST(id); } for (BeanId ref : bean.getReferences()) { Bean refBean = all.get(ref); if (bean == null) { throw CFG301_MISSING_RUNTIME_REF(ref); } ref.setBean(refBean); } return bean; }
private static String validateSingle(Bean bean, AbstractSchemaProperty prop) { List<String> values = bean.getValues(prop.getName()); if (values == null) { return null; } if (prop.isImmutable()) { throw Events.CFG306_PROPERTY_IMMUTABLE(bean.getId(), prop.getName()); } if (values.size() > 1) { throw Events.CFG106_WRONG_MULTIPLICITY_TYPE(bean.getId(), prop.getName()); } if (values.size() == 0) { return null; } return values.get(0); }
private Map<BeanId, Bean> getDirectPredecessors(Bean bean, Map<BeanId, Bean> all) { Map<BeanId, Bean> predecessors = new HashMap<BeanId, Bean>(); for (BeanId ref : bean.getReferences()) { Bean predecessor = all.get(ref); if (predecessor == null) { throw CFG304_BEAN_DOESNT_EXIST(ref); } predecessors.put(predecessor.getId(), predecessor); } return predecessors; }
private static void checkUniquness(Bean bean, Map<BeanId, Bean> storage) { Collection<Bean> beans = storage.values(); for (Bean existing : beans) { if (bean.getId().equals(existing.getId())) { throw CFG303_BEAN_ALREADY_EXIST(bean.getId()); } } }
@Override public Schema getSchema(String schemaName) { Map<String, Schema> values = readValues(); Schema schema = values.get(schemaName); if (schema == null) { throw CFG101_SCHEMA_NOT_EXIST(schemaName); } return schema; }
private Schema getSchema(Class<?> clazz) { 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(); } return schemaManager.getSchema(schemaName); } }
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); } } }
throw CFG104_UNSUPPORTED_PROPERTY(String.class, name, type);
private Map<BeanId, Bean> getDirectPredecessors(Bean bean) { Map<BeanId, Bean> predecessors = new HashMap<>(); for (BeanId ref : bean.getReferences()) { Bean predecessor = storage.get(ref); if (predecessor == null) { throw CFG304_BEAN_DOESNT_EXIST(ref); } predecessors.put(predecessor.getId(), predecessor); } return predecessors; }
private Bean getEagerly(BeanId id, Map<BeanId, Bean> all) { Bean result = all.get(id); if (result == null) { throw CFG304_BEAN_DOESNT_EXIST(id); } // bean found, initalize references. for (BeanId ref : result.getReferences()) { if (ref.getBean() != null) { continue; } Bean refBean = all.get(ref); if (refBean == null) { throw CFG301_MISSING_RUNTIME_REF(result.getId(), ref); } ref.setBean(refBean); getEagerly(ref, all); } return result; }
private static String validateSingle(Bean bean, AbstractSchemaProperty prop) { List<String> values = bean.getValues(prop.getName()); if (values == null) { return null; } if (prop.isImmutable()) { throw Events.CFG306_PROPERTY_IMMUTABLE(bean.getId(), prop.getName()); } if (values.size() > 1) { throw Events.CFG106_WRONG_MULTIPLICITY_TYPE(bean.getId(), prop.getName()); } return values.get(0); } }
private static void checkUniquness(Bean bean) { Collection<Bean> beans = storage.all(); for (Bean existing : beans) { if (bean.getId().equals(existing.getId())) { throw CFG303_BEAN_ALREADY_EXIST(bean.getId()); } } }
@Override public <T> ConfigQuery newQuery(Class<T> configurable) { doLookup(); Schema schema = getSchema(configurable); ConfigIndexedCollection collection = core.get(schema); if(collection == null) { throw Events.CFG101_SCHEMA_NOT_EXIST(configurable.getName()); } if(!cacheManager.isPresent()) { throw new IllegalStateException("Queries are not possible without a cache manager."); } return new ConfigQuery<T>(collection, cacheManager.get()); }
@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); 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<Object>(); fields.addAll(introspector.getFieldList(Config.class)); 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(); ClassRepository repos = new ClassRepository(); for (SchemaProperty prop : schema.get(SchemaProperty.class)) { String value = validateSingle(bean, prop); if (value == null) { continue; } try { conversion.convert(value, repos.loadClass(prop.getType())); } catch (Exception e) { throw Events.CFG105_WRONG_PROPERTY_TYPE(bean.getId(), prop.getName(), prop.getType(), value); } } }
private AbstractSchemaProperty convertSimple(FieldWrap<Config> source) { String name = source.getAnnotation().name(); String desc = source.getAnnotation().desc(); String fieldName = source.getFieldName(); if (name == null || "".equals(name)) { name = fieldName; } Class<?> type = source.getType(); validateField(source); try { if (source.isCollection()) { Collection<String> converted = conversion.convert(source.getDefaultValues(), String.class); List<String> defaultValues = new ArrayList<String>(converted); return SchemaPropertyList.create(name, fieldName, type.getName(), desc, source .isFinal(), source.isEnum(), defaultValues, source.getCollRawType() .getName()); } else { return SchemaProperty.create(name, fieldName, type.getName(), desc, source.isFinal(), source.isEnum(), conversion.convert(source.getDefaultValue(), String.class)); } } catch (ConversionException e) { throw CFG104_UNSUPPORTED_PROPERTY(String.class, name, type); } }
private static void checkInstanceExist(Bean bean, Map<BeanId, Bean> storage) { Collection<Bean> beans = storage.values(); for (Bean existingBean : beans) { if (existingBean.getId().equals(bean.getId())) { return; } } throw CFG304_BEAN_DOESNT_EXIST(bean.getId()); }