Skip to content

Commit

Permalink
Refactor the logic for identifying matching arg for the parameter type
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeshLK committed Feb 17, 2025
1 parent aa6ced0 commit 0e17f32
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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<BString, Object> message;
private final BObject httpHeaders;

InteropArgs(BMap<BString, Object> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> remoteMethodNames = new ArrayList<>();
private final Map<String, List<Type>> methodParameterMapping = new HashMap<>();


Expand All @@ -50,7 +45,6 @@ public class NativeHubService {
String methodName = remoteMethod.getName();
List<Type> paramTypeInOrder = Stream.of(remoteMethod.getParameters()).map(p -> p.type).toList();
methodParameterMapping.put(methodName, paramTypeInOrder);
remoteMethodNames.add(methodName);
}
this.bHubService = bHubService;
}
Expand All @@ -59,21 +53,13 @@ public BObject getBHubService() {
return bHubService;
}

public List<String> getRemoteMethodNames() {
return remoteMethodNames;
public Set<String> getRemoteMethodNames() {
return methodParameterMapping.keySet();
}

public Object[] getMethodArgs(String methodName, BMap<BString, Object> 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<BString, Object> message, BObject bHttpHeaders) {
String argTypeName = argType.toString();
if (HTTP_HEADERS_TYPE.equals(argTypeName)) {
return bHttpHeaders;
}
return message;
}
}

0 comments on commit 0e17f32

Please sign in to comment.