]> WPIA git - gigi.git/blob - util-testing/org/cacert/gigi/DevelLauncher.java
Fix: clean up launching process.
[gigi.git] / util-testing / org / cacert / gigi / DevelLauncher.java
1 package org.cacert.gigi;
2
3 import java.io.BufferedReader;
4 import java.io.ByteArrayInputStream;
5 import java.io.ByteArrayOutputStream;
6 import java.io.DataOutputStream;
7 import java.io.FileInputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.io.OutputStream;
12 import java.lang.reflect.Field;
13 import java.net.URL;
14 import java.nio.file.Files;
15 import java.nio.file.Paths;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Properties;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.cacert.gigi.dbObjects.ObjectCache;
25 import org.cacert.gigi.pages.Page;
26 import org.kamranzafar.jtar.TarEntry;
27 import org.kamranzafar.jtar.TarHeader;
28 import org.kamranzafar.jtar.TarOutputStream;
29
30 public class DevelLauncher {
31
32     public static void main(String[] args) throws Exception {
33         Properties mainProps = new Properties();
34         try (FileInputStream inStream = new FileInputStream("config/gigi.properties")) {
35             mainProps.load(inStream);
36         }
37         for (int i = 0; i < args.length; i++) {
38             if (args[i].equals("--port")) {
39                 mainProps.setProperty("port", args[i + 1]);
40             }
41             i++;
42         }
43         killPreviousInstance(mainProps);
44
45         ByteArrayOutputStream chunkConfig = new ByteArrayOutputStream();
46         DataOutputStream dos = new DataOutputStream(chunkConfig);
47         byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks"));
48         byte[] keystore = Files.readAllBytes(Paths.get("config/keystore.pkcs12"));
49
50         DevelLauncher.writeGigiConfig(dos, "changeit".getBytes("UTF-8"), "changeit".getBytes("UTF-8"), mainProps, cacerts, keystore);
51         dos.flush();
52         InputStream oldin = System.in;
53         System.setIn(new ByteArrayInputStream(chunkConfig.toByteArray()));
54         new Launcher().boot();
55         addDevelPage();
56         System.setIn(oldin);
57         BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
58         System.out.println("Cacert-gigi system sucessfully started.");
59         System.out.println("Press enter to shutdown.");
60         br.readLine();
61         System.exit(0);
62     }
63
64     private static void killPreviousInstance(Properties mainProps) {
65         try {
66             String targetPort = mainProps.getProperty("http.port");
67             String targetHost = mainProps.getProperty("name.www");
68             URL u = new URL("http://" + targetHost + ":" + targetPort + "/kill");
69             u.openStream();
70         } catch (IOException e) {
71         }
72     }
73
74     public static void addDevelPage() {
75         try {
76             Field instF = Gigi.class.getDeclaredField("instance");
77             Field pageF = Gigi.class.getDeclaredField("pages");
78             instF.setAccessible(true);
79             pageF.setAccessible(true);
80             Object gigi = instF.get(null);
81
82             // Check if we got a proper map (as much as we can tell)
83             Object pagesObj = pageF.get(gigi);
84             @SuppressWarnings("unchecked")
85             HashMap<String, Page> pages = pagesObj instanceof Map ? new HashMap<>((Map<String, Page>) pagesObj) : null;
86
87             pages.put("/manage", new Page("Page-manager") {
88
89                 @Override
90                 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
91                     ObjectCache.clearAllCaches();
92                     resp.getWriter().println("All caches cleared.");
93                     System.out.println("Caches cleared.");
94
95                 }
96
97                 @Override
98                 public boolean needsLogin() {
99                     return false;
100                 }
101
102             });
103
104             pages.put("/kill", new Page("Kill") {
105
106                 /**
107                  * The contained call to {@link System#exit(int)} is mainly
108                  * needed to kill this instance immediately if another
109                  * {@link DevelLauncher} is booting up to free all ports This is
110                  * required for fast development cycles.
111                  * 
112                  * @see #killPreviousInstance(Properties)
113                  */
114                 @Override
115                 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
116                     System.exit(0);
117                 }
118
119                 @Override
120                 public boolean needsLogin() {
121                     return false;
122                 }
123             });
124
125             pageF.set(gigi, Collections.unmodifiableMap(pages));
126         } catch (ReflectiveOperationException e) {
127             e.printStackTrace();
128         }
129     }
130
131     public static void writeGigiConfig(OutputStream target, byte[] keystorepw, byte[] truststorepw, Properties mainprop, byte[] cacerts, byte[] keystore) throws IOException {
132         TarOutputStream tos = new TarOutputStream(target);
133         ByteArrayOutputStream baos = new ByteArrayOutputStream();
134         mainprop.store(baos, "");
135
136         putTarEntry(baos.toByteArray(), tos, "gigi.properties");
137         putTarEntry(keystorepw, tos, "keystorepw");
138         putTarEntry(truststorepw, tos, "truststorepw");
139         putTarEntry(keystore, tos, "keystore.pkcs12");
140         putTarEntry(cacerts, tos, "cacerts.jks");
141         tos.close();
142
143     }
144
145     private static void putTarEntry(byte[] data, TarOutputStream tos, String name) throws IOException {
146         TarHeader th = new TarHeader();
147         th.name = new StringBuffer(name);
148         th.size = data.length;
149         tos.putNextEntry(new TarEntry(th));
150         tos.write(data);
151     }
152
153 }