001: package pygmy.handlers.jython;
002:
003: import pygmy.core.*;
004:
005: import java.io.IOException;
006: import java.io.Writer;
007: import java.util.logging.Logger;
008: import java.util.logging.Level;
009: import java.util.Properties;
010: import java.net.HttpURLConnection;
011:
012: import org.python.util.PythonInterpreter;
013: import org.python.core.PyException;
014: import org.python.core.PySystemState;
015:
016: public class JythonHandler extends AbstractHandler {
017:
018: private static final Logger log = Logger
019: .getLogger(JythonHandler.class.getName());
020:
021: private static final ConfigOption SCRIPT_DIRECTORY_OPTION = new ConfigOption(
022: "script-dir", true,
023: "Location of the scripts you want to run.");
024:
025: String pythonDir;
026: PythonInterpreter interpreter;
027: private static final ConfigOption PYTHON_HOME = new ConfigOption(
028: "python.home", true, "Home of the jython interpreter.");
029: private static final ConfigOption PYTHON_PATH = new ConfigOption(
030: "python.path", true,
031: "Path used to resolve jython libaries.");
032:
033: public boolean initialize(String handlerName, Server server) {
034: super .initialize(handlerName, server);
035: pythonDir = SCRIPT_DIRECTORY_OPTION.getProperty(server,
036: handlerName);
037:
038: Properties props = new Properties();
039:
040: putPythonProperty(PYTHON_HOME, props);
041: putPythonProperty(PYTHON_PATH, props);
042: PythonInterpreter.initialize(System.getProperties(), props,
043: new String[0]);
044:
045: interpreter = new PythonInterpreter(null, new PySystemState());
046: interpreter.setErr(new LogWriter(Level.SEVERE));
047: interpreter.setOut(new LogWriter(Level.INFO));
048: // PySystemState sys = Py.getSystemState();
049: // sys.path.append(new PyString(rootPath));
050: return true;
051: }
052:
053: private void putPythonProperty(ConfigOption option, Properties props) {
054: if (System.getProperty(option.getName()) == null) {
055: String pythonHome = option.getProperty(server, handlerName);
056: if (pythonHome != null)
057: props.put(option.getName(), pythonHome);
058: }
059: }
060:
061: protected boolean handleBody(HttpRequest request,
062: HttpResponse response) throws IOException {
063: if (request.getUrl().endsWith(".py")) {
064: try {
065: if (log.isLoggable(Level.INFO)) {
066: log.log(Level.INFO, "Executing script: "
067: + request.getUrl());
068: }
069: interpreter.set("request", request);
070: interpreter.set("response", response);
071: interpreter.execfile(Http.translatePath(pythonDir,
072: request.getUrl()).getAbsolutePath());
073: } catch (PyException e) {
074: log.log(Level.SEVERE, e.getMessage(), e);
075: response.sendError(
076: HttpURLConnection.HTTP_INTERNAL_ERROR,
077: "Script error", e);
078: }
079: return true;
080: }
081: return false;
082: }
083:
084: public class LogWriter extends Writer {
085: Level level;
086: StringBuffer buf = new StringBuffer();
087:
088: public LogWriter(Level level) {
089: this .level = level;
090: }
091:
092: public synchronized void write(char cbuf[], int off, int len)
093: throws IOException {
094: buf.append(cbuf, off, len);
095: }
096:
097: public synchronized void flush() throws IOException {
098: log.log(level, buf.toString());
099: buf.delete(0, buf.length() - 1);
100: }
101:
102: public void close() throws IOException {
103: flush();
104: }
105: }
106: }
|