Skip to content

Instantly share code, notes, and snippets.

@Vanuan
Forked from vkravets/JythonNailgun.java
Created October 26, 2012 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vanuan/3960023 to your computer and use it in GitHub Desktop.
Save Vanuan/3960023 to your computer and use it in GitHub Desktop.
Running jython via Nailgun server
package com.github.vanuan.jython.nailgun;
import java.util.Arrays;
import org.python.core.Py;
import org.python.core.PyList;
import org.python.core.PySystemState;
import org.python.core.PyType;
import org.python.util.PythonInterpreter;
import com.martiansoftware.nailgun.NGContext;
public class Wrapper {
private static PythonInterpreter interpreter = null;
public static void nailMain (NGContext context) {
if (interpreter == null) {
interpreter = new PythonInterpreter();
} else {
Py.setSystemState(new PySystemState());
}
PySystemState systemState = interpreter.getSystemState();
systemState.setCurrentWorkingDir(context.getWorkingDirectory());
interpreter.setErr(context.err);
interpreter.setIn(context.in);
interpreter.setOut(context.out);
systemState.argv = new PyList(PyType.fromClass(String.class), Arrays.asList(context.getArgs()));
String pythonPath = System.getProperties().getProperty("python.path");
if (pythonPath != null) {
String[] tokens = pythonPath.split(context.getPathSeparator());
systemState.path = new PyList(PyType.fromClass(String.class), Arrays.asList(tokens));
}
interpreter.execfile(context.getArgs()[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment