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);
}
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;
}
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);
}
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;
}
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;
}
Aggregations