diff --git a/apps/desktopgui/src/net/i2p/desktopgui/Main.java b/apps/desktopgui/src/net/i2p/desktopgui/Main.java index c24cdd4184d26d15d4b7600c918b30587786ab4c..7bed90e3a65fbfd8dbb058adddc5c69aa79bc3fa 100644 --- a/apps/desktopgui/src/net/i2p/desktopgui/Main.java +++ b/apps/desktopgui/src/net/i2p/desktopgui/Main.java @@ -9,6 +9,7 @@ import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import net.i2p.I2PAppContext; +import net.i2p.desktopgui.router.RouterManager; import net.i2p.desktopgui.util.*; import net.i2p.util.Log; import net.i2p.util.Translate; @@ -25,23 +26,24 @@ public class Main { /** * 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.startManager(); - I2PAppContext.getCurrentContext().addPropertyCallback(new I2PPropertyCallback() { - - @Override - public String getPropertyKey() { - return Translate.PROP_LANG; - } + + if(RouterManager.inI2P()) { + RouterManager.getRouterContext().addPropertyCallback(new I2PPropertyCallback() { - @Override - public void propertyChanged(String arg0, String arg1) { - trayManager.languageChanged(); - } - - }); + @Override + public void propertyChanged(String arg0, String arg1) { + if(arg0.equals(Translate.PROP_LANG)) { + trayManager.languageChanged(); + } + } + + }); + } } /** @@ -71,7 +73,12 @@ public class Main { @Override public void run() { - main.startUp(); + try { + main.startUp(); + } + catch(Exception e) { + log.error("Failed while running desktopgui!", e); + } } diff --git a/apps/desktopgui/src/net/i2p/desktopgui/router/RouterManager.java b/apps/desktopgui/src/net/i2p/desktopgui/router/RouterManager.java index fd36219fa44c47b87466ddf645a0bacd20a97108..8caafe397a5f4750a175ca46c29993ff7fe837f5 100644 --- a/apps/desktopgui/src/net/i2p/desktopgui/router/RouterManager.java +++ b/apps/desktopgui/src/net/i2p/desktopgui/router/RouterManager.java @@ -17,9 +17,28 @@ import net.i2p.util.Log; public class RouterManager { 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() { - 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 * (there is no I2P instance running to collect information from). * - * It needs to determine itself where the I2P instance is located, - * except if the location has been given through command-line arguments. + * It determines the I2P location using the I2PAppContext. */ public static void start() { try { //TODO: set/get PID 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"); } catch (IOException e) { @@ -46,24 +64,24 @@ public class RouterManager { * Restart the running I2P instance. */ public static void restart() { - getRouter().restart(); + if(inI2P()) { + getRouter().restart(); + } } /** * Stop the running I2P instance. */ public static void shutDown() { - getRouter().shutdownGracefully(); + if(inI2P()) { + getRouter().shutdownGracefully(); + } } /** * Check if we are running inside I2P. */ public static boolean inI2P() { - return (RouterContext.listContexts().size() > 0); + return context.isRouterContext(); } - - private static String _(String s) { - return DesktopguiTranslator._(s); - } } diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ContentHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/ContentHelper.java index adc245a6bba6c367f57deb79eb993db9f8a08fa3..7a9d9479342858eea60d59e40255a55491c92dee 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/ContentHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/ContentHelper.java @@ -19,7 +19,7 @@ public class ContentHelper extends HelperBase { _startAtBeginning = Boolean.valueOf(""+moo).booleanValue(); } public void setLang(String l) { - if(_lang == null || !_lang.equals(l)) { + if((_lang == null || !_lang.equals(l)) && (l != null)) { //Set language for router console _lang = l; diff --git a/core/java/src/net/i2p/I2PAppContext.java b/core/java/src/net/i2p/I2PAppContext.java index 4b023034b83be8dcdbb8a1f04440e0c98253777e..1ac74c2ce39af8e7efe715f1174696d5331b37cb 100644 --- a/core/java/src/net/i2p/I2PAppContext.java +++ b/core/java/src/net/i2p/I2PAppContext.java @@ -64,7 +64,7 @@ public class I2PAppContext { /** the context that components without explicit root are bound */ protected static I2PAppContext _globalAppContext; - private I2PProperties _overrideProps; + protected I2PProperties _overrideProps; private StatManager _statManager; private SessionKeyManager _sessionKeyManager; @@ -480,26 +480,13 @@ public class I2PAppContext { 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 * given in the specific callback. + * Unimplemented in I2PAppContext: this only makes sense in a router context. * @param callback The implementation of the callback. */ - public void addPropertyCallback(I2PPropertyCallback callback) { - _overrideProps.addCallBack(callback); - } + public void addPropertyCallback(I2PPropertyCallback callback) {} /** * The statistics component with which we can track various events diff --git a/core/java/src/net/i2p/util/I2PProperties.java b/core/java/src/net/i2p/util/I2PProperties.java index b6dfca8a165147aa8f0d9e02b3fbea00bf703d0f..e943f561fdfa094ce58c82af8c62b6f1f9c7ea9f 100644 --- a/core/java/src/net/i2p/util/I2PProperties.java +++ b/core/java/src/net/i2p/util/I2PProperties.java @@ -17,7 +17,6 @@ public class I2PProperties extends Properties { /** * Keep a list of callbacks to contact the interested parties * 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>(); @@ -40,9 +39,7 @@ public class I2PProperties extends Properties { public Object setProperty(String key, String value) { Object returnValue = super.setProperty(key, value); for(I2PPropertyCallback callback: _callbacks) { - if(callback.getPropertyKey().equals(key)) { - callback.propertyChanged(key, value); - } + callback.propertyChanged(key, value); } return returnValue; } @@ -51,7 +48,6 @@ public class I2PProperties extends Properties { public void propertyChanged(String key, String value); - public String getPropertyKey(); } } diff --git a/router/java/src/net/i2p/router/RouterContext.java b/router/java/src/net/i2p/router/RouterContext.java index 3d5ed609edb65cb900a9ebc68bad1cf9af8b93ce..51b7bf1873c2248de9545708781fcd3fd51ce2fc 100644 --- a/router/java/src/net/i2p/router/RouterContext.java +++ b/router/java/src/net/i2p/router/RouterContext.java @@ -23,6 +23,7 @@ import net.i2p.router.tunnel.TunnelDispatcher; import net.i2p.router.tunnel.pool.TunnelPoolManager; import net.i2p.util.Clock; 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 @@ -104,6 +105,24 @@ public class RouterContext extends I2PAppContext { } 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() { if ("false".equals(getProperty("i2p.dummyClientFacade", "false")))