Context: Hopefully fix rare NPE on Android (ticket #2092)

This commit is contained in:
zzz
2017-11-26 14:16:16 +00:00
parent 7a36b07cf7
commit fb4fb47ee3
5 changed files with 78 additions and 11 deletions

View File

@ -149,6 +149,26 @@ public class I2PAppContext {
return _globalAppContext;
}
/**
* Sets the default context, unless there is one already.
* NOT a public API, for use by RouterContext only, NOT for external use.
*
* @param ctx context constructed with doInit = false
* @return success (false if previously set)
* @since 0.9.33
*/
protected static boolean setGlobalContext(I2PAppContext ctx) {
synchronized (I2PAppContext.class) {
if (_globalAppContext == null) {
_globalAppContext = ctx;
return true;
}
}
System.out.println("Warning - New context not replacing old one, you now have a second one");
(new Exception("I did it")).printStackTrace();
return false;
}
/**
* Pull the default context, WITHOUT creating a new one.
* Use this in static methods used early in router initialization,
@ -190,10 +210,13 @@ public class I2PAppContext {
* additional resources and threads, and may be the cause of logging
* problems or hard-to-isolate bugs.
*
* NOT a public API, for use by RouterContext only, NOT for external use.
*
* @param doInit should this context be used as the global one (if necessary)?
* Will only apply if there is no global context now.
* @since protected since 0.9.33, NOT for external use
*/
private I2PAppContext(boolean doInit, Properties envProps) {
protected I2PAppContext(boolean doInit, Properties envProps) {
synchronized (I2PAppContext.class) {
_overrideProps = new I2PProperties();
if (envProps != null)
@ -311,12 +334,9 @@ public class I2PAppContext {
******/
if (doInit) {
if (_globalAppContext == null) {
_globalAppContext = this;
} else {
System.out.println("Warning - New context not replacing old one, you now have a second one");
(new Exception("I did it")).printStackTrace();
}
// Bad practice, sets a static field to this in constructor.
// doInit will be false when instantiated via Router.
setGlobalContext(this);
}
} // synch
}