@Override public Schema getSchema(String schemaName) { Schema schema = NAME_TO_SCHEMA.get(schemaName); if (schema == null) { throw CFG101_SCHEMA_NOT_EXIST(schemaName); } return schema; }
private BeanId getBeanId(Object bean) { Config config = bean.getClass().getAnnotation(Config.class); if (config == null) { throw CFG102_NOT_CONFIGURABLE(bean.getClass()); } 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 lookup return BeanId.createSingleton(schemaName); } FieldWrap id = ids.get(0); BeanId targetId; Object idValue = id.getValue(bean); if (idValue == null) { throw Events.CFG107_MISSING_ID(); } targetId = BeanId.create(id.getValue(bean).toString(), schemaName); return targetId; }
private void handleResponse(Response response) { try { if (response.getStatus() == Status.NOT_FOUND.getStatusCode()) { throw Events.CFG90_SCHEMA_OR_ID_NOT_EXIST(); } else if (response.getStatus() == Status.CONFLICT.getStatusCode()) { throw Events.CFG303_BEAN_ALREADY_EXIST(); } else if (response.getStatus() == Status.FORBIDDEN.getStatusCode()) { throw Events.CFG089_MODIFICATION_CONFLICT(); } else if (response.getStatus() == Status.BAD_REQUEST.getStatusCode()) { throw Events.CFG088_INVALID_DATA(); } else if (response.getStatus() == Status.INTERNAL_SERVER_ERROR.getStatusCode()) { throw Events.CFG088_INVALID_DATA(); } } finally { response.close(); } } private void handleReadResponse(Response response) {
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; }
throw Events.CFG101_SCHEMA_NOT_EXIST(schemaName); Optional<Bean> optional = get(id, schemas); if (!optional.isPresent()) { throw CFG301_MISSING_RUNTIME_REF(id); Optional<Bean> ref = get(beanId, schemas); if (!ref.isPresent()) { throw CFG301_MISSING_RUNTIME_REF(beanId); Optional<Bean> idBean = get(id, schemas); if (!idBean.isPresent()) { throw Events.CFG301_MISSING_RUNTIME_REF(id);
/** * 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); }
/** * Check that references does not point to self. * @param references from */ private void checkCircularReference(final BeanId... references) { for (BeanId beanId : references) { if (getId().equals(beanId)) { throw CFG310_CIRCULAR_REF(getId(), getId()); } } }
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()); } } }
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); } } }
@Override public void register(Class<?>... classes) { for (Class<?> cls : classes) { if (CLASS_TO_SCHEMA.get(cls) != null) { continue; } ClassIntrospector introspector = new ClassIntrospector(cls); Config config = introspector.getAnnotation(Config.class); if (config == null) { throw CFG102_NOT_CONFIGURABLE(cls); } String schemaName = config.name(); if (schemaName == null || "".equals(schemaName)) { schemaName = cls.getName(); } Schema schema = CONVERSION.convert(cls, Schema.class); CLASS_TO_SCHEMA.put(cls, schema); NAME_TO_SCHEMA.put(schemaName, schema); for (Class<?> refCls : schema.getReferenceSchemaTypes()) { if (CLASS_TO_SCHEMA.get(refCls) != null) { register(cls); } } } }
throw CFG104_UNSUPPORTED_PROPERTY(String.class, name, type);
@Override public void delete(BeanId beanId) { Preconditions.checkNotNull(beanId); doLookup(); Optional<Bean> before = get(beanId); if (!before.isPresent()) { throw Events.CFG304_BEAN_DOESNT_EXIST(beanId); } Bean bean = beanManager.delete(beanId); if (bean == null) { throw Events.CFG304_BEAN_DOESNT_EXIST(beanId); } if (cacheManager.isPresent()) { cacheManager.get().remove(beanId); } notificationManager.fireDelete(Arrays.asList(before.get())); }
/** * 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); }
/** * Check that references does not point to self. * @param references from */ private void checkCircularReference(final BeanId... references) { for (BeanId beanId : references) { if (getId().equals(beanId)) { throw CFG310_CIRCULAR_REF(getId(), getId()); } } }
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); } } } }
@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) { // lookup 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 void handleReadResponse(Response response) { if (response.getStatus() == Status.NOT_FOUND.getStatusCode()) { response.close(); throw Events.CFG90_SCHEMA_OR_ID_NOT_EXIST(); } else if (response.getStatus() == Status.CONFLICT.getStatusCode()) { response.close(); throw Events.CFG303_BEAN_ALREADY_EXIST(); } else if (response.getStatus() == Status.FORBIDDEN.getStatusCode()) { response.close(); throw Events.CFG089_MODIFICATION_CONFLICT(); } else if (response.getStatus() == Status.BAD_REQUEST.getStatusCode()) { response.close(); throw Events.CFG088_INVALID_DATA(); } else if (response.getStatus() == Status.INTERNAL_SERVER_ERROR.getStatusCode()) { response.close(); throw Events.CFG088_INVALID_DATA(); } } }
@Override public void merge(Bean bean) { Bean b = storage.get(bean.getId()); if (b == null) { throw CFG304_BEAN_DOESNT_EXIST(bean.getId()); } replace(b, bean); }
@GET @Produces({ APPLICATION_JSON }) @Path("listObjects/{className}") public JaxrsObjects listObjects(@PathParam("className") String className) throws AbortRuntimeException { try { Collection<?> objects = admin.list(Class.forName(className)); return new JaxrsObjects(objects); } catch (ClassNotFoundException e) { throw Events.CFG101_SCHEMA_NOT_EXIST(className); } }
private static void validateId(Bean bean) { if (bean.getId().getInstanceId() == null || "".equals(bean.getId().getInstanceId())) { throw Events.CFG107_MISSING_ID(); } }