/** * <p> * Replace the callback client with the new one which connects to the given address * and port. This method is useful if for some reason your CallbackServer changes its * address or you come to know of the address after Gateway has already instantiated. * </p> * <p> * This method <strong>is not thread-safe</strong>! Make sure that only * one thread calls this method. * </p> * * @param pythonAddress * The address used by a PythonProxyHandler to connect to a * Python gateway. * @param pythonPort * The port used by a PythonProxyHandler to connect to a Python * gateway. Essentially the port used for Python callbacks. */ public void resetCallbackClient(InetAddress pythonAddress, int pythonPort) { if (cbClient == null) { throw new Py4JException("Callback Client is already null and cannot be reset"); } cbClient.shutdown(); this.cbClient = cbClient.copyWith(pythonAddress, pythonPort); }
/** * <p> * Gets a reference to the entry point on the Python side. This is often * necessary if Java is driving the communication because Java cannot call * static methods, initialize Python objects or load Python modules yet. * </p> * * @param interfacesToImplement * @return */ public Object getPythonServerEntryPoint(@SuppressWarnings("rawtypes") Class[] interfacesToImplement) { return getCallbackClient().getPythonServerEntryPoint(gateway, interfacesToImplement); }
@Override protected void finalize() throws Throwable { try { if (gateway.getCallbackClient().isMemoryManagementEnabled() && this.id != Protocol.ENTRY_POINT_OBJECT_ID) { logger.fine("Finalizing python proxy id " + this.id); gateway.getCallbackClient().sendCommand(finalizeCommand, false); } } catch (Exception e) { logger.warning("Python Proxy ID could not send a finalize message: " + this.id); } finally { super.finalize(); } }
GatewayServer(Object entryPoint, int port, InetAddress address, int connectTimeout, int readTimeout, List<Class<? extends Command>> customCommands, Py4JPythonClient cbClient, ServerSocketFactory sSocketFactory, String authToken) { super(); this.port = port; this.address = address; this.connectTimeout = connectTimeout; this.readTimeout = readTimeout; this.gateway = new Gateway(entryPoint, cbClient); this.pythonPort = cbClient.getPort(); this.pythonAddress = cbClient.getAddress(); this.gateway.putObject(GATEWAY_SERVER_ID, this); if (customCommands != null) { this.customCommands = customCommands; } else { this.customCommands = new ArrayList<Class<? extends Command>>(); } this.listeners = new CopyOnWriteArrayList<GatewayServerListener>(); this.sSocketFactory = sSocketFactory; this.authToken = authToken; }
/** * Updates the python callback port in the {@link GatewayServer}. */ public static void setGatewayCallbackPort(GatewayServer gatewayServer, int port) { gatewayServer.resetCallbackClient(gatewayServer.getCallbackClient().getAddress(), port); }
/** * <p> * Releases all objects that were referenced by this Gateway and * optionally shut down the callback client. * <p> * * @param shutdownCallbackClient Shuts down the CallbackClient instance * if true. */ public void shutdown(boolean shutdownCallbackClient) { isStarted = false; bindings.clear(); if (cbClient != null && shutdownCallbackClient) { cbClient.shutdown(); } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { logger.fine("Method " + method.getName() + " called on Python object " + id); StringBuilder sBuilder = new StringBuilder(); sBuilder.append(CALL_PROXY_COMMAND_NAME); sBuilder.append(id); sBuilder.append("\n"); sBuilder.append(method.getName()); sBuilder.append("\n"); if (args != null) { for (Object arg : args) { sBuilder.append(gateway.getReturnObject(arg).getCommandPart()); sBuilder.append("\n"); } } sBuilder.append("e\n"); String returnCommand = gateway.getCallbackClient().sendCommand(sBuilder.toString()); Object output = Protocol.getReturnValue(returnCommand, gateway); Object convertedOutput = convertOutput(method, output); return convertedOutput; }
private GatewayServer(Gateway gateway, int port, InetAddress address, int connectTimeout, int readTimeout, List<Class<? extends Command>> customCommands, ServerSocketFactory sSocketFactory, String authToken) { super(); this.port = port; this.address = address; this.connectTimeout = connectTimeout; this.readTimeout = readTimeout; this.gateway = gateway; this.pythonPort = gateway.getCallbackClient().getPort(); this.pythonAddress = gateway.getCallbackClient().getAddress(); this.gateway.putObject(GATEWAY_SERVER_ID, this); if (customCommands != null) { this.customCommands = customCommands; } else { this.customCommands = new ArrayList<Class<? extends Command>>(); } this.listeners = new CopyOnWriteArrayList<GatewayServerListener>(); this.sSocketFactory = sSocketFactory; this.authToken = authToken; }
/** * Updates the python callback port in the {@link GatewayServer}. */ public static void setGatewayCallbackPort(GatewayServer gatewayServer, int port) { gatewayServer.resetCallbackClient(gatewayServer.getCallbackClient().getAddress(), port); }