Skip to content

Commit ac8be8b

Browse files
committed
Separate closing the connection from shutting down the application
1 parent 8098eb8 commit ac8be8b

File tree

7 files changed

+39
-6
lines changed

7 files changed

+39
-6
lines changed

src/main/java/org/keepassxc/Connection.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public void run() {
9292
if (errorCount > MAX_ERROR_COUNT) {
9393
log.info("Too much errors - stopping MessagePublisher");
9494
doStop();
95+
try {
96+
terminateConnection();
97+
} catch (IOException e) {
98+
log.error(e.toString(), e.getCause());
99+
}
95100
reconnect();
96101
}
97102
}
@@ -715,6 +720,8 @@ public void setCredentials(Optional<Credentials> credentials) {
715720

716721
protected abstract boolean isConnected();
717722

723+
public abstract void terminateConnection() throws IOException;
724+
718725
@Override
719726
public abstract void close() throws Exception;
720727
}

src/main/java/org/keepassxc/LinuxMacConnection.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ protected boolean isConnected() {
123123
return null != socket && socket.isOpen();
124124
}
125125

126+
@Override
127+
public void terminateConnection() throws IOException {
128+
if (isConnected()) socket.close();
129+
}
130+
126131
@Override
127132
public void close() throws Exception {
128133
if (null != messagePublisher) messagePublisher.doStop();

src/main/java/org/keepassxc/WindowsConnection.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ protected boolean isConnected() {
9090
return null != pipe && pipe.isOpen();
9191
}
9292

93+
@Override
94+
public void terminateConnection() throws IOException {
95+
if (isConnected()) pipe.close();
96+
}
97+
9398
@Override
9499
public void close() throws Exception {
95100
if (null != messagePublisher) messagePublisher.doStop();

src/main/java/org/purejava/KeepassProxyAccess.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public KeepassProxyAccess() {
5959
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
6060
connection.removePropertyChangeListener(this);
6161
try {
62-
closeConnection();
62+
shutdown();
6363
} catch (Exception e) {
6464
log.error(e.toString(), e.getCause());
6565
}
@@ -495,16 +495,32 @@ public String getAssociateId() {
495495
return connection.getAssociateId();
496496
}
497497

498+
/**
499+
* Shut down the application and close the connection to the socket (for Linux and Mac)
500+
* or the named pipe (for Windows) respectively.
501+
*
502+
* @return True, in case the connection was shut down without an error, false otherwise.
503+
*/
504+
public boolean shutdown() {
505+
try {
506+
connection.close();
507+
return true;
508+
} catch (Exception e) {
509+
log.error(e.toString(), e.getCause());
510+
return false;
511+
}
512+
}
513+
498514
/**
499515
* Close the connection to the socket (for Linux and Mac) or the named pipe (for Windows) respectively.
500516
*
501517
* @return True, in case the connection was closed without an error, false otherwise.
502518
*/
503519
public boolean closeConnection() {
504520
try {
505-
connection.close();
521+
connection.terminateConnection();
506522
return true;
507-
} catch (Exception e) {
523+
} catch (IOException e) {
508524
log.error(e.toString(), e.getCause());
509525
return false;
510526
}

src/test/java/org/purejava/KeepassProxyAccessTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public class KeepassProxyAccessTest {
1818
@DisplayName("Testing availability of a socket to KeePassXC")
1919
public void shouldHaveNoErrors() {
2020
assertFalse(kpa.connect());
21-
assertTrue(kpa.closeConnection());
21+
assertTrue(kpa.shutdown());
2222
}
2323
}

src/test/java/org/purejava/LockedDatabaseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public class LockedDatabaseTest {
1818
public void shouldHaveNoErrors() {
1919
assertTrue(kpa.connect());
2020
assertTrue(kpa.getDatabasehash().isEmpty());
21-
assertTrue(kpa.closeConnection());
21+
assertTrue(kpa.shutdown());
2222
}
2323
}

src/test/java/org/purejava/UnlockedDatabaseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ public void shouldHaveNoErrors() throws InterruptedException {
5252
assertTrue(kpa.deleteEntry("2aafee1a89fd435c8bad7df12bbaaa3e"));
5353
log.info("Please deny to save changes");
5454
assertTrue(kpa.lockDatabase());
55-
assertTrue(kpa.closeConnection());
55+
assertTrue(kpa.shutdown());
5656
}
5757
}

0 commit comments

Comments
 (0)