]> WPIA git - gigi.git/commitdiff
Implement template hot-redeploy :-).
authorFelix Dörre <felix@dogcraft.de>
Thu, 10 Jul 2014 18:31:52 +0000 (20:31 +0200)
committerFelix Dörre <felix@dogcraft.de>
Thu, 10 Jul 2014 22:35:16 +0000 (00:35 +0200)
src/org/cacert/gigi/DevelLauncher.java
src/org/cacert/gigi/output/Template.java

index 6fa8a29a8cada7de1c5d1f27326168b34bddc8b4..254b1e72f05d1e37b3816d0a9154b452874d9468 100644 (file)
@@ -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"));
index c7e90674c0370d4e46f0c78f92173630ea48d238..0b0fe3ba4067a7e0de5efbb63243c633d3ae774a 100644 (file)
@@ -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<String, Object> 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) {