Search in sources :

Example 16 with OpenOption

use of java.nio.file.OpenOption in project winery by eclipse.

the class Generator method generateJavaFile.

private void generateJavaFile(File javaService) throws IOException {
    // Generate methods
    StringBuilder sb = new StringBuilder();
    for (TOperation op : this.tInterface.getOperations()) {
        // Annotations
        sb.append("\t@WebMethod\n");
        sb.append("\t@SOAPBinding\n");
        sb.append("\t@Oneway\n");
        // Signatur
        String operationReturn = "void";
        sb.append("\tpublic ").append(operationReturn).append(" ").append(op.getName()).append("(\n");
        // Parameter
        boolean first = true;
        if (op.getInputParameters() != null) {
            for (TParameter parameter : op.getInputParameters()) {
                String parameterName = parameter.getName();
                if (first) {
                    first = false;
                    sb.append("\t\t");
                } else {
                    sb.append(",\n\t\t");
                }
                // Generate @WebParam
                sb.append("@WebParam(name=\"").append(parameterName).append("\", targetNamespace=\"").append(this.namespace).append("\") ");
                sb.append("String ").append(parameterName);
            }
        }
        sb.append("\n\t) {\n");
        // If there are output parameters we generate the respective HashMap
        boolean outputParamsExist = (op.getOutputParameters() != null) && (!op.getOutputParameters().isEmpty());
        if (outputParamsExist) {
            sb.append("\t\t// This HashMap holds the return parameters of this operation.\n");
            sb.append("\t\tfinal HashMap<String,String> returnParameters = new HashMap<String, String>();\n\n");
        }
        sb.append("\t\t// TODO: Implement your operation here.\n");
        // Generate code to set output parameters
        if (outputParamsExist) {
            for (TParameter outputParam : op.getOutputParameters()) {
                sb.append("\n\n\t\t// Output Parameter '").append(outputParam.getName()).append("' ");
                if (outputParam.getRequired()) {
                    sb.append("(required)");
                } else {
                    sb.append("(optional)");
                }
                sb.append("\n\t\t// TODO: Set ").append(outputParam.getName()).append(" parameter here.");
                sb.append("\n\t\t// Do NOT delete the next line of code. Set \"\" as value if you want to return nothing or an empty result!");
                sb.append("\n\t\treturnParameters.put(\"").append(outputParam.getName()).append("\", \"TODO\");");
            }
            sb.append("\n\n\t\tsendResponse(returnParameters);\n");
        }
        sb.append("\t}\n\n");
    }
    // Read file and replace placeholders
    Charset cs = Charset.defaultCharset();
    List<String> lines = new ArrayList<>();
    for (String line : Files.readAllLines(javaService.toPath(), cs)) {
        // Replace web service method
        line = line.replaceAll(Generator.PLACEHOLDER_GENERATED_WEBSERVICE_METHODS, sb.toString());
        lines.add(line);
    }
    // Write file
    OpenOption[] options = new OpenOption[] { StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING };
    Files.write(javaService.toPath(), lines, cs, options);
}
Also used : OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) TOperation(org.eclipse.winery.model.tosca.TOperation) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) TParameter(org.eclipse.winery.model.tosca.TParameter)

Example 17 with OpenOption

use of java.nio.file.OpenOption in project ballerina by ballerina-lang.

the class TestUtil method openForReading.

/**
 * Opens a channel from a specified file.
 *
 * @param filePath the path to the file.
 * @return the file channel.
 * @throws IOException during I/O error.
 * @throws URISyntaxException during failure to validate uri syntax.
 */
public static ByteChannel openForReading(String filePath) throws IOException, URISyntaxException {
    Set<OpenOption> opts = new HashSet<>();
    opts.add(StandardOpenOption.READ);
    URL fileResource = TestUtil.class.getClassLoader().getResource(filePath);
    ByteChannel channel = null;
    if (null != fileResource) {
        Path path = Paths.get(fileResource.toURI());
        channel = Files.newByteChannel(path, opts);
    }
    return channel;
}
Also used : Path(java.nio.file.Path) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) ByteChannel(java.nio.channels.ByteChannel) URL(java.net.URL) HashSet(java.util.HashSet)

Example 18 with OpenOption

use of java.nio.file.OpenOption in project ballerina by ballerina-lang.

the class TestUtil method openForWriting.

/**
 * Opens a file for writing.
 *
 * @param filePath the path the file should be opened for writing.
 * @return the writable channel.
 * @throws IOException during I/O error.
 */
public static ByteChannel openForWriting(String filePath) throws IOException {
    Set<OpenOption> opts = new HashSet<>();
    opts.add(StandardOpenOption.CREATE);
    opts.add(StandardOpenOption.WRITE);
    Path path = Paths.get(filePath);
    return Files.newByteChannel(path, opts);
}
Also used : Path(java.nio.file.Path) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) HashSet(java.util.HashSet)

Example 19 with OpenOption

use of java.nio.file.OpenOption in project ballerina by ballerina-lang.

the class OpenFile method inFlow.

/**
 * {@inheritDoc}
 */
@Override
public Channel inFlow(Context context) throws BallerinaException {
    String pathUrl = context.getStringArgument(PATH_FIELD_INDEX);
    String accessMode = context.getStringArgument(FILE_ACCESS_MODE_INDEX);
    Path path = null;
    Channel channel;
    try {
        String accessLC = accessMode.toLowerCase(Locale.getDefault());
        path = Paths.get(pathUrl);
        Set<OpenOption> opts = new HashSet<>();
        if (accessLC.contains("r")) {
            if (!Files.exists(path)) {
                throw new BallerinaException("file not found: " + path);
            }
            if (!Files.isReadable(path)) {
                throw new BallerinaException("file is not readable: " + path);
            }
            opts.add(StandardOpenOption.READ);
        }
        boolean write = accessLC.contains("w");
        boolean append = accessLC.contains("a");
        if (write || append) {
            if (Files.exists(path) && !Files.isWritable(path)) {
                throw new BallerinaException("file is not writable: " + path);
            }
            createDirs(path);
            opts.add(StandardOpenOption.CREATE);
            if (append) {
                opts.add(StandardOpenOption.APPEND);
            } else {
                opts.add(StandardOpenOption.WRITE);
            }
        }
        FileChannel byteChannel = FileChannel.open(path, opts);
        // channel = new FileIOChannel(byteChannel, IOConstants.CHANNEL_BUFFER_SIZE);
        channel = new FileIOChannel(byteChannel);
    } catch (AccessDeniedException e) {
        throw new BallerinaException("Do not have access to write file: " + path, e);
    } catch (Throwable e) {
        throw new BallerinaException("failed to open file: " + e.getMessage(), e);
    }
    return channel;
}
Also used : Path(java.nio.file.Path) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) AccessDeniedException(java.nio.file.AccessDeniedException) FileChannel(java.nio.channels.FileChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) FileIOChannel(org.ballerinalang.nativeimpl.io.channels.FileIOChannel) AbstractNativeChannel(org.ballerinalang.nativeimpl.io.channels.AbstractNativeChannel) FileChannel(java.nio.channels.FileChannel) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) FileIOChannel(org.ballerinalang.nativeimpl.io.channels.FileIOChannel) HashSet(java.util.HashSet)

Example 20 with OpenOption

use of java.nio.file.OpenOption in project ballerina by ballerina-lang.

the class OpenChannel method inFlow.

/**
 * {@inheritDoc}
 */
@Override
public Channel inFlow(Context context) throws BallerinaException {
    BStruct fileStruct = (BStruct) context.getRefArgument(FILE_CHANNEL_INDEX);
    String accessMode = context.getStringArgument(FILE_ACCESS_MODE_INDEX);
    Path path = null;
    Channel channel;
    try {
        String accessLC = accessMode.toLowerCase(Locale.getDefault());
        path = Paths.get(fileStruct.getStringField(PATH_FIELD_INDEX));
        Set<OpenOption> opts = new HashSet<>();
        if (accessLC.contains("r")) {
            if (!Files.exists(path)) {
                throw new BallerinaException("file not found: " + path);
            }
            if (!Files.isReadable(path)) {
                throw new BallerinaException("file is not readable: " + path);
            }
            opts.add(StandardOpenOption.READ);
        }
        boolean write = accessLC.contains("w");
        boolean append = accessLC.contains("a");
        if (write || append) {
            if (Files.exists(path) && !Files.isWritable(path)) {
                throw new BallerinaException("file is not writable: " + path);
            }
            createDirs(path);
            opts.add(StandardOpenOption.CREATE);
            if (append) {
                opts.add(StandardOpenOption.APPEND);
            } else {
                opts.add(StandardOpenOption.WRITE);
            }
        }
        FileChannel byteChannel = FileChannel.open(path, opts);
        channel = new FileIOChannel(byteChannel);
    } catch (AccessDeniedException e) {
        throw new BallerinaException("Do not have access to write file: " + path, e);
    } catch (Throwable e) {
        throw new BallerinaException("failed to open file: " + e.getMessage(), e);
    }
    return channel;
}
Also used : Path(java.nio.file.Path) BStruct(org.ballerinalang.model.values.BStruct) AccessDeniedException(java.nio.file.AccessDeniedException) FileChannel(java.nio.channels.FileChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) FileIOChannel(org.ballerinalang.nativeimpl.io.channels.FileIOChannel) AbstractNativeChannel(org.ballerinalang.nativeimpl.io.channels.AbstractNativeChannel) FileChannel(java.nio.channels.FileChannel) FileIOChannel(org.ballerinalang.nativeimpl.io.channels.FileIOChannel) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) HashSet(java.util.HashSet)

Aggregations

OpenOption (java.nio.file.OpenOption)102 StandardOpenOption (java.nio.file.StandardOpenOption)76 IOException (java.io.IOException)41 HashSet (java.util.HashSet)34 Path (java.nio.file.Path)31 File (java.io.File)19 FileChannel (java.nio.channels.FileChannel)19 Test (org.junit.Test)18 NoSuchFileException (java.nio.file.NoSuchFileException)16 OutputStream (java.io.OutputStream)12 ArrayList (java.util.ArrayList)11 InputStream (java.io.InputStream)10 SeekableByteChannel (java.nio.channels.SeekableByteChannel)10 FileIO (org.apache.ignite.internal.processors.cache.persistence.file.FileIO)10 FileIODecorator (org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator)10 ByteBuffer (java.nio.ByteBuffer)9 Set (java.util.Set)8 FileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory)8 RandomAccessFileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory)8 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)7