Package util :: Module jvm
[hide private]
[frames] | no frames]

Source Code for Module util.jvm

 1  ''' 
 2  Created on 22 Jun 2012 
 3   
 4  @author: Eleftherios Avramidis 
 5  ''' 
 6  import subprocess 
 7  import time 
 8  import os 
 9   
10 -def get_libs():
11 """ 12 Return libs directory and files that need to be included in the java classpath 13 @return: file patter and directories to be included in the java classpath 14 @rtype: list(str) 15 """ 16 path = os.path.abspath(__file__) 17 components = path.split(os.sep) 18 rootpath = os.path.join(os.sep, *components[:-3]) 19 libpath = os.path.join(rootpath, "lib") 20 libpath_all = os.path.join(libpath, "*") 21 libs = [libpath, libpath_all] 22 return libs
23
24 -class JVM(object):
25 ''' 26 A wrapper for the Py4J server of Java Virtual Machine, so that java libraries can be accessed via Py4J. 27 @ivar jvm: The object of the wrapped Java Virtual Machine process 28 @ivar socket_no: The socket that the Java Server is responding to Py4J requests 29 @ivar pid: The system process id of the Java Server 30 ''' 31
32 - def __init__(self, java_classpath):
33 ''' 34 Star running java 35 ''' 36 37 #java_classpath.extend(get_libs()) 38 java_classpath = get_libs() 39 path = os.path.abspath(__file__) 40 #java_classpath.append(os.path.dirname(path)) 41 42 classpath = ":".join(java_classpath) 43 print "classpath = ", classpath 44 45 #since code ships without compiled java, we run this command to make sure that the necessary java .class file is ready 46 #commenting out as it is better handled by bash script in /lib 47 #try: 48 # subprocess.check_call(["javac", "-classpath", classpath, "%s/JavaServer.java" % os.path.dirname(path) ]) 49 #except: 50 # pass 51 52 # prepare and run Java server 53 #cmd = "java -cp %s:%s:%s JavaServer" % (berkeley_parser_jar, py4j_jar, dir_path) 54 cmd = ["java", "-cp", classpath, "JavaServer" ] 55 print cmd 56 cmd = " ".join(cmd) 57 58 self.jvm = subprocess.Popen(cmd, shell=True, bufsize=0, stdout=subprocess.PIPE) #shell=True, 59 time.sleep(3) 60 self.jvm.stdout.flush() 61 self.socket_no = int(self.jvm.stdout.readline().strip()) 62 self.pid = self.jvm.pid
63
64 - def terminate(self):
65 """ 66 Stop the Java Server 67 """ 68 self.jvm.terminate()
69 70
71 - def __del__(self):
72 """ 73 Stop the Java Server if the object stops existing 74 """ 75 try: 76 self.jvm.terminate() 77 except: 78 pass
79