From f0e46d3e67ef4142ada2aec2af512e76dfda01e5 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Fri, 3 Oct 2014 18:24:50 +0200 Subject: Use the Java7 java.nio.file interfaces, make output atomic. --- org/madore/damlengine/DamlEngine.java | 192 +++++++++++++++------------ org/madore/damlengine/TodoDamlElement.java | 6 +- org/madore/damlengine/WeblogIndexSelect.java | 4 +- org/madore/damlengine/WeblogSelect.java | 4 +- 4 files changed, 115 insertions(+), 91 deletions(-) (limited to 'org/madore') diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java index f708d81..85ebeed 100644 --- a/org/madore/damlengine/DamlEngine.java +++ b/org/madore/damlengine/DamlEngine.java @@ -5,14 +5,18 @@ import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.Properties; import java.io.InputStream; -import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.PrintStream; -import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.nio.file.NoSuchFileException; import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; import org.w3c.dom.*; @@ -151,44 +155,69 @@ public final class DamlEngine { } public static Properties appProps; - public static String basePath; - public static String templatePath; + public static Path basePath; + public static Path templatePath; + + private static interface Processor { + public void process(OutputStream out) throws Exception; + } + private static void processWithTemp(Processor p, String outf) + throws Exception { + Path tempPath = (outf != null) ? Paths.get(outf+".temp") : null; + OutputStream out = (tempPath != null) + ? Files.newOutputStream(tempPath) + : System.out; + try { + p.process(out); + if ( tempPath != null ) { + out.close(); + Files.move(tempPath, Paths.get(outf), + StandardCopyOption.REPLACE_EXISTING, + StandardCopyOption.ATOMIC_MOVE); + } + } finally { + try { + if (tempPath != null ) + Files.deleteIfExists(tempPath); + } catch ( Exception e ) { } + } + } public static void main(String[] args) throws Exception { appProps = new Properties(); - String appPropsFile = null; + Path appPropsFile = null; if ( System.getenv("DAMLENGINE_PROPERTIES_PATH") != null ) - appPropsFile = System.getenv("DAMLENGINE_PROPERTIES_PATH"); + appPropsFile = Paths.get(System.getenv("DAMLENGINE_PROPERTIES_PATH")); if ( appPropsFile == null ) { if ( System.getProperty("user.home") != null ) - appPropsFile = System.getProperty("user.home") + "/damlengine.properties"; + appPropsFile = Paths.get(System.getProperty("user.home"),"damlengine.properties"); else - appPropsFile = "damlengine.properties"; + appPropsFile = Paths.get("damlengine.properties"); } try { - appProps.load(new FileInputStream(appPropsFile)); - } catch (FileNotFoundException e) { } + appProps.load(Files.newInputStream(appPropsFile)); + } catch (NoSuchFileException e) { } basePath = null; if ( System.getenv("DAMLENGINE_BASE_PATH") != null ) - basePath = System.getenv("DAMLENGINE_BASE_PATH"); - if ( basePath == null ) - basePath = appProps.getProperty("base_path"); + basePath = Paths.get(System.getenv("DAMLENGINE_BASE_PATH")); + if ( basePath == null && appProps.getProperty("base_path") != null ) + basePath = Paths.get(appProps.getProperty("base_path")); if ( basePath == null ) { - basePath = "."; + basePath = Paths.get("."); System.err.println("warning: using working directory as base path"); } templatePath = null; if ( System.getenv("DAMLENGINE_TEMPLATE_PATH") != null ) - templatePath = System.getenv("DAMLENGINE_TEMPLATE_PATH"); - if ( templatePath == null ) - templatePath = appProps.getProperty("template_path"); + templatePath = Paths.get(System.getenv("DAMLENGINE_TEMPLATE_PATH")); + if ( templatePath == null && appProps.getProperty("template_path") != null ) + templatePath = Paths.get(appProps.getProperty("template_path")); if ( templatePath == null ) { - templatePath = basePath + "/templates"; - System.err.println("warning: using "+templatePath+" as template path"); + templatePath = basePath.resolve("templates"); + System.err.println("warning: using "+templatePath.toString()+" as template path"); } BufferedReader buf = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); @@ -200,82 +229,77 @@ public final class DamlEngine { continue; System.err.println(line); if ( (matcher=Pattern.compile("process\\s+(\\S+)(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { - String inf = matcher.group(1); - String outf = matcher.group(2); - InputStream in = new FileInputStream((inf.charAt(0)=='/'?"":basePath+"/")+inf); - OutputStream out = (outf != null) - ? new FileOutputStream(outf) - : System.out; - fullProcess(in, out); - if ( out != System.out ) - out.close(); + final String inf = matcher.group(1); + final String outf = matcher.group(2); + final InputStream in = Files.newInputStream(basePath.resolve(inf)); + processWithTemp(new Processor() { + public void process(OutputStream out) throws Exception { + fullProcess(in, out); + } + }, outf); } else if ( (matcher=Pattern.compile("process-weblog-month\\s+(\\d{4})\\-(\\d{2})(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { - String year = matcher.group(1); - String month = matcher.group(2); - String outf = matcher.group(3); - OutputStream out = (outf != null) - ? new FileOutputStream(outf) - : System.out; - WeblogSelect.fullProcess(new Context.WeblogMonthSelectionContext(year,month), - out); - if ( out != System.out ) - out.close(); + final String year = matcher.group(1); + final String month = matcher.group(2); + final String outf = matcher.group(3); + processWithTemp(new Processor() { + public void process(OutputStream out) throws Exception { + WeblogSelect.fullProcess(new Context.WeblogMonthSelectionContext(year,month), + out); + } + }, outf); } else if ( (matcher=Pattern.compile("process-weblog-cat\\s+([a-z0-9\\-]+)(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { - String code = matcher.group(1); - String outf = matcher.group(2); - OutputStream out = (outf != null) - ? new FileOutputStream(outf) - : System.out; - WeblogSelect.fullProcess(new Context.WeblogCategorySelectionContext(code), - out); - if ( out != System.out ) - out.close(); + final String code = matcher.group(1); + final String outf = matcher.group(2); + processWithTemp(new Processor() { + public void process(OutputStream out) throws Exception { + WeblogSelect.fullProcess(new Context.WeblogCategorySelectionContext(code), + out); + } + }, outf); } else if ( (matcher=Pattern.compile("process-weblog-recent\\s+(\\d+)(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { - int count = Integer.parseInt(matcher.group(1)); - String outf = matcher.group(2); - OutputStream out = (outf != null) - ? new FileOutputStream(outf) - : System.out; - WeblogSelect.fullProcess(new Context.WeblogRecentSelectionContext(count), - out); + final int count = Integer.parseInt(matcher.group(1)); + final String outf = matcher.group(2); + processWithTemp(new Processor() { + public void process(OutputStream out) throws Exception { + WeblogSelect.fullProcess(new Context.WeblogRecentSelectionContext(count), + out); + } + }, outf); } else if ( (matcher=Pattern.compile("process-weblog-single\\s+(\\d+)(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { - int number = Integer.parseInt(matcher.group(1)); - String outf = matcher.group(2); - OutputStream out = (outf != null) - ? new FileOutputStream(outf) - : System.out; - WeblogSelect.fullProcess(new Context.WeblogSingleSelectionContext(number), - out); - if ( out != System.out ) - out.close(); + final int number = Integer.parseInt(matcher.group(1)); + final String outf = matcher.group(2); + processWithTemp(new Processor() { + public void process(OutputStream out) throws Exception { + WeblogSelect.fullProcess(new Context.WeblogSingleSelectionContext(number), + out); + } + }, outf); } else if ( (matcher=Pattern.compile("process-weblog-index(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { - String outf = matcher.group(1); - OutputStream out = (outf != null) - ? new FileOutputStream(outf) - : System.out; - WeblogIndexSelect.fullProcess(out); - if ( out != System.out ) - out.close(); + final String outf = matcher.group(1); + processWithTemp(new Processor() { + public void process(OutputStream out) throws Exception { + WeblogIndexSelect.fullProcess(out); + } + }, outf); } else if ( (matcher=Pattern.compile("process-weblog-rss(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { - String outf = matcher.group(1); - OutputStream out = (outf != null) - ? new FileOutputStream(outf) - : System.out; - WeblogRSS.fullProcess(out); - if ( out != System.out ) - out.close(); + final String outf = matcher.group(1); + processWithTemp(new Processor() { + public void process(OutputStream out) throws Exception { + WeblogRSS.fullProcess(out); + } + }, outf); } else if ( (matcher=Pattern.compile("populate-weblog\\s+(\\S+)\\s*").matcher(line)).matches() ) { - String inf = matcher.group(1); - InputStream in = new FileInputStream((inf.charAt(0)=='/'?"":basePath+"/")+inf); + final String inf = matcher.group(1); + final InputStream in = Files.newInputStream(basePath.resolve(inf)); WeblogPopulate.populate(in); } else if ( (matcher=Pattern.compile("echo\\s+(\\S+)(?:\\s+\\>\\s*(\\S+))?\\s*").matcher(line)).matches() ) { - String str = matcher.group(1); - String outf = matcher.group(2); - OutputStream out = (outf != null) - ? new FileOutputStream(outf) + final String str = matcher.group(1); + final String outf = matcher.group(2); + final OutputStream out = (outf != null) + ? Files.newOutputStream(Paths.get(outf)) : System.out; new PrintStream(out, true).println(str); - if ( out != System.out ) + if ( outf != null ) out.close(); } else { throw new IllegalArgumentException("couldn't understand command"); diff --git a/org/madore/damlengine/TodoDamlElement.java b/org/madore/damlengine/TodoDamlElement.java index 9de2714..6949086 100644 --- a/org/madore/damlengine/TodoDamlElement.java +++ b/org/madore/damlengine/TodoDamlElement.java @@ -2,11 +2,11 @@ package org.madore.damlengine; import java.util.ArrayList; import java.util.regex.Pattern; -import java.io.FileInputStream; import java.io.Reader; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; +import java.nio.file.Files; import org.w3c.dom.*; public final class TodoDamlElement extends TodoDefaultElement { @@ -62,7 +62,7 @@ public final class TodoDamlElement extends TodoDefaultElement { ctx.gc.styleContent = new StringBuffer(); try { - Reader rd = new BufferedReader(new InputStreamReader(new FileInputStream(DamlEngine.templatePath+"/included.css"), "UTF-8")); + Reader rd = new BufferedReader(new InputStreamReader(Files.newInputStream(DamlEngine.templatePath.resolve("included.css")), "UTF-8")); int ch; while ((ch = rd.read()) > -1) ctx.gc.styleContent.append((char)ch); @@ -72,7 +72,7 @@ public final class TodoDamlElement extends TodoDefaultElement { } ctx.gc.scriptContent = new StringBuffer(); try { - Reader rd = new BufferedReader(new InputStreamReader(new FileInputStream(DamlEngine.templatePath+"/included.js"), "UTF-8")); + Reader rd = new BufferedReader(new InputStreamReader(Files.newInputStream(DamlEngine.templatePath.resolve("included.js")), "UTF-8")); int ch; while ((ch = rd.read()) > -1) ctx.gc.scriptContent.append((char)ch); diff --git a/org/madore/damlengine/WeblogIndexSelect.java b/org/madore/damlengine/WeblogIndexSelect.java index 0361177..8f8da29 100644 --- a/org/madore/damlengine/WeblogIndexSelect.java +++ b/org/madore/damlengine/WeblogIndexSelect.java @@ -1,7 +1,7 @@ package org.madore.damlengine; -import java.io.FileInputStream; import java.io.OutputStream; +import java.nio.file.Files; public final class WeblogIndexSelect { @@ -12,7 +12,7 @@ public final class WeblogIndexSelect { public static void fullProcess(OutputStream out) throws Exception { - DamlEngine.fullProcess(new FileInputStream(DamlEngine.templatePath+"/weblog-index-template.daml"), + DamlEngine.fullProcess(Files.newInputStream(DamlEngine.templatePath.resolve("weblog-index-template.daml")), out, null); } diff --git a/org/madore/damlengine/WeblogSelect.java b/org/madore/damlengine/WeblogSelect.java index 075cab1..a15a672 100644 --- a/org/madore/damlengine/WeblogSelect.java +++ b/org/madore/damlengine/WeblogSelect.java @@ -2,8 +2,8 @@ package org.madore.damlengine; import java.util.TreeSet; import java.util.ArrayList; -import java.io.FileInputStream; import java.io.OutputStream; +import java.nio.file.Files; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -51,7 +51,7 @@ public final class WeblogSelect { wsc.xmlData.add(content); } - DamlEngine.fullProcess(new FileInputStream(DamlEngine.templatePath+"/"+templateResourceName), + DamlEngine.fullProcess(Files.newInputStream(DamlEngine.templatePath.resolve(templateResourceName)), out, wsc); } -- cgit v1.2.3