]> WPIA git - gigi.git/blob - src/org/cacert/gigi/DevelLauncher.java
[EMPTY] Formatting with configured formatter.
[gigi.git] / src / org / cacert / gigi / DevelLauncher.java
1 package org.cacert.gigi;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.DataOutputStream;
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import java.nio.file.Files;
12 import java.nio.file.Paths;
13 import java.util.Properties;
14
15 import org.kamranzafar.jtar.TarEntry;
16 import org.kamranzafar.jtar.TarHeader;
17 import org.kamranzafar.jtar.TarOutputStream;
18
19 public class DevelLauncher {
20         public static void main(String[] args) throws Exception {
21                 Properties mainProps = new Properties();
22                 mainProps.load(new FileInputStream("config/gigi.properties"));
23                 for (int i = 0; i < args.length; i++) {
24                         if (args[i].equals("--port")) {
25                                 mainProps.setProperty("port", args[i + 1]);
26                         }
27                         i++;
28                 }
29
30                 ByteArrayOutputStream chunkConfig = new ByteArrayOutputStream();
31                 DataOutputStream dos = new DataOutputStream(chunkConfig);
32                 byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks"));
33                 byte[] keystore = Files.readAllBytes(Paths.get("config/keystore.pkcs12"));
34
35                 DevelLauncher.writeGigiConfig(dos, "changeit".getBytes(), "changeit".getBytes(), mainProps, cacerts, keystore);
36                 dos.flush();
37                 InputStream oldin = System.in;
38                 System.setIn(new ByteArrayInputStream(chunkConfig.toByteArray()));
39                 Launcher.main(args);
40                 System.setIn(oldin);
41         }
42
43         public static void writeGigiConfig(OutputStream target, byte[] keystorepw, byte[] truststorepw,
44                 Properties mainprop, byte[] cacerts, byte[] keystore) throws IOException {
45                 TarOutputStream tos = new TarOutputStream(target);
46                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
47                 mainprop.store(baos, "");
48
49                 putTarEntry(baos.toByteArray(), tos, "gigi.properties");
50                 putTarEntry(keystorepw, tos, "keystorepw");
51                 putTarEntry(truststorepw, tos, "truststorepw");
52                 putTarEntry(keystore, tos, "keystore.pkcs12");
53                 putTarEntry(cacerts, tos, "cacerts.jks");
54                 tos.close();
55
56         }
57
58         private static void putTarEntry(byte[] data, TarOutputStream tos, String name) throws IOException {
59                 TarHeader th = new TarHeader();
60                 th.name = new StringBuffer(name);
61                 th.size = data.length;
62                 tos.putNextEntry(new TarEntry(th));
63                 tos.write(data);
64         }
65
66         public static void writeChunk(DataOutputStream dos, byte[] chunk) throws IOException {
67                 dos.writeInt(chunk.length);
68                 dos.write(chunk);
69         }
70
71         public static void launch(Properties props, File cacerts, File keystore) throws IOException {
72                 ByteArrayOutputStream config = new ByteArrayOutputStream();
73                 props.store(config, "");
74         }
75 }