1 '''
2 Created on 22 Jun 2012
3
4 @author: Eleftherios Avramidis
5 '''
6 import subprocess
7 import time
8 import os
9
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
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
33 '''
34 Star running java
35 '''
36
37
38 java_classpath = get_libs()
39 path = os.path.abspath(__file__)
40
41
42 classpath = ":".join(java_classpath)
43 print "classpath = ", classpath
44
45
46
47
48
49
50
51
52
53
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)
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
65 """
66 Stop the Java Server
67 """
68 self.jvm.terminate()
69
70
72 """
73 Stop the Java Server if the object stops existing
74 """
75 try:
76 self.jvm.terminate()
77 except:
78 pass
79