public class I2PAppContext extends Object
Provide a base scope for accessing singletons that I2P exposes. Rather than using the traditional singleton, where any component can access the component in question directly, all of those I2P related singletons are exposed through a particular I2PAppContext. This helps not only with understanding their use and the components I2P exposes, but it also allows multiple isolated environments to operate concurrently within the same JVM - particularly useful for stubbing out implementations of the rooted components and simulating the software's interaction between multiple instances.
As a simplification, there is also a global context - if some component needs access to one of the singletons but doesn't have its own context from which to root itself, it binds to the I2PAppContext's globalAppContext(), which is the first context that was created within the JVM, or a new one if no context existed already. This functionality is often used within the I2P core for logging - e.g.
private static final Log _log = new Log(someClass.class);
It is for this reason that applications that care about working with multiple
contexts should build their own context as soon as possible (within the main(..))
so that any referenced components will latch on to that context instead of
instantiating a new one. However, there are situations in which both can be
relevant.| Modifier and Type | Field and Description |
|---|---|
protected Clock |
_clock |
protected boolean |
_clockInitialized |
protected static I2PAppContext |
_globalAppContext
the context that components without explicit root are bound
|
protected KeyRing |
_keyRing |
protected boolean |
_keyRingInitialized |
protected I2PProperties |
_overrideProps |
protected SessionKeyManager |
_sessionKeyManager |
protected boolean |
_sessionKeyManagerInitialized |
protected Set<Runnable> |
_shutdownTasks |
| Modifier | Constructor and Description |
|---|---|
|
I2PAppContext()
Create a brand new context.
|
protected |
I2PAppContext(boolean doInit,
Properties envProps)
Create a brand new context.
|
|
I2PAppContext(Properties envProps)
Create a brand new context.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addPropertyCallback(I2PProperties.I2PPropertyCallback callback)
Add a callback, which will fire upon changes in the property
given in the specific callback.
|
void |
addShutdownTask(Runnable task)
WARNING - Shutdown tasks are not executed in an I2PAppContext.
|
AESEngine |
aes()
Ok, I'll admit it, there is no good reason for having a context specific AES engine.
|
ClientAppManager |
clientAppManager()
As of 0.9.30, returns non-null in I2PAppContext, null in RouterContext.
|
Clock |
clock()
The context's synchronized clock, which is kept context specific only to
enable simulators to play with clock skew among different instances.
|
void |
deleteTempDir()
don't rely on deleteOnExit()
|
DSAEngine |
dsa()
Our DSA engine (see HMAC and SHA above)
|
net.i2p.crypto.ElGamalEngine |
elGamalEngine()
This is the ElGamal engine used within this context.
|
File |
getAppDir()
Where applications may store data.
|
File |
getBaseDir()
This is the installation dir, often referred to as $I2P.
|
boolean |
getBooleanProperty(String propName)
Default false
|
boolean |
getBooleanPropertyDefaultTrue(String propName)
Get a boolean property, defaulting to true if not set.
|
File |
getConfigDir()
The base dir for config files.
|
static I2PAppContext |
getCurrentContext()
Pull the default context, WITHOUT creating a new one.
|
long |
getEstimatedDowntime()
How long this router was down before it started, or 0 if unknown.
|
static I2PAppContext |
getGlobalContext()
Pull the default context, creating a new one if necessary, else using
the first one created.
|
File |
getLibDir()
This is the library dir, which is usually $I2P/lib.
|
File |
getLogDir()
Where the router keeps its log directory.
|
File |
getPIDDir()
Where router.ping goes.
|
Properties |
getProperties()
Access the configuration attributes of this context, listing the properties
provided during the context construction, as well as the ones included in
System.getProperties.
|
String |
getProperty(String propName)
Access the configuration attributes of this context, using properties
provided during the context construction, or falling back on
System.getProperty if no properties were provided during construction
(or the specified prop wasn't included).
|
boolean |
getProperty(String propName,
boolean defaultVal)
Return a boolean with a boolean default
|
float |
getProperty(String propName,
float defaultVal)
Return a float with a float default
|
int |
getProperty(String propName,
int defaultVal)
Return an int with an int default
|
long |
getProperty(String propName,
long defaultVal)
Return a long with a long default
|
String |
getProperty(String propName,
String defaultValue)
Access the configuration attributes of this context, using properties
provided during the context construction, or falling back on
System.getProperty if no properties were provided during construction
(or the specified prop wasn't included).
|
Set<String> |
getPropertyNames()
Access the configuration attributes of this context, listing the properties
provided during the context construction, as well as the ones included in
System.getProperties.
|
File |
getRouterDir()
Where the router keeps its files.
|
Set<Runnable> |
getShutdownTasks()
Get the list of shutdown tasks.
|
File |
getTempDir()
Where anybody may store temporary data.
|
boolean |
hasWrapper()
Is the wrapper present?
|
HMAC256Generator |
hmac256()
Un-deprecated in 0.9.38
|
protected void |
initializeClock() |
protected void |
initializeKeyRing() |
protected void |
initializeSessionKeyManager() |
InternalClientManager |
internalClientManager()
Use this to connect to the router in the same JVM.
|
boolean |
isRouterContext()
Use this instead of context instanceof RouterContext
|
KeyGenerator |
keyGenerator()
Component to generate ElGamal, DSA, and Session keys.
|
KeyRing |
keyRing()
Basic hash map
|
LogManager |
logManager()
Query the log manager for this context, which may in turn have its own
set of configuration settings (loaded from the context's properties).
|
NamingService |
namingService()
Pull up the naming service used in this context.
|
PortMapper |
portMapper()
Basic mapping from service names to ports
|
RandomSource |
random()
[insert snarky comment here]
|
void |
removeShutdownTask(Runnable task)
Remove a shutdown task.
|
RoutingKeyGenerator |
routingKeyGenerator()
Determine how much do we want to mess with the keys to turn them
into something we can route.
|
SessionKeyManager |
sessionKeyManager()
The session key manager which coordinates the sessionKey / sessionTag
data.
|
protected static boolean |
setGlobalContext(I2PAppContext ctx)
Sets the default context, unless there is one already.
|
void |
setLogManager(LogManager logManager)
Overwrites the LogManager instance to be used by the router.
|
SHA256Generator |
sha()
Our SHA256 instance (see the hmac discussion for why its context specific)
|
SimpleTimer2 |
simpleTimer2()
Use instead of SimpleTimer2.getInstance()
|
StatManager |
statManager()
The statistics component with which we can track various events
over time.
|
protected Clock _clock
protected volatile boolean _clockInitialized
protected static volatile I2PAppContext _globalAppContext
protected KeyRing _keyRing
protected volatile boolean _keyRingInitialized
protected final I2PProperties _overrideProps
protected SessionKeyManager _sessionKeyManager
protected volatile boolean _sessionKeyManagerInitialized
public I2PAppContext()
protected I2PAppContext(boolean doInit,
Properties envProps)
doInit - should this context be used as the global one (if necessary)?
Will only apply if there is no global context now.public I2PAppContext(Properties envProps)
public void addPropertyCallback(I2PProperties.I2PPropertyCallback callback)
callback - The implementation of the callback.public void addShutdownTask(Runnable task)
public AESEngine aes()
public ClientAppManager clientAppManager()
public Clock clock()
public void deleteTempDir()
public DSAEngine dsa()
public net.i2p.crypto.ElGamalEngine elGamalEngine()
public File getAppDir()
public File getBaseDir()
public boolean getBooleanProperty(String propName)
public boolean getBooleanPropertyDefaultTrue(String propName)
propName - the property namepublic File getConfigDir()
public static I2PAppContext getCurrentContext()
public long getEstimatedDowntime()
public static I2PAppContext getGlobalContext()
public File getLibDir()
public File getLogDir()
public File getPIDDir()
public Properties getProperties()
public String getProperty(String propName)
public boolean getProperty(String propName, boolean defaultVal)
public float getProperty(String propName, float defaultVal)
public int getProperty(String propName, int defaultVal)
public long getProperty(String propName, long defaultVal)
public String getProperty(String propName, String defaultValue)
public Set<String> getPropertyNames()
public File getRouterDir()
public Set<Runnable> getShutdownTasks()
public File getTempDir()
public boolean hasWrapper()
public HMAC256Generator hmac256()
protected void initializeClock()
protected void initializeKeyRing()
protected void initializeSessionKeyManager()
public InternalClientManager internalClientManager()
public boolean isRouterContext()
public KeyGenerator keyGenerator()
public KeyRing keyRing()
public LogManager logManager()
public NamingService namingService()
public PortMapper portMapper()
public RandomSource random()
public void removeShutdownTask(Runnable task)
task - the task to removepublic RoutingKeyGenerator routingKeyGenerator()
public SessionKeyManager sessionKeyManager()
protected static boolean setGlobalContext(I2PAppContext ctx)
ctx - context constructed with doInit = falsepublic void setLogManager(LogManager logManager)
public SHA256Generator sha()
public SimpleTimer2 simpleTimer2()
public StatManager statManager()