public Gateway(Object entryPoint, Py4JPythonClient cbClient) { this.entryPoint = entryPoint; this.cbClient = cbClient; this.defaultJVMView = new JVMView("default", Protocol.DEFAULT_JVM_OBJECT_ID); }
private String doImport(BufferedReader reader) throws IOException { String jvmId = reader.readLine(); String importString = StringUtil.unescape(reader.readLine()); reader.readLine(); JVMView view = (JVMView) Protocol.getObject(jvmId, gateway); if (importString.endsWith("*")) { view.addStarImport(importString); } else { view.addSingleImport(importString); } return Protocol.getOutputVoidCommand(); }
private String removeImport(BufferedReader reader) throws IOException { String jvmId = reader.readLine(); String importString = StringUtil.unescape(reader.readLine()); reader.readLine(); JVMView view = (JVMView) Protocol.getObject(jvmId, gateway); boolean removed = false; if (importString.endsWith("*")) { removed = view.removeStarImport(importString); } else { removed = view.removeSingleImport(importString); } return Protocol.getOutputCommand(ReturnObject.getPrimitiveReturnObject(removed)); }
private String[] getJvmViewNames(String jvmId, BufferedReader reader) throws IOException { String lastSequenceIdString = (String) Protocol.getObject(reader.readLine(), gateway); final int lastSequenceId; if (lastSequenceIdString == null) { lastSequenceId = 0; } else { lastSequenceId = Integer.parseInt(lastSequenceIdString); } JVMView view = (JVMView) Protocol.getObject(jvmId, gateway); int sequenceId = view.getSequenceId(); if (lastSequenceId == sequenceId) { return null; } String[] importedNames = view.getImportedNames(); String[] returnValue = new String[importedNames.length + 1]; returnValue[0] = Integer.toString(sequenceId); System.arraycopy(importedNames, 0, returnValue, 1, importedNames.length); return returnValue; }
private String createJVMView(BufferedReader reader) throws IOException { String name = StringUtil.unescape(reader.readLine()); reader.readLine(); JVMView newView = new JVMView(name, null); ReturnObject rObject = gateway.getReturnObject(newView); newView.setId(rObject.getName()); return Protocol.getOutputCommand(rObject); }
public static Class<?> getClass(String simpleName, JVMView view) throws ClassNotFoundException { Class<?> clazz = null; try { // First, try the fqn clazz = ReflectionUtil.classForName(simpleName); } catch (Exception e) { // Then try the single import Map<String, String> singleImportsMap = view.getSingleImportsMap(); String newFQN = singleImportsMap.get(simpleName); if (newFQN != null) { clazz = ReflectionUtil.classForName(newFQN); } else { // Or try star imports for (String starImport : view.getStarImports()) { try { clazz = ReflectionUtil.classForName(starImport + "." + simpleName); break; } catch (Exception e2) { // Ignore } } } } if (clazz == null) { throw new ClassNotFoundException(simpleName + " not found."); } return clazz; }