# HG changeset patch # User Omair Majid # Date 1361900859 18000 # Node ID b0879f47909f42267514955637c441441660bc4b # Parent 0e3c1efc6c15b1ce88a48409298a072f76633498 Reduce duplication in c.r.t.client.swing.internal.Main Reviewed-by: jerboaa, neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-February/005675.html diff -r 0e3c1efc6c15 -r b0879f47909f client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/Main.java --- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/Main.java Tue Feb 26 12:04:12 2013 -0500 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/Main.java Tue Feb 26 12:47:39 2013 -0500 @@ -54,7 +54,6 @@ import com.redhat.thermostat.client.core.views.ClientConfigurationView; import com.redhat.thermostat.client.locale.LocaleResources; -import com.redhat.thermostat.client.swing.internal.config.ConnectionConfiguration; import com.redhat.thermostat.client.swing.internal.views.ClientConfigurationSwing; import com.redhat.thermostat.client.ui.ClientConfigReconnector; import com.redhat.thermostat.client.ui.ClientConfigurationController; @@ -66,7 +65,6 @@ import com.redhat.thermostat.common.config.ClientPreferences; import com.redhat.thermostat.common.locale.Translate; import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.common.utils.OSGIUtils; import com.redhat.thermostat.storage.core.Connection.ConnectionListener; import com.redhat.thermostat.storage.core.Connection.ConnectionStatus; import com.redhat.thermostat.storage.core.DbService; @@ -86,46 +84,32 @@ private ApplicationService appSvc; private UiFacadeFactory uiFacadeFactory; private DbServiceFactory dbServiceFactory; - private DbService dbService; + private Keyring keyring; private MultipleServiceTracker tracker; public Main(BundleContext context, Keyring keyring, ApplicationService appSvc, UiFacadeFactory uiFacadeFactory, String[] args) { - ClientPreferences prefs = new ClientPreferences(keyring); - ConnectionConfiguration config = new ConnectionConfiguration(prefs); + DbServiceFactory dbServiceFactory = new DbServiceFactory(); - init(context, appSvc, uiFacadeFactory, dbServiceFactory, - config.getUsername(), config.getPassword(), - config.getDBConnectionString()); + init(context, appSvc, uiFacadeFactory, dbServiceFactory, keyring); } Main(BundleContext context, ApplicationService appSvc, UiFacadeFactory uiFacadeFactory, DbServiceFactory dbServiceFactory, - String username, String password, String connectionURL) { - init(context, appSvc, uiFacadeFactory, dbServiceFactory, username, - password, connectionURL); + Keyring keyring) { + init(context, appSvc, uiFacadeFactory, dbServiceFactory, keyring); } private void init(BundleContext context, ApplicationService appSvc, UiFacadeFactory uiFacadeFactory, DbServiceFactory dbServiceFactory, - String username, String password, String connectionURL) { + Keyring keyring) { this.context = context; this.appSvc = appSvc; this.uiFacadeFactory = uiFacadeFactory; this.dbServiceFactory = dbServiceFactory; - try { - // See IcedTea BZ#1294 - // This may throw a StorageException if no suitable storage provider - // is registered. This most likely means a user has a wrong - // connection-url saved in her preferences. Continue, and catch - // this case in ConnectionSetup#run() - this.dbService = dbServiceFactory.createDbService(username, password, - connectionURL); - } catch (StorageException e) { - logger.log(Level.WARNING, "Error looking up storage provider", e); - } + this.keyring = keyring; } private void setLAF() { @@ -183,11 +167,12 @@ UIManager.getDefaults().put("OptionPane.isYesLast", true); UIManager.getDefaults().put("OptionPane.sameSizeButtons", true); - showGui(); } }); + tryConnecting(); + try { uiFacadeFactory.awaitShutdown(); } catch (InterruptedException e) { @@ -195,46 +180,10 @@ } } - private void showGui() { + private void tryConnecting() { final ExecutorService service = appSvc.getApplicationExecutor(); - service.execute(new ConnectorSetup(service)); - } - - private class ConnectorSetup implements Runnable { - - private ExecutorService service; - public ConnectorSetup(ExecutorService service) { - this.service = service; - } - - @Override - public void run() { - ConnectionListener connectionListener = new ConnectionHandler(service); - // dbService may be null at this point (see constructor). Fire - // failed connection attempt immediately. - if (dbService == null) { - connectionListener.changed(ConnectionStatus.FAILED_TO_CONNECT); - return; - } - dbService.addConnectionListener(connectionListener); - try { - dbService.connect(); - } catch (Throwable t) { - logger.log(Level.WARNING, "connection attempt failed: ", t); - } - } - } - - private class Connector implements Runnable { - - @Override - public void run() { - try { - dbService.connect(); - } catch (Throwable t) { - logger.log(Level.WARNING, "connection attempt failed: ", t); - } - } + ClientPreferences prefs = new ClientPreferences(keyring); + connect(prefs, service); } private class ConnectionAttempt implements Runnable { @@ -274,8 +223,31 @@ } } - private void connect(ExecutorService service) { - service.execute(new Connector()); + private void connect(ClientPreferences prefs, ExecutorService service) { + final ConnectionHandler reconnectionHandler = new ConnectionHandler(service); + try { + // create DbService with potentially modified parameters + final DbService dbService = dbServiceFactory.createDbService(prefs.getUserName(), prefs.getPassword(), prefs.getConnectionUrl()); + dbService.addConnectionListener(reconnectionHandler); + service.execute(new Runnable() { + @Override + public void run() { + try { + dbService.connect(); + } catch (Throwable t) { + logger.log(Level.WARNING, "connection attempt failed: ", t); + // is this ever possible? + reconnectionHandler.changed(ConnectionStatus.FAILED_TO_CONNECT); + } + } + }); + } catch (StorageException se) { + logger.log(Level.WARNING, "Unable to find appropriate storage provider", se); + // Prevent Icedtea BZ#1294, where no matching StorageProvider + // could potentially be found for the given connection URL. + // Indicate connection failure immediately. + reconnectionHandler.changed(ConnectionStatus.FAILED_TO_CONNECT); + } } private class MainClientConfigReconnector implements ClientConfigReconnector { @@ -286,20 +258,7 @@ @Override public void reconnect(ClientPreferences prefs) { - ConnectionHandler connectionListener = new ConnectionHandler(service); - try { - // Recreate DbService with potentially modified parameters. - dbService = dbServiceFactory.createDbService(prefs.getUserName(), prefs.getPassword(), prefs.getConnectionUrl()); - } catch (StorageException e) { - // Prevent Icedtea BZ#1294, where no matching StorageProvider - // could potentially be found for the given connection URL. - // Indicate connection failure immediately. - logger.log(Level.WARNING, "Error looking up storage provider", e); - connectionListener.changed(ConnectionStatus.FAILED_TO_CONNECT); - return; - } - dbService.addConnectionListener(connectionListener); - connect(service); + connect(prefs, service); } @Override @@ -309,8 +268,7 @@ } private void createPreferencesDialog(final ExecutorService service) { - - ClientPreferences prefs = new ClientPreferences(OSGIUtils.getInstance().getService(Keyring.class)); + ClientPreferences prefs = new ClientPreferences(keyring); ClientConfigurationView configDialog = new ClientConfigurationSwing(); ClientConfigurationController controller = new ClientConfigurationController(prefs, configDialog, new MainClientConfigReconnector(service)); @@ -384,15 +342,14 @@ } private void showMainWindow() { - SwingUtilities.invokeLater(new ShowMainWindow()); + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + MainWindowController mainController = uiFacadeFactory.getMainWindow(); + mainController.showMainMainWindow(); + } + }); } - - private class ShowMainWindow implements Runnable { - @Override - public void run() { - MainWindowController mainController = uiFacadeFactory.getMainWindow(); - mainController.showMainMainWindow(); - } - } + } diff -r 0e3c1efc6c15 -r b0879f47909f client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/config/ConnectionConfiguration.java --- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/config/ConnectionConfiguration.java Tue Feb 26 12:04:12 2013 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,66 +0,0 @@ -/* - * Copyright 2012, 2013 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * . - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.client.swing.internal.config; - -import com.redhat.thermostat.common.config.ClientPreferences; -import com.redhat.thermostat.storage.config.AuthenticationConfiguration; -import com.redhat.thermostat.storage.config.StartupConfiguration; - -public class ConnectionConfiguration implements StartupConfiguration, AuthenticationConfiguration { - - private final ClientPreferences clientPrefs; - - public ConnectionConfiguration(ClientPreferences clientPrefs) { - this.clientPrefs = clientPrefs; - } - - @Override - public String getDBConnectionString() { - return clientPrefs.getConnectionUrl(); - } - - @Override - public String getPassword() { - return clientPrefs.getPassword(); - } - - @Override - public String getUsername() { - return clientPrefs.getUserName(); - } -} - diff -r 0e3c1efc6c15 -r b0879f47909f client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/MainTest.java --- a/client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/MainTest.java Tue Feb 26 12:04:12 2013 -0500 +++ b/client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/MainTest.java Tue Feb 26 12:47:39 2013 -0500 @@ -66,6 +66,7 @@ import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.testutils.StubBundleContext; +import com.redhat.thermostat.utils.keyring.Keyring; public class MainTest { @@ -82,6 +83,7 @@ private TimerFactory timerFactory; private StubBundleContext context; private ApplicationService appService; + private Keyring keyring; @Before public void setUp() { @@ -115,6 +117,8 @@ timerFactory = mock(TimerFactory.class); when(appService.getTimerFactory()).thenReturn(timerFactory); + + keyring = mock(Keyring.class); } /** @@ -132,7 +136,7 @@ @Test public void verifyRunWaitsForShutdown() throws Exception { - Main main = new Main(context, appService, uiFactory, dbServiceFactory, null, null, null); + Main main = new Main(context, appService, uiFactory, dbServiceFactory, keyring); main.run(); @@ -143,7 +147,7 @@ @Test public void verifyConnectionIsMade() throws Exception { - Main main = new Main(context, appService, uiFactory, dbServiceFactory, null, null, null); + Main main = new Main(context, appService, uiFactory, dbServiceFactory, keyring); main.run(); @@ -159,7 +163,7 @@ context.registerService(HostInfoDAO.class, hostInfoDAO, null); VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class); context.registerService(VmInfoDAO.class, vmInfoDAO, null); - Main main = new Main(context, appService, uiFactory, dbServiceFactory, null, null, null); + Main main = new Main(context, appService, uiFactory, dbServiceFactory, keyring); main.run(); @@ -177,7 +181,7 @@ @Test public void verifyFailedConnectionTriggersShutdown() throws Exception { - Main main = new Main(context, appService, uiFactory, dbServiceFactory, null, null, null); + Main main = new Main(context, appService, uiFactory, dbServiceFactory, keyring); main.run();