changeset 684:c2f3b3b23e0a

Remove ViewFactory in memory-stats-panel bundle. This patch removes references to ViewFactory in the memory-stats-panel. It registers ViewProviders as services instead which are then used to create the views. Review-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-October/003630.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Tue, 09 Oct 2012 17:14:38 +0200
parents 3dd95a7bfa9f
children 0cb6631d2bca
files client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsController.java client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsPanelActivator.java client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsService.java client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsView.java client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsViewImpl.java client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsViewProvider.java client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/SwingMemoryStatsViewProvider.java client/memory-stats-panel/src/test/java/com/redhat/thermostat/client/stats/memory/MemoryStatsControllerTest.java client/memory-stats-panel/src/test/java/com/redhat/thermostat/client/stats/memory/MemoryStatsPanelActivatorTest.java
diffstat 9 files changed, 172 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsController.java	Tue Oct 09 17:10:16 2012 +0200
+++ b/client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsController.java	Tue Oct 09 17:14:38 2012 +0200
@@ -41,10 +41,9 @@
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
-import com.redhat.thermostat.client.common.controllers.VmInformationServiceController;
-import com.redhat.thermostat.client.common.views.BasicView;
-import com.redhat.thermostat.client.common.views.UIComponent;
-import com.redhat.thermostat.client.common.views.BasicView.Action;
+import com.redhat.thermostat.client.core.controllers.VmInformationServiceController;
+import com.redhat.thermostat.client.core.views.BasicView.Action;
+import com.redhat.thermostat.client.core.views.UIComponent;
 import com.redhat.thermostat.client.locale.LocaleResources;
 import com.redhat.thermostat.client.locale.Translate;
 import com.redhat.thermostat.common.ActionEvent;
@@ -136,12 +135,12 @@
         }
     }
     
-    public MemoryStatsController(final VmMemoryStatDAO vmMemoryStatDao, final VmRef ref) {
+    public MemoryStatsController(final VmMemoryStatDAO vmMemoryStatDao, final VmRef ref, MemoryStatsViewProvider viewProvider) {
         
         regions = new HashMap<>();
         this.ref = ref;
-        view = ApplicationContext.getInstance().getViewFactory().getView(MemoryStatsView.class);
         vmDao = vmMemoryStatDao;
+        view = viewProvider.createView();
         
         timer = ApplicationContext.getInstance().getTimerFactory().createTimer();
         
--- a/client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsPanelActivator.java	Tue Oct 09 17:10:16 2012 +0200
+++ b/client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsPanelActivator.java	Tue Oct 09 17:14:38 2012 +0200
@@ -42,11 +42,10 @@
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 
-import com.redhat.thermostat.client.common.VmInformationService;
+import com.redhat.thermostat.client.core.VmInformationService;
 import com.redhat.thermostat.client.osgi.service.ApplicationService;
 import com.redhat.thermostat.common.MultipleServiceTracker;
 import com.redhat.thermostat.common.MultipleServiceTracker.Action;
-import com.redhat.thermostat.common.appctx.ApplicationContext;
 import com.redhat.thermostat.common.dao.VmMemoryStatDAO;
 
 public class MemoryStatsPanelActivator implements BundleActivator {
@@ -56,13 +55,16 @@
 
     @Override
     public void start(final BundleContext context) throws Exception {
+        MemoryStatsViewProvider provider = new SwingMemoryStatsViewProvider();
+        context.registerService(MemoryStatsViewProvider.class.getName(), provider, null);
+        
         Class<?>[] deps = new Class<?>[] {
             ApplicationService.class,
             VmMemoryStatDAO.class,
         };
 
         tracker = new MultipleServiceTracker(context, deps, new Action() {
-
+            
             @Override
             public void dependenciesUnavailable() {
                 memoryStatRegistration.unregister();
@@ -71,7 +73,6 @@
 
             @Override
             public void dependenciesAvailable(Map<String, Object> services) {
-                ApplicationContext.getInstance().getViewFactory().setViewClass(MemoryStatsView.class, MemoryStatsViewImpl.class);
                 VmMemoryStatDAO memoryStatDao = (VmMemoryStatDAO) services.get(VmMemoryStatDAO.class.getName());
                 MemoryStatsService impl = new MemoryStatsService(memoryStatDao);
                 memoryStatRegistration = context.registerService(VmInformationService.class.getName(), impl , null);
--- a/client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsService.java	Tue Oct 09 17:10:16 2012 +0200
+++ b/client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsService.java	Tue Oct 09 17:14:38 2012 +0200
@@ -36,13 +36,13 @@
 
 package com.redhat.thermostat.client.stats.memory;
 
-import com.redhat.thermostat.client.common.VmFilter;
-import com.redhat.thermostat.client.common.VmInformationService;
-import com.redhat.thermostat.client.common.controllers.VmInformationServiceController;
+import com.redhat.thermostat.client.core.VmFilter;
+import com.redhat.thermostat.client.core.VmInformationService;
+import com.redhat.thermostat.client.core.controllers.VmInformationServiceController;
 import com.redhat.thermostat.client.osgi.service.AlwaysMatchFilter;
 import com.redhat.thermostat.common.dao.VmMemoryStatDAO;
-import com.redhat.thermostat.common.dao.Ref;
 import com.redhat.thermostat.common.dao.VmRef;
+import com.redhat.thermostat.common.utils.OSGIUtils;
 
 class MemoryStatsService implements VmInformationService {
     
@@ -56,7 +56,8 @@
     
     @Override
     public VmInformationServiceController getInformationServiceController(VmRef ref) {
-        return new MemoryStatsController(vmMemoryStatDao, ref);
+        MemoryStatsViewProvider viewProvider = OSGIUtils.getInstance().getService(MemoryStatsViewProvider.class);
+        return new MemoryStatsController(vmMemoryStatDao, ref, viewProvider);
     }
 
     @Override
--- a/client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsView.java	Tue Oct 09 17:10:16 2012 +0200
+++ b/client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsView.java	Tue Oct 09 17:14:38 2012 +0200
@@ -36,9 +36,10 @@
 
 package com.redhat.thermostat.client.stats.memory;
 
-import com.redhat.thermostat.client.common.views.BasicView;
+import com.redhat.thermostat.client.core.views.BasicView;
+import com.redhat.thermostat.client.core.views.UIComponent;
 
-public abstract class MemoryStatsView extends BasicView {
+public abstract class MemoryStatsView extends BasicView implements UIComponent {
     
     public abstract void addRegion(Payload region);
     public abstract void updateRegion(Payload region);
--- a/client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsViewImpl.java	Tue Oct 09 17:10:16 2012 +0200
+++ b/client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsViewImpl.java	Tue Oct 09 17:14:38 2012 +0200
@@ -47,7 +47,7 @@
 import javax.swing.JPanel;
 import javax.swing.SwingUtilities;
 
-import com.redhat.thermostat.client.common.views.BasicView;
+import com.redhat.thermostat.client.core.views.BasicView;
 import com.redhat.thermostat.client.ui.ComponentVisibleListener;
 import com.redhat.thermostat.client.ui.SwingComponent;
 import com.redhat.thermostat.swing.HeaderPanel;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/MemoryStatsViewProvider.java	Tue Oct 09 17:14:38 2012 +0200
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2012 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
+ * <http://www.gnu.org/licenses/>.
+ *
+ * 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.stats.memory;
+
+import com.redhat.thermostat.client.core.views.ViewProvider;
+
+public interface MemoryStatsViewProvider extends ViewProvider {
+
+    @Override
+    public MemoryStatsView createView();
+    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/memory-stats-panel/src/main/java/com/redhat/thermostat/client/stats/memory/SwingMemoryStatsViewProvider.java	Tue Oct 09 17:14:38 2012 +0200
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2012 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
+ * <http://www.gnu.org/licenses/>.
+ *
+ * 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.stats.memory;
+
+public class SwingMemoryStatsViewProvider implements MemoryStatsViewProvider {
+
+    @Override
+    public MemoryStatsView createView() {
+        return new MemoryStatsViewImpl();
+    }
+
+}
--- a/client/memory-stats-panel/src/test/java/com/redhat/thermostat/client/stats/memory/MemoryStatsControllerTest.java	Tue Oct 09 17:10:16 2012 +0200
+++ b/client/memory-stats-panel/src/test/java/com/redhat/thermostat/client/stats/memory/MemoryStatsControllerTest.java	Tue Oct 09 17:14:38 2012 +0200
@@ -40,7 +40,6 @@
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyLong;
-import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
@@ -55,7 +54,6 @@
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 
-import com.redhat.thermostat.client.common.views.ViewFactory;
 import com.redhat.thermostat.common.ActionEvent;
 import com.redhat.thermostat.common.ActionListener;
 import com.redhat.thermostat.common.Timer;
@@ -82,6 +80,7 @@
     
     private Space canary;
     
+    @SuppressWarnings({ "unchecked", "rawtypes" })
     @Before
     public void setUp() {
         timer = mock(Timer.class);
@@ -130,9 +129,8 @@
         when(memoryStatDao.getLatestVmMemoryStats(any(VmRef.class), anyLong())).thenReturn(vmInfo);
         
         view = mock(MemoryStatsView.class);
-        ViewFactory viewFactory = mock(ViewFactory.class);
-        when(viewFactory.getView(eq(MemoryStatsView.class))).thenReturn(view);
-        ApplicationContext.getInstance().setViewFactory(viewFactory);
+        MemoryStatsViewProvider viewProvider = mock(MemoryStatsViewProvider.class);
+        when(viewProvider.createView()).thenReturn(view);
         
         ArgumentCaptor<ActionListener> viewArgumentCaptor =
                 ArgumentCaptor.forClass(ActionListener.class);
@@ -140,7 +138,7 @@
 
         VmRef ref = mock(VmRef.class);
         
-        controller = new MemoryStatsController(memoryStatDao, ref);
+        controller = new MemoryStatsController(memoryStatDao, ref, viewProvider);
         
         viewListener = viewArgumentCaptor.getValue();
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/memory-stats-panel/src/test/java/com/redhat/thermostat/client/stats/memory/MemoryStatsPanelActivatorTest.java	Tue Oct 09 17:14:38 2012 +0200
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2012 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
+ * <http://www.gnu.org/licenses/>.
+ *
+ * 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.stats.memory;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import com.redhat.thermostat.test.StubBundleContext;
+
+public class MemoryStatsPanelActivatorTest {
+
+    @Test
+    public void verifyStartRegistersViewProvider() throws Exception {
+        StubBundleContext ctx = new StubBundleContext();
+        MemoryStatsPanelActivator activator = new MemoryStatsPanelActivator();
+        activator.start(ctx);
+        assertTrue(ctx.isServiceRegistered(MemoryStatsViewProvider.class.getName(), SwingMemoryStatsViewProvider.class));
+        assertEquals(1, ctx.getAllServices().size());
+    }
+}