Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay initialization of the UserPreferencesManager. #4005

Merged
merged 2 commits into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions java/src/jmri/InstanceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,26 @@
* {@link InstanceManager#getNullableDefault} method instead.
* <p>
* Multiple items can be held, and are retrieved as a list with
* {@link InstanceManager#getList}.
* {@link InstanceManager#getList}.
* <p>
* If a specific item is needed, e.g. one that has been constructed via a
* complex process during startup, it should be installed with
* {@link InstanceManager#store}.
* <p>
* If it's OK for the InstanceManager to create an object on first request, have
* that object's class implement the {@link InstanceManagerAutoDefault} flag
* interface. The InstanceManager will then construct a default object via the
* no-argument constructor when one is first requested.
* If it is desirable for the InstanceManager to create an object on first
* request, have that object's class implement the
* {@link InstanceManagerAutoDefault} flag interface. The InstanceManager will
* then construct a default object via the no-argument constructor when one is
* first requested.
* <p>
* For initialization of more complex default objects, see the
* {@link InstanceInitializer} mechanism and its default implementation in
* {@link jmri.managers.DefaultInstanceInitializer}.
* <p>
* Implement the {@link InstanceManagerAutoInitialize} interface when default
* objects need to be initialized after the default instance has been
* constructed and registered with the InstanceManager. This will allow
* references to the default instance during initialization to work as expected.
* <hr>
* This file is part of JMRI.
* <P>
Expand Down Expand Up @@ -218,6 +224,7 @@ static public <T> T getNullableDefault(@Nonnull Class<T> type) {
try {
T obj = (T) type.getConstructor((Class[]) null).newInstance((Object[]) null);
l.add(obj);
// obj has been added, now initialize it if needed
if (obj instanceof InstanceManagerAutoInitialize) {
((InstanceManagerAutoInitialize) obj).initialize();
}
Expand All @@ -236,6 +243,7 @@ static public <T> T getNullableDefault(@Nonnull Class<T> type) {
T obj = (T) getDefault().initializers.get(type).getDefault(type);
log.debug(" initializer created default of {}", type.getName());
l.add(obj);
// obj has been added, now initialize it if needed
if (obj instanceof InstanceManagerAutoInitialize) {
((InstanceManagerAutoInitialize) obj).initialize();
}
Expand Down
12 changes: 8 additions & 4 deletions java/src/jmri/managers/JmriUserPreferencesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import jmri.ConfigureManager;
import jmri.InstanceInitializer;
import jmri.InstanceManager;
import jmri.InstanceManagerAutoInitialize;
import jmri.JmriException;
import jmri.UserPreferencesManager;
import jmri.beans.Bean;
Expand Down Expand Up @@ -57,7 +58,7 @@
*
* @author Randall Wood (C) 2016
*/
public class JmriUserPreferencesManager extends Bean implements UserPreferencesManager {
public class JmriUserPreferencesManager extends Bean implements UserPreferencesManager, InstanceManagerAutoInitialize {

public final static String SAVE_ALLOWED = "saveAllowed";

Expand Down Expand Up @@ -1218,6 +1219,11 @@ private void savePreferences() {
});
}

@Override
public void initialize() {
this.readUserPreferences();
}

/**
* Holds details about the specific class.
*/
Expand Down Expand Up @@ -1449,9 +1455,7 @@ public static class Initializer extends AbstractInstanceInitializer {
@Override
public <T> Object getDefault(Class<T> type) throws IllegalArgumentException {
if (type.equals(UserPreferencesManager.class)) {
JmriUserPreferencesManager instance = new JmriUserPreferencesManager();
instance.readUserPreferences();
return instance;
return new JmriUserPreferencesManager();
}
return super.getDefault(type);
}
Expand Down