in dbus-java/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java [942:1016]
public void disconnect() {
if (!isConnected()) { // already disconnected
return;
}
// if this is a shared connection, keep track of disconnect calls
if (shared) {
synchronized (CONNECTIONS) {
DBusConnection connection = CONNECTIONS.get(getAddress().getRawAddress());
if (connection != null) {
if (connection.getConcurrentConnections().get() <= 1) { // one left, this should be ourselfs
logger.debug("Disconnecting last remaining DBusConnection");
// Set all pending messages to have an error.
try {
Error err = new Error("org.freedesktop.DBus.Local", "org.freedesktop.DBus.Local.Disconnected",
0, "s", new Object[] {
"Disconnected"
});
cleanupPendingCalls(err, true);
synchronized (getPendingErrorQueue()) {
getPendingErrorQueue().add(err);
}
} catch (DBusException dbe) {
}
CONNECTIONS.remove(getAddress().getRawAddress());
super.disconnect();
} else {
logger.debug("Still {} connections left, decreasing connection counter", connection.getConcurrentConnections().get() -1);
connection.getConcurrentConnections().addAndGet(-1);
}
}
}
} else { // this is a standalone non-shared session, disconnect directly using super's implementation
IDisconnectAction beforeDisconnectAction = () -> {
// get all busnames from the list which matches the usual pattern
// this is required as the list also contains internal names like ":1.11"
// it is also required to put the results in a new list, otherwise we would get a
// concurrent modification exception later (calling releaseBusName() will modify the busnames List)
synchronized (busnames) {
List<String> lBusNames = busnames.stream()
.filter(busName -> busName != null && !(!busName.matches(BUSNAME_REGEX) || busName.length() > MAX_NAME_LENGTH))
.collect(Collectors.toList());
lBusNames.forEach(busName -> {
try {
releaseBusName(busName);
} catch (DBusException _ex) {
logger.error("Error while releasing busName '" + busName + "'.", _ex);
}
});
}
// remove all exported objects before disconnecting
Map<String, ExportedObject> exportedObjects = getExportedObjects();
synchronized (exportedObjects) {
List<String> exportedKeys = exportedObjects.keySet().stream().filter(f -> f != null).collect(Collectors.toList());
for (String key : exportedKeys) {
unExportObject(key);
}
}
};
super.disconnect(beforeDisconnectAction, null);
}
}