markap14 commented on code in PR #7003: URL: https://github.com/apache/nifi/pull/7003#discussion_r1163227701
########## nifi-nar-bundles/nifi-py4j-bundle/nifi-py4j-bridge/src/main/java/org/apache/nifi/py4j/client/PythonProxyInvocationHandler.java: ########## @@ -0,0 +1,174 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.nifi.py4j.client; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import py4j.Protocol; +import py4j.Py4JException; +import py4j.StringUtil; +import py4j.reflection.MethodInvoker; +import py4j.reflection.ReflectionEngine; +import py4j.reflection.TypeConverter; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class PythonProxyInvocationHandler implements InvocationHandler { + public static final String VOID_ARGUMENT_LINE = String.valueOf(Protocol.VOID); + public static final String METHOD_INVOCATION_START_CLAUSE = "c\n"; + public static final String METHOD_INVOCATION_END_CLAUSE = "e\n"; + private static final Logger logger = LoggerFactory.getLogger(PythonProxyInvocationHandler.class); + + private final String objectId; + private final NiFiPythonGateway gateway; + private final JavaObjectBindings bindings; + + public PythonProxyInvocationHandler(final NiFiPythonGateway gateway, final String objectId) { + this.objectId = objectId; + this.gateway = gateway; + this.bindings = gateway.getObjectBindings(); + } + + @Override + public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { + if (args == null && method.getName().equals("toString")) { + return "PythonProxy[targetObjectId=" + objectId + "]"; + } + + final CommandBuilder commandBuilder = new CommandBuilder(bindings, objectId, method.getName()); + final String command = commandBuilder.buildCommand(args); + + if (logger.isDebugEnabled()) { + final List<Object> argList = args == null ? Collections.emptyList() : Arrays.asList(args); + logger.debug("Invoking {} on {} with args {} using command {}", method, proxy, argList, command); + } + + gateway.beginInvocation(this.objectId, method, args); + + final String response = gateway.getCallbackClient().sendCommand(command); + final Object output = Protocol.getReturnValue(response, gateway); + final Object convertedOutput = convertOutput(method, output); + + // TODO: While we're waiting for this call to return, the Python side may call back into the Java side and create objects. + // When that happens we need to throw those onto the stack also! + if (gateway.isUnbind(method)) { + commandBuilder.getBoundIds().forEach(bindings::unbind); + commandBuilder.getBoundIds().forEach(i -> logger.debug("For method invocation {} unbound {} (from command builder)", method.getName(), i)); + } else { + commandBuilder.getBoundIds().forEach(i -> logger.debug("For method invocation {} will not unbind {} (from command builder) because arguments of this method are not to be unbound", + method.getName(), i)); + } + + gateway.endInvocation(this.objectId, method, args); + + return convertedOutput; + } + + + private String buildInvocationCommand(final String methodName, final Object[] args) { + final StringBuilder sb = new StringBuilder(); + sb.append(METHOD_INVOCATION_START_CLAUSE); + sb.append(objectId).append("\n"); + sb.append(methodName).append("\n"); + + if (args != null) { + for (final Object arg : args) { + final String argumentLine = getArgumentLine(arg); + sb.append(argumentLine).append("\n"); + } + } + + sb.append(METHOD_INVOCATION_END_CLAUSE); + return sb.toString(); + } + + private String getArgumentLine(final Object arg) { Review Comment: Actually, I think this logic was moved into `CommandBuilder` but not removed. The method is not invoked. Will remove it from here. Thanks for pointing that out. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org