@Test(expected = ContainerConfigException.class) public void badConfigThrows() throws Exception { JSONObject json = new JSONObject(); json.put(CONTAINER_KEY, new String[]{CHILD_CONTAINER}); json.put(PARENT_KEY, "bad bad bad parent!"); json.put(ARRAY_NAME, ARRAY_ALT_VALUE); new JsonContainerConfig(createContainer(json).getAbsolutePath(), Expressions.forTesting()); }
@Test public void nullEntryEvaluation() throws Exception { // We use a JSON Object here to guarantee that we're well formed up front. JSONObject json = new JSONObject("{ 'gadgets.container' : ['default'], features : { osapi : null }}"); JsonContainerConfig config = new JsonContainerConfig(createContainer(json).getAbsolutePath(), Expressions.forTesting()); assertNull(config.getMap("default", "features").get("osapi")); } }
/** * Creates a new configuration from files. * @throws ContainerConfigException */ @Inject public JsonContainerConfig(@Named("shindig.containers.default") String containers, @Nullable @Named("shindig.host") String host, @Nullable @Named("shindig.port") String port, Expressions expressions) throws ContainerConfigException { this.expressions = expressions; this.host = host; this.port = port; JSONObject configJson = loadContainers(containers); config = createContainers(configJson); init(); }
/** * Creates a new configuration from a JSON Object, for use in testing. */ public JsonContainerConfig(JSONObject json, Expressions expressions) { this.expressions = expressions; host = null; port = null; config = createContainers(json); init(); }
/** * Initialize each container's configuration. */ private Map<String, Map<String, Object>> createContainers(JSONObject json) { Map<String, Map<String, Object>> map = Maps.newHashMap(); for (String container : JSONObject.getNames(json)) { ELContext context = createExpressionContext(container); map.put(container, jsonToMap(json.optJSONObject(container), expressions, context)); } return map; }
/** * Initializes the configuration. Called during construction. */ protected void init() { for (Map.Entry<String, Map<String, Object>> configEntry : config.entrySet()) { @SuppressWarnings("unchecked") Map<String, Object> value = (Map<String, Object>) evaluateAll(configEntry.getValue()); configEntry.setValue(value); } }
/** * Protected to allow overriding. */ protected ELContext createExpressionContext(String container) { return getExpressions().newELContext(new ContainerConfigELResolver(this, container)); }
@Override public Object getProperty(String container, String property) { if (property.startsWith("${")) { // An expression! try { ValueExpression expression = expressions.parse(property, Object.class); return expression.getValue(createExpressionContext(container)); } catch (ELException e) { return null; } } Map<String, Object> containerData = config.get(container); if (containerData == null) { return null; } return containerData.get(property); }
/** * Initialize each container's configuration. */ private Map<String, Map<String, Object>> createContainers(JSONObject json) { Map<String, Map<String, Object>> map = Maps.newHashMap(); for (String container : JSONObject.getNames(json)) { ELContext context = createExpressionContext(container); map.put(container, jsonToMap(json.optJSONObject(container), expressions, context)); } return map; }
/** * Creates a new configuration from a JSON Object, for use in testing. */ public JsonContainerConfig(JSONObject json, Expressions expressions) { this.expressions = expressions; host = null; port = null; config = createContainers(json); init(); }
/** * Initializes the configuration. Called during construction. */ protected void init() { for (Map.Entry<String, Map<String, Object>> configEntry : config.entrySet()) { @SuppressWarnings("unchecked") Map<String, Object> value = (Map<String, Object>) evaluateAll(configEntry.getValue()); configEntry.setValue(value); } }
/** * Protected to allow overriding. */ protected ELContext createExpressionContext(String container) { return getExpressions().newELContext(new ContainerConfigELResolver(this, container)); }
@Override public Object getProperty(String container, String property) { if (property.startsWith("${")) { // An expression! try { ValueExpression expression = expressions.parse(property, Object.class); return expression.getValue(createExpressionContext(container)); } catch (ELException e) { return null; } } Map<String, Object> containerData = config.get(container); if (containerData == null) { return null; } return containerData.get(property); }
@Test(expected = ContainerConfigException.class) public void badConfigThrows() throws Exception { JSONObject json = new JSONObject(); json.put(CONTAINER_KEY, new String[]{CHILD_CONTAINER}); json.put(PARENT_KEY, "bad bad bad parent!"); json.put(ARRAY_NAME, ARRAY_ALT_VALUE); new JsonContainerConfig(createContainer(json).getAbsolutePath(), Expressions.forTesting()); }
@Test public void nullEntryEvaluation() throws Exception { // We use a JSON Object here to guarantee that we're well formed up front. JSONObject json = new JSONObject("{ 'gadgets.container' : ['default'], features : { osapi : null }}"); JsonContainerConfig config = new JsonContainerConfig(createContainer(json).getAbsolutePath(), Expressions.forTesting()); assertNull(config.getMap("default", "features").get("osapi")); } }
/** * Creates a new configuration from files. * @throws ContainerConfigException */ @Inject public JsonContainerConfig(@Named("shindig.containers.default") String containers, @Nullable @Named("shindig.host") String host, @Nullable @Named("shindig.port") String port, Expressions expressions) throws ContainerConfigException { this.expressions = expressions; this.host = host; this.port = port; JSONObject configJson = loadContainers(containers); config = createContainers(configJson); init(); }
private Object evaluateAll(Object value) { if (value instanceof CharSequence) { return value.toString(); } else if (value instanceof Map<?, ?>) { Map<?, ?> mapValue = (Map<?, ?>) value; ImmutableMap.Builder<Object, Object> newMap = ImmutableMap.builder(); for (Map.Entry<?, ?> entry : mapValue.entrySet()) { newMap.put(entry.getKey(), evaluateAll(entry.getValue())); } return newMap.build(); } else if (value instanceof List<?>) { ImmutableList.Builder<Object> newList = ImmutableList.builder(); for (Object entry : (List<?>) value) { newList.add(evaluateAll(entry)); } return newList.build(); } else { return value; } } }
@Test(expected = ContainerConfigException.class) public void badConfigThrows() throws Exception { JSONObject json = new JSONObject(); json.put(CONTAINER_KEY, new String[]{CHILD_CONTAINER}); json.put(PARENT_KEY, "bad bad bad parent!"); json.put(ARRAY_NAME, ARRAY_ALT_VALUE); new JsonContainerConfig(createContainer(json).getAbsolutePath(), Expressions.forTesting()); }
@Test public void nullEntryEvaluation() throws Exception { // We use a JSON Object here to guarantee that we're well formed up front. JSONObject json = new JSONObject("{ 'gadgets.container' : ['default'], features : { osapi : null }}"); JsonContainerConfig config = new JsonContainerConfig(createContainer(json).getAbsolutePath(), Expressions.forTesting()); assertNull(config.getMap("default", "features").get("osapi")); } }
private Object evaluateAll(Object value) { if (value instanceof CharSequence) { return value.toString(); } else if (value instanceof Map<?, ?>) { Map<?, ?> mapValue = (Map<?, ?>) value; ImmutableMap.Builder<Object, Object> newMap = ImmutableMap.builder(); for (Map.Entry<?, ?> entry : mapValue.entrySet()) { newMap.put(entry.getKey(), evaluateAll(entry.getValue())); } return newMap.build(); } else if (value instanceof List<?>) { ImmutableList.Builder<Object> newList = ImmutableList.builder(); for (Object entry : (List<?>) value) { newList.add(evaluateAll(entry)); } return newList.build(); } else { return value; } } }