Search in sources :

Example 1 with BalGenerationException

use of org.ballerinalang.net.grpc.exception.BalGenerationException in project ballerina by ballerina-lang.

the class BallerinaFileBuilder method build.

public void build() {
    try {
        InputStream targetStream = new ByteArrayInputStream(rootDescriptor);
        DescriptorProtos.FileDescriptorProto fileDescriptorSet = DescriptorProtos.FileDescriptorProto.parseFrom(targetStream);
        List<DescriptorProtos.DescriptorProto> messageTypeList = fileDescriptorSet.getMessageTypeList();
        List<DescriptorProtos.MethodDescriptorProto> methodList = fileDescriptorSet.getService(SERVICE_INDEX).getMethodList();
        String methodName;
        String reqMessageName;
        String resMessageName;
        String methodID;
        String packageName = EMPTY_STRING.equals(fileDescriptorSet.getPackage()) ? BalGenConstants.DEFAULT_PACKAGE : fileDescriptorSet.getPackage() + PACKAGE_SEPARATOR + BalGenConstants.DEFAULT_PACKAGE;
        ClientBuilder clientStubBal = new ClientBuilder(packageName, fileDescriptorSet.getService(SERVICE_INDEX).getName());
        DescriptorBuilder descriptorBuilder;
        String protoPackage = fileDescriptorSet.getPackage();
        if (EMPTY_STRING.equals(protoPackage)) {
            descriptorBuilder = new DescriptorBuilder(dependentDescriptors, fileDescriptorSet.getName(), clientStubBal);
        } else {
            descriptorBuilder = new DescriptorBuilder(dependentDescriptors, fileDescriptorSet.getPackage() + "." + fileDescriptorSet.getName(), clientStubBal);
        }
        descriptorBuilder.setRootDescriptor(rootDescriptor);
        descriptorBuilder.buildMap();
        descriptorBuilder.buildKey();
        for (DescriptorProtos.MethodDescriptorProto methodDescriptorProto : methodList) {
            MethodDescriptor.MethodType methodType = MessageUtils.getMethodType(methodDescriptorProto);
            String[] outputTypes = methodDescriptorProto.getOutputType().split(PACKAGE_SEPARATOR_REGEX);
            String typeOut = outputTypes[outputTypes.length - 1];
            String[] inputTypes = methodDescriptorProto.getInputType().split(PACKAGE_SEPARATOR_REGEX);
            String typeIn = inputTypes[inputTypes.length - 1];
            methodName = methodDescriptorProto.getName();
            if (!EMPTY_STRING.equals(fileDescriptorSet.getPackage())) {
                methodID = fileDescriptorSet.getPackage() + PACKAGE_SEPARATOR + fileDescriptorSet.getService(SERVICE_INDEX).getName() + FILE_SEPARATOR + methodName;
            } else {
                methodID = fileDescriptorSet.getService(SERVICE_INDEX).getName() + FILE_SEPARATOR + methodName;
            }
            reqMessageName = getMappingBalType(typeIn);
            resMessageName = getMappingBalType(typeOut);
            ActionBuilder.build(methodName, reqMessageName, resMessageName, methodID, methodType, clientStubBal);
        }
        for (DescriptorProtos.DescriptorProto descriptorProto : messageTypeList) {
            String[] attributesNameArr = new String[descriptorProto.getFieldCount()];
            String[] attributesTypeArr = new String[descriptorProto.getFieldCount()];
            int j = 0;
            for (DescriptorProtos.FieldDescriptorProto fieldDescriptorProto : descriptorProto.getFieldList()) {
                attributesNameArr[j] = fieldDescriptorProto.getName();
                attributesTypeArr[j] = !fieldDescriptorProto.getTypeName().equals("") ? fieldDescriptorProto.getTypeName().split(PACKAGE_SEPARATOR_REGEX)[fieldDescriptorProto.getTypeName().split(PACKAGE_SEPARATOR_REGEX).length - 1] : getTypeName(fieldDescriptorProto.getType().getNumber());
                j++;
            }
            clientStubBal.addStruct(descriptorProto.getName(), attributesNameArr, attributesTypeArr);
        }
        StubBuilder.build(clientStubBal, clientStubBal.isFunctionsUnaryNotEmpty());
        ClientStruct sampleClient = new ClientStruct(clientStubBal.isFunctionsStremingNotEmpty(), clientStubBal.isFunctionsUnaryNotEmpty(), fileDescriptorSet.getService(SERVICE_INDEX).getName(), packageName);
        if (this.balOutPath == null) {
            String path = balOutPathGenerator(packageName + PACKAGE_SEPARATOR + fileDescriptorSet.getService(SERVICE_INDEX).getName());
            writeBallerina(clientStubBal, DEFAULT_SKELETON_DIR, SKELETON_TEMPLATE_NAME, path + STUB_FILE_PRIFIX);
            writeBallerina(sampleClient, DEFAULT_SAMPLE_DIR, SAMPLE_TEMPLATE_NAME, path + SAMPLE_FILE_PRIFIX);
        } else {
            String path = this.balOutPath + FILE_SEPARATOR + fileDescriptorSet.getService(SERVICE_INDEX).getName();
            writeBallerina(clientStubBal, DEFAULT_SKELETON_DIR, SKELETON_TEMPLATE_NAME, path + STUB_FILE_PRIFIX);
            File sampleFile = new File(path + SAMPLE_FILE_PRIFIX);
            if (!sampleFile.isFile()) {
                Files.createFile(Paths.get(sampleFile.getAbsolutePath()));
            }
            writeBallerina(sampleClient, DEFAULT_SAMPLE_DIR, SAMPLE_TEMPLATE_NAME, path + SAMPLE_FILE_PRIFIX);
        }
    } catch (IOException e) {
        throw new BalGenerationException("Error while generating .bal file.", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ClientStruct(org.ballerinalang.net.grpc.builder.components.ClientStruct) BalGenerationException(org.ballerinalang.net.grpc.exception.BalGenerationException) DescriptorProtos(com.google.protobuf.DescriptorProtos) IOException(java.io.IOException) MethodDescriptor(io.grpc.MethodDescriptor) ByteArrayInputStream(java.io.ByteArrayInputStream) DescriptorBuilder(org.ballerinalang.net.grpc.builder.components.DescriptorBuilder) File(java.io.File) ClientBuilder(org.ballerinalang.net.grpc.builder.components.ClientBuilder)

Example 2 with BalGenerationException

use of org.ballerinalang.net.grpc.exception.BalGenerationException in project ballerina by ballerina-lang.

the class GrpcCmd method execute.

@Override
public void execute() {
    File protoFile = new File(protoPath);
    if (!protoFile.isFile() || !protoFile.exists() || EMPTY_STRING.equals(protoPath) || !protoPath.contains(".proto")) {
        String errorMessage = "Invalid proto file location. Please input valid profile location.";
        errStream.println(errorMessage);
        throw new BalGenToolException(errorMessage);
    }
    if (helpFlag) {
        String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(parentCmdParser, "build");
        outStream.println(commandUsageInfo);
        return;
    }
    try {
        downloadProtocexe();
    } catch (BalGenToolException e) {
        LOG.error("Error while generating protoc executable. ", e);
        throw new BalGenToolException("Error while generating protoc executable. ", e);
    }
    ClassLoader classLoader = this.getClass().getClassLoader();
    String descriptorPath = BalGenerationConstants.META_LOCATION + getProtoFileName() + "-descriptor.desc";
    File descFile = generateDescTempFolder(descriptorPath);
    StringBuilder msg = new StringBuilder();
    LOG.debug("Initializing the ballerina code generation.");
    List<String> protoFiles = readProperties(classLoader);
    for (String file : protoFiles) {
        try {
            exportResource(file, classLoader);
        } catch (Exception e) {
            msg.append("Error extracting resource file ").append(file).append(NEW_LINE_CHARACTER);
            outStream.println(msg.toString());
            LOG.error("Error exacting resource file " + file, e);
        }
    }
    msg.append("Successfully generated initial files.").append(NEW_LINE_CHARACTER);
    byte[] root = BalFileGenerationUtils.getProtoByteArray(this.exePath, this.protoPath, descFile.getAbsolutePath());
    LOG.debug("Successfully generated root descriptor.");
    List<byte[]> dependant;
    dependant = org.ballerinalang.protobuf.cmd.DescriptorsGenerator.generatedependentDescriptor(descriptorPath, this.protoPath, new ArrayList<>(), exePath, classLoader);
    LOG.debug("Successfully generated dependent descriptor.");
    // Path balPath = Paths.get(balOutPath);
    try {
        BallerinaFileBuilder ballerinaFileBuilder;
        // By this user can generate stub at different location
        if (EMPTY_STRING.equals(balOutPath)) {
            ballerinaFileBuilder = new BallerinaFileBuilder(dependant);
        } else {
            ballerinaFileBuilder = new BallerinaFileBuilder(dependant, balOutPath);
        }
        ballerinaFileBuilder.setRootDescriptor(root);
        ballerinaFileBuilder.build();
    } catch (BalGenerationException e) {
        LOG.error("Error generating ballerina file.", e);
        msg.append("Error generating ballerina file.").append(NEW_LINE_CHARACTER);
        outStream.println(msg.toString());
    }
    msg.append("Successfully generated ballerina file.").append(NEW_LINE_CHARACTER);
    // delete temporary meta files
    delete(new File("desc_gen"));
    delete(new File("google"));
    LOG.debug("Successfully deleted temporary files.");
    outStream.println(msg.toString());
}
Also used : BallerinaFileBuilder(org.ballerinalang.net.grpc.builder.BallerinaFileBuilder) ArrayList(java.util.ArrayList) BalGenerationException(org.ballerinalang.net.grpc.exception.BalGenerationException) File(java.io.File) BalFileGenerationUtils.saveFile(org.ballerinalang.protobuf.utils.BalFileGenerationUtils.saveFile) BalGenerationException(org.ballerinalang.net.grpc.exception.BalGenerationException) BalGenToolException(org.ballerinalang.protobuf.exception.BalGenToolException) IOException(java.io.IOException) BalGenToolException(org.ballerinalang.protobuf.exception.BalGenToolException)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 BalGenerationException (org.ballerinalang.net.grpc.exception.BalGenerationException)2 DescriptorProtos (com.google.protobuf.DescriptorProtos)1 MethodDescriptor (io.grpc.MethodDescriptor)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 BallerinaFileBuilder (org.ballerinalang.net.grpc.builder.BallerinaFileBuilder)1 ClientBuilder (org.ballerinalang.net.grpc.builder.components.ClientBuilder)1 ClientStruct (org.ballerinalang.net.grpc.builder.components.ClientStruct)1 DescriptorBuilder (org.ballerinalang.net.grpc.builder.components.DescriptorBuilder)1 BalGenToolException (org.ballerinalang.protobuf.exception.BalGenToolException)1 BalFileGenerationUtils.saveFile (org.ballerinalang.protobuf.utils.BalFileGenerationUtils.saveFile)1