public static ReturnObject getPrimitiveReturnObject(Object primitive) { ReturnObject rObject = new ReturnObject(); rObject.primitiveObject = primitive; char primitiveType = Protocol.getPrimitiveType(primitive); if (primitiveType == Protocol.STRING_TYPE) { rObject.commandPart = primitiveType + StringUtil.escape(primitive.toString()); } else if (primitiveType == Protocol.BYTES_TYPE) { rObject.commandPart = primitiveType + Protocol.encodeBytes((byte[]) primitive); } else { rObject.commandPart = primitiveType + primitive.toString(); } return rObject; }
/** * <p>Performs authentication on the reader / writer representing a * connection to a server.</p> * * <p>To be reusable, this function performs the read and write through raw sockets, * and inspects the output immediately. It is essential that we do not try to evaluate * the output or we could end up executing a non-authenticated method or raising an * unexpected exception.</p> * * @param reader Reader connected to the remote endpoint. * @param writer Writer connected to the remote endpoint. * @param authToken The auth token. * @throws IOException On I/O error, or if authentication fails. */ static void authToServer(BufferedReader reader, BufferedWriter writer, String authToken) throws IOException { writer.write(Protocol.getAuthCommand(authToken)); writer.flush(); String returnCommand = reader.readLine(); if (returnCommand == null || !returnCommand.equals(Protocol.getOutputVoidCommand().trim())) { logger.log(Level.SEVERE, "Could not authenticate connection. Received this response: " + returnCommand); throw new IOException("Authentication with callback server unsuccessful."); } }
public final static Object getObject(String commandPart, Gateway gateway) { if (isEmpty(commandPart) || isEnd(commandPart)) { throw new Py4JException("Command Part is Empty or is the End of Command Part"); } else { switch (commandPart.charAt(0)) { case BOOLEAN_TYPE: return getBoolean(commandPart); case DOUBLE_TYPE: return getDouble(commandPart); case LONG_TYPE: return getLong(commandPart); case INTEGER_TYPE: try { return getInteger(commandPart); } catch (NumberFormatException e) { return getLong(commandPart); return getBytes(commandPart); case NULL_TYPE: return getNull(commandPart); case VOID: return getNull(commandPart); case REFERENCE_TYPE: return getReference(commandPart, gateway); case STRING_TYPE: return getString(commandPart); case DECIMAL_TYPE: return getDecimal(commandPart); case PYTHON_PROXY_TYPE: return getPythonProxy(commandPart, gateway);
private String getUnknownMember(BufferedReader reader) throws IOException { String fqn = reader.readLine(); String jvmId = reader.readLine(); JVMView view = (JVMView) Protocol.getObject(jvmId, this.gateway); reader.readLine(); String returnCommand = null; try { // TODO APPEND CLASS NAME, because it might not be the fqn, but a // new one because of imports! String fullyQualifiedName = TypeUtil.forName(fqn, view).getName(); returnCommand = Protocol.getMemberOutputCommand(Protocol.CLASS_TYPE, fullyQualifiedName); } catch (ClassNotFoundException e) { returnCommand = Protocol.getMemberOutputCommand(Protocol.PACKAGE_TYPE); } catch (Exception e) { returnCommand = Protocol.getOutputErrorCommand(e); } return returnCommand; }
private String getHelpObject(BufferedReader reader) throws IOException { String objectId = reader.readLine(); String pattern = (String) Protocol.getObject(reader.readLine(), this.gateway); String shortName = safeReadLine(reader, false); // EoC reader.readLine(); String returnCommand; try { Object obj = gateway.getObject(objectId); Py4JClass clazz = Py4JClass.buildClass(obj.getClass(), true); boolean isShortName = Protocol.getBoolean(shortName); String helpPage = HelpPageGenerator.getHelpPage(clazz, pattern, isShortName); ReturnObject rObject = gateway.getReturnObject(helpPage); returnCommand = Protocol.getOutputCommand(rObject); } catch (Exception e) { returnCommand = Protocol.getOutputErrorCommand(e); } return returnCommand; }
private String setField(BufferedReader reader) throws IOException { String targetObjectId = reader.readLine(); String fieldName = reader.readLine(); String value = reader.readLine(); reader.readLine(); // read EndOfCommand; Object valueObject = Protocol.getObject(value, this.gateway); Object object = gateway.getObject(targetObjectId); Field field = reflectionEngine.getField(object, fieldName); logger.finer("Setting field " + fieldName); String returnCommand = null; if (field == null) { returnCommand = Protocol.getNoSuchFieldOutputCommand(); } else { reflectionEngine.setFieldValue(object, field, valueObject); returnCommand = Protocol.getOutputVoidCommand(); } return returnCommand; }
Object obj = rEngine.getFieldValue(null, f); ReturnObject rObject = gateway.getReturnObject(obj); returnCommand = Protocol.getOutputCommand(rObject); if (m != null) { if (Modifier.isStatic(m.getModifiers())) { returnCommand = Protocol.getMemberOutputCommand(Protocol.METHOD_TYPE); } else { returnCommand = Protocol .getOutputErrorCommand("Trying to access a non-static member from a static context."); Class<?> c = rEngine.getClass(clazz, member); if (c != null) { returnCommand = Protocol.getMemberOutputCommand(Protocol.CLASS_TYPE); } else { returnCommand = Protocol.getOutputErrorCommand(); returnCommand = Protocol.getOutputErrorCommand();
@SuppressWarnings({ "rawtypes", "unchecked" }) private String mult_list(BufferedReader reader) throws IOException { List list1 = (List) gateway.getObject(reader.readLine()); int n = Protocol.getInteger(reader.readLine()); // Read end reader.readLine(); List list2 = new ArrayList(); for (int i = 0; i < n; i++) { list2.addAll(list1); } ReturnObject returnObject = gateway.getReturnObject(list2); return Protocol.getOutputCommand(returnObject); }
private String getField(BufferedReader reader) throws IOException { String targetObjectId = reader.readLine(); String fieldName = reader.readLine(); reader.readLine(); // read EndOfCommand. Object object = gateway.getObject(targetObjectId); Field field = reflectionEngine.getField(object, fieldName); logger.finer("Getting field " + fieldName); String returnCommand = null; if (field == null) { returnCommand = Protocol.getNoSuchFieldOutputCommand(); } else { Object fieldObject = reflectionEngine.getFieldValue(object, field); ReturnObject rObject = gateway.getReturnObject(fieldObject); returnCommand = Protocol.getOutputCommand(rObject); } return returnCommand; }
@SuppressWarnings({ "rawtypes", "unchecked" }) private String imult_list(BufferedReader reader) throws IOException { List list1 = (List) gateway.getObject(reader.readLine()); List tempList = new ArrayList(list1.subList(0, list1.size())); int n = Protocol.getInteger(reader.readLine()); // Read end reader.readLine(); if (n <= 0) { list1.clear(); } else { for (int i = 1; i < n; i++) { list1.addAll(tempList); } } return RETURN_VOID; }
private String getHelpClass(BufferedReader reader) throws IOException { String className = reader.readLine(); String pattern = (String) Protocol.getObject(reader.readLine(), this.gateway); String shortName = safeReadLine(reader, false); // EoC reader.readLine(); String returnCommand; try { Py4JClass clazz = Py4JClass.buildClass(ReflectionUtil.classForName(className), true); boolean isShortName = Protocol.getBoolean(shortName); String helpPage = HelpPageGenerator.getHelpPage(clazz, pattern, isShortName); ReturnObject rObject = gateway.getReturnObject(helpPage); returnCommand = Protocol.getOutputCommand(rObject); } catch (Exception e) { returnCommand = Protocol.getOutputErrorCommand(e); } return returnCommand; }