Moved I2PProperties callback to the RouterContext.

This commit is contained in:
mathiasdm
2011-01-07 17:09:27 +00:00
parent 9b0c42ca6f
commit 7710b22cfd
6 changed files with 74 additions and 47 deletions

View File

@ -9,6 +9,7 @@ import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException; import javax.swing.UnsupportedLookAndFeelException;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
import net.i2p.desktopgui.router.RouterManager;
import net.i2p.desktopgui.util.*; import net.i2p.desktopgui.util.*;
import net.i2p.util.Log; import net.i2p.util.Log;
import net.i2p.util.Translate; import net.i2p.util.Translate;
@ -25,24 +26,25 @@ public class Main {
/** /**
* Start the tray icon code (loads tray icon in the tray area). * Start the tray icon code (loads tray icon in the tray area).
* @throws Exception
*/ */
private void startUp() { private void startUp() throws Exception {
trayManager = TrayManager.getInstance(); trayManager = TrayManager.getInstance();
trayManager.startManager(); trayManager.startManager();
I2PAppContext.getCurrentContext().addPropertyCallback(new I2PPropertyCallback() {
@Override if(RouterManager.inI2P()) {
public String getPropertyKey() { RouterManager.getRouterContext().addPropertyCallback(new I2PPropertyCallback() {
return Translate.PROP_LANG;
}
@Override @Override
public void propertyChanged(String arg0, String arg1) { public void propertyChanged(String arg0, String arg1) {
if(arg0.equals(Translate.PROP_LANG)) {
trayManager.languageChanged(); trayManager.languageChanged();
} }
}
}); });
} }
}
/** /**
* Main method launching the application. * Main method launching the application.
@ -71,7 +73,12 @@ public class Main {
@Override @Override
public void run() { public void run() {
try {
main.startUp(); main.startUp();
}
catch(Exception e) {
log.error("Failed while running desktopgui!", e);
}
} }

View File

@ -17,9 +17,28 @@ import net.i2p.util.Log;
public class RouterManager { public class RouterManager {
private final static Log log = new Log(RouterManager.class); private final static Log log = new Log(RouterManager.class);
private static I2PAppContext context = I2PAppContext.getCurrentContext();
public static I2PAppContext getAppContext() {
return context;
}
public static RouterContext getRouterContext() throws Exception {
if(context.isRouterContext()) {
return (RouterContext) context;
}
else {
throw new Exception("No RouterContext available!");
}
}
private static Router getRouter() { private static Router getRouter() {
return RouterContext.listContexts().get(0).router(); try {
return getRouterContext().router();
} catch (Exception e) {
log.error("Failed to get router. Why did we request it if no RouterContext is available?", e);
return null;
}
} }
/** /**
@ -27,14 +46,13 @@ public class RouterManager {
* This method has limited knowledge * This method has limited knowledge
* (there is no I2P instance running to collect information from). * (there is no I2P instance running to collect information from).
* *
* It needs to determine itself where the I2P instance is located, * It determines the I2P location using the I2PAppContext.
* except if the location has been given through command-line arguments.
*/ */
public static void start() { public static void start() {
try { try {
//TODO: set/get PID //TODO: set/get PID
String separator = System.getProperty("file.separator"); String separator = System.getProperty("file.separator");
String location = I2PAppContext.getCurrentContext().getBaseDir().getAbsolutePath(); String location = getAppContext().getBaseDir().getAbsolutePath();
Runtime.getRuntime().exec(location + separator + "i2psvc " + location + separator + "wrapper.config"); Runtime.getRuntime().exec(location + separator + "i2psvc " + location + separator + "wrapper.config");
} catch (IOException e) { } catch (IOException e) {
@ -46,24 +64,24 @@ public class RouterManager {
* Restart the running I2P instance. * Restart the running I2P instance.
*/ */
public static void restart() { public static void restart() {
if(inI2P()) {
getRouter().restart(); getRouter().restart();
} }
}
/** /**
* Stop the running I2P instance. * Stop the running I2P instance.
*/ */
public static void shutDown() { public static void shutDown() {
if(inI2P()) {
getRouter().shutdownGracefully(); getRouter().shutdownGracefully();
} }
}
/** /**
* Check if we are running inside I2P. * Check if we are running inside I2P.
*/ */
public static boolean inI2P() { public static boolean inI2P() {
return (RouterContext.listContexts().size() > 0); return context.isRouterContext();
}
private static String _(String s) {
return DesktopguiTranslator._(s);
} }
} }

View File

@ -19,7 +19,7 @@ public class ContentHelper extends HelperBase {
_startAtBeginning = Boolean.valueOf(""+moo).booleanValue(); _startAtBeginning = Boolean.valueOf(""+moo).booleanValue();
} }
public void setLang(String l) { public void setLang(String l) {
if(_lang == null || !_lang.equals(l)) { if((_lang == null || !_lang.equals(l)) && (l != null)) {
//Set language for router console //Set language for router console
_lang = l; _lang = l;

View File

@ -64,7 +64,7 @@ public class I2PAppContext {
/** the context that components without explicit root are bound */ /** the context that components without explicit root are bound */
protected static I2PAppContext _globalAppContext; protected static I2PAppContext _globalAppContext;
private I2PProperties _overrideProps; protected I2PProperties _overrideProps;
private StatManager _statManager; private StatManager _statManager;
private SessionKeyManager _sessionKeyManager; private SessionKeyManager _sessionKeyManager;
@ -480,26 +480,13 @@ public class I2PAppContext {
return names; return names;
} }
/**
* Modify the configuration attributes of this context, changing
* one of the properties provided during the context construction.
* @param propName The name of the property.
* @param value The new value for the property.
*/
public void setProperty(String propName, String value) {
if(_overrideProps != null) {
_overrideProps.setProperty(propName, value);
}
}
/** /**
* Add a callback, which will fire upon changes in the property * Add a callback, which will fire upon changes in the property
* given in the specific callback. * given in the specific callback.
* Unimplemented in I2PAppContext: this only makes sense in a router context.
* @param callback The implementation of the callback. * @param callback The implementation of the callback.
*/ */
public void addPropertyCallback(I2PPropertyCallback callback) { public void addPropertyCallback(I2PPropertyCallback callback) {}
_overrideProps.addCallBack(callback);
}
/** /**
* The statistics component with which we can track various events * The statistics component with which we can track various events

View File

@ -17,7 +17,6 @@ public class I2PProperties extends Properties {
/** /**
* Keep a list of callbacks to contact the interested parties * Keep a list of callbacks to contact the interested parties
* that want to know about property changes. * that want to know about property changes.
* @todo Use a map of lists, so we don't need to loop over all the callbacks on every change.
*/ */
private final List<I2PPropertyCallback> _callbacks = new CopyOnWriteArrayList<I2PPropertyCallback>(); private final List<I2PPropertyCallback> _callbacks = new CopyOnWriteArrayList<I2PPropertyCallback>();
@ -40,10 +39,8 @@ public class I2PProperties extends Properties {
public Object setProperty(String key, String value) { public Object setProperty(String key, String value) {
Object returnValue = super.setProperty(key, value); Object returnValue = super.setProperty(key, value);
for(I2PPropertyCallback callback: _callbacks) { for(I2PPropertyCallback callback: _callbacks) {
if(callback.getPropertyKey().equals(key)) {
callback.propertyChanged(key, value); callback.propertyChanged(key, value);
} }
}
return returnValue; return returnValue;
} }
@ -51,7 +48,6 @@ public class I2PProperties extends Properties {
public void propertyChanged(String key, String value); public void propertyChanged(String key, String value);
public String getPropertyKey();
} }
} }

View File

@ -23,6 +23,7 @@ import net.i2p.router.tunnel.TunnelDispatcher;
import net.i2p.router.tunnel.pool.TunnelPoolManager; import net.i2p.router.tunnel.pool.TunnelPoolManager;
import net.i2p.util.Clock; import net.i2p.util.Clock;
import net.i2p.util.KeyRing; import net.i2p.util.KeyRing;
import net.i2p.util.I2PProperties.I2PPropertyCallback;
/** /**
* Build off the core I2P context to provide a root for a router instance to * Build off the core I2P context to provide a root for a router instance to
@ -105,6 +106,24 @@ public class RouterContext extends I2PAppContext {
return envProps; return envProps;
} }
/**
* Modify the configuration attributes of this context, changing
* one of the properties provided during the context construction.
* @param propName The name of the property.
* @param value The new value for the property.
*/
public void setProperty(String propName, String value) {
if(_overrideProps != null) {
_overrideProps.setProperty(propName, value);
}
}
public void addPropertyCallback(I2PPropertyCallback callback) {
_overrideProps.addCallBack(callback);
}
public void initAll() { public void initAll() {
if ("false".equals(getProperty("i2p.dummyClientFacade", "false"))) if ("false".equals(getProperty("i2p.dummyClientFacade", "false")))
_clientManagerFacade = new ClientManagerFacadeImpl(this); _clientManagerFacade = new ClientManagerFacadeImpl(this);