diff --git a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/client/QueryExecutor.java b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/client/QueryExecutor.java index fa0d1f888..b46b3d863 100644 --- a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/client/QueryExecutor.java +++ b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/client/QueryExecutor.java @@ -24,10 +24,6 @@ import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.api.values.BTypedesc; -import java.util.concurrent.CompletableFuture; - -import static io.ballerina.stdlib.graphql.runtime.utils.ModuleUtils.getResult; - /** * This class is used to execute a GraphQL document using the Ballerina GraphQL client. */ @@ -67,15 +63,11 @@ private static Object invokeClientMethod(Environment env, BObject client, BStrin private static Object invokeClientMethod(Environment env, BObject client, String methodName, Object[] paramFeed) { return env.yieldAndRun(() -> { - CompletableFuture balFuture = new CompletableFuture<>(); - QueryExecutorCallback executorCallback = new QueryExecutorCallback(balFuture); try { - Object result = env.getRuntime().callMethod(client, methodName, null, paramFeed); - executorCallback.notifySuccess(result); + return env.getRuntime().callMethod(client, methodName, null, paramFeed); } catch (BError bError) { - executorCallback.notifyFailure(bError); + return bError; } - return getResult(balFuture); }); } } diff --git a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/client/QueryExecutorCallback.java b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/client/QueryExecutorCallback.java deleted file mode 100644 index 8aacaf047..000000000 --- a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/client/QueryExecutorCallback.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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.graphql.runtime.client; - -import io.ballerina.runtime.api.values.BError; - -import java.util.concurrent.CompletableFuture; - -/** - * This class implements async callback methods of {@link QueryExecutor}. - */ -public class QueryExecutorCallback { - private final CompletableFuture balFuture; - - public QueryExecutorCallback(CompletableFuture balFuture) { - this.balFuture = balFuture; - } - - public void notifySuccess(Object result) { - balFuture.complete(result); - } - - public void notifyFailure(BError bError) { - balFuture.complete(bError); - } -} diff --git a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/ArgumentHandler.java b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/ArgumentHandler.java index 9b8c2eaf6..492eb6af6 100644 --- a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/ArgumentHandler.java +++ b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/ArgumentHandler.java @@ -51,7 +51,6 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.concurrent.CompletableFuture; import static io.ballerina.runtime.api.types.TypeTags.INTERSECTION_TAG; import static io.ballerina.stdlib.graphql.runtime.engine.EngineUtils.ARGUMENTS_FIELD; @@ -64,6 +63,7 @@ import static io.ballerina.stdlib.graphql.runtime.engine.EngineUtils.isEnum; import static io.ballerina.stdlib.graphql.runtime.engine.EngineUtils.isIgnoreType; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.INTERNAL_NODE; +import static io.ballerina.stdlib.graphql.runtime.utils.Utils.handleBErrorAndExit; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.isContext; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.isField; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.isFileUpload; @@ -540,16 +540,13 @@ private boolean isRepresentationArgument(Type type) { private void addConstraintValidationErrors(Environment environment, BArray errors) { environment.yieldAndRun(() -> { - CompletableFuture future = new CompletableFuture<>(); - ExecutionCallback executionCallback = new ExecutionCallback(future); BObject fieldNode = this.field.getObjectValue(INTERNAL_NODE); Object[] arguments = {errors, fieldNode}; try { - Object result = environment.getRuntime() - .callMethod(this.responseGenerator, ADD_CONSTRAINT_ERRORS_METHOD, null, arguments); - executionCallback.notifySuccess(result); + environment.getRuntime().callMethod(this.responseGenerator, ADD_CONSTRAINT_ERRORS_METHOD, null, + arguments); } catch (BError bError) { - executionCallback.notifyFailure(bError); + handleBErrorAndExit(bError); } return null; }); diff --git a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/DataLoader.java b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/DataLoader.java index b9dfd9764..44dcd9cd9 100644 --- a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/DataLoader.java +++ b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/DataLoader.java @@ -23,9 +23,7 @@ import io.ballerina.runtime.api.values.BObject; import io.ballerina.runtime.api.values.BTypedesc; -import java.util.concurrent.CompletableFuture; - -import static io.ballerina.stdlib.graphql.runtime.utils.ModuleUtils.getResult; +import static io.ballerina.stdlib.graphql.runtime.utils.Utils.handleBErrorAndExit; /** * This class provides native implementations of the Ballerina DataLoader class. @@ -38,17 +36,13 @@ private DataLoader() { public static Object get(Environment env, BObject dataLoader, Object key, BTypedesc typedesc) { return env.yieldAndRun(() -> { - CompletableFuture balFuture = new CompletableFuture<>(); Object[] paramFeed = getProcessGetMethodParams(key, typedesc); - ExecutionCallback executionCallback = new ExecutionCallback(balFuture); try { - Object result = env.getRuntime().callMethod(dataLoader, DATA_LOADER_PROCESSES_GET_METHOD_NAME, null, - paramFeed); - executionCallback.notifySuccess(result); + return env.getRuntime().callMethod(dataLoader, DATA_LOADER_PROCESSES_GET_METHOD_NAME, null, paramFeed); } catch (BError bError) { - executionCallback.notifyFailure(bError); + handleBErrorAndExit(bError); } - return getResult(balFuture); + return null; }); } diff --git a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/Engine.java b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/Engine.java index a3873458f..b3684ac88 100644 --- a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/Engine.java +++ b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/Engine.java @@ -49,7 +49,6 @@ import java.util.Base64; import java.util.HashMap; import java.util.List; -import java.util.concurrent.CompletableFuture; import static io.ballerina.runtime.observability.ObservabilityConstants.KEY_OBSERVER_CONTEXT; import static io.ballerina.stdlib.graphql.commons.utils.TypeUtils.removeEscapeCharacter; @@ -64,10 +63,10 @@ import static io.ballerina.stdlib.graphql.runtime.engine.EngineUtils.SUBSCRIBE_ACCESSOR; import static io.ballerina.stdlib.graphql.runtime.engine.EngineUtils.isPathsMatching; import static io.ballerina.stdlib.graphql.runtime.utils.ModuleUtils.getModule; -import static io.ballerina.stdlib.graphql.runtime.utils.ModuleUtils.getResult; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.ERROR_TYPE; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.INTERNAL_NODE; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.createError; +import static io.ballerina.stdlib.graphql.runtime.utils.Utils.handleBErrorAndExit; /** * This handles Ballerina GraphQL Engine. @@ -133,16 +132,13 @@ private static Object getResultObject(Environment environment, BObject context, return null; } return environment.yieldAndRun(() -> { - CompletableFuture subscriptionFutureResult = new CompletableFuture<>(); - ExecutionCallback executionCallback = new ExecutionCallback(subscriptionFutureResult); Object[] args = argumentHandler.getArguments(); try { - Object result = callResourceMethod(environment.getRuntime(), service, methodName, args); - executionCallback.notifySuccess(result); + return callResourceMethod(environment.getRuntime(), service, methodName, args); } catch (BError bError) { - executionCallback.notifyFailure(bError); + handleBErrorAndExit(bError); } - return getResult(subscriptionFutureResult); + return null; }); } @@ -183,17 +179,13 @@ public static Object executeInterceptor(Environment environment, BObject interce return null; } return environment.yieldAndRun(() -> { - CompletableFuture future = new CompletableFuture<>(); - ExecutionCallback executionCallback = new ExecutionCallback(future); Object[] arguments = getInterceptorArguments(context, field); try { - Object result = callResourceMethod(environment.getRuntime(), interceptor, remoteMethod.getName(), - arguments); - executionCallback.notifySuccess(result); + return callResourceMethod(environment.getRuntime(), interceptor, remoteMethod.getName(), arguments); } catch (BError bError) { - executionCallback.notifyFailure(bError); + handleBErrorAndExit(bError); } - return getResult(future); + return null; }); } @@ -283,16 +275,12 @@ public static boolean hasPrefetchMethod(BObject serviceObject, BString prefetchM public static void executePrefetchMethod(Environment environment, BObject context, BObject service, MethodType resourceMethod, BObject fieldObject) { environment.yieldAndRun(() -> { - CompletableFuture future = new CompletableFuture<>(); - ExecutionCallback executionCallback = new ExecutionCallback(future); ArgumentHandler argumentHandler = new ArgumentHandler(resourceMethod, context, fieldObject, null, false); Object[] arguments = argumentHandler.getArguments(); try { - Object result = callResourceMethod(environment.getRuntime(), service, resourceMethod.getName(), - arguments); - executionCallback.notifySuccess(result); + return callResourceMethod(environment.getRuntime(), service, resourceMethod.getName(), arguments); } catch (BError bError) { - executionCallback.notifyFailure(bError); + handleBErrorAndExit(bError); } return null; }); diff --git a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/ExecutionCallback.java b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/ExecutionCallback.java deleted file mode 100644 index e71082abc..000000000 --- a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/ExecutionCallback.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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.graphql.runtime.engine; - -import io.ballerina.runtime.api.values.BError; - -import java.util.concurrent.CompletableFuture; - -/** - * Callback class for executing Ballerina dependently type methods. - */ -public class ExecutionCallback { - private final CompletableFuture future; - - ExecutionCallback(CompletableFuture future) { - this.future = future; - } - - public void notifySuccess(Object o) { - this.future.complete(o); - } - - public void notifyFailure(BError bError) { - bError.printStackTrace(); - // Service level `panic` is captured in this method. - // Since, `panic` is due to a critical application bug or resource exhaustion we need to exit the application. - // Please refer: https://github.com/ballerina-platform/ballerina-standard-library/issues/2714 - System.exit(1); - } -} diff --git a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/utils/Utils.java b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/utils/Utils.java index fd368186d..c080c8a43 100644 --- a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/utils/Utils.java +++ b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/utils/Utils.java @@ -96,4 +96,13 @@ private static boolean hasExpectedModuleName(Type type, String expectedModuleNam public static BString getHashCode(BObject object) { return StringUtils.fromString(Integer.toString(object.hashCode())); } + + public static void handleBErrorAndExit(BError bError) { + bError.printStackTrace(); + // Service level `panic` is captured in this method. + // Since, `panic` is due to a critical application bug or resource exhaustion we need to exit the + // application. + // Please refer: https://github.com/ballerina-platform/ballerina-standard-library/issues/2714 + System.exit(1); + } } diff --git a/spotbugs-exclude.xml b/spotbugs-exclude.xml index df23d34d2..b04f5a9de 100644 --- a/spotbugs-exclude.xml +++ b/spotbugs-exclude.xml @@ -20,13 +20,6 @@ - - - - - - - @@ -53,4 +46,11 @@ + + + + + + +