Skip to content

Commit

Permalink
fix findbugs warning in cli (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
dguy authored Oct 26, 2017
1 parent 6943a55 commit 0454344
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 31 deletions.
31 changes: 13 additions & 18 deletions ksql-cli/src/main/java/io/confluent/ksql/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -158,7 +159,7 @@ private void displayWelcomeMessage() {
// Math.min(terminal.getWidth(), helpReminderMessage.length())
int paddedLogoWidth = Math.min(terminal.getWidth(), helpReminderMessage.length());
int paddingWidth = (paddedLogoWidth - logoWidth) / 2;
String leftPadding = new String(new byte[paddingWidth]).replaceAll(".", " ");
String leftPadding = new String(new byte[paddingWidth], StandardCharsets.UTF_8).replaceAll(".", " ");
terminal.writer().printf("%s======================================%n", leftPadding);
terminal.writer().printf("%s= _ __ _____ ____ _ =%n", leftPadding);
terminal.writer().printf("%s= | |/ // ____|/ __ \\| | =%n", leftPadding);
Expand Down Expand Up @@ -318,7 +319,7 @@ private void handleStatements(String line)
String schemaFilePath = AstBuilder.unquote(runScriptContext.STRING().getText(), "'");
String fileContent;
try {
fileContent = new String(Files.readAllBytes(Paths.get(schemaFilePath)));
fileContent = new String(Files.readAllBytes(Paths.get(schemaFilePath)), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new KsqlException(" Could not read statements from file: " + schemaFilePath + ". "
+ "Details: " + e.getMessage(), e);
Expand Down Expand Up @@ -430,26 +431,20 @@ private void handlePrintedTopic(String printTopic)
restClient.makePrintTopicRequest(printTopic);

if (topicResponse.isSuccessful()) {
try (Scanner topicStreamScanner = new Scanner(topicResponse.getResponse())) {
Future<?> topicPrintFuture = queryStreamExecutorService.submit(new Runnable() {
@Override
public void run() {
while (topicStreamScanner.hasNextLine()) {
String line = topicStreamScanner.nextLine();
if (!line.isEmpty()) {
terminal.writer().println(line);
terminal.flush();
}
try (Scanner topicStreamScanner = new Scanner(topicResponse.getResponse(), StandardCharsets.UTF_8.name())) {
Future<?> topicPrintFuture = queryStreamExecutorService.submit(() -> {
while (topicStreamScanner.hasNextLine()) {
String line = topicStreamScanner.nextLine();
if (!line.isEmpty()) {
terminal.writer().println(line);
terminal.flush();
}
}
});

terminal.handle(Terminal.Signal.INT, new Terminal.SignalHandler() {
@Override
public void handle(Terminal.Signal signal) {
terminal.handle(Terminal.Signal.INT, Terminal.SignalHandler.SIG_IGN);
topicPrintFuture.cancel(true);
}
terminal.handle(Terminal.Signal.INT, signal -> {
terminal.handle(Terminal.Signal.INT, Terminal.SignalHandler.SIG_IGN);
topicPrintFuture.cancel(true);
});

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ private void addDefaultProperties(Properties properties) {

private void addFileProperties(Properties properties) throws IOException {
if (propertiesFile != null) {
properties.load(new FileInputStream(propertiesFile));
try(final FileInputStream input = new FileInputStream(propertiesFile)) {
properties.load(input);
}

if (properties.containsKey(KsqlConfig.KSQL_SERVICE_ID_CONFIG)) {
properties
.put(StreamsConfig.APPLICATION_ID_CONFIG,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ private Properties getStandaloneProperties() throws IOException {

private void addFileProperties(Properties properties) throws IOException {
if (propertiesFile != null) {
properties.load(new FileInputStream(propertiesFile));
try(final FileInputStream input = new FileInputStream(propertiesFile)) {
properties.load(input);
}
if (properties.containsKey(KsqlConfig.KSQL_SERVICE_ID_CONFIG)) {
properties
.put(StreamsConfig.APPLICATION_ID_CONFIG,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,8 @@
public class Standalone extends AbstractCliCommands {

private static final String PROPERTIES_FILE_OPTION_NAME = "--properties-file";

private static final String KAFKA_BOOTSTRAP_SERVER_OPTION_NAME = "--bootstrap-server";
private static final String KAFKA_BOOTSTRAP_SERVER_OPTION_DEFAULT = "localhost:9092";

private static final String APPLICATION_ID_OPTION_NAME = "--application-id";
private static final String APPLICATION_ID_OPTION_DEFAULT = "ksql_standalone_cli";

@Option(
name = PROPERTIES_FILE_OPTION_NAME,
description = "A file specifying properties for Ksql and its underlying Kafka Streams "
Expand Down Expand Up @@ -97,7 +92,9 @@ private void addDefaultProperties(Properties properties) {

private void addFileProperties(Properties properties) throws IOException {
if (propertiesFile != null) {
properties.load(new FileInputStream(propertiesFile));
try(final FileInputStream inputStream = new FileInputStream(propertiesFile)) {
properties.load(inputStream);
}
if (properties.containsKey(KsqlConfig.KSQL_SERVICE_ID_CONFIG)) {
properties
.put(StreamsConfig.APPLICATION_ID_CONFIG,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,7 @@ public void printHelp() {
writer().println("output:");
writer().println("\tView the current output format.");
writer().println("");
writer() .printf(
"output <format>:",
OutputFormat.VALID_FORMATS
);
writer().println("output <format>");
writer().println("");
writer() .printf(
"\tSet the output format to <format> (valid formats: %s)%n",
Expand Down
7 changes: 6 additions & 1 deletion ksql-cli/src/main/java/io/confluent/ksql/util/CliUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ public static String getLocalServerAddress(int portNumber) {

public static boolean createFile(Path path) {
try {
Files.createDirectories(path.getParent());
final Path parent = path.getParent();
if (parent == null) {
log.warn("Failed to create file as the parent was null. path: {}", path);
return false;
}
Files.createDirectories(parent);
if (Files.notExists(path)) {
Files.createFile(path);
}
Expand Down

0 comments on commit 0454344

Please sign in to comment.