From 0e17f322af5fff7787899cbe8fd467c78b70a7df Mon Sep 17 00:00:00 2001 From: Ayesh Almeida Date: Mon, 17 Feb 2025 19:43:52 +0530 Subject: [PATCH] Refactor the logic for identifying matching arg for the parameter type --- .../stdlib/websubhub/InteropArgs.java | 47 +++++++++++++++++++ .../NativeHttpToWebsubhubAdaptor.java | 22 +++++---- .../stdlib/websubhub/NativeHubService.java | 24 ++-------- 3 files changed, 65 insertions(+), 28 deletions(-) create mode 100644 native/src/main/java/io/ballerina/stdlib/websubhub/InteropArgs.java diff --git a/native/src/main/java/io/ballerina/stdlib/websubhub/InteropArgs.java b/native/src/main/java/io/ballerina/stdlib/websubhub/InteropArgs.java new file mode 100644 index 00000000..f302e907 --- /dev/null +++ b/native/src/main/java/io/ballerina/stdlib/websubhub/InteropArgs.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. 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 io.ballerina.stdlib.websubhub; + +import io.ballerina.runtime.api.types.Type; +import io.ballerina.runtime.api.values.BMap; +import io.ballerina.runtime.api.values.BObject; +import io.ballerina.runtime.api.values.BString; + +import static io.ballerina.stdlib.websubhub.Constants.HTTP_HEADERS_TYPE; + +/** + * {@code InteropArgs} is a wrapper object which contains the parameters for inter-op calls. + */ +public class InteropArgs { + private final BMap message; + private final BObject httpHeaders; + + InteropArgs(BMap message, BObject httpHeaders) { + this.message = message; + this.httpHeaders = httpHeaders; + } + + public Object getMappingArg(Type argType) { + String argTypeName = argType.toString(); + if (HTTP_HEADERS_TYPE.equals(argTypeName)) { + return httpHeaders; + } + return message; + } +} diff --git a/native/src/main/java/io/ballerina/stdlib/websubhub/NativeHttpToWebsubhubAdaptor.java b/native/src/main/java/io/ballerina/stdlib/websubhub/NativeHttpToWebsubhubAdaptor.java index 4331f2af..f36ca5ec 100644 --- a/native/src/main/java/io/ballerina/stdlib/websubhub/NativeHttpToWebsubhubAdaptor.java +++ b/native/src/main/java/io/ballerina/stdlib/websubhub/NativeHttpToWebsubhubAdaptor.java @@ -74,7 +74,7 @@ public static Object callRegisterMethod(Environment env, BObject adaptor, if (isReadOnly) { message.freezeDirect(); } - Object[] args = nativeHubService.getMethodArgs(ON_REGISTER_TOPIC, message, bHttpHeaders); + Object[] args = nativeHubService.getMethodArgs(ON_REGISTER_TOPIC, new InteropArgs(message, bHttpHeaders)); return invokeRemoteFunction(env, bHubService, args, "callRegisterMethod", ON_REGISTER_TOPIC); } @@ -87,7 +87,7 @@ public static Object callDeregisterMethod(Environment env, BObject adaptor, if (isReadOnly) { message.freezeDirect(); } - Object[] args = nativeHubService.getMethodArgs(ON_DEREGISTER_TOPIC, message, bHttpHeaders); + Object[] args = nativeHubService.getMethodArgs(ON_DEREGISTER_TOPIC, new InteropArgs(message, bHttpHeaders)); return invokeRemoteFunction(env, bHubService, args, "callDeregisterMethod", ON_DEREGISTER_TOPIC); } @@ -100,7 +100,7 @@ public static Object callOnUpdateMethod(Environment env, BObject adaptor, if (isReadOnly) { message.freezeDirect(); } - Object[] args = nativeHubService.getMethodArgs(ON_UPDATE_MESSAGE, message, bHttpHeaders); + Object[] args = nativeHubService.getMethodArgs(ON_UPDATE_MESSAGE, new InteropArgs(message, bHttpHeaders)); return invokeRemoteFunction(env, bHubService, args, "callOnUpdateMethod", ON_UPDATE_MESSAGE); } @@ -113,7 +113,7 @@ public static Object callOnSubscriptionMethod(Environment env, BObject adaptor, if (isReadOnly) { message.freezeDirect(); } - Object[] args = nativeHubService.getMethodArgs(ON_SUBSCRIPTION, message, bHttpHeaders); + Object[] args = nativeHubService.getMethodArgs(ON_SUBSCRIPTION, new InteropArgs(message, bHttpHeaders)); return invokeRemoteFunction(env, bHubService, args, "callOnSubscriptionMethod", ON_SUBSCRIPTION); } @@ -126,7 +126,8 @@ public static Object callOnSubscriptionValidationMethod(Environment env, BObject if (isReadOnly) { message.freezeDirect(); } - Object[] args = nativeHubService.getMethodArgs(ON_SUBSCRIPTION_VALIDATION, message, bHttpHeaders); + Object[] args = nativeHubService.getMethodArgs(ON_SUBSCRIPTION_VALIDATION, + new InteropArgs(message, bHttpHeaders)); return invokeRemoteFunction(env, bHubService, args, "callOnSubscriptionValidationMethod", ON_SUBSCRIPTION_VALIDATION); } @@ -139,7 +140,8 @@ public static Object callOnSubscriptionIntentVerifiedMethod(Environment env, BOb if (isReadOnly) { message.freezeDirect(); } - Object[] args = nativeHubService.getMethodArgs(ON_SUBSCRIPTION_INTENT_VERIFIED, message, bHttpHeaders); + Object[] args = nativeHubService.getMethodArgs(ON_SUBSCRIPTION_INTENT_VERIFIED, + new InteropArgs(message, bHttpHeaders)); return invokeRemoteFunction(env, bHubService, args, "callOnSubscriptionIntentVerifiedMethod", ON_SUBSCRIPTION_INTENT_VERIFIED); @@ -153,7 +155,7 @@ public static Object callOnUnsubscriptionMethod(Environment env, BObject adaptor if (isReadOnly) { message.freezeDirect(); } - Object[] args = nativeHubService.getMethodArgs(ON_UNSUBSCRIPTION, message, bHttpHeaders); + Object[] args = nativeHubService.getMethodArgs(ON_UNSUBSCRIPTION, new InteropArgs(message, bHttpHeaders)); return invokeRemoteFunction(env, bHubService, args, "callOnUnsubscriptionMethod", ON_UNSUBSCRIPTION); } @@ -166,7 +168,8 @@ public static Object callOnUnsubscriptionValidationMethod(Environment env, BObje if (isReadOnly) { message.freezeDirect(); } - Object[] args = nativeHubService.getMethodArgs(ON_UNSUBSCRIPTION_VALIDATION, message, bHttpHeaders); + Object[] args = nativeHubService.getMethodArgs(ON_UNSUBSCRIPTION_VALIDATION, + new InteropArgs(message, bHttpHeaders)); return invokeRemoteFunction(env, bHubService, args, "callOnUnsubscriptionValidationMethod", ON_UNSUBSCRIPTION_VALIDATION); } @@ -179,7 +182,8 @@ public static Object callOnUnsubscriptionIntentVerifiedMethod(Environment env, B if (isReadOnly) { message.freezeDirect(); } - Object[] args = nativeHubService.getMethodArgs(ON_UNSUBSCRIPTION_INTENT_VERIFIED, message, bHttpHeaders); + Object[] args = nativeHubService.getMethodArgs(ON_UNSUBSCRIPTION_INTENT_VERIFIED, + new InteropArgs(message, bHttpHeaders)); return invokeRemoteFunction(env, bHubService, args, "callOnUnsubscriptionIntentVerifiedMethod", ON_UNSUBSCRIPTION_INTENT_VERIFIED); } diff --git a/native/src/main/java/io/ballerina/stdlib/websubhub/NativeHubService.java b/native/src/main/java/io/ballerina/stdlib/websubhub/NativeHubService.java index e9502ead..9b023004 100644 --- a/native/src/main/java/io/ballerina/stdlib/websubhub/NativeHubService.java +++ b/native/src/main/java/io/ballerina/stdlib/websubhub/NativeHubService.java @@ -22,25 +22,20 @@ import io.ballerina.runtime.api.types.ServiceType; import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.utils.TypeUtils; -import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BObject; -import io.ballerina.runtime.api.values.BString; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.stream.Stream; -import static io.ballerina.stdlib.websubhub.Constants.HTTP_HEADERS_TYPE; - /** * {@code NativeBHubService} is a Java wrapper for Ballerina `websubhub:Service` object. */ public class NativeHubService { private final BObject bHubService; - private final List remoteMethodNames = new ArrayList<>(); private final Map> methodParameterMapping = new HashMap<>(); @@ -50,7 +45,6 @@ public class NativeHubService { String methodName = remoteMethod.getName(); List paramTypeInOrder = Stream.of(remoteMethod.getParameters()).map(p -> p.type).toList(); methodParameterMapping.put(methodName, paramTypeInOrder); - remoteMethodNames.add(methodName); } this.bHubService = bHubService; } @@ -59,21 +53,13 @@ public BObject getBHubService() { return bHubService; } - public List getRemoteMethodNames() { - return remoteMethodNames; + public Set getRemoteMethodNames() { + return methodParameterMapping.keySet(); } - public Object[] getMethodArgs(String methodName, BMap message, BObject bHttpHeaders) { + public Object[] getMethodArgs(String methodName, InteropArgs args) { return methodParameterMapping.getOrDefault(methodName, Collections.emptyList()).stream() - .map(argType -> getMappingArg(argType, message, bHttpHeaders)) + .map(args::getMappingArg) .toArray(); } - - private Object getMappingArg(Type argType, BMap message, BObject bHttpHeaders) { - String argTypeName = argType.toString(); - if (HTTP_HEADERS_TYPE.equals(argTypeName)) { - return bHttpHeaders; - } - return message; - } }