aboutsummaryrefslogtreecommitdiffstats
path: root/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/JythonRuntime.java
blob: c5810a9aa7af2902ae7bdcbe71c5071556464eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.openecomp.sdc.toscaparser;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Properties;

import org.openecomp.sdc.toscaparser.utils.JarExtractor;
import org.python.util.PythonInterpreter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JythonRuntime {

    private static final Logger LOGGER = LoggerFactory.getLogger(JythonRuntime.class);
    private final JarExtractor jarExtractor;
    private Path homePath;

    public JythonRuntime(JarExtractor jarExtractor) {
        this.jarExtractor = jarExtractor;
    }

    public void initialize() throws IOException {
        tryExtractPyhtonPackages();
        initRuntime();
    }

    private void initRuntime() {
        Properties systemProperties = System.getProperties();
        Properties properties = getPythonProperties();
        PythonInterpreter.initialize(systemProperties, properties, new String[0]);
    }

    private void tryExtractPyhtonPackages() throws IOException {
        homePath = jarExtractor.extractPyhtonPackages();
    }

    private Properties getPythonProperties() {
        Properties properties = new Properties();
        if (homePath != null) {
            LOGGER.debug("getPythonProperties - Setting python.home to {}", homePath);
            properties.put("python.home", homePath.toString());
        }
        // Used to prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
        properties.put("python.console.encoding", "UTF-8");
        return properties;
    }

    public void terminate() throws IOException {
        if (homePath != null) {
            jarExtractor.deleteDirectory(homePath);
        }
    }
}