+ command.handleCommand(ns, new SignalCreator() {
+ private List<Consumer<Manager>> onManagerAddedHandlers = new ArrayList<>();
+
+ @Override
+ public List<String> getAccountNumbers() {
+ synchronized (managers) {
+ return managers.stream().map(Manager::getSelfNumber).collect(Collectors.toList());
+ }
+ }
+
+ @Override
+ public void addManager(final Manager m) {
+ synchronized (managers) {
+ if (!managers.contains(m)) {
+ managers.add(m);
+ for (final var handler : onManagerAddedHandlers) {
+ handler.accept(m);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void addOnManagerAddedHandler(final Consumer<Manager> handler) {
+ onManagerAddedHandlers.add(handler);
+ }
+
+ @Override
+ public Manager getManager(final String phoneNumber) {
+ synchronized (managers) {
+ return managers.stream()
+ .filter(m -> m.getSelfNumber().equals(phoneNumber))
+ .findFirst()
+ .orElse(null);
+ }
+ }
+
+ @Override
+ public ProvisioningManager getNewProvisioningManager() {
+ return ProvisioningManager.init(dataPath, serviceEnvironment, BaseConfig.USER_AGENT);
+ }
+
+ @Override
+ public RegistrationManager getNewRegistrationManager(String username) throws IOException {
+ return RegistrationManager.init(username, dataPath, serviceEnvironment, BaseConfig.USER_AGENT);
+ }
+ }, outputWriter);
+ } finally {
+ synchronized (managers) {
+ for (var m : managers) {
+ try {
+ m.close();
+ } catch (IOException e) {
+ logger.warn("Cleanup failed", e);
+ }
+ }
+ managers.clear();