Skip to content
Permalink
Browse files

fix: accept early registration of Internal connection

Because the Internal system connection can get registered early now that all managers need a memo, the ManagerDefaultSelector needs to be more tolerant of it being the first manager registered, so do the forced setting of defaults if first manager registered is not internal or if second manager registered is external when first is internal.
  • Loading branch information...
rhwood committed Jul 22, 2019
1 parent c6f8378 commit a44e3d951b7f7a44a83137015ce06c2e36f7a8bd
@@ -40,10 +40,10 @@
* {@code true}, that a non-Internal connection (other than type None in the
* preferences window) is the default for at least one type of manager.
* <p>
* allInternalDefaults is preserved as a preference when set here,
* but {@link #setAllInternalDefaultsValid} is not (originally)
* invoked from the GUI.
*
* allInternalDefaults is preserved as a preference when set here, but
* {@link #setAllInternalDefaultsValid} is not (originally) invoked from the
* GUI.
*
* @author Bob Jacobsen Copyright (C) 2010
* @author Randall Wood Copyright (C) 2015, 2017
* @since 2.9.4
@@ -115,13 +115,16 @@ public ManagerDefaultSelector() {
// check for special case of anything else then Internal
// and set first system to be default for all provided defaults
List<SystemConnectionMemo> list = InstanceManager.getList(SystemConnectionMemo.class);

log.debug("Start CONNECTION_ADDED processing with {} existing", list.size());
if (list.size() > 0) log.debug(" System 0: {}", list.get(0));
if (list.size() > 1) log.debug(" System 1: {}", list.get(1));
if (list.size() > 2) log.debug(" System 2: {}", list.get(2));

if (list.size() == 1 && ! (list.get(0) instanceof InternalSystemConnectionMemo)) {

if (log.isDebugEnabled()) {
log.debug("Start CONNECTION_ADDED processing with {} existing", list.size());
for (int i = 0; i < list.size(); i++) {
log.debug(" System {}: {}", i, list.get(i));
}
}

if ((list.size() == 1 && !(list.get(0) instanceof InternalSystemConnectionMemo)) ||
(list.size() == 2 && !(list.get(0) instanceof InternalSystemConnectionMemo) && list.get(1) instanceof InternalSystemConnectionMemo)) {
// first system added is hardware, gets defaults for everything it supports
log.debug("First real system added, reset defaults");
for (Item item : knownManagers) {
@@ -183,7 +186,7 @@ public String getDefault(Class<?> managerClass) {
/**
* Record the userName of the system that provides the default instance for
* a specific class.
*
* <p>
* To ensure compatibility of different preference versions, only classes
* that are current registered are preserved. This way, reading in an old
* file will just have irrelevant items ignored.
@@ -332,13 +335,13 @@ private boolean isPreferencesValid(Profile profile, List<SystemConnectionMemo> c
return true;
}
boolean usesExternalConnections = false;

// classes of managers being provided, and set of which SystemConnectionMemos can provide each
Map<Class<?>, Set<SystemConnectionMemo>> providing = new HashMap<>();

// list of all external providers (i.e. SystemConnectionMemos) that provide at least one known manager type
Set<SystemConnectionMemo> providers = new HashSet<>();

if (connections.size() > 1) {
connections.stream().filter((memo) -> (!(memo instanceof InternalSystemConnectionMemo))).forEachOrdered((memo) -> {
// populate providers by adding all external (non-internal) connections that provide at least one default
@@ -361,7 +364,7 @@ private boolean isPreferencesValid(Profile profile, List<SystemConnectionMemo> c
}
}
});

if (log.isDebugEnabled()) {
// avoid unneeded overhead of looping through providers
providing.forEach((cls, clsProviders) -> {
@@ -371,7 +374,7 @@ private boolean isPreferencesValid(Profile profile, List<SystemConnectionMemo> c
});
});
}

for (SystemConnectionMemo memo : providers) {
if (providing.keySet().stream().filter((cls) -> {
Set<SystemConnectionMemo> provides = providing.get(cls);
@@ -384,7 +387,7 @@ private boolean isPreferencesValid(Profile profile, List<SystemConnectionMemo> c
log.trace("memo stream returns true because there's no default defined and an external provider exists");
return true;
}
log.trace("memo stream returns {} due to memo.getUserName() {} and {}",(memo.getUserName().equals(defaults.get(cls))), memo.getUserName(), defaults.get(cls));
log.trace("memo stream returns {} due to memo.getUserName() {} and {}", (memo.getUserName().equals(defaults.get(cls))), memo.getUserName(), defaults.get(cls));
return memo.getUserName().equals(defaults.get(cls));
})) {
log.trace("setting usesExternalConnections true");
@@ -1,5 +1,6 @@
package jmri.managers;

import jmri.InstanceManager;
import jmri.PowerManager;
import jmri.jmrix.internal.InternalSystemConnectionMemo;
import jmri.jmrix.loconet.LnCommandStationType;
@@ -67,9 +68,8 @@ public void testInitialPreferencesValid() throws InitializationException {

private LocoNetSystemConnectionMemo getLocoNetTestConnection() {
// create a test loconet connection
LnTrafficController lnis = new LocoNetInterfaceScaffold();
LocoNetSystemConnectionMemo memo = new LocoNetSystemConnectionMemo();
lnis.setSystemConnectionMemo(memo);
LnTrafficController lnis = new LocoNetInterfaceScaffold(memo);
memo.setLnTrafficController(lnis);
memo.configureCommandStation(LnCommandStationType.COMMAND_STATION_DCS100, false, false, false);
return memo;
@@ -148,8 +148,8 @@ public void testAuxInternalPreferencesValid() throws InitializationException {
}, "Registration Complete");
new org.netbeans.jemmy.QueueTool().waitEmpty(20);

// add Internal as a second connection
InternalSystemConnectionMemo internal = new InternalSystemConnectionMemo(false); // self registering
// get existing Internal connection
InternalSystemConnectionMemo internal = InstanceManager.getDefault(InternalSystemConnectionMemo.class); // self registering

// wait for notifications
JUnitUtil.waitFor(() -> {

0 comments on commit a44e3d9

Please sign in to comment.
You can’t perform that action at this time.