Skip to content

Commit a307a62

Browse files
App.java: code quality improvements
1 parent e9bb3ca commit a307a62

1 file changed

Lines changed: 20 additions & 27 deletions

File tree

  • src/main/java/com/testingbot/tunnel

src/main/java/com/testingbot/tunnel/App.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static void main(String... args) throws Exception {
113113
fastFail.setArgName("OPTIONS");
114114
options.addOption(fastFail);
115115

116-
Option metrics = OptionBuilder.withLongOpt("metrics-port").hasArg().withValueSeparator().withDescription("Use the specified port to access metrics. Default port 8003").create();
116+
Option metrics = Option.builder().longOpt("metrics-port").hasArg().valueSeparator().desc("Use the specified port to access metrics. Default port 8003").build();
117117
options.addOption(metrics);
118118

119119
Option proxy = new Option("Y", "proxy", true, "Specify an upstream proxy.");
@@ -125,8 +125,8 @@ public static void main(String... args) throws Exception {
125125
basicAuth.setArgName("host:port:user:passwd");
126126
options.addOption(basicAuth);
127127

128-
Option pac = OptionBuilder.withLongOpt("pac").hasArg().withDescription("Proxy autoconfiguration. Should be a http(s) URL").create();
129-
options.addOption(pac);
128+
Option pacOption = Option.builder().longOpt("pac").hasArg().desc("Proxy autoconfiguration. Should be a http(s) URL").build();
129+
options.addOption(pacOption);
130130

131131
Option proxyAuth = new Option("z", "proxy-userpwd", true, "Username and password required to access the proxy configured with --proxy.");
132132
proxyAuth.setArgName("user:pwd");
@@ -381,8 +381,8 @@ public void init() {
381381
public void run() {
382382
if (readyFile != null) {
383383
File f = new File(readyFile);
384-
if (f.exists()) {
385-
f.delete();
384+
if (f.exists() && !f.delete()) {
385+
Logger.getLogger(App.class.getName()).log(Level.WARNING, "Could not delete ready file: " + readyFile);
386386
}
387387
}
388388

@@ -510,11 +510,9 @@ private void startProxies() {
510510
if (f.exists()) {
511511
f.setLastModified(System.currentTimeMillis());
512512
} else {
513-
try {
514-
FileWriter fw = new FileWriter(f.getAbsoluteFile());
515-
try (BufferedWriter bw = new BufferedWriter(fw)) {
516-
bw.write("TestingBot Tunnel Ready");
517-
}
513+
try (FileWriter fw = new FileWriter(f.getAbsoluteFile());
514+
BufferedWriter bw = new BufferedWriter(fw)) {
515+
bw.write("TestingBot Tunnel Ready");
518516
} catch (IOException ex) {
519517
Logger.getLogger(App.class.getName()).log(Level.SEVERE, "Could not create readyfile. Please make sure the directory exists and we have permission write to this directory." , ex);
520518
}
@@ -546,18 +544,17 @@ public int getJettyPort() {
546544
}
547545

548546
public void setFreeJettyPort() {
549-
ServerSocket serverSocket;
550-
int port;
551-
try {
552-
serverSocket = _findAvailableSocket();
547+
try (ServerSocket serverSocket = _findAvailableSocket()) {
553548
if (serverSocket == null) {
549+
Logger.getLogger(App.class.getName()).log(Level.WARNING, "Could not find available port for Jetty, using default 8087");
550+
setJettyPort(8087);
554551
return;
555552
}
556-
557-
port = serverSocket.getLocalPort();
558-
serverSocket.close();
553+
int port = serverSocket.getLocalPort();
559554
setJettyPort(port);
560-
} catch (IOException ignored) {
555+
} catch (IOException e) {
556+
Logger.getLogger(App.class.getName()).log(Level.WARNING, "Error finding available port: " + e.getMessage());
557+
setJettyPort(8087);
561558
}
562559
}
563560

@@ -766,19 +763,15 @@ public void setBasicAuth(String[] basicAuth) {
766763
public int getSSHPort() {
767764
if (sshPort == 0) {
768765
// find available port
769-
ServerSocket serverSocket;
770-
int port;
771-
try {
772-
serverSocket = _findAvailableSocket();
766+
try (ServerSocket serverSocket = _findAvailableSocket()) {
773767
if (serverSocket == null) {
774768
sshPort = 4446;
775769
return sshPort;
776770
}
777-
778-
port = serverSocket.getLocalPort();
779-
serverSocket.close();
780-
sshPort = port;
781-
} catch (IOException ignored) {
771+
sshPort = serverSocket.getLocalPort();
772+
} catch (IOException e) {
773+
Logger.getLogger(App.class.getName()).log(Level.WARNING, "Error finding available SSH port: " + e.getMessage());
774+
sshPort = 4446;
782775
}
783776
}
784777
return sshPort;

0 commit comments

Comments
 (0)