From 862eaa5913c657d2760337635de517471f0424cf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Thu, 10 Jul 2014 20:31:52 +0200 Subject: [PATCH] Implement template hot-redeploy :-). --- src/org/cacert/gigi/DevelLauncher.java | 2 ++ src/org/cacert/gigi/output/Template.java | 27 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/org/cacert/gigi/DevelLauncher.java b/src/org/cacert/gigi/DevelLauncher.java index 6fa8a29a..254b1e72 100644 --- a/src/org/cacert/gigi/DevelLauncher.java +++ b/src/org/cacert/gigi/DevelLauncher.java @@ -17,6 +17,8 @@ import org.kamranzafar.jtar.TarHeader; import org.kamranzafar.jtar.TarOutputStream; public class DevelLauncher { + public static final boolean DEVEL = true; + public static void main(String[] args) throws Exception { Properties mainProps = new Properties(); mainProps.load(new FileInputStream("config/gigi.properties")); diff --git a/src/org/cacert/gigi/output/Template.java b/src/org/cacert/gigi/output/Template.java index c7e90674..0b0fe3ba 100644 --- a/src/org/cacert/gigi/output/Template.java +++ b/src/org/cacert/gigi/output/Template.java @@ -1,26 +1,38 @@ package org.cacert.gigi.output; import java.io.EOFException; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.Reader; +import java.net.URISyntaxException; import java.net.URL; import java.util.LinkedList; import java.util.Map; +import org.cacert.gigi.DevelLauncher; import org.cacert.gigi.Language; public class Template implements Outputable { String[] contents; Outputable[] vars; - URL source; + long lastLoaded; + File source; public Template(URL u) { try { - source = u; Reader r = new InputStreamReader(u.openStream(), "UTF-8"); + try { + if (u.getProtocol().equals("file") && DevelLauncher.DEVEL) { + source = new File(u.toURI()); + lastLoaded = source.lastModified() + 1000; + } + } catch (URISyntaxException e) { + e.printStackTrace(); + } parse(r); } catch (IOException e) { throw new Error(e); @@ -122,6 +134,17 @@ public class Template implements Outputable { } public void output(PrintWriter out, Language l, Map vars) { + if (source != null && DevelLauncher.DEVEL) { + if (lastLoaded < source.lastModified()) { + try { + System.out.println("Reloading template.... " + source); + parse(new InputStreamReader(new FileInputStream(source), "UTF-8")); + lastLoaded = source.lastModified() + 1000; + } catch (IOException e) { + e.printStackTrace(); + } + } + } for (int i = 0; i < contents.length; i++) { out.print(contents[i]); if (i < this.vars.length) { -- 2.39.2