public void update(InputPortContext context) {
log.debug(String.format("%s - In port configuration has changed, updating connections", this));
// Copy the context in order to ensure that future changes via the
// observer will not effect this update.
final InputPortContext update = context.copy();
// All updates are run sequentially to prevent race conditions
// during configuration changes. Without essentially locking the
// object, it could be possible that connections are simultaneously
// added and removed or opened and closed on the object.
tasks.runTask(new Handler<Task>() {
@Override
public void handle(final Task task) {
// Iterate through existing connections and try to determine
// whether any of them have been removed from the network.
Iterator<InputConnection> iter = connections.iterator();
while (iter.hasNext()) {
final InputConnection connection = iter.next();
boolean exists = false;
for (InputConnectionContext input : update.connections()) {
if (input.address().equals(connection.address())) {
exists = true;
break;
}
}
// If a connection was removed from the network, close
// and remove the connection regardless of whether the
// close is actually successful.
if (!exists) {
connection.close(new Handler<AsyncResult<Void>>() {
@Override
public void handle(AsyncResult<Void> result) {
if (result.failed()) {
log.error(String.format("%s - Failed to close input connection: %s", DefaultInputPort.this, connection));
} else {
log.info(String.format("%s - Closed input connection: %s", DefaultInputPort.this, connection));
}
}
});
iter.remove();
}
}
// Now try to determine whether any connections were added to the network.
final List<InputConnection> newConnections = new ArrayList<>();
for (InputConnectionContext input : update.connections()) {
boolean exists = false;
for (InputConnection connection : connections) {
if (connection.address().equals(input.address())) {
exists = true;
break;