/** * Writes dynamic configuration file */ public static void writeDynamicConfig(final String dynamicConfigFilename, final QuorumVerifier qv, final boolean needKeepVersion) throws IOException { new AtomicFileWritingIdiom(new File(dynamicConfigFilename), new WriterStatement() { @Override public void write(Writer out) throws IOException { Properties cfg = new Properties(); cfg.load( new StringReader( qv.toString())); List<String> servers = new ArrayList<String>(); for (Entry<Object, Object> entry : cfg.entrySet()) { String key = entry.getKey().toString().trim(); if ( !needKeepVersion && key.startsWith("version")) continue; String value = entry.getValue().toString().trim(); servers.add(key .concat("=") .concat(value)); } Collections.sort(servers); out.write(StringUtils.joinStrings(servers, "\n")); } }); }
/** * Backward compatibility -- It would backup static config file on bootup * if users write dynamic configuration in "zoo.cfg". */ private void backupOldConfig() throws IOException { new AtomicFileWritingIdiom(new File(configFileStr + ".bak"), new OutputStreamStatement() { @Override public void write(OutputStream output) throws IOException { InputStream input = null; try { input = new FileInputStream(new File(configFileStr)); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) > 0) { output.write(buf, 0, bytesRead); } } finally { if( input != null) { input.close(); } } } }); }
/** * Write a long value to disk atomically. Either succeeds or an exception * is thrown. * @param name file name to write the long to * @param value the long value to write to the named file * @throws IOException if the file cannot be written atomically */ private void writeLongToFile(String name, final long value) throws IOException { File file = new File(logFactory.getSnapDir(), name); new AtomicFileWritingIdiom(file, new WriterStatement() { @Override public void write(Writer bw) throws IOException { bw.write(Long.toString(value)); } }); }
@Test public void testOutputStreamSuccessNE() throws IOException { File target = new File(tmpdir, "target.txt"); final File tmp = new File(tmpdir, "target.txt.tmp"); target.delete(); assertFalse("file should not exist", target.exists()); new AtomicFileWritingIdiom(target, new OutputStreamStatement() { @Override public void write(OutputStream os) throws IOException { os.write("after".getBytes("ASCII")); assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); } }); // content changed assertEquals("after", getContent(target)); target.delete(); }
new AtomicFileWritingIdiom(new File(configFileStr), new WriterStatement() { @Override public void write(Writer out) throws IOException {
@Test public void testWriterSuccessNE() throws IOException { File target = new File(tmpdir, "target.txt"); final File tmp = new File(tmpdir, "target.txt.tmp"); target.delete(); assertFalse("file should not exist", target.exists()); new AtomicFileWritingIdiom(target, new WriterStatement() { @Override public void write(Writer os) throws IOException { os.write("after"); assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); } }); assertFalse("tmp file should have been deleted", tmp.exists()); // content changed assertEquals("after", getContent(target)); target.delete(); }
@Test public void testOutputStreamFailureNE() throws IOException { File target = new File(tmpdir, "target.txt"); final File tmp = new File(tmpdir, "target.txt.tmp"); target.delete(); assertFalse("file should not exist", target.exists()); boolean exception = false; try { new AtomicFileWritingIdiom(target, new OutputStreamStatement() { @Override public void write(OutputStream os) throws IOException { os.write("after".getBytes("ASCII")); os.flush(); assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); throw new RuntimeException(); } }); } catch (RuntimeException ex) { exception = true; } assertFalse("tmp file should have been deleted", tmp.exists()); assertTrue("should have raised an exception", exception); // file should not exist assertFalse("file should not exist", target.exists()); }
@Test public void testWriterFailureNE() throws IOException { File target = new File(tmpdir, "target.txt"); final File tmp = new File(tmpdir, "target.txt.tmp"); target.delete(); assertFalse("file should not exist", target.exists()); boolean exception = false; try { new AtomicFileWritingIdiom(target, new WriterStatement() { @Override public void write(Writer os) throws IOException { os.write("after"); os.flush(); assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); throw new RuntimeException(); } }); } catch (RuntimeException ex) { exception = true; } assertFalse("tmp file should have been deleted", tmp.exists()); assertTrue("should have raised an exception", exception); // file should not exist assertFalse("file should not exist", target.exists()); }
@Test public void testOutputStreamSuccess() throws IOException { File target = new File(tmpdir, "target.txt"); final File tmp = new File(tmpdir, "target.txt.tmp"); createFile(target, "before"); assertEquals("before", getContent(target)); new AtomicFileWritingIdiom(target, new OutputStreamStatement() { @Override public void write(OutputStream os) throws IOException { os.write("after".getBytes("ASCII")); assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); } }); assertFalse("tmp file should have been deleted", tmp.exists()); // content changed assertEquals("after", getContent(target)); target.delete(); }
@Test public void testWriterSuccess() throws IOException { File target = new File(tmpdir, "target.txt"); final File tmp = new File(tmpdir, "target.txt.tmp"); createFile(target, "before"); assertEquals("before", getContent(target)); new AtomicFileWritingIdiom(target, new WriterStatement() { @Override public void write(Writer os) throws IOException { os.write("after"); assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); } }); assertFalse("tmp file should have been deleted", tmp.exists()); // content changed assertEquals("after", getContent(target)); target.delete(); }
@Test public void testOutputStreamFailureError() throws IOException { File target = new File(tmpdir, "target.txt"); final File tmp = new File(tmpdir, "target.txt.tmp"); createFile(target, "before"); assertEquals("before", getContent(target)); boolean exception = false; try { new AtomicFileWritingIdiom(target, new OutputStreamStatement() { @Override public void write(OutputStream os) throws IOException { os.write("after".getBytes("ASCII")); os.flush(); assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); throw new Error(); } }); } catch (Error ex) { exception = true; } assertFalse("tmp file should have been deleted", tmp.exists()); assertTrue("should have raised an exception", exception); // content preserved assertEquals("before", getContent(target)); target.delete(); }
@Test public void testOutputStreamFailureIOException() throws IOException { File target = new File(tmpdir, "target.txt"); final File tmp = new File(tmpdir, "target.txt.tmp"); createFile(target, "before"); assertEquals("before", getContent(target)); boolean exception = false; try { new AtomicFileWritingIdiom(target, new OutputStreamStatement() { @Override public void write(OutputStream os) throws IOException { os.write("after".getBytes("ASCII")); os.flush(); assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); throw new IOException(); } }); } catch (IOException ex) { exception = true; } assertFalse("tmp file should have been deleted", tmp.exists()); assertTrue("should have raised an exception", exception); // content preserved assertEquals("before", getContent(target)); target.delete(); }
@Test public void testOutputStreamFailure() throws IOException { File target = new File(tmpdir, "target.txt"); final File tmp = new File(tmpdir, "target.txt.tmp"); createFile(target, "before"); assertEquals("before", getContent(target)); boolean exception = false; try { new AtomicFileWritingIdiom(target, new OutputStreamStatement() { @Override public void write(OutputStream os) throws IOException { os.write("after".getBytes("ASCII")); os.flush(); assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); throw new RuntimeException(); } }); } catch (RuntimeException ex) { exception = true; } assertFalse("tmp file should have been deleted", tmp.exists()); assertTrue("should have raised an exception", exception); // content preserved assertEquals("before", getContent(target)); target.delete(); }
@Test public void testWriterFailure() throws IOException { File target = new File(tmpdir, "target.txt"); final File tmp = new File(tmpdir, "target.txt.tmp"); createFile(target, "before"); assertEquals("before", getContent(target)); boolean exception = false; try { new AtomicFileWritingIdiom(target, new WriterStatement() { @Override public void write(Writer os) throws IOException { os.write("after"); os.flush(); assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); throw new RuntimeException(); } }); } catch (RuntimeException ex) { exception = true; } assertFalse("tmp file should have been deleted", tmp.exists()); assertTrue("should have raised an exception", exception); // content preserved assertEquals("before", getContent(target)); target.delete(); }
@Test public void testWriterFailureIOException() throws IOException { File target = new File(tmpdir, "target.txt"); final File tmp = new File(tmpdir, "target.txt.tmp"); createFile(target, "before"); assertEquals("before", getContent(target)); boolean exception = false; try { new AtomicFileWritingIdiom(target, new WriterStatement() { @Override public void write(Writer os) throws IOException { os.write("after"); os.flush(); assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); throw new IOException(); } }); } catch (IOException ex) { exception = true; } assertFalse("tmp file should have been deleted", tmp.exists()); assertTrue("should have raised an exception", exception); // content preserved assertEquals("before", getContent(target)); target.delete(); }
@Test public void testWriterFailureError() throws IOException { File target = new File(tmpdir, "target.txt"); final File tmp = new File(tmpdir, "target.txt.tmp"); createFile(target, "before"); assertEquals("before", getContent(target)); boolean exception = false; try { new AtomicFileWritingIdiom(target, new WriterStatement() { @Override public void write(Writer os) throws IOException { os.write("after"); os.flush(); assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); throw new Error(); } }); } catch (Error ex) { exception = true; } assertFalse("tmp file should have been deleted", tmp.exists()); assertTrue("should have raised an exception", exception); // content preserved assertEquals("before", getContent(target)); target.delete(); }