@Override /* ConfigStore */ public synchronized String write(String name, String expectedContents, String newContents) throws IOException { // This is a no-op. if (isEquals(expectedContents, newContents)) return null; String currentContents = read(name); if (expectedContents != null && ! isEquals(currentContents, expectedContents)) return currentContents; update(name, newContents); return null; }
@Override /* ContextBuilder */ public ConfigClasspathStore build() { return new ConfigClasspathStore(getPropertyStore()); } }
@Override /* Context */ public ConfigClasspathStoreBuilder builder() { return new ConfigClasspathStoreBuilder(getPropertyStore()); }
private synchronized static Config find(String name) { if (name == null) return null; if (ConfigFileStore.DEFAULT.exists(name)) return Config.create(name).store(ConfigFileStore.DEFAULT).build(); if (ConfigClasspathStore.DEFAULT.exists(name)) return Config.create(name).store(ConfigClasspathStore.DEFAULT).build(); return null; }
@Test public void testExists() throws Exception { ConfigClasspathStore.DEFAULT.write("foo.cfg", null, "foo"); assertTrue(ConfigClasspathStore.DEFAULT.exists("foo.cfg")); assertFalse(ConfigClasspathStore.DEFAULT.exists("foo2.cfg")); ConfigClasspathStore.DEFAULT.write("foo.cfg", "foo", null); assertFalse(ConfigClasspathStore.DEFAULT.exists("foo.cfg")); assertFalse(ConfigClasspathStore.DEFAULT.exists("foo2.cfg")); } }
@Test public void testUpdate() throws Exception { ConfigClasspathStore fs = ConfigClasspathStore.create().build(); final CountDownLatch latch = new CountDownLatch(2); fs.register("X.cfg", new ConfigStoreListener() { @Override public void onChange(String contents) { if ("xxx".equals(contents)) latch.countDown(); } }); fs.register("Y.cfg", new ConfigStoreListener() { @Override public void onChange(String contents) { if ("yyy".equals(contents)) latch.countDown(); } }); fs.update("X.cfg", "xxx"); fs.update("Y.cfg", "yyy"); if (! latch.await(10, TimeUnit.SECONDS)) throw new Exception("CountDownLatch never reached zero."); }
@Override /* ConfigStore */ public synchronized boolean exists(String name) { try { return ! read(name).isEmpty(); } catch (IOException e) { return false; } }
@Test public void testFailOnMismatch() throws Exception { ConfigClasspathStore fs = ConfigClasspathStore.create().build(); assertNotNull(fs.write("X.cfg", "xxx", "foo")); assertEquals("", fs.read("X.cfg")); assertNull(fs.write("X.cfg", null, "foo")); assertEquals("foo", fs.read("X.cfg")); assertNotNull(fs.write("X.cfg", "xxx", "foo")); assertEquals("foo", fs.read("X.cfg")); assertNull(fs.write("X.cfg", "foo", "bar")); assertEquals("bar", fs.read("X.cfg")); }
private synchronized static Config find(String name) { if (name == null) return null; if (ConfigFileStore.DEFAULT.exists(name)) return Config.create(name).store(ConfigFileStore.DEFAULT).build(); if (ConfigClasspathStore.DEFAULT.exists(name)) return Config.create(name).store(ConfigClasspathStore.DEFAULT).build(); return null; }
@Override /* ConfigStore */ public synchronized boolean exists(String name) { try { return ! read(name).isEmpty(); } catch (IOException e) { return false; } }
@Override /* ConfigStore */ public synchronized String write(String name, String expectedContents, String newContents) throws IOException { // This is a no-op. if (isEquals(expectedContents, newContents)) return null; String currentContents = read(name); if (expectedContents != null && ! isEquals(currentContents, expectedContents)) return currentContents; update(name, newContents); return null; }
private synchronized static Config find(String name) { if (name == null) return null; if (ConfigFileStore.DEFAULT.exists(name)) return Config.create(name).store(ConfigFileStore.DEFAULT).build(); if (ConfigClasspathStore.DEFAULT.exists(name)) return Config.create(name).store(ConfigClasspathStore.DEFAULT).build(); return null; }
@Override /* ConfigStore */ public synchronized boolean exists(String name) { try { return ! read(name).isEmpty(); } catch (IOException e) { return false; } }
@Override /* ContextBuilder */ public ConfigClasspathStore build() { return new ConfigClasspathStore(getPropertyStore()); } }
@Override /* Context */ public ConfigClasspathStoreBuilder builder() { return new ConfigClasspathStoreBuilder(getPropertyStore()); }
@Override /* ConfigStore */ public synchronized String write(String name, String expectedContents, String newContents) throws IOException { // This is a no-op. if (isEquals(expectedContents, newContents)) return null; String currentContents = read(name); if (expectedContents != null && ! isEquals(currentContents, expectedContents)) return currentContents; update(name, newContents); return null; }