1 package org.simantics.district.maps.server;
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.net.URISyntaxException;
6 import java.nio.charset.StandardCharsets;
7 import java.nio.file.Path;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.List;
11 import java.util.Optional;
12 import java.util.concurrent.TimeoutException;
14 import org.simantics.district.maps.server.utils.EnvUtil;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 import org.zeroturnaround.exec.InvalidExitValueException;
18 import org.zeroturnaround.exec.ProcessExecutor;
19 import org.zeroturnaround.exec.stream.slf4j.Slf4jInfoOutputStream;
23 public static final String NODEJS_VERSION = "v4.8.0";
25 private static final Logger LOGGER = LoggerFactory.getLogger(NodeJS.class);
26 private static final String NODE = "node";
28 private static Path nodeJSFolder() throws IOException, URISyntaxException {
29 StringBuilder sb = new StringBuilder();
30 sb.append(NODE).append("-").append(NODEJS_VERSION).append("-").append(getOs()).append("-").append(getArch());
31 return Activator.getNodeJSRoot().resolve(sb.toString());
34 public static Path executable() throws IOException, URISyntaxException {
35 String nodeJSexecutable = NODE;
36 if (EnvUtil.calculateOS() == "win")
37 nodeJSexecutable = nodeJSexecutable + ".exe";
38 return nodeJSFolder().resolve(nodeJSexecutable).toAbsolutePath().normalize();
41 private static Path npmExecutable() throws IOException, URISyntaxException {
42 String npmExecutable = "npm";
43 if (EnvUtil.calculateOS() == "win")
44 npmExecutable = npmExecutable + ".cmd";
45 return nodeJSFolder().resolve(npmExecutable).toAbsolutePath().normalize();
48 private static String getArch() {
49 return EnvUtil.calculateArch();
52 private static String getOs() {
53 return EnvUtil.calculateOS();
56 public static int npm(OutputStream output, String... args) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException, URISyntaxException {
57 LOGGER.info("Executing npm with args {}", Arrays.toString(args));
58 List<String> actualArgs = new ArrayList<>();
59 actualArgs.add(npmExecutable().toString());
60 actualArgs.addAll(Arrays.asList(args));
61 ProcessExecutor exec = new ProcessExecutor().command(actualArgs).redirectOutput(new Slf4jInfoOutputStream(LOGGER) {
64 protected void processLine(String line) {
65 // Convert to UTF-8 string
66 String utf8Line = new String(line.getBytes(), StandardCharsets.UTF_8);
72 exec.redirectOutputAlsoTo(output);
73 return exec.execute().getExitValue();
77 public static void main(String[] args) throws Exception {
78 TileserverMapnik server = TileserverMapnikInstance.get();
79 server.start(Optional.empty());