/
java_gateway.py
1173 lines (966 loc) · 42.6 KB
/
java_gateway.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: UTF-8 -*-
"""Module to interact with objects in a Java Virtual Machine from a
Pyton Virtual Machine.
Variables that might clash with the JVM start with an underscore
(Java Naming Convention do not recommend to start with an underscore
so clashes become unlikely).
Created on Dec 3, 2009
:author: Barthelemy Dagenais
"""
from __future__ import unicode_literals, absolute_import
import atexit
from collections import deque
import logging
import os
from pydoc import ttypager
import socket
from subprocess import Popen, PIPE
import sys
from threading import Thread, RLock
import weakref
from py4j.compat import range, hasattr2
from py4j.finalizer import ThreadSafeFinalizer
from py4j.protocol import *
from py4j.version import __version__
class NullHandler(logging.Handler):
def emit(self, record):
pass
null_handler = NullHandler()
logging.getLogger("py4j").addHandler(null_handler)
logger = logging.getLogger("py4j.java_gateway")
BUFFER_SIZE = 4096
DEFAULT_ADDRESS = '127.0.0.1'
DEFAULT_PORT = 25333
DEFAULT_PYTHON_PROXY_PORT = 25334
PY4J_SKIP_COLLECTIONS = 'PY4J_SKIP_COLLECTIONS'
PY4J_TRUE = set(['yes', 'y', 't', 'true'])
def java_import(jvm_view, import_str):
"""Imports the package or class specified by `import_str` in the
jvm view namespace.
:param jvm_view: The jvm_view in which to import a class/package.
:import_str: The class (e.g., java.util.List) or the package
(e.g., java.io.*) to import
"""
gateway_client = jvm_view._gateway_client
command = JVMVIEW_COMMAND_NAME + JVM_IMPORT_SUB_COMMAND_NAME +\
jvm_view._id + '\n' + escape_new_line(import_str) + '\n' +\
END_COMMAND_PART + '\n' + escape_new_line(import_str) + '\n' +\
END_COMMAND_PART
answer = gateway_client.send_command(command)
return_value = get_return_value(answer, gateway_client, None, None)
return return_value
def find_jar_path():
"""Tries to find the path where the py4j jar is located.
"""
paths = []
jar_file = "py4j{0}.jar".format(__version__)
paths.append(jar_file)
paths.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),
"../../../py4j-java/" + jar_file))
paths.append("../../../current-release/" + jar_file)
paths.append(os.path.join(sys.prefix, "share/py4j/" + jar_file))
for path in paths:
if os.path.exists(path):
return path
return ""
def launch_gateway(port=0, jarpath="", classpath="", javaopts=[],
die_on_exit=False):
"""Launch a `Gateway` in a new Java process.
:param port: the port to launch the Java Gateway on. If no port is
specified then an ephemeral port is used.
:param jarpath: the path to the Py4J jar. Only necessary if the jar
was installed at a non-standard location or if Python is using
a different `sys.prefix` than the one that Py4J was installed
under.
:param classpath: the classpath used to launch the Java Gateway.
:param javaopts: an array of extra options to pass to Java (the classpath
should be specified using the `classpath` parameter, not `javaopts`.)
:param die_on_exit: if `True`, the Java gateway process will die when
this Python process exits or is killed.
:rtype: the port number of the `Gateway` server.
"""
if not jarpath:
jarpath = find_jar_path()
# Fail if the jar does not exist.
if not os.path.exists(jarpath):
raise Py4JError("Could not find py4j jar at {0}".format(jarpath))
# Launch the server in a subprocess.
classpath = ":".join((jarpath, classpath))
command = ["java", "-classpath", classpath] + javaopts + \
["py4j.GatewayServer"]
if die_on_exit:
command.append("--die-on-broken-pipe")
command.append(str(port))
logger.debug("Lauching gateway with command {0}".format(command))
proc = Popen(command, stdout=PIPE, stdin=PIPE)
# Determine which port the server started on (needed to support
# ephemeral ports)
_port = int(proc.stdout.readline())
return _port
def get_field(java_object, field_name):
"""Retrieves the field named `field_name` from the `java_object`.
This function is useful when `auto_field=false` in a gateway or
Java object.
:param java_object: the instance containing the field
:param field_name: the name of the field to retrieve
"""
command = FIELD_COMMAND_NAME + FIELD_GET_SUBCOMMAND_NAME +\
java_object._target_id + '\n' + field_name + '\n' +\
END_COMMAND_PART
answer = java_object._gateway_client.send_command(command)
if answer == NO_MEMBER_COMMAND or is_error(answer)[0]:
raise Py4JError('no field {0} in object {1}'.format(
field_name, java_object._target_id))
else:
return get_return_value(answer, java_object._gateway_client,
java_object._target_id, field_name)
def set_field(java_object, field_name, value):
"""Sets the field named `field_name` of `java_object` to `value`.
This function is the only way to set a field because the assignment
operator in Python cannot be overloaded.
:param java_object: the instance containing the field
:param field_name: the name of the field to set
:param value: the value to assign to the field
"""
command_part = get_command_part(value,
java_object._gateway_client.gateway_property.pool)
command = FIELD_COMMAND_NAME + FIELD_SET_SUBCOMMAND_NAME +\
java_object._target_id + '\n' + field_name + '\n' +\
command_part + '\n' + END_COMMAND_PART
answer = java_object._gateway_client.send_command(command)
if answer == NO_MEMBER_COMMAND or is_error(answer)[0]:
raise Py4JError('no field {0} in object {1}'.format(
field_name, java_object._target_id))
return get_return_value(answer, java_object._gateway_client,
java_object._target_id, field_name)
def get_method(java_object, method_name):
"""Retrieves a reference to the method of an object.
This function is useful when `auto_field=true` and an instance field has
the same name as a method. The full signature of the method is not
required: it is determined when the method is called.
:param java_object: the instance containing the method
:param method_name: the name of the method to retrieve
"""
return JavaMember(method_name, java_object, java_object._target_id,
java_object._gateway_client)
def _garbage_collect_object(gateway_client, target_id):
# print(target_id + ' deleted')
ThreadSafeFinalizer.remove_finalizer(smart_decode(gateway_client.address) +
smart_decode(gateway_client.port) + target_id)
if target_id != ENTRY_POINT_OBJECT_ID and gateway_client.is_connected:
try:
gateway_client.send_command(MEMORY_COMMAND_NAME +
MEMORY_DEL_SUBCOMMAND_NAME +
target_id +
'\ne\n'
)
except Exception:
pass
def _garbage_collect_connection(socket_instance):
"""Closes the socket if auto_delete is True and the socket is opened.
This is an acceptable practice if you know that your Python VM implements
garbage collection and closing sockets immediately is not a concern.
Otherwise, it is always better (because it is predictable) to explicitly
close the socket by calling `GatewayConnection.close()`.
"""
# print('delete connection')
if socket_instance != None:
try:
socket_instance.shutdown(socket.SHUT_RDWR)
socket_instance.close()
except Exception:
pass
class DummyRLock(object):
def __init__(self):
pass
def acquire(self, blocking=1):
pass
def release(self):
pass
def __enter__(self):
pass
def __exit__(self, type, value, tb):
pass
class GatewayClient(object):
"""Responsible for managing connections to the JavaGateway.
This implementation is thread-safe and connections are created on-demand.
This means that Py4J-Python can be accessed by multiple threads and
messages are sent to and processed concurrently by the Java Gateway.
When creating a custom :class:`JavaGateway`, it is recommended to pass an
instance of :class:`GatewayClient` instead of a :class:`GatewayConnection`:
both have the same interface, but the client supports multiple threads and
connections, which is essential when using callbacks. """
def __init__(self, address=DEFAULT_ADDRESS, port=25333, auto_close=True,
gateway_property=None):
"""
:param address: the address to which the client will request a
connection
:param port: the port to which the client will request a connection.
Default is 25333.
:param auto_close: if `True`, the connections created by the client
close the socket when they are garbage collected.
:param gateway_property: used to keep gateway preferences without a
cycle with the gateway
"""
self.address = address
self.port = port
self.is_connected = True
self.auto_close = auto_close
self.gateway_property = gateway_property
self.deque = deque()
def _get_connection(self):
if not self.is_connected:
raise Py4JNetworkError('Gateway is not connected.')
try:
connection = self.deque.pop()
except Exception:
connection = self._create_connection()
return connection
def _create_connection(self):
# print('Creating connection')
connection = GatewayConnection(self.address, self.port,
self.auto_close, self.gateway_property)
connection.start()
return connection
def _give_back_connection(self, connection):
try:
self.deque.append(connection)
except Exception:
pass
def shutdown_gateway(self):
"""Sends a shutdown command to the gateway. This will close the
gateway server: all active connections will be closed. This may
be useful if the lifecycle of the Java program must be tied to
the Python program.
"""
connection = self._get_connection()
try:
connection.shutdown_gateway()
self.close()
self.is_connected = False
except Py4JNetworkError:
self.shutdown_gateway()
def send_command(self, command, retry=True):
"""Sends a command to the JVM. This method is not intended to be
called directly by Py4J users. It is usually called by
:class:`JavaMember` instances.
:param command: the `string` command to send to the JVM. The command
must follow the Py4J protocol.
:param retry: if `True`, the GatewayClient tries to resend a message
if it fails.
:rtype: the `string` answer received from the JVM. The answer follows
the Py4J protocol.
"""
connection = self._get_connection()
try:
response = connection.send_command(command)
self._give_back_connection(connection)
except Py4JNetworkError:
if retry:
#print_exc()
response = self.send_command(command)
else:
response = ERROR
return response
def close(self):
"""Closes all currently opened connections.
This operation is not thread safe and is only a best effort strategy
to close active connections.
All connections are guaranteed to be closed only if no other thread
is accessing the client and no call is pending.
"""
size = len(self.deque)
for _ in range(0, size):
try:
connection = self.deque.pop()
connection.close()
except Exception:
pass
class GatewayConnection(object):
"""Default gateway connection (socket based) responsible for communicating
with the Java Virtual Machine."""
def __init__(self, address=DEFAULT_ADDRESS, port=25333, auto_close=True,
gateway_property=None):
"""
:param address: the address to which the connection will be established
:param port: the port to which the connection will be established.
Default is 25333.
:param auto_close: if `True`, the connection closes the socket when it
is garbage collected.
:param gateway_property: contains gateway preferences to avoid a cycle
with gateway
"""
self.address = address
self.port = port
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.is_connected = False
self.auto_close = auto_close
self.gateway_property = gateway_property
self.wr = weakref.ref(self,
lambda wr, socket_instance=self.socket:
_garbage_collect_connection(socket_instance))
def start(self):
"""Starts the connection by connecting to the `address` and the `port`
"""
try:
self.socket.connect((self.address, self.port))
self.is_connected = True
self.stream = self.socket.makefile('rb', 0)
except Exception:
msg = 'An error occurred while trying to connect to the Java '\
'server'
logger.exception(msg)
raise Py4JNetworkError(msg)
def close(self, throw_exception=False):
"""Closes the connection by closing the socket."""
try:
self.stream.close()
self.socket.shutdown(socket.SHUT_RDWR)
self.socket.close()
except Exception as e:
if throw_exception:
raise e
finally:
self.is_connected = False
def shutdown_gateway(self):
"""Sends a shutdown command to the gateway. This will close the gateway
server: all active connections will be closed. This may be useful
if the lifecycle of the Java program must be tied to the Python
program.
"""
if (not self.is_connected):
raise Py4JError('Gateway must be connected to send shutdown cmd.')
try:
self.stream.close()
self.socket.sendall(SHUTDOWN_GATEWAY_COMMAND_NAME.encode('utf-8'))
self.socket.close()
self.is_connected = False
except Exception:
# Do nothing! Exceptions might occur anyway.
pass
def send_command(self, command):
"""Sends a command to the JVM. This method is not intended to be
called directly by Py4J users: it is usually called by JavaMember
instances.
:param command: the `string` command to send to the JVM. The command
must follow the Py4J protocol.
:rtype: the `string` answer received from the JVM. The answer follows
the Py4J protocol.
"""
logger.debug("Command to send: {0}".format(command))
try:
self.socket.sendall(command.encode('utf-8'))
answer = smart_decode(self.stream.readline()[:-1])
logger.debug("Answer received: {0}".format(answer))
# Happens when a the other end is dead. There might be an empty
# answer before the socket raises an error.
if answer.strip() == '':
self.close()
raise Py4JError("Answer from Java side is empty")
return answer
except Exception:
#print_exc()
logger.exception('Error while sending or receiving.')
raise Py4JNetworkError('Error while sending or receiving')
class JavaMember(object):
"""Represents a member (i.e., method) of a :class:`JavaObject`. For now,
only methods are supported. Fields are retrieved directly and are not
contained in a JavaMember.
"""
def __init__(self, name, container, target_id, gateway_client):
self.name = name
self.container = container
self.target_id = target_id
self.gateway_client = gateway_client
self.command_header = self.target_id + '\n' + self.name + '\n'
self.pool = self.gateway_client.gateway_property.pool
self.converters = self.gateway_client.converters
def _get_args(self, args):
temp_args = []
new_args = []
for arg in args:
if not isinstance(arg, JavaObject) and \
not isinstance(arg, basestring):
for converter in self.gateway_client.converters:
if converter.can_convert(arg):
temp_arg = converter.convert(arg, self.gateway_client)
temp_args.append(temp_arg)
new_args.append(temp_arg)
break
else:
new_args.append(arg)
else:
new_args.append(arg)
return (new_args, temp_args)
def __call__(self, *args):
if self.converters is not None and len(self.converters) > 0:
(new_args, temp_args) = self._get_args(args)
else:
new_args = args
temp_args = []
args_command = ''.join(
[get_command_part(arg, self.pool) for arg in new_args])
command = CALL_COMMAND_NAME +\
self.command_header +\
args_command +\
END_COMMAND_PART
answer = self.gateway_client.send_command(command)
return_value = get_return_value(answer, self.gateway_client,
self.target_id, self.name)
for temp_arg in temp_args:
temp_arg._detach()
return return_value
class JavaObject(object):
"""Represents a Java object from which you can call methods or access
fields."""
def __init__(self, target_id, gateway_client):
"""
:param target_id: the identifier of the object on the JVM side. Given
by the JVM.
:param gateway_client: the gateway client used to communicate with
the JVM.
"""
# print(target_id + ' created.')
self._target_id = target_id
self._gateway_client = gateway_client
self._auto_field = gateway_client.gateway_property.auto_field
self._methods = {}
key = smart_decode(self._gateway_client.address) +\
smart_decode(self._gateway_client.port) +\
self._target_id
value = weakref.ref(self, lambda wr, cc=self._gateway_client,
id=self._target_id: _garbage_collect_object(cc, id))
ThreadSafeFinalizer.add_finalizer(key, value)
def _detach(self):
_garbage_collect_object(self._gateway_client, self._target_id)
def _get_object_id(self):
return self._target_id
def __getattr__(self, name):
if name not in self._methods:
if (self._auto_field):
(is_field, return_value) = self._get_field(name)
if (is_field):
return return_value
# Theoretically, not thread safe, but the worst case scenario is
# cache miss or double overwrite of the same method...
self._methods[name] = JavaMember(name, self, self._target_id,
self._gateway_client)
# The name is a method
return self._methods[name]
def _get_field(self, name):
command = FIELD_COMMAND_NAME +\
FIELD_GET_SUBCOMMAND_NAME +\
self._target_id + '\n' +\
name + '\n' +\
END_COMMAND_PART
answer = self._gateway_client.send_command(command)
if answer == NO_MEMBER_COMMAND or is_error(answer)[0]:
return (False, None)
else:
return_value = get_return_value(answer, self._gateway_client,
self._target_id, name)
return (True, return_value)
def __eq__(self, other):
if other == None:
return False
elif (hasattr2(other, '_get_object_id')):
return self.equals(other)
else:
return other.__eq__(self)
def __hash__(self):
return self.hashCode()
def __str__(self):
return self.toString()
def __repr__(self):
# For now...
return 'JavaObject id=' + self._target_id
class JavaClass():
"""A `JavaClass` represents a Java Class from which static members can be
retrieved. `JavaClass` instances are also needed to initialize an array.
Usually, `JavaClass` are not initialized using their constructor, but
they are created while accessing the `jvm` property of a gateway, e.g.,
`gateway.jvm.java.lang.String`.
"""
def __init__(self, fqn, gateway_client):
self._fqn = fqn
self._gateway_client = gateway_client
self._command_header = fqn + '\n'
def __getattr__(self, name):
command = REFLECTION_COMMAND_NAME +\
REFL_GET_MEMBER_SUB_COMMAND_NAME +\
self._fqn + '\n' +\
name + '\n' +\
END_COMMAND_PART
answer = self._gateway_client.send_command(command)
if len(answer) > 1 and answer[0] == SUCCESS:
if answer[1] == METHOD_TYPE:
return JavaMember(name, None, STATIC_PREFIX + self._fqn,
self._gateway_client)
elif answer[1].startswith(CLASS_TYPE):
return JavaClass(self._fqn + '$' + name,
self._gateway_client)
else:
return get_return_value(answer, self._gateway_client,
self._fqn, name)
else:
raise Py4JError('{0} does not exist in the JVM'.
format(self._fqn + name))
def __call__(self, *args):
args_command = ''.join([get_command_part(arg) for arg in args])
command = CONSTRUCTOR_COMMAND_NAME +\
self._command_header +\
args_command +\
END_COMMAND_PART
answer = self._gateway_client.send_command(command)
return_value = get_return_value(answer, self._gateway_client, None,
self._fqn)
return return_value
class JavaPackage():
"""A `JavaPackage` represents part of a Java package from which Java
classes can be accessed.
Usually, `JavaPackage` are not initialized using their constructor, but
they are created while accessing the `jvm` property of a gateway, e.g.,
`gateway.jvm.java.lang`.
"""
def __init__(self, fqn, gateway_client, jvm_id=None):
self._fqn = fqn
self._gateway_client = gateway_client
if jvm_id is None:
self._jvm_id = DEFAULT_JVM_ID
self._jvm_id = jvm_id
def __getattr__(self, name):
if name == '__call__':
raise Py4JError('Trying to call a package.')
new_fqn = self._fqn + '.' + name
command = REFLECTION_COMMAND_NAME +\
REFL_GET_UNKNOWN_SUB_COMMAND_NAME +\
new_fqn + '\n' +\
self._jvm_id + '\n' +\
END_COMMAND_PART
answer = self._gateway_client.send_command(command)
if answer == SUCCESS_PACKAGE:
return JavaPackage(new_fqn, self._gateway_client, self._jvm_id)
elif answer.startswith(SUCCESS_CLASS):
return JavaClass(answer[CLASS_FQN_START:], self._gateway_client)
else:
raise Py4JError('{0} does not exist in the JVM'.format(new_fqn))
class JVMView(object):
"""A `JVMView` allows access to the Java Virtual Machine of a
`JavaGateway`.
This can be used to reference static members (fields and methods) and
to call constructors.
"""
def __init__(self, gateway_client, jvm_name, id=None, jvm_object=None):
self._gateway_client = gateway_client
self._jvm_name = jvm_name
if id is not None:
self._id = id
elif jvm_object is not None:
self._id = REFERENCE_TYPE + jvm_object._get_object_id()
# So that both JVMView instances (on Python and Java) have the
# same lifecycle. Theoretically, JVMView could inherit from
# JavaObject, but I would like to avoid the use of reflection
# for regular Py4J classes.
self._jvm_object = jvm_object
def __getattr__(self, name):
answer = self._gateway_client.send_command(REFLECTION_COMMAND_NAME +\
REFL_GET_UNKNOWN_SUB_COMMAND_NAME + name + '\n' + self._id +\
'\n' + END_COMMAND_PART)
if answer == SUCCESS_PACKAGE:
return JavaPackage(name, self._gateway_client, jvm_id=self._id)
elif answer.startswith(SUCCESS_CLASS):
return JavaClass(answer[CLASS_FQN_START:], self._gateway_client)
else:
raise Py4JError('{0} does not exist in the JVM'.format(name))
class GatewayProperty(object):
def __init__(self, auto_field, pool):
self.auto_field = auto_field
self.pool = pool
class JavaGateway(object):
"""A `JavaGateway` is the main interaction point between a Python VM and
a JVM.
* A `JavaGateway` instance is connected to a `Gateway` instance on the
Java side.
* The `entry_point` field of a `JavaGateway` instance is connected to
the `Gateway.entryPoint` instance on the Java side.
* The `jvm` field of `JavaGateway` enables user to access classes, static
members (fields and methods) and call constructors.
Methods that are not defined by `JavaGateway` are always redirected to
`entry_point`. For example, ``gateway.doThat()`` is equivalent to
``gateway.entry_point.doThat()``. This is a trade-off between convenience
and potential confusion.
"""
def __init__(self, gateway_client=None, auto_field=False,
python_proxy_port=DEFAULT_PYTHON_PROXY_PORT,
start_callback_server=False, auto_convert=False):
"""
:param gateway_client: gateway client used to connect to the JVM. If
`None`, a gateway client based on a socket with the default
parameters is created.
:param auto_field: if `False`, each object accessed through this
gateway won't try to lookup fields (they will be accessible only by
calling get_field). If `True`, fields will be automatically looked
up, possibly hiding methods of the same name and making method calls
less efficient.
:param python_proxy_port: port used to receive callback from the JVM.
:param start_callback_server: if `True`, the callback server is
started.
:param auto_convert: if `True`, try to automatically convert Python
objects like sequences and maps to Java Objects. Default value is
`False` to improve performance and because it is still possible to
explicitly perform this conversion.
"""
self.gateway_property = GatewayProperty(auto_field, PythonProxyPool())
self._python_proxy_port = python_proxy_port
if gateway_client == None:
gateway_client = GatewayClient()
if auto_convert:
gateway_client.converters = INPUT_CONVERTER
else:
gateway_client.converters = None
gateway_client.gateway_property = self.gateway_property
self._gateway_client = gateway_client
self.entry_point = JavaObject(ENTRY_POINT_OBJECT_ID, gateway_client)
self.jvm = JVMView(gateway_client, jvm_name=DEFAULT_JVM_NAME,
id=DEFAULT_JVM_ID)
if start_callback_server:
self._callback_server = CallbackServer(self.gateway_property.pool,
self._gateway_client, python_proxy_port)
self._callback_server.start()
def __getattr__(self, name):
return self.entry_point.__getattr__(name)
def new_jvm_view(self, name='custom jvm'):
"""Creates a new JVM view with its own imports. A JVM view ensures
that the import made in one view does not conflict with the import
of another view.
Generally, each Python module should have its own view (to replicate
Java behavior).
:param name: Optional name of the jvm view. Does not need to be
unique, i.e., two distinct views can have the same name
(internally, they will have a distinct id).
:rtype: A JVMView instance (same class as the gateway.jvm instance).
"""
command = JVMVIEW_COMMAND_NAME +\
JVM_CREATE_VIEW_SUB_COMMAND_NAME +\
get_command_part(name) +\
END_COMMAND_PART
answer = self._gateway_client.send_command(command)
java_object = get_return_value(answer, self._gateway_client)
return JVMView(gateway_client=self._gateway_client, jvm_name=name,
jvm_object=java_object)
def new_array(self, java_class, *dimensions):
"""Creates a Java array of type `java_class` of `dimensions`
:param java_class: The :class:`JavaClass` instance representing the
type of the array.
:param dimensions: A list of dimensions of the array. For example
`[1,2]` would produce an `array[1][2]`.
:rtype: A :class:`JavaArray <py4j.java_collections.JavaArray>`
instance.
"""
if len(dimensions) == 0:
raise Py4JError('new arrays must have at least one dimension')
command = ARRAY_COMMAND_NAME +\
ARRAY_CREATE_SUB_COMMAND_NAME +\
get_command_part(java_class._fqn)
for dimension in dimensions:
command += get_command_part(dimension)
command += END_COMMAND_PART
answer = self._gateway_client.send_command(command)
return get_return_value(answer, self._gateway_client)
def shutdown(self):
"""Shuts down the :class:`GatewayClient` and the
:class:`CallbackServer <py4j.java_callback.CallbackServer>`.
"""
try:
self._gateway_client.shutdown_gateway()
except Exception:
pass
self._shutdown_callback_server()
def _shutdown_callback_server(self):
"""Shuts down the
:class:`CallbackServer <py4j.java_callback.CallbackServer>`.
"""
try:
self._callback_server.shutdown()
except Exception:
pass
def restart_callback_server(self):
"""Shuts down the callback server (if started) and restarts a new one.
"""
self._shutdown_callback_server()
self._callback_server = CallbackServer(self.gateway_property.pool,
self._gateway_client, self._python_proxy_port)
self._callback_server.start()
def close(self, keep_callback_server=False):
"""Closes all gateway connections. A connection will be reopened if
necessary (e.g., if a :class:`JavaMethod` is called).
:param keep_callback_server: if `True`, the callback server is not
shut down.
"""
self._gateway_client.close()
if not keep_callback_server:
self._shutdown_callback_server()
def detach(self, java_object):
"""Makes the Java Gateway dereference this object.
The equivalent of this method is called when a JavaObject instance
is garbage collected on the Python side. This method, or gc.collect()
should still be invoked when memory is limited or when too many objects
are created on the Java side.
:param java_object: The JavaObject instance to dereference (free) on
the Java side.
"""
java_object._detach()
def help(self, var, pattern=None, short_name=True, display=True):
"""Displays a help page about a class or an object.
:param var: JavaObject or JavaClass for which a help page will be
generated.
:param pattern: Star-pattern used to filter the members. For example
'get*Foo' may return getMyFoo, getFoo, getFooBar, but not bargetFoo.
:param short_name: If True, only the simple name of the parameter
types and return types will be displayed. If False, the fully
qualified name of the types will be displayed.
:param display: If True, the help page is displayed in an interactive
page similar to the `help` command in Python. If False, the page is
returned as a string.
"""
if hasattr2(var, '_get_object_id'):
command = HELP_COMMAND_NAME +\
HELP_OBJECT_SUBCOMMAND_NAME +\
var._get_object_id() + '\n' +\
get_command_part(pattern) +\
get_command_part(short_name) +\
END_COMMAND_PART
answer = self._gateway_client.send_command(command)
elif hasattr2(var, '_fqn'):
command = HELP_COMMAND_NAME +\
HELP_CLASS_SUBCOMMAND_NAME +\
var._fqn + '\n' +\
get_command_part(pattern) +\
get_command_part(short_name) +\
END_COMMAND_PART
answer = self._gateway_client.send_command(command)
else:
raise Py4JError('var is neither a Java Object nor a Java Class')
help_page = get_return_value(answer, self._gateway_client, None, None)
if (display):
ttypager(help_page)
else:
return help_page
@classmethod
def launch_gateway(cls, port=0, jarpath="", classpath="", javaopts=[],
die_on_exit=False):
"""Launch a `Gateway` in a new Java process and create a default
:class:`JavaGateway <py4j.java_gateway.JavaGateway>` to connect to
it.
:param port: the port to launch the Java Gateway on. If no port is
specified then an ephemeral port is used.
:param jarpath: the path to the Py4J jar. Only necessary if the jar
was installed at a non-standard location or if Python is using
a different `sys.prefix` than the one that Py4J was installed
under.
:param classpath: the classpath used to launch the Java Gateway.
:param javaopts: an array of extra options to pass to Java (the classpath
should be specified using the `classpath` parameter, not `javaopts`.)
:param die_on_exit: if `True`, the Java gateway process will die when
this Python process exits or is killed.
:rtype: a :class:`JavaGateway <py4j.java_gateway.JavaGateway>`
connected to the `Gateway` server.
"""
_port = launch_gateway(port, jarpath, classpath, javaopts, die_on_exit)
gateway = JavaGateway(GatewayClient(port=_port))
return gateway
# CALLBACK SPECIFIC
class CallbackServer(object):
"""The CallbackServer is responsible for receiving call back connection
requests from the JVM. Usually connections are reused on the Java side,
but there is at least one connection per concurrent thread.
"""
def __init__(self, pool, gateway_client, port=DEFAULT_PYTHON_PROXY_PORT,
address=DEFAULT_ADDRESS):
"""
:param pool: the pool responsible of tracking Python objects passed to
the Java side.
:param gateway_client: the gateway client used to call Java objects.
:param port: the port the CallbackServer is listening to.
"""
super(CallbackServer, self).__init__()
self.gateway_client = gateway_client
self.port = port
self.address = address
self.pool = pool
self.connections = []
# Lock is used to isolate critical region like connection creation.
# Some code can produce exceptions when ran in parallel, but
# They will be caught and dealt with.
self.lock = RLock()
self.is_shutdown = False
def start(self):
"""Starts the CallbackServer. This method should be called by the
client instead of run()."""
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
1)
self.server_socket.bind((self.address, self.port))
# Maybe thread needs to be cleanup up?
self.thread = Thread(target=self.run)
self.thread.start()
def run(self):
"""Starts listening and accepting connection requests.
This method is called when invoking `CallbackServer.start()`. A
CallbackServer instance is created and started automatically when
a :class:`JavaGateway <py4j.java_gateway.JavaGateway>` instance is
created.
"""
try:
with self.lock:
self.is_shutdown = False
logger.info('Callback Server Starting')
self.server_socket.listen(5)