Skip to content

Commit

Permalink
Fix redirect to app_script.log file after update Jython to 2.7.1 #75
Browse files Browse the repository at this point in the history
  • Loading branch information
yurem committed Apr 11, 2018
1 parent a6cdac4 commit 4cbf287
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions oxService/src/main/java/org/xdi/service/PythonService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand Down Expand Up @@ -44,6 +46,18 @@ public class PythonService implements Serializable {
private PythonInterpreter pythonInterpreter;
private boolean interpereterReady;

private OutputStreamWriter logOut, logErr;

@PostConstruct
public void init() {
try {
this.logOut = new OutputStreamWriter(new PythonLoggerOutputStream(log, false), "UTF-8");
this.logErr = new OutputStreamWriter(new PythonLoggerOutputStream(log, true), "UTF-8");
} catch (UnsupportedEncodingException ex) {
log.error("Failed to initialize Jython out/err loggers", ex);
}
}

/*
* Initialize singleton instance during startup
*/
Expand All @@ -54,11 +68,9 @@ public boolean initPythonInterpreter(String pythonModulesDir) {
try {
PythonInterpreter.initialize(getPreProperties(), getPostProperties(pythonModulesDir), null);
this.pythonInterpreter = new PythonInterpreter();

// Init output redirect for all new interpreters
this.pythonInterpreter.setOut(new OutputStreamWriter(new PythonLoggerOutputStream(log, false), "UTF-8"));
this.pythonInterpreter.setErr(new OutputStreamWriter(new PythonLoggerOutputStream(log, true), "UTF-8"));


initPythonInterpreter(this.pythonInterpreter);

result = true;
} catch (PyException ex) {
log.error("Failed to initialize PythonInterpreter correctly", ex);
Expand All @@ -72,6 +84,17 @@ public boolean initPythonInterpreter(String pythonModulesDir) {
return result;
}

private void initPythonInterpreter(PythonInterpreter interpreter) {
// Init output redirect interpreter
if (this.logOut != null) {
interpreter.setOut(this.logOut);
}

if (this.logErr != null) {
interpreter.setErr(this.logErr);
}
}

/**
* When application undeploy we need clean up pythonInterpreter
*/
Expand Down Expand Up @@ -132,6 +155,8 @@ public <T> T loadPythonScript(String scriptName, String scriptPythonType, Class<
}

PythonInterpreter currentPythonInterpreter = PythonInterpreter.threadLocalStateInterpreter(null);
initPythonInterpreter(currentPythonInterpreter);

try {
currentPythonInterpreter.execfile(scriptName);
} catch (Exception ex) {
Expand All @@ -148,7 +173,9 @@ public <T> T loadPythonScript(InputStream scriptFile, String scriptName, String
}

PythonInterpreter currentPythonInterpreter = PythonInterpreter.threadLocalStateInterpreter(null);
try {
initPythonInterpreter(currentPythonInterpreter);

try {
currentPythonInterpreter.execfile(scriptFile, scriptName);
} catch (Exception ex) {
log.error("Failed to load python file", ex.getMessage(), ex);
Expand Down

0 comments on commit 4cbf287

Please sign in to comment.