public static Status getStatus(Event e) { Status status; switch (e.getCode()) { case Events.CFG101: status = Status.NOT_FOUND;
@EventDoc(module = MODULE_NAME, code = CFG001, desc = CFG001_MSG) static Event CFG001_SUCCESS() { return new Event(MODULE_NAME, CFG001, CFG001_MSG); }
/** * A human readable and informative error message. */ @Override public String getMessage() { return event.toString(); } }
assertThat(e.getEvent().getCode(), is(CFG309)); System.out.println(e.getEvent().getMessage());
public Response toResponse(Exception ex) { if (ex instanceof AbortRuntimeException) { AbortRuntimeException abort = (AbortRuntimeException) ex; log.warn("{}", ex.getMessage()); log.debug("Exception occured.", ex); Event e = abort.getEvent(); Status status = getStatus(e); return Response.serverError().status(status).entity(e.getMessage()).build(); } else if (ex instanceof WebApplicationException) { WebApplicationException webex = (WebApplicationException) ex; return Response.serverError().status(webex.getResponse().getStatus()).build(); } else { log.warn("Unexpected INTERNAL_SERVER_ERROR ", ex); return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); } }
assertThat(e.getEvent().getCode(), is(CFG309)); System.out.println(e.getEvent().getMessage());
@Test public void test_delete_reference_violation() { admin.createObjects(Arrays.asList(g1, g2, p1, p2, c1, c2)); // test single try { admin.delete(BeanId.create("c1", ConfigTestData.CHILD_SCHEMA_NAME)); fail("Should not be possible to delete a bean with references"); } catch (AbortRuntimeException e) { assertThat(e.getEvent().getCode(), is(CFG302)); } // test multiple try { admin.delete(ConfigTestData.CHILD_SCHEMA_NAME, Arrays.asList("c1", "c2")); fail("Should not be possible to delete a bean with references"); } catch (AbortRuntimeException e) { assertThat(e.getEvent().getCode(), is(CFG302)); } }
@EventDoc(module = MODULE_NAME, code = CFG001, desc = CFG001_MSG) static Event CFG001_SUCCESS() { return new Event(MODULE_NAME, CFG001, CFG001_MSG); }
/** * A human readable and informative error message. */ @Override public String getMessage() { return event.toString(); } }
@Test public void test_set_merge_without_schema() { Bean b = Bean.create(BeanId.create("1", "missing_schema_name")); try { admin.create(b); fail("Cant add beans without a schema."); } catch (AbortRuntimeException e) { assertThat(e.getEvent().getCode(), is(CFG101)); } try { admin.merge(b); fail("Cant add beans without a schema."); } catch (AbortRuntimeException e) { assertThat(e.getEvent().getCode(), is(CFG101)); } }
@EventDoc(module = MODULE_NAME, code = CFG102, desc = "Class is not configurable.") public static AbortRuntimeException CFG102_NOT_CONFIGURABLE(Class<?> clazz) { Event event = new Event(MODULE_NAME, CFG102, MessageFormat.format(CFG102_MSG, clazz.getName())); return new AbortRuntimeException(event); }
@Test public void test_set_merge_without_schema() { Bean b = Bean.create(BeanId.create("1", "missing_schema_name")); try { admin.create(b); fail("Cant add beans without a schema."); } catch (AbortRuntimeException e) { assertThat(e.getEvent().getCode(), is(CFG101)); assertThat(observer.getChanges().size(), is(0)); } try { admin.merge(b); fail("Cant add beans without a schema."); } catch (AbortRuntimeException e) { assertThat(e.getEvent().getCode(), is(CFG101)); assertThat(observer.getChanges().size(), is(0)); } }
@EventDoc(module = MODULE_NAME, code = CFG104, desc = "Configurable class have a property of unsupported type.") public static AbortRuntimeException CFG104_UNSUPPORTED_PROPERTY(Class<?> clazz, String property, Class<?> type) { Event event = new Event(MODULE_NAME, CFG104, MessageFormat.format(CFG104_MSG, clazz.getName(), property, type.getName())); return new AbortRuntimeException(event); }
@Override public void registerDefault(Object... instances) { doLookup(); for (Object instance : instances) { Bean bean = schemaManager.convertObject(instance); bean.setDefault(); try { beanManager.create(bean); } catch (AbortRuntimeException e) { // ignore if bean already exist if (e.getEvent().getCode() != CFG303) { throw e; } } } }
@EventDoc(module = MODULE_NAME, code = CFG109, desc = "Bean property have illegal map parameterization.") public static AbortRuntimeException CFG109_ILLEGAL_MAP(String propertyName) { Event event = new Event(MODULE_NAME, CFG109, MessageFormat.format(CFG109_MSG, propertyName)); return new AbortRuntimeException(event); }
@Produces public ServerSpecificConfigProxy<T> produceServerConfigProxy(InjectionPoint ip){ Class<?> declaringClass = ip.getMember().getDeclaringClass(); Class<?> configCls = getParameterizedType(declaringClass, ip.getAnnotated().getBaseType()); try { Optional<?> optionalServer = config.get(serverName.getName(), configCls); if (optionalServer.isPresent()) { return new ServerSpecificConfigProxy(optionalServer.get()); } else { log.debug("Config instance {} of type {} not found", serverName.getName(), configCls); Object config = newInstance(configCls, serverName.getName()); return new ServerSpecificConfigProxy(config); } } catch (AbortRuntimeException e) { if (e.getEvent().getCode() != Events.CFG304){ throw e; } log.debug("Config instance {} of type {} not found", serverName.getName(), configCls); Object config = newInstance(configCls, serverName.getName()); return new ServerSpecificConfigProxy(config); } }
@EventDoc(module = MODULE_NAME, code = CFG111, desc = "Bean have reference that does not exist in schema.") public static AbortRuntimeException CFG111_REF_NOT_EXIST_IN_SCHEMA(String referenceName) { Event event = new Event(MODULE_NAME, CFG111, MessageFormat.format(CFG111_MSG, referenceName)); return new AbortRuntimeException(event); }
@Test public void test_delete_reference_violation() { admin.create(toBeans(g1, g2, p1, p2, c1, c2)); observer.clear(); // test single try { admin.delete(BeanId.create("c1", CHILD_SCHEMA_NAME)); fail("Should not be possible to delete a bean with references"); } catch (AbortRuntimeException e) { assertThat(e.getEvent().getCode(), is(CFG302)); assertThat(observer.getChanges().size(), is(0)); } // test multiple try { admin.delete(CHILD_SCHEMA_NAME, Arrays.asList("c1", "c2")); fail("Should not be possible to delete a bean with references"); } catch (AbortRuntimeException e) { assertThat(e.getEvent().getCode(), is(CFG302)); assertThat(observer.getChanges().size(), is(0)); } }
@EventDoc(module = MODULE_NAME, code = CFG102, desc = "Class is not configurable.") public static AbortRuntimeException CFG102_NOT_CONFIGURABLE(Class<?> clazz) { Event event = new Event(MODULE_NAME, CFG102, MessageFormat.format(CFG102_MSG, clazz.getName())); return new AbortRuntimeException(event); }
@Test public void test_delete_default() { List<Bean> beans = generateBeans(2, 2); for (Bean bean : beans) { bean.setDefault(); } manager.create(beans); for (Bean bean : beans) { try { manager.delete(bean.getId()); fail("Should not be able to delete default instances"); } catch (AbortRuntimeException e) { assertThat(e.getEvent().getCode(), is(CFG311)); } } } }