mpmunasinghe commented on a change in pull request #1: Adding initial sources 
for Ballerina runtime
URL: 
https://github.com/apache/incubator-openwhisk-runtime-ballerina/pull/1#discussion_r204620060
 
 

 ##########
 File path: 
ballerina/proxy/src/main/java/org/ballerinalang/openwhisk/runtime/BallerinaProxy.java
 ##########
 @@ -0,0 +1,243 @@
+/*
+ * 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.ballerinalang.openwhisk.runtime;
+
+import com.google.gson.JsonObject;
+import org.ballerinalang.BLangProgramLoader;
+import org.ballerinalang.logging.BLogManager;
+import org.ballerinalang.model.values.BJSON;
+import org.ballerinalang.model.values.BValue;
+import org.ballerinalang.util.codegen.ProgramFile;
+import org.ballerinalang.util.exceptions.BLangRuntimeException;
+import org.ballerinalang.util.exceptions.ProgramFileFormatException;
+import org.ballerinalang.util.program.BLangFunctions;
+import org.wso2.msf4j.Request;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Field;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.logging.LogManager;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Response;
+
+/**
+ * OpenWhisk Ballerina Runtime Proxy Service
+ * Exposes /init and /run resources
+ */
+@Path("/") public class BallerinaProxy {
+    private ProgramFile programFile;
+    private String mainFunction = Constants.FUNCTION_CALLABLE_NAME;
+
+    @POST
+    @Path("init")
+    public Response init(@Context Request request) throws IOException {
+        Optional<ProgramFile> optionalValue = Optional.ofNullable(programFile);
+
+        // Check whether init being called before
+        if (optionalValue.isPresent()) {
+            System.err.println(Constants.INIT_ONCE_ERROR);
+            return buildResponse(Response.Status.BAD_GATEWAY, 
Constants.RESPONSE_ERROR, Constants.INIT_ONCE_ERROR);
+        }
+
+        InputStream balxIs = null;
+        try {
+            ((BLogManager) 
LogManager.getLogManager()).loadUserProvidedLogConfiguration();
+            JsonObject payload = BalxLoader.requestToJson(request);
+
+            if (payload.size() == 0) {
+                return buildResponse(Response.Status.INTERNAL_SERVER_ERROR, 
Constants.RESPONSE_ERROR,
+                                     Constants.FAILED_TO_LOCATE_BINARY);
+
+            }
+
+            JsonObject requestElements = 
payload.getAsJsonObject(Constants.JSON_VALUE);
+            Boolean isBinary = 
requestElements.get(Constants.BINARY).getAsBoolean();
+            String main = 
requestElements.get(Constants.FUNCTION_MAIN).getAsString();
+
+            if (!Constants.FUNCTION_MAIN.equals(main)) {
+                mainFunction = main;
+            }
+
+            // Check for binary value. .balx should be received with the 
binary parameter
+            if (isBinary) {
+                String base64Balx = 
requestElements.get(Constants.CODE).getAsString();
+
+                balxIs = new 
ByteArrayInputStream(base64Balx.getBytes(StandardCharsets.UTF_8));
+
+                java.nio.file.Path destinationPath = 
BalxLoader.saveBase64EncodedFile(balxIs);
+
+                programFile = BLangProgramLoader.read(destinationPath);
+
+                return buildResponse(Response.Status.OK, 
Constants.RESPONSE_SUCCESS, Constants.INIT_SUCCESS);
+            } else {
+                return buildResponse(Response.Status.INTERNAL_SERVER_ERROR, 
Constants.RESPONSE_ERROR,
+                                     Constants.FAILED_TO_LOCATE_BINARY);
+            }
+        } catch (ProgramFileFormatException | BLangRuntimeException e) {
+            return buildResponse(Response.Status.INTERNAL_SERVER_ERROR, 
Constants.RESPONSE_ERROR,
+                                 Constants.FAILED_TO_LOCATE_BINARY);
+        } catch (IOException e) {
+            return buildResponse(Response.Status.INTERNAL_SERVER_ERROR, 
Constants.RESPONSE_ERROR,
+                                 Constants.MISSING_MAIN_ERROR);
+        } finally {
+            if (balxIs != null) {
+                balxIs.close();
+            }
+        }
+    }
+
+    @POST
+    @Path("run")
+    public Response run(@Context Request request) {
+        Optional<ProgramFile> optionalValue = Optional.ofNullable(programFile);
+        JsonObject requestElements;
+        BValue[] result;
+
+        // Check whether init function has success and the program file is set
+        if (!optionalValue.isPresent()) {
+            return buildRunResponse(Response.Status.BAD_REQUEST, 
Constants.RESPONSE_ERROR,
+                                    Constants.FUNCTION_NOT_INITIALIZED);
+        }
+
+        requestElements = BalxLoader.requestToJson(request);
+
+        if (requestElements.size() == 0 || 
requestElements.getAsJsonObject(Constants.JSON_VALUE) == null) {
+            return buildRunResponse(Response.Status.BAD_REQUEST, 
Constants.RESPONSE_ERROR,
+                                    Constants.INVALID_INPUT_PARAMS);
+        }
+
+        //Preparing input parameters
+        BValue bJson = new 
BJSON(requestElements.getAsJsonObject(Constants.JSON_VALUE).toString());
+        BValue[] parameters = new BValue[1];
+        parameters[0] = bJson;
+
+        //Setting up runtime environment variables
+        augmentEnv(requestElements);
+
+        //Invoking the program file
+        try {
+            programFile = BalxLoader.initProgramFile(programFile);
+            result = BLangFunctions
+                    .invokeEntrypointCallable(programFile, 
programFile.getEntryPkgName(), mainFunction, parameters);
+        } catch (Exception e) {
+            return buildRunResponse(Response.Status.BAD_REQUEST, 
Constants.RESPONSE_ERROR,
+                                    Constants.FUNCTION_RUN_FAILURE);
+        }
+
+        //Preparing function response
+        StringBuilder response = new StringBuilder();
+        for (BValue bValue : result) {
+            if ("json".equals(bValue.getType().toString())) {
 
 Review comment:
   bValue is defined as json hence it is expected to Return the type String as 
json. Hence it is case sensitive

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to