/** * Gets called when the watcher service on this store is triggered with a file system change. * * @param e The file system event. * @throws IOException */ protected synchronized void onFileEvent(WatchEvent<Path> e) throws IOException { String fn = e.context().getFileName().toString(); String oldContents = cache.get(fn); cache.remove(fn); String newContents = read(fn); if (! isEquals(oldContents, newContents)) { update(fn, newContents); } } }
@Override /* ContextBuilder */ public ConfigFileStore build() { return new ConfigFileStore(getPropertyStore()); } }
/** * Constructor. * * @param ps The settings for this content store. */ protected ConfigFileStore(PropertyStore ps) { super(ps); try { dir = new File(getStringProperty(FILESTORE_directory, ".")).getCanonicalFile(); dir.mkdirs(); charset = getProperty(FILESTORE_charset, Charset.class, Charset.defaultCharset()); updateOnWrite = getBooleanProperty(FILESTORE_updateOnWrite, false); extensions = getArrayProperty(FILESTORE_extensions, String.class, new String[]{"cfg"}); WatcherSensitivity ws = getProperty(FILESTORE_watcherSensitivity, WatcherSensitivity.class, WatcherSensitivity.MEDIUM); watcher = getBooleanProperty(FILESTORE_useWatcher, false) ? new WatcherThread(dir, ws) : null; if (watcher != null) watcher.start(); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Constructor. * * @param ps The settings for this content store. */ protected ConfigFileStore(PropertyStore ps) { super(ps); try { dir = new File(getStringProperty(FILESTORE_directory, ".")).getCanonicalFile(); dir.mkdirs(); charset = getProperty(FILESTORE_charset, Charset.class, Charset.defaultCharset()); updateOnWrite = getBooleanProperty(FILESTORE_updateOnWrite, false); WatcherSensitivity ws = getProperty(FILESTORE_watcherSensitivity, WatcherSensitivity.class, WatcherSensitivity.MEDIUM); watcher = getBooleanProperty(FILESTORE_useWatcher, false) ? new WatcherThread(dir, ws) : null; if (watcher != null) watcher.start(); } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void testSimpleCreateAndDelete() throws Exception { ConfigFileStore fs = ConfigFileStore.create().directory(DIR).build(); assertNull(fs.write("X.cfg", null, "foo")); assertEquals("foo", fs.read("X.cfg")); assertFileExists("X.cfg"); fs.write("X.cfg", "foo", null); assertFileNotExists("X.cfg"); }
name = resolveName(name); Path p = resolveFile(name); name = p.getFileName().toString(); return ""; if (isWritable(p)) { if (newContents == null) Files.delete(p); update(name, newContents); else
@Override /* ConfigStore */ public synchronized String read(String name) throws IOException { name = resolveName(name); Path p = resolveFile(name); name = p.getFileName().toString(); String s = cache.get(name); if (s != null) return s; dir.mkdirs(); // If file doesn't exist, don't trigger creation. if (! Files.exists(p)) return ""; boolean isWritable = isWritable(p); OpenOption[] oo = isWritable ? new OpenOption[]{READ,WRITE,CREATE} : new OpenOption[]{READ}; try (FileChannel fc = FileChannel.open(p, oo)) { try (FileLock lock = isWritable ? fc.lock() : null) { ByteBuffer buf = ByteBuffer.allocate(1024); StringBuilder sb = new StringBuilder(); while (fc.read(buf) != -1) { sb.append(charset.decode((ByteBuffer)(buf.flip()))); buf.clear(); } s = sb.toString(); cache.put(name, s); } } return cache.get(name); }
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; }
return ""; if (isWritable(p)) { if (newContents == null) Files.delete(p); update(name, newContents); else
@Test public void testUpdate() throws Exception { ConfigFileStore fs = ConfigFileStore.create().directory(DIR).build(); fs.register("X.cfg", new ConfigStoreListener() { @Override public void onChange(String contents) { fs.register("X", new ConfigStoreListener() { @Override public void onChange(String contents) { fs.register("Y.cfg", new ConfigStoreListener() { @Override public void onChange(String contents) { fs.register("Y", new ConfigStoreListener() { @Override public void onChange(String contents) { fs.update("X.cfg", "xxx"); fs.update("Y.cfg", "yyy"); if (! latch.await(10, TimeUnit.SECONDS)) throw new Exception("CountDownLatch never reached zero.");
@Test public void testExists() throws IOException { ConfigFileStore cs = ConfigFileStore.DEFAULT; assertTrue(cs.exists("test.cfg")); assertTrue(cs.exists("test")); assertFalse(cs.exists("test2.cfg")); assertFalse(cs.exists("foo.cfg")); cs.write("foo.cfg", null, "foo"); assertTrue(cs.exists("foo.cfg")); assertTrue(cs.exists("foo")); cs.write("foo.cfg", "foo", null); assertFalse(cs.exists("foo.cfg")); assertFalse(cs.exists("foo")); IOUtils.write(new File("Foox.cfg"), new StringReader("xxx")); assertTrue(cs.exists("Foox.cfg")); assertTrue(cs.exists("Foox")); new File("Foox.cfg").delete(); assertFalse(cs.exists("Foox.cfg")); assertFalse(cs.exists("Foox")); }
@Override /* Context */ public ConfigFileStoreBuilder builder() { return new ConfigFileStoreBuilder(getPropertyStore()); }
@Override /* ConfigStore */ public synchronized String read(String name) throws IOException { String s = cache.get(name); if (s != null) return s; dir.mkdirs(); // If file doesn't exist, don't trigger creation. Path p = dir.toPath().resolve(name); if (! Files.exists(p)) return ""; boolean isWritable = isWritable(p); OpenOption[] oo = isWritable ? new OpenOption[]{READ,WRITE,CREATE} : new OpenOption[]{READ}; try (FileChannel fc = FileChannel.open(p, oo)) { try (FileLock lock = isWritable ? fc.lock() : null) { ByteBuffer buf = ByteBuffer.allocate(1024); StringBuilder sb = new StringBuilder(); while (fc.read(buf) != -1) { sb.append(charset.decode((ByteBuffer)(buf.flip()))); buf.clear(); } s = sb.toString(); cache.put(name, s); } } return cache.get(name); }
name = resolveName(name); Path p = resolveFile(name); name = p.getFileName().toString(); return ""; if (isWritable(p)) { if (newContents == null) Files.delete(p); update(name, newContents); else
@Override /* ConfigStore */ public synchronized String read(String name) throws IOException { name = resolveName(name); Path p = resolveFile(name); name = p.getFileName().toString(); String s = cache.get(name); if (s != null) return s; dir.mkdirs(); // If file doesn't exist, don't trigger creation. if (! Files.exists(p)) return ""; boolean isWritable = isWritable(p); OpenOption[] oo = isWritable ? new OpenOption[]{READ,WRITE,CREATE} : new OpenOption[]{READ}; try (FileChannel fc = FileChannel.open(p, oo)) { try (FileLock lock = isWritable ? fc.lock() : null) { ByteBuffer buf = ByteBuffer.allocate(1024); StringBuilder sb = new StringBuilder(); while (fc.read(buf) != -1) { sb.append(charset.decode((ByteBuffer)(buf.flip()))); buf.clear(); } s = sb.toString(); cache.put(name, s); } } return cache.get(name); }
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 /* Context */ public ConfigFileStoreBuilder builder() { return new ConfigFileStoreBuilder(getPropertyStore()); }