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 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);
}
}

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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

View File

@ -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();
}
}

View File

@ -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
@ -105,6 +106,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")))
_clientManagerFacade = new ClientManagerFacadeImpl(this);