blob: fc35b15a0fab1f7b6073e60a8c94365e6bfc590a [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.jython;
2
3import java.net.URL;
4import java.util.HashMap;
5import java.util.Map;
6
7import org.python.util.PythonInterpreter;
8import org.slf4j.Logger;
9import org.slf4j.LoggerFactory;
10
11/**
12 * This class starts a thread that runs a jython interpreter that
13 * can be used for debug (or even development).
14 *
15 * @author mandeepdhami
16 *
17 */
18public class JythonServer extends Thread {
19 protected static Logger log = LoggerFactory.getLogger(JythonServer.class);
20
21 int port;
22 Map<String, Object> locals;
23
24 /**
25 * @param port_ Port to use for jython server
26 * @param locals_ Locals to add to the interpreters top level name space
27 */
28 public JythonServer(int port_, Map<String, Object> locals_) {
29 this.port = port_ ;
30 this.locals = locals_;
31 if (this.locals == null) {
32 this.locals = new HashMap<String, Object>();
33 }
34 this.locals.put("log", JythonServer.log);
35 this.setName("debugserver");
36 }
37
38 /**
39 * The main thread for this class invoked by Thread.run()
40 *
41 * @see java.lang.Thread#run()
42 */
43 public void run() {
44 PythonInterpreter p = new PythonInterpreter();
45 for (String name : this.locals.keySet()) {
46 p.set(name, this.locals.get(name));
47 }
48
49 URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
50 String jarPath = jarUrl.getPath();
51 if (jarUrl.getProtocol().equals("file")) {
52 // If URL is of type file, assume that we are in dev env and set path to python dir.
53 // else use the jar file as is
54 jarPath = jarPath + "../../src/main/python/";
55 }
56
57 p.exec("import sys");
58 p.exec("sys.path.append('" + jarPath + "')");
59 p.exec("from debugserver import run_server");
60 p.exec("run_server(" + this.port + ", '0.0.0.0', locals())");
61 }
62
63}