Un-static part 1

Replace TrayManager.getInstance() with constructor
Pass Main to TrayManager
Create WindowHandler in TrayManager
Pass WindowHandler to RegisteredFrames
Only create ItoopieHostnameVerifier once, in gui/Main
Pass gui/Main to SettingsFrame
Pass gui/Main to ItoopieHostnameVerifier and CertificateGui
Sync on lock in ItoopieHostnameVerifier.clearRecentlyDenied()
Comment out some main() and other test code
Make some fields non-static
Remove some static initializers
Start gui/Main before the tray icon
Fixed the tray icon on Gnome with a delay in TrayManager
This commit is contained in:
zzz
2022-01-11 11:05:00 -05:00
parent 0498ea69ea
commit 25dd896d26
10 changed files with 98 additions and 97 deletions

View File

@ -10,7 +10,6 @@ import java.util.Timer;
import java.util.Map.Entry;
import java.util.Set;
import javax.net.ssl.HttpsURLConnection;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
@ -23,7 +22,6 @@ import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
import net.i2p.itoopie.configuration.ConfigurationManager;
import net.i2p.itoopie.gui.GUIHelper;
import net.i2p.itoopie.gui.TrayManager;
import net.i2p.itoopie.gui.WindowHandler;
import net.i2p.itoopie.i2pcontrol.InvalidParametersException;
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
import net.i2p.itoopie.i2pcontrol.JSONRPC2Interface;
@ -39,7 +37,6 @@ import net.i2p.itoopie.i2pcontrol.methods.SetI2PControl;
import net.i2p.itoopie.i2pcontrol.methods.SetNetworkSetting;
import net.i2p.itoopie.i2pcontrol.methods.SetRouterManager;
import net.i2p.itoopie.maintenance.ReseedMonitor;
import net.i2p.itoopie.security.ItoopieHostnameVerifier;
/**
* The main class of the application.
@ -47,32 +44,35 @@ import net.i2p.itoopie.security.ItoopieHostnameVerifier;
public class Main {
///Manages the lifetime of the tray icon.
private TrayManager trayManager = null;
private static ConfigurationManager _conf;
private TrayManager trayManager;
private final ConfigurationManager _conf;
private static Timer reseedMonitor;
private static Log _log;
private final Log _log;
public Main() {
_conf = ConfigurationManager.getInstance();
_log = LogFactory.getLog(Main.class);
}
/**
* Start the tray icon code (loads tray icon in the tray area).
* @throws Exception
*/
public void startUp() throws Exception {
trayManager = TrayManager.getInstance();
trayManager = new TrayManager(this);
trayManager.startManager();
}
public static void main(String[] args) {
beginStartup(args);
Main main = new Main();
main.beginStartup(args);
}
/**
* Main method launching the application.
*/
public static void beginStartup(String[] args) {
public void beginStartup(String[] args) {
System.setProperty("java.awt.headless", "false");
_conf = ConfigurationManager.getInstance();
_log = LogFactory.getLog(Main.class);
HttpsURLConnection.setDefaultHostnameVerifier(new ItoopieHostnameVerifier());
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
@ -84,16 +84,13 @@ public class Main {
}
final Main main = new Main();
main.launchForeverLoop();
launchForeverLoop();
try {
main.startUp();
startUp();
} catch (Exception e) {
_log.error("Error during TrayManager launch.", e);
}
// Popup Main window.
WindowHandler.toggleFrames();
// Start running periodic task after 2 minutes, run periodically every 10th minute.
@ -104,7 +101,7 @@ public class Main {
}
@SuppressWarnings("static-access")
public static void beginShutdown(){
public void beginShutdown(){
_conf.writeConfFile();
System.exit(0);
}
@ -131,6 +128,7 @@ public class Main {
}
/*
private static void testStuff(){
_conf.parseConfigStr("server.hostname=127.0.0.1");
_conf.parseConfigStr("server.port=5555");
@ -138,7 +136,6 @@ public class Main {
// Try port switching
/*
System.out.println("\nI2PControl - Port Switch");
try {
HashMap<I2P_CONTROL, String> hm = new HashMap<I2P_CONTROL, String>();
@ -268,7 +265,6 @@ public class Main {
System.out.println("Bad parameters sent..");
}
*/
// Test reading all router info
System.out.println("\nGetRouterInfo");
try {
@ -287,7 +283,6 @@ public class Main {
// Test restart - worked at one point :) Possibly now as well.
/*
System.out.println("\nSetRouterRunner: Restart");
try {
SetRouterRunner.execute(ROUTER_RUNNER.RESTART);
@ -295,10 +290,9 @@ public class Main {
System.out.println("Invalid password..");
} catch (JSONRPC2SessionException e) {
System.out.println("Connection failed..");
}*/
}
// Test restart graceful - worked at one point :) Possibly now as well.
/*
System.out.println("\nSetRouterRunner: Restart Graceful");
try {
SetRouterRunner.execute(ROUTER_RUNNER.RESTART_GRACEFUL);
@ -306,9 +300,8 @@ public class Main {
System.out.println("Invalid password..");
} catch (JSONRPC2SessionException e) {
System.out.println("Connection failed..");
}*/
}
/*
// Test shutdown - worked at one point :) Possibly now as well.
System.out.println("\nSetRouterRunner: Shutdown ");
try {
@ -318,7 +311,6 @@ public class Main {
} catch (JSONRPC2SessionException e) {
System.out.println("Connection failed..");
}
*/
}
*/
}

View File

@ -15,12 +15,14 @@ import net.i2p.itoopie.security.CertificateManager;
public class CertificateGUI {
/*
public static void main(String[] args){
System.out.println("Save new cert: " + saveNewCert(null,null));
System.out.println("Overwrite cert: " + overwriteCert(null,null));
}
*/
public static synchronized boolean saveNewCert(String hostname, X509Certificate cert){
public static synchronized boolean saveNewCert(Main main, String hostname, X509Certificate cert){
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JButton bt = new JButton();
@ -58,7 +60,7 @@ public class CertificateGUI {
if (n == JOptionPane.YES_OPTION){
CertificateManager.forcePutServerCert(hostname, CertificateHelper.convert(cert));
updateUI();
updateUI(main);
return true;
} else {
return false;
@ -66,7 +68,7 @@ public class CertificateGUI {
}
public static boolean overwriteCert(String hostname, X509Certificate cert){
public static boolean overwriteCert(Main main, String hostname, X509Certificate cert){
JFrame frame = new JFrame();
String title = Transl._t("Warning, new remote host detected");
@ -108,7 +110,7 @@ public class CertificateGUI {
JOptionPane.ERROR_MESSAGE);
if (n == JOptionPane.YES_OPTION){
CertificateManager.forcePutServerCert(hostname, CertificateHelper.convert(cert));
updateUI();
updateUI(main);
return true; // Confirmation positive
} else {
return false; // Confirmation negative
@ -121,7 +123,7 @@ public class CertificateGUI {
/**
* Upon new cert accepted it is probable a good idea to show it by updating the GUI.
*/
private static void updateUI(){
private static void updateUI(final Main main) {
// Sleep before updating.
(new Thread(){
@Override
@ -133,7 +135,7 @@ public class CertificateGUI {
@Override
public void run() {
Main.fireNewChange();
main.fireNewChange();
}
});
}

View File

@ -3,6 +3,7 @@ package net.i2p.itoopie.gui;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.net.ssl.HttpsURLConnection;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
@ -19,6 +20,7 @@ import net.i2p.itoopie.gui.component.RegisteredFrame;
import net.i2p.itoopie.gui.component.TabLogoPanel;
import net.i2p.itoopie.gui.component.util.TabChangeListener;
import net.i2p.itoopie.i18n.Transl;
import net.i2p.itoopie.security.ItoopieHostnameVerifier;
import net.i2p.itoopie.util.IconLoader;
import javax.swing.border.Border;
@ -31,8 +33,9 @@ import java.awt.FlowLayout;
public class Main {
private static JFrame frame;
private static JTabbedPane tabbedPane;
private JFrame frame;
private JTabbedPane tabbedPane;
private final WindowHandler windowHandler;
public final static int FRAME_WIDTH = 550;
public final static int FRAME_HEIGHT = 400;
public final static int TABBED_PANE_HEIGHT = FRAME_HEIGHT - 66;
@ -40,6 +43,7 @@ public class Main {
/**
* Launch the application.
*/
/*
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
@ -52,11 +56,14 @@ public class Main {
}
});
}
*/
/**
* Create the application.
*/
public Main() {
public Main(WindowHandler wh) {
windowHandler = wh;
HttpsURLConnection.setDefaultHostnameVerifier(new ItoopieHostnameVerifier(this));
initialize();
}
@ -67,7 +74,7 @@ public class Main {
GUIHelper.setDefaultStyle();
//GUIHelper.setTabLooks();
frame = new RegisteredFrame("itoopie");
frame = new RegisteredFrame("itoopie", windowHandler);
frame.setBounds(0, 0, FRAME_WIDTH, FRAME_HEIGHT);
frame.setResizable(false);
frame.setBackground(GUIHelper.VERY_LIGHT);
@ -75,7 +82,7 @@ public class Main {
root.setLayout(null);
//root.setBorder(BorderFactory.createLineBorder(GUIHelper.MEDIUM));
WindowHandler.registerMain(frame);
windowHandler.registerMain(frame);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
root.add(tabbedPane);
@ -93,7 +100,7 @@ public class Main {
// pass overview tab to settingsframe to reset the charts on change
TabLogoPanel settingsTab = new SettingsFrame("itoopie-opaque12", overviewTab);
TabLogoPanel settingsTab = new SettingsFrame("itoopie-opaque12", this, overviewTab);
tabbedPane.addTab(' ' + Transl._t("Settings") + ' ', icon, settingsTab, null);
tabbedPane.addChangeListener(new TabChangeListener(settingsTab));
@ -129,7 +136,7 @@ public class Main {
/**
* Used to manually trigger updates for the tab being shown.
*/
public static void fireNewChange(){
public void fireNewChange() {
for (ChangeListener ch : tabbedPane.getChangeListeners()){
ch.stateChanged(new ChangeEvent(tabbedPane));
}

View File

@ -90,15 +90,17 @@ public class SettingsFrame extends TabLogoPanel {
private final ConfigurationManager _conf;
private final OverviewTab _otab;
private final Main _main;
/**
* Create the application.
*/
public SettingsFrame(String imageName, OverviewTab otab) {
public SettingsFrame(String imageName, Main m, OverviewTab otab) {
super(imageName);
setLayout(null);
_conf = ConfigurationManager.getInstance();
_otab = otab;
_main = m;
initialize();
}
@ -200,7 +202,7 @@ public class SettingsFrame extends TabLogoPanel {
newAddressStatus != REMOTE_SAVE_STATUS.SAVE_FAILED_LOCALLY && newAddressStatus != REMOTE_SAVE_STATUS.SAVE_FAILED_REMOTELY
*/
) {
Main.fireNewChange();
_main.fireNewChange();
}
}
});

View File

@ -28,45 +28,44 @@ import net.i2p.itoopie.util.IsJar;
*/
public class TrayManager {
private static TrayManager instance = null;
///The tray area, or null if unsupported
protected SystemTray tray = null;
protected SystemTray tray;
///Our tray icon, or null if unsupported
protected TrayIcon trayIcon = null;
protected TrayIcon trayIcon;
private final net.i2p.itoopie.Main main;
/**
* Instantiate tray manager.
*/
protected TrayManager() {}
public static synchronized TrayManager getInstance() {
if(instance == null) {
instance = new TrayManager();
}
return instance;
public TrayManager(net.i2p.itoopie.Main m) {
main = m;
}
/**
* Add the tray icon to the system tray and start everything up.
*/
public synchronized void startManager() {
final WindowHandler windowHandler = new WindowHandler();
windowHandler.toggleFrames();
// so the tray icon works right on Gnome
try { Thread.sleep(500); } catch (InterruptedException ie) {}
SwingUtilities.invokeLater(new Runnable(){
public void run(){
if(SystemTray.isSupported()) {
final Image img = IconLoader.getTrayImage();
tray = SystemTray.getSystemTray();
trayIcon = new TrayIcon(IconLoader.getTrayImage(), "itoopie", getMainMenu());
trayIcon = new TrayIcon(img, "itoopie", getMainMenu());
trayIcon.setImageAutoSize(true); //Resize image to fit the system tray
try {
tray.add(trayIcon);
} catch (AWTException e) { e.printStackTrace(); }
trayIcon.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent arg0) {
WindowHandler.toggleFrames();
windowHandler.toggleFrames();
}
});
try {
tray.add(trayIcon);
} catch (AWTException e) {}
}
}
});
@ -106,7 +105,7 @@ public class TrayManager {
@Override
protected Object doInBackground() throws Exception {
Main.beginShutdown();
main.beginShutdown();
return null;
}
}.execute();

View File

@ -6,19 +6,19 @@ import java.util.HashSet;
import javax.swing.JFrame;
public class WindowHandler {
private static final HashSet<JFrame> _frames = new HashSet<JFrame>();
private static JFrame mainFrame;
private static boolean areFramesShown = false;
private final HashSet<JFrame> _frames = new HashSet<JFrame>();
private JFrame mainFrame;
private boolean areFramesShown;
public static void register(JFrame frame){
public void register(JFrame frame){
_frames.add(frame);
}
public static void registerMain(JFrame frame){
public void registerMain(JFrame frame){
mainFrame = frame;
}
public static void deRegister(JFrame frame){
public void deRegister(JFrame frame){
// don't remove the main frame when
// the user clicks on the X, so we have the updated
// graph when the user clicks on the icon again
@ -28,7 +28,7 @@ public class WindowHandler {
_frames.remove(frame);
}
public static void hideFrames(){
public void hideFrames(){
for (JFrame frame : _frames){
frame.setVisible(false);
}
@ -38,7 +38,7 @@ public class WindowHandler {
areFramesShown = false;
}
public static void showFrames(){
public void showFrames(){
for (JFrame frame : _frames){
frame.setVisible(true);
}
@ -48,9 +48,9 @@ public class WindowHandler {
areFramesShown = true;
}
public static void toggleFrames(){
public void toggleFrames(){
if (_frames.isEmpty()){
new Main();
new Main(this);
} else {
if (areFramesShown){
hideFrames();

View File

@ -10,25 +10,18 @@ import net.i2p.itoopie.util.IconLoader;
public class RegisteredFrame extends JFrame implements WindowListener{
private static final long serialVersionUID = 3351260168651061327L;
private final WindowHandler windowHandler;
public RegisteredFrame(String name) {
super(name);
super.addWindowListener(this);
WindowHandler.register(this);
this.setIconImage(IconLoader.getIcon("itoopie", 128));
public RegisteredFrame(String name, WindowHandler wh) {
super(name);
super.addWindowListener(this);
windowHandler = wh;
windowHandler.register(this);
this.setIconImage(IconLoader.getIcon("itoopie", 128));
}
public RegisteredFrame() {
super();
super.addWindowListener(this);
WindowHandler.register(this);
this.setIconImage(IconLoader.getIcon("itoopie", 128));
}
public void windowClosing(WindowEvent e) {
WindowHandler.deRegister(this);
windowHandler.deRegister(this);
this.dispose();
}

View File

@ -73,7 +73,8 @@ public class JSONRPC2Interface {
}
public static void testSettings() throws InvalidPasswordException, JSONRPC2SessionException{
HttpsURLConnection.setDefaultHostnameVerifier(new ItoopieHostnameVerifier());
// set in gui/Main
//HttpsURLConnection.setDefaultHostnameVerifier(new ItoopieHostnameVerifier());
setupSession();
Authenticate.execute();
}

View File

@ -32,11 +32,7 @@ import net.i2p.itoopie.security.ItoopieHostnameVerifier;
*/
public class ReseedMonitor extends TimerTask{
private static final Long MIN_KNOWN_PEERS = new Long(30);
private static final Log _log;
static {
_log = LogFactory.getLog(ReseedMonitor.class);
}
private final Log _log = LogFactory.getLog(ReseedMonitor.class);
@Override
public void run(){
@ -60,6 +56,7 @@ public class ReseedMonitor extends TimerTask{
}
/*
public static void main(String[] args){
System.out.println("Reading config file..");
ConfigurationManager _conf = ConfigurationManager.getInstance();
@ -105,5 +102,5 @@ public class ReseedMonitor extends TimerTask{
e.printStackTrace();
}
}
*/
}

View File

@ -11,11 +11,17 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.i2p.itoopie.gui.CertificateGUI;
import net.i2p.itoopie.gui.Main;
public class ItoopieHostnameVerifier implements HostnameVerifier {
private static final Log _log = LogFactory.getLog(ItoopieHostnameVerifier.class);
private final Log _log = LogFactory.getLog(ItoopieHostnameVerifier.class);
private static final HashSet<String> recentlyDeniedHosts = new HashSet<String>();
private static final Object _uiLock = new Object();
private final Main _main;
public ItoopieHostnameVerifier(Main main) {
_main = main;
}
public boolean verify(String urlHostName, SSLSession session) {
String serverHost = session.getPeerHost() + ":" + session.getPeerPort();
@ -32,7 +38,7 @@ public class ItoopieHostnameVerifier implements HostnameVerifier {
return true; // Remote host has provided valid certificate that is stored locally.
} else {
// Remote host has provided a certificate that != the stored certificate for this host
if (CertificateGUI.overwriteCert(serverHost, certs[0])) {
if (CertificateGUI.overwriteCert(_main, serverHost, certs[0])) {
return true;
} else {
recentlyDeniedHosts.add(session.getPeerHost() + ":" + session.getPeerPort());
@ -41,7 +47,7 @@ public class ItoopieHostnameVerifier implements HostnameVerifier {
}
} else {
// GUI, Add new host! new host
if (CertificateGUI.saveNewCert(serverHost, certs[0])) {
if (CertificateGUI.saveNewCert(_main, serverHost, certs[0])) {
return true;
} else {
recentlyDeniedHosts.add(session.getPeerHost() + ":" + session.getPeerPort());
@ -60,6 +66,8 @@ public class ItoopieHostnameVerifier implements HostnameVerifier {
* host that has had it's certificate denied recently.
*/
public static void clearRecentlyDenied() {
recentlyDeniedHosts.clear();
synchronized (_uiLock) {
recentlyDeniedHosts.clear();
}
}
}