Search in sources :

Example 1 with BalGenToolException

use of org.ballerinalang.protobuf.exception.BalGenToolException in project ballerina by ballerina-lang.

the class BalFileGenerationUtils method generateDescriptor.

/**
 * Execute command and generate file descriptor.
 *
 * @param command protoc executor command.
 */
public static void generateDescriptor(String command) {
    boolean isWindows = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).startsWith("windows");
    ProcessBuilder builder = new ProcessBuilder();
    if (isWindows) {
        builder.command("cmd.exe", "/c", "dir");
    } else {
        builder.command("sh", "-c", command);
    }
    builder.directory(new File(System.getProperty("user.home")));
    Process process;
    try {
        process = builder.start();
    } catch (IOException e) {
        throw new BalGenToolException("Error in executing protoc command '" + command + "'.", e);
    }
    try {
        process.waitFor();
    } catch (InterruptedException e) {
        throw new BalGenToolException("Process not successfully completed. Process is interrupted while" + " running the protoC executor.", e);
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) BalGenToolException(org.ballerinalang.protobuf.exception.BalGenToolException)

Example 2 with BalGenToolException

use of org.ballerinalang.protobuf.exception.BalGenToolException in project ballerina by ballerina-lang.

the class BalFileGenerationUtils method createMetaFolder.

/**
 * Create meta folder for storing meta files which is used in intermediate processing.
 *
 * @param folderPath folder path which is needed to be created.
 */
public static void createMetaFolder(String folderPath) {
    boolean isFileCreated = new File(folderPath).getParentFile().mkdirs();
    if (!isFileCreated) {
        LOG.debug("Meta folder did not create successfully '" + folderPath + "'");
    }
    byte[] dataBytes = new byte[0];
    try {
        Path file = Paths.get(folderPath);
        Files.write(file, dataBytes);
    } catch (IOException e) {
        throw new BalGenToolException("Error creating .desc meta files.", e);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) File(java.io.File) BalGenToolException(org.ballerinalang.protobuf.exception.BalGenToolException)

Example 3 with BalGenToolException

use of org.ballerinalang.protobuf.exception.BalGenToolException in project ballerina by ballerina-lang.

the class GrpcCmd method generateDescTempFolder.

/**
 * Generate the meta folder which needed for intermediate processing.
 *
 * @param descriptorPath proto descriptor path
 * @return Temporary Created meta file.
 */
private File generateDescTempFolder(String descriptorPath) {
    File descFile = new File(descriptorPath);
    String path = descFile.getAbsolutePath().substring(0, descFile.getAbsolutePath().lastIndexOf(BalGenerationConstants.FILE_SEPARATOR));
    File folderPath = new File(path);
    try {
        if (!folderPath.exists() && !folderPath.mkdirs()) {
            throw new IllegalStateException("Couldn't create dir: " + descFile);
        }
        descriptorPath = descFile.getAbsolutePath();
        byte[] data = new byte[0];
        Path file = Paths.get(descriptorPath);
        Files.write(file, data);
    } catch (IOException e) {
        throw new BalGenToolException("Error creating " + descriptorPath + " file.", e);
    }
    File targetFile = new File(PLUGIN_PROTO_FILEPATH);
    File parent1 = targetFile.getParentFile();
    File parent2 = targetFile.getParentFile().getParentFile();
    File parent3 = targetFile.getParentFile().getParentFile().getParentFile();
    if (!parent1.exists() && !parent1.mkdirs()) {
        throw new IllegalStateException("Couldn't create dir: " + parent1);
    }
    if (!parent2.exists() && !parent2.mkdirs()) {
        throw new IllegalStateException("Couldn't create dir: " + parent2);
    }
    if (!parent3.exists() && !parent3.mkdirs()) {
        throw new IllegalStateException("Couldn't create dir: " + parent3);
    }
    return descFile;
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) File(java.io.File) BalFileGenerationUtils.saveFile(org.ballerinalang.protobuf.utils.BalFileGenerationUtils.saveFile) BalGenToolException(org.ballerinalang.protobuf.exception.BalGenToolException)

Example 4 with BalGenToolException

use of org.ballerinalang.protobuf.exception.BalGenToolException in project ballerina by ballerina-lang.

the class GrpcCmd method readProperties.

private List<String> readProperties(ClassLoader classLoader) {
    String fileName;
    List<String> protoFilesList = new ArrayList<>();
    try (InputStream initialStream = classLoader.getResourceAsStream("standardProtos.properties");
        BufferedReader reader = new BufferedReader(new InputStreamReader(initialStream, StandardCharsets.UTF_8))) {
        while ((fileName = reader.readLine()) != null) {
            protoFilesList.add(fileName);
        }
    } catch (IOException e) {
        throw new BalGenToolException("Error in reading standardProtos.properties.", e);
    }
    return protoFilesList;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) BalGenToolException(org.ballerinalang.protobuf.exception.BalGenToolException)

Example 5 with BalGenToolException

use of org.ballerinalang.protobuf.exception.BalGenToolException in project ballerina by ballerina-lang.

the class DescriptorsGenerator method generatedependentDescriptor.

public static List<byte[]> generatedependentDescriptor(String parentDescPath, String parentProtoPath, List<byte[]> list, String exePath, ClassLoader classLoader) {
    File initialFile = new File(parentDescPath);
    try (InputStream targetStream = new FileInputStream(initialFile)) {
        DescriptorProtos.FileDescriptorSet descSet = DescriptorProtos.FileDescriptorSet.parseFrom(targetStream);
        for (String depPath : descSet.getFile(0).getDependencyList()) {
            String path = BalGenerationConstants.META_DEPENDENCY_LOCATION + depPath.substring(depPath.lastIndexOf(BalGenerationConstants.FILE_SEPARATOR), depPath.length()).replace(".proto", "") + ".desc";
            createMetaFolder(path);
            String protoPath;
            if (!depPath.contains(GOOGLE_STANDARD_LIB)) {
                protoPath = new File(new File(resolveProtoFloderPath(parentProtoPath)).toURI().getPath() + depPath).toURI().getPath();
            } else {
                // Get file from resources folder
                File dependentDesc = new File(META_LOCATION + depPath);
                File parentFile = dependentDesc.getParentFile();
                if (!parentFile.exists() && !parentFile.mkdirs()) {
                    throw new IllegalStateException("Couldn't create directory '" + META_LOCATION + depPath + "'");
                }
                try (InputStream initialStream = classLoader.getResourceAsStream(depPath);
                    OutputStream outStream = new FileOutputStream(dependentDesc)) {
                    byte[] buffer = new byte[initialStream.available()];
                    int read = initialStream.read(buffer);
                    if (read == -1) {
                        throw new IllegalStateException("Couldn't read input stream of 'google/protobuf'" + " resource: ");
                    }
                    outStream.write(buffer);
                    outStream.close();
                    protoPath = dependentDesc.getAbsolutePath();
                } catch (IOException e) {
                    throw new BalGenToolException("Error reading resource file '" + depPath + "'", e);
                }
            }
            String command = new ProtocCommandBuilder(exePath, protoPath, resolveProtoFloderPath(protoPath), new File(getDescriptorPath(depPath)).getAbsolutePath()).build();
            generateDescriptor(command);
            File childFile = new File(path);
            try (InputStream childStream = new FileInputStream(childFile)) {
                DescriptorProtos.FileDescriptorSet childDescSet = DescriptorProtos.FileDescriptorSet.parseFrom(childStream);
                if (childDescSet.getFile(0).getDependencyCount() != 0) {
                    List<byte[]> newList = new ArrayList<>();
                    generatedependentDescriptor(path, protoPath, newList, exePath, classLoader);
                } else {
                    initialFile = new File(path);
                    try (InputStream dependentStream = new FileInputStream(initialFile)) {
                        DescriptorProtos.FileDescriptorSet set = DescriptorProtos.FileDescriptorSet.parseFrom(dependentStream);
                        list.add(set.getFile(0).toByteArray());
                    } catch (IOException e) {
                        throw new BalGenToolException("Error reading dependent descriptor.", e);
                    }
                }
            } catch (IOException e) {
                throw new BalGenToolException("Error extracting dependent bal.", e);
            }
        }
    } catch (IOException e) {
        throw new BalGenToolException("Error parsing descriptor file " + initialFile, e);
    }
    return list;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) DescriptorProtos(com.google.protobuf.DescriptorProtos) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BalGenToolException(org.ballerinalang.protobuf.exception.BalGenToolException) FileOutputStream(java.io.FileOutputStream) ProtocCommandBuilder(org.ballerinalang.protobuf.utils.ProtocCommandBuilder) File(java.io.File)

Aggregations

IOException (java.io.IOException)10 BalGenToolException (org.ballerinalang.protobuf.exception.BalGenToolException)10 File (java.io.File)8 InputStream (java.io.InputStream)5 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 BalFileGenerationUtils.saveFile (org.ballerinalang.protobuf.utils.BalFileGenerationUtils.saveFile)3 DescriptorProtos (com.google.protobuf.DescriptorProtos)2 OutputStream (java.io.OutputStream)2 Path (java.nio.file.Path)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 BallerinaFileBuilder (org.ballerinalang.net.grpc.builder.BallerinaFileBuilder)1 BalGenerationException (org.ballerinalang.net.grpc.exception.BalGenerationException)1 ProtocCommandBuilder (org.ballerinalang.protobuf.utils.ProtocCommandBuilder)1