JPS Gateway
JPSGateway
is the parent class which should be instantiated by every resource gateway class.The resource gateway class is automatically created upon the resource installation, whose class name is the same as the name of the resource given during the installation.
Therefore, one should ideally only work with the resource classes, e.g.
JpsBaseLib
, rather than with the parentJPSGateway
class.The documentation here is provided for developers' information.
JPSGatewaySingletonMeta ¶
JPSGateway ¶
Wrapper class of the py4j JavaGateway class for managing Python-Java communication.
The class can be used in the following way:
from twa import JPSGateway
yourGateway = JPSGateway(resName=yourResName, jarPath=yourResJarPath, **JGkwargs)
Note that if you wish to access an already installed resource through the
JPSGateway
(not recommended), then thejarPath
argument can be omitted as it can be looked up by the resource name in the resource registry. Also, note that some of thepy4j.java_gateway.JavaGateway
constructor arguments are not allowed or should be passed to thepy4j.java_gateway.launch_gateway
method instead. If that is the case, the code will print a warning message which shows how to set the desired argument correctly. Please also note that according to thepy4j
documentation, thegateway_parameters
argument to thepy4j.java_gateway.JavaGateway
constructor must have the type of the py4j GatewayParameters object. However, to make it easy for thetwa
users, this argument can be passed as a dictionary which then is automatically converted into thepy4j GatewayParameters
object.
Attributes:
Name | Type | Description |
---|---|---|
resName |
str
|
name of the Java resource |
jarPath |
str
|
absolute path to the main jar file of the java resource |
gateway |
JavaGateway
|
the gateway object handling Python-Java communication |
_isStarted |
bool
|
flag indicating if the gateway was launched |
_gatewayUserParams |
dict
|
dictionary storing user provided JavaGateway parameters |
_launchGatewayUserParams |
dict
|
dictionary storing user provided launch_gateway parameters |
_initialised |
bool
|
flag inticating if the instance is already initialised |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resName
|
str
|
name of the Java resource |
None
|
jarPath
|
str
|
absolute path to the main jar file of the java resource |
None
|
JGkwargs
|
dict
|
dictionary storing user provided JavaGateway parameters
|
{}
|
Note that the JGkwargs related to the 'gateway_parameters' argument should be passed as a dictionary which is then automatically converted into the 'GatewayParameters' object. Please refer to the py4j documentation for the description of all the possible arguments.
As an example, the following arguments:
will be automatically converted to:
Note that the 'java_process' and 'auth_token' arguments will be skipped if present and they are automatically set by the py4j.java_gateway.launch_gateway method
Note that the 'port' argument will skipped if present as it can only be passed to the py4j.java_gateway.launch_gateway call
Note that setting the JavaGateway 'eager_load' and the py4j.java_gateway.launch_gateway 'enable_auth' arguments to True at the same time does NOT work. The arguments are mutually exclusive
Note that the most important and useful settings are set by default in this constructor so a user hardly ever need to pass any arguments in that call. If required, however, the defaults of this constructor can be overwritten by simply passing their new values. Please also note that this constructor only instantiates the
JPSGateway
object, and it DOES NOT instantiate thepy4j.java_gateway.JavaGateway
, whose instantiation only happens in thetwa.JPSGateway.launchGateway
method explained in more details below.
Source code in JPS_BASE_LIB/python_wrapper/twa/JPSGateway.py
launchGateway ¶
Wrapper method for the py4j.java_gateway.launch_gateway method which launches the Gateway in a new Java process and creates a default JavaGateway to connect to it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
LGkwargs
|
dict
|
a dictionary containing the py4j.java_gateway.launch_gateway method arguments |
{}
|
Note that the 'jarpath' and 'return_proc' arguments cannot be changed and will be skipped if provided
Note that this calls an internal py4j.java_gateway.launch_gateway function which is different from the launch_gateway function described in py4j web documentation. The py4j function described in py4j web documentation is a JavaGateway classmethod which in turn calls the function below. It is a bit confusing as the two functions have the same name. The difference between the two is that the launch_gateway classmethod launches the java process and then creates a JavaGateway object connected to it, the problem is that this function call does not accept any user JavaGateway constructor arguments. The non classmethod call on the other hand only launches the java process without creating the JavaGateway instance. The JavaGateway instance can be then created at a later stage with user defined parameters plus the parameters returned from the launch_gateway method call that connect the running java process and the JavaGateway. Therefore, the non classmethod py4j.java_gateway.launch_gateway is called herein and its outputs are passed to the JavaGateway constructor.
Source code in JPS_BASE_LIB/python_wrapper/twa/JPSGateway.py
shutdown ¶
Wrapper method for the py4j shutdown method to stop the JavaGateway client.
createModuleView ¶
Wrapper method for the py4j new_jvm_view method. Creates a new JVM view with its own imports.
Returns:
Name | Type | Description |
---|---|---|
new_jvm_view |
JVM
|
A new JVM view object |
Source code in JPS_BASE_LIB/python_wrapper/twa/JPSGateway.py
importPackages ¶
Wrapper method for the py4j java_import method. Imports a class / package into the specified JVM view
Parameters:
Name | Type | Description | Default |
---|---|---|---|
moduleView
|
JVM
|
A new JVM view object |
required |
importStatement
|
str
|
The class / package name to import |
required |