public void updateGlobalConfig(byte[] data) throws IOException { if (data == null) throw new IllegalStateException("global config data cannot be null"); updateGlobalConfig(new ByteArrayInputStream(data)); }
public void updateGlobalConfig(Map<String, Object> globalConfig) { if(globalConfig != null) { getConfigurations().put(ConfigurationType.GLOBAL.getTypeName(), globalConfig); validations = FieldValidator.readValidations(getGlobalConfig()); } }
public Configurations getConfiguration(String config) throws IOException { Configurations configurations = new Configurations(); configurations.updateGlobalConfig(Bytes.toBytes(config)); return configurations; }
@Test public void test() throws IOException { EqualsVerifier.forClass(Configurations.class).suppress(Warning.NONFINAL_FIELDS, Warning.NULL_FIELDS).usingGetClass().verify(); Configurations configurations = new Configurations(); try { configurations.updateGlobalConfig((byte[]) null); Assert.fail("Updating a config with null should throw an IllegalStateException"); } catch(IllegalStateException e) {} Assert.assertTrue(configurations.toString() != null && configurations.toString().length() > 0); } }
@SuppressWarnings("unchecked") public Map<String, Object> getGlobalConfig() { return getGlobalConfig(true); }
@Override public String toString() { return "Configurations{" + "validations=" + validations + ", configurations=" + getConfigurations()+ '}'; } }
public FieldValidator getValidator(Configurations configurations) throws IOException { return configurations.getFieldValidations().get(0); }
/** * Delete callback, this is called whenever a path is deleted in zookeeper which we are monitoring. * * @param client The CuratorFramework * @param path The zookeeper path * @param data The change. * @throws IOException When update is impossible. */ public void delete(CuratorFramework client, String path, byte[] data) throws IOException { String name = path.substring(path.lastIndexOf("/") + 1); if (path.startsWith(getType().getZookeeperRoot())) { LOG.debug("Deleting {} {} config from internal cache", getType().name(), name); delete(name); reloadCallback(name, getType()); } else if (ConfigurationType.GLOBAL.getZookeeperRoot().equals(path)) { LOG.debug("Deleting global config from internal cache"); getConfigurations().deleteGlobalConfig(); reloadCallback(name, ConfigurationType.GLOBAL); } }
public static Configurations getSampleConfigs() throws IOException { Configurations configurations = new Configurations(); configurations.updateGlobalConfig(ConfigurationsUtils.readGlobalConfigFromFile(TestConstants.SAMPLE_CONFIG_PATH)); return configurations; }
public boolean execute(String config, Map<String, Object> input) throws IOException { Configurations configurations = getConfiguration(config); FieldValidator validator = getValidator(configurations); return validator.isValid(new JSONObject(input) ,configurations.getGlobalConfig() , Context.EMPTY_CONTEXT() ); } }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Configurations that = (Configurations) o; if (validations != null ? !validations.equals(that.validations) : that.validations != null) return false; return getConfigurations() != null ? getConfigurations().equals(that.getConfigurations()) : that.getConfigurations() == null; }
@Test public void testValidConfiguration() throws IOException { { Configurations configurations = getConfiguration(validValidationConfigWithStringInput); Assert.assertNotNull(configurations.getFieldValidations()); Assert.assertEquals(1, configurations.getFieldValidations().size()); Assert.assertEquals(ImmutableList.of("field1"), configurations.getFieldValidations().get(0).getInput()); } { Configurations configurations = getConfiguration(validValidationConfigWithListInput); Assert.assertNotNull(configurations.getFieldValidations()); Assert.assertEquals(1, configurations.getFieldValidations().size()); Assert.assertEquals(ImmutableList.of("field1", "field2"), configurations.getFieldValidations().get(0).getInput()); } }
public static void updateConfigsFromZookeeper(Configurations configurations, CuratorFramework client) throws Exception { configurations.updateGlobalConfig(readGlobalConfigBytesFromZookeeper(client)); }
@Override public int hashCode() { int result = validations != null ? validations.hashCode() : 0; result = 31 * result + (getConfigurations() != null ? getConfigurations().hashCode() : 0); return result; }
public void updateGlobalConfig(InputStream io) throws IOException { Map<String, Object> globalConfig = JSONUtils.INSTANCE.load(io, JSONUtils.MAP_SUPPLIER); updateGlobalConfig(globalConfig); }
public void deleteGlobalConfig() { getConfigurations().remove(ConfigurationType.GLOBAL.getTypeName()); }
/** * Update callback, this is called whenever a path is updated in zookeeper which we are monitoring. * * @param client The CuratorFramework * @param path The zookeeper path * @param data The change. * @throws IOException When update is impossible. */ public void update(CuratorFramework client, String path, byte[] data) throws IOException { if (data.length != 0) { String name = path.substring(path.lastIndexOf("/") + 1); if (path.startsWith(getType().getZookeeperRoot())) { LOG.debug("Updating the {} config: {} -> {}", getType().name(), name, new String(data == null?"".getBytes():data)); update(name, data); reloadCallback(name, getType()); } else if (ConfigurationType.GLOBAL.getZookeeperRoot().equals(path)) { LOG.debug("Updating the global config: {}", new String(data == null?"".getBytes():data)); getConfigurations().updateGlobalConfig(data); reloadCallback(name, ConfigurationType.GLOBAL); } } }
public Map<String, Object> getGlobalConfig(boolean emptyMapOnNonExistent) { return (Map<String, Object>) getConfigurations().getOrDefault(ConfigurationType.GLOBAL.getTypeName(), emptyMapOnNonExistent?new HashMap():null); }