]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/src/org/simantics/district/maps/server/NodeJS.java
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / src / org / simantics / district / maps / server / NodeJS.java
1 package org.simantics.district.maps.server;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.net.URISyntaxException;
6 import java.nio.file.Path;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.List;
10 import java.util.concurrent.TimeoutException;
11
12 import org.simantics.district.maps.server.utils.EnvUtil;
13 import org.zeroturnaround.exec.InvalidExitValueException;
14 import org.zeroturnaround.exec.ProcessExecutor;
15 import org.zeroturnaround.exec.stream.slf4j.Slf4jStream;
16
17 public class NodeJS {
18
19     public static final String NODEJS_VERSION = "v4.8.0";
20     
21     private static final String NODE = "node";
22     
23     private static Path nodeJSFolder() throws IOException, URISyntaxException {
24         StringBuilder sb = new StringBuilder();
25         sb.append(NODE).append("-").append(NODEJS_VERSION).append("-").append(getOs()).append("-").append(getArch());
26         return Activator.getNodeJSRoot().resolve(sb.toString());
27     }
28     
29     public static Path executable() throws IOException, URISyntaxException {
30         String nodeJSexecutable = NODE;
31         if (EnvUtil.calculateOS() == "win")
32             nodeJSexecutable = nodeJSexecutable + ".exe";
33         return nodeJSFolder().resolve(nodeJSexecutable).toAbsolutePath().normalize();
34     }
35     
36     private static Path npmExecutable() throws IOException, URISyntaxException {
37         String npmExecutable = "npm";
38         if (EnvUtil.calculateOS() == "win")
39             npmExecutable = npmExecutable + ".cmd";
40         return nodeJSFolder().resolve(npmExecutable).toAbsolutePath().normalize();
41     }
42     
43     private static String getArch() {
44         return EnvUtil.calculateArch();
45     }
46
47     private static String getOs() {
48         return EnvUtil.calculateOS();
49     }
50
51     public static int npm(OutputStream output, String... args) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException, URISyntaxException {
52         List<String> actualArgs = new ArrayList<>();
53         actualArgs.add(npmExecutable().toString());
54         actualArgs.addAll(Arrays.asList(args));
55         ProcessExecutor exec = new ProcessExecutor().command(actualArgs).redirectOutput(Slf4jStream.ofCaller().asInfo()).destroyOnExit();
56         if (output != null)
57             exec.redirectOutputAlsoTo(output);
58         return exec.execute().getExitValue();
59     }
60     
61
62     public static void main(String[] args) throws Exception {
63         TileserverMapnik server = TileserverMapnikInstance.get();
64         server.start();
65     }
66 }