Mercurial > hg > release > thermostat-0.6
changeset 670:1310dc5d5e48
Use VmInfoDAO as a service
VmInfoDAO should be used directly or as an OSGiService, but never
through DaoFactory.
Reviewed-by: jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-September/003489.html
line wrap: on
line diff
--- a/agent/core/src/main/java/com/redhat/thermostat/backend/system/JvmStatHostListener.java Wed Oct 03 18:05:54 2012 -0400 +++ b/agent/core/src/main/java/com/redhat/thermostat/backend/system/JvmStatHostListener.java Wed Oct 03 18:05:54 2012 -0400 @@ -77,9 +77,9 @@ private Set<JvmStatusListener> statusListeners = new CopyOnWriteArraySet<JvmStatusListener>(); - JvmStatHostListener(DAOFactory df, boolean attachNew) { + JvmStatHostListener(DAOFactory df, VmInfoDAO vmInfoDAO, boolean attachNew) { this.df = df; - this.vmInfoDAO = df.getVmInfoDAO(); + this.vmInfoDAO = vmInfoDAO; this.attachNew = attachNew; }
--- a/agent/core/src/main/java/com/redhat/thermostat/backend/system/SystemBackend.java Wed Oct 03 18:05:54 2012 -0400 +++ b/agent/core/src/main/java/com/redhat/thermostat/backend/system/SystemBackend.java Wed Oct 03 18:05:54 2012 -0400 @@ -131,7 +131,7 @@ memoryStats = df.getMemoryStatDAO(); vmCpuStats = df.getVmCpuStatDAO(); networkInterfaces = df.getNetworkInterfaceInfoDAO(); - hostListener = new JvmStatHostListener(df, getObserveNewJvm()); + hostListener = new JvmStatHostListener(df, df.getVmInfoDAO(), getObserveNewJvm()); } @Override
--- a/agent/core/src/test/java/com/redhat/thermostat/backend/system/JvmStatHostListenerTest.java Wed Oct 03 18:05:54 2012 -0400 +++ b/agent/core/src/test/java/com/redhat/thermostat/backend/system/JvmStatHostListenerTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -83,8 +83,7 @@ VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class); DAOFactory df = mock(DAOFactory.class); when(df.getVmClassStatsDAO()).thenReturn(vmClassDAO); - when(df.getVmInfoDAO()).thenReturn(vmInfoDAO); - JvmStatHostListener l = new JvmStatHostListener(df, true); + JvmStatHostListener l = new JvmStatHostListener(df, vmInfoDAO, true); SystemBackend backend = mock(SystemBackend.class); when(backend.getObserveNewJvm()).thenReturn(true);
--- a/client/core/src/main/java/com/redhat/thermostat/client/internal/Main.java Wed Oct 03 18:05:54 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/internal/Main.java Wed Oct 03 18:05:54 2012 -0400 @@ -289,6 +289,8 @@ uiFacadeFactory.setMemoryStatDao(daoFactory.getMemoryStatDAO()); uiFacadeFactory.setNetworkInfoDao(daoFactory.getNetworkInterfaceInfoDAO()); + uiFacadeFactory.setVmInfoDao(daoFactory.getVmInfoDAO()); + showMainWindow(); } else if (newStatus == ConnectionStatus.FAILED_TO_CONNECT) { if (retry) {
--- a/client/core/src/main/java/com/redhat/thermostat/client/internal/MainWindowControllerImpl.java Wed Oct 03 18:05:54 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/internal/MainWindowControllerImpl.java Wed Oct 03 18:05:54 2012 -0400 @@ -38,7 +38,6 @@ import java.awt.event.MouseEvent; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -77,7 +76,6 @@ import com.redhat.thermostat.common.Timer.SchedulingType; import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.config.ClientPreferences; -import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef; import com.redhat.thermostat.common.dao.Ref; @@ -162,7 +160,7 @@ private VmInformationControllerProvider vmInfoControllerProvider; - public MainWindowControllerImpl(UiFacadeFactory facadeFactory, MainView view, RegistryFactory registryFactory, HostInfoDAO hostsDao) + public MainWindowControllerImpl(UiFacadeFactory facadeFactory, MainView view, RegistryFactory registryFactory, HostInfoDAO hostsDao, VmInfoDAO vmsDAO) { try { vmFilterRegistry = registryFactory.createVmFilterRegistry(); @@ -182,10 +180,8 @@ this.facadeFactory = facadeFactory; - ApplicationContext ctx = ApplicationContext.getInstance(); - DAOFactory daoFactory = ctx.getDAOFactory(); this.hostsDAO = hostsDao; - vmsDAO = daoFactory.getVmInfoDAO(); + this.vmsDAO = vmsDAO; initView(view); @@ -426,7 +422,7 @@ Ref ref = view.getSelectedHostOrVm(); if (ref == null) { - SummaryController controller = new SummaryController(hostsDAO); + SummaryController controller = new SummaryController(hostsDAO, vmsDAO); view.setSubView(controller.getView()); } else if (ref instanceof HostRef) { HostRef hostRef = (HostRef) ref;
--- a/client/core/src/main/java/com/redhat/thermostat/client/internal/UiFacadeFactoryImpl.java Wed Oct 03 18:05:54 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/internal/UiFacadeFactoryImpl.java Wed Oct 03 18:05:54 2012 -0400 @@ -55,6 +55,7 @@ import com.redhat.thermostat.common.dao.HostRef; import com.redhat.thermostat.common.dao.MemoryStatDAO; import com.redhat.thermostat.common.dao.NetworkInterfaceInfoDAO; +import com.redhat.thermostat.common.dao.VmInfoDAO; import com.redhat.thermostat.common.dao.VmRef; public class UiFacadeFactoryImpl implements UiFacadeFactory { @@ -70,6 +71,8 @@ private MemoryStatDAO memoryStatDao; private NetworkInterfaceInfoDAO networkInfoDao; + private VmInfoDAO vmInfoDao; + public UiFacadeFactoryImpl(BundleContext context) { this.context = context; } @@ -91,16 +94,20 @@ this.networkInfoDao = networkInfoDao; } + public void setVmInfoDao(VmInfoDAO vmInfoDao) { + this.vmInfoDao = vmInfoDao; + } + @Override public MainWindowController getMainWindow() { MainView mainView = new MainWindow(); RegistryFactory registryFactory = new RegistryFactory(context); - return new MainWindowControllerImpl(this, mainView, registryFactory, hostInfoDao); + return new MainWindowControllerImpl(this, mainView, registryFactory, hostInfoDao, vmInfoDao); } @Override public SummaryController getSummary() { - return new SummaryController(hostInfoDao); + return new SummaryController(hostInfoDao, vmInfoDao); } @@ -112,7 +119,7 @@ @Override public VmInformationController getVmController(VmRef ref) { - return new VmInformationController(this, ref); + return new VmInformationController(this, vmInfoDao, ref); }
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/SummaryController.java Wed Oct 03 18:05:54 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/SummaryController.java Wed Oct 03 18:05:54 2012 -0400 @@ -45,11 +45,9 @@ import com.redhat.thermostat.common.Timer; import com.redhat.thermostat.common.Timer.SchedulingType; import com.redhat.thermostat.common.appctx.ApplicationContext; -import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.VmInfoDAO; - public class SummaryController { private final HostInfoDAO hostsDAO; @@ -59,7 +57,7 @@ private final Timer backgroundUpdateTimer; - public SummaryController(HostInfoDAO hostInfoDao) { + public SummaryController(HostInfoDAO hostInfoDao, VmInfoDAO vmInfoDao) { ApplicationContext ctx = ApplicationContext.getInstance(); this.view = ctx.getViewFactory().getView(SummaryView.class); @@ -80,9 +78,8 @@ } }); - DAOFactory daoFactory = ctx.getDAOFactory(); hostsDAO = hostInfoDao; - vmsDAO = daoFactory.getVmInfoDAO(); + vmsDAO = vmInfoDao; backgroundUpdateTimer = ApplicationContext.getInstance().getTimerFactory().createTimer(); backgroundUpdateTimer.setAction(new Runnable() {
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/UiFacadeFactory.java Wed Oct 03 18:05:54 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/UiFacadeFactory.java Wed Oct 03 18:05:54 2012 -0400 @@ -45,17 +45,17 @@ import com.redhat.thermostat.common.dao.HostRef; import com.redhat.thermostat.common.dao.MemoryStatDAO; import com.redhat.thermostat.common.dao.NetworkInterfaceInfoDAO; +import com.redhat.thermostat.common.dao.VmInfoDAO; import com.redhat.thermostat.common.dao.VmRef; public interface UiFacadeFactory { void setHostInfoDao(HostInfoDAO hostInfoDao); - void setCpuStatDao(CpuStatDAO cpuStatDAO); + void setMemoryStatDao(MemoryStatDAO memoryStatDAO); + void setNetworkInfoDao(NetworkInterfaceInfoDAO networkInterfaceInfoDAO); - void setMemoryStatDao(MemoryStatDAO memoryStatDAO); - - void setNetworkInfoDao(NetworkInterfaceInfoDAO networkInterfaceInfoDAO); + void setVmInfoDao(VmInfoDAO vmInfoDAO); public MainWindowController getMainWindow();
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/VmInformationController.java Wed Oct 03 18:05:54 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/VmInformationController.java Wed Oct 03 18:05:54 2012 -0400 @@ -45,6 +45,7 @@ import com.redhat.thermostat.client.osgi.service.VmInformationService; import com.redhat.thermostat.client.osgi.service.VmInformationServiceController; import com.redhat.thermostat.common.appctx.ApplicationContext; +import com.redhat.thermostat.common.dao.VmInfoDAO; import com.redhat.thermostat.common.dao.VmRef; public class VmInformationController { @@ -55,8 +56,8 @@ private final VmCpuController cpuController; private final VmGcController gcController; - public VmInformationController(UiFacadeFactory uiFacadeFactory, VmRef vmRef) { - overviewController = new VmOverviewController(vmRef); + public VmInformationController(UiFacadeFactory uiFacadeFactory, VmInfoDAO vmInfoDao, VmRef vmRef) { + overviewController = new VmOverviewController(vmInfoDao, vmRef); cpuController = new VmCpuController(vmRef); gcController = new VmGcController(vmRef);
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/VmOverviewController.java Wed Oct 03 18:05:54 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/VmOverviewController.java Wed Oct 03 18:05:54 2012 -0400 @@ -43,7 +43,6 @@ import java.util.concurrent.TimeUnit; import com.redhat.thermostat.client.locale.LocaleResources; -import com.redhat.thermostat.client.osgi.service.BasicView; import com.redhat.thermostat.client.osgi.service.BasicView.Action; import com.redhat.thermostat.common.ActionEvent; import com.redhat.thermostat.common.ActionListener; @@ -65,11 +64,11 @@ private final VmOverviewView view; - public VmOverviewController(VmRef vmRef) { + public VmOverviewController(VmInfoDAO vmDao, VmRef vmRef) { this.ref = vmRef; this.view = ApplicationContext.getInstance().getViewFactory().getView(VmOverviewView.class); - dao = ApplicationContext.getInstance().getDAOFactory().getVmInfoDAO(); + dao = vmDao; timer = ApplicationContext.getInstance().getTimerFactory().createTimer(); vmRunningTimeFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL);
--- a/client/core/src/test/java/com/redhat/thermostat/client/internal/MainWindowControllerImplTest.java Wed Oct 03 18:05:54 2012 -0400 +++ b/client/core/src/test/java/com/redhat/thermostat/client/internal/MainWindowControllerImplTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -84,7 +84,6 @@ import com.redhat.thermostat.common.ViewFactory; import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.appctx.ApplicationContextUtil; -import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef; import com.redhat.thermostat.common.dao.VmInfoDAO; @@ -143,7 +142,9 @@ uiFacadeFactory = mock(UiFacadeFactory.class); when(uiFacadeFactory.getSummary()).thenReturn(summaryController); - setupDAOs(); + + mockHostsDAO = mock(HostInfoDAO.class); + mockVmsDAO = mock(VmInfoDAO.class); // Setup View view = mock(MainView.class); @@ -185,7 +186,7 @@ setUpVMContextActions(); - controller = new MainWindowControllerImpl(uiFacadeFactory, view, registryFactory, mockHostsDAO); + controller = new MainWindowControllerImpl(uiFacadeFactory, view, registryFactory, mockHostsDAO, mockVmsDAO); l = grabListener.getValue(); hostFiltersListener = grabHostFiltersListener.getValue(); @@ -216,16 +217,6 @@ when(uiFacadeFactory.getVMContextActions()).thenReturn(actions); } - - private void setupDAOs() { - mockHostsDAO = mock(HostInfoDAO.class); - mockVmsDAO = mock(VmInfoDAO.class); - - DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getVmInfoDAO()).thenReturn(mockVmsDAO); - ApplicationContext.getInstance().setDAOFactory(daoFactory); - - } @After public void tearDown() {
--- a/client/core/src/test/java/com/redhat/thermostat/client/ui/SummaryControllerTest.java Wed Oct 03 18:05:54 2012 -0400 +++ b/client/core/src/test/java/com/redhat/thermostat/client/ui/SummaryControllerTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -36,7 +36,6 @@ package com.redhat.thermostat.client.ui; -import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.isNotNull; import static org.mockito.Mockito.doNothing; @@ -44,8 +43,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import java.util.ArrayList; -import java.util.List; import java.util.concurrent.TimeUnit; import org.junit.After; @@ -61,12 +58,8 @@ import com.redhat.thermostat.common.TimerFactory; import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.appctx.ApplicationContextUtil; -import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; -import com.redhat.thermostat.common.dao.HostRef; -import com.redhat.thermostat.common.dao.MongoDAOFactory; import com.redhat.thermostat.common.dao.VmInfoDAO; -import com.redhat.thermostat.common.dao.VmRef; public class SummaryControllerTest { @@ -94,11 +87,6 @@ VmInfoDAO vDAO = mock(VmInfoDAO.class); when(vDAO.getCount()).thenReturn(42l); - DAOFactory daoFactory = mock(MongoDAOFactory.class); - when(daoFactory.getVmInfoDAO()).thenReturn(vDAO); - - ctx.setDAOFactory(daoFactory); - // Setup view view = mock(SummaryView.class); ArgumentCaptor<ActionListener> viewArgumentCaptor = ArgumentCaptor.forClass(ActionListener.class); @@ -108,7 +96,7 @@ when(viewFactory.getView(eq(SummaryView.class))).thenReturn(view); ApplicationContext.getInstance().setViewFactory(viewFactory); - SummaryController summaryCtrl = new SummaryController(hDAO); + SummaryController summaryCtrl = new SummaryController(hDAO, vDAO); timerAction = actionCaptor.getValue(); viewListener = viewArgumentCaptor.getValue();
--- a/client/heapdumper/src/main/java/com/redhat/thermostat/client/heap/LocaleResources.java Wed Oct 03 18:05:54 2012 -0400 +++ b/client/heapdumper/src/main/java/com/redhat/thermostat/client/heap/LocaleResources.java Wed Oct 03 18:05:54 2012 -0400 @@ -39,7 +39,8 @@ public enum LocaleResources { MISSING_INFO, - HOST_SERVICE_UNAVAILALBE, + HOST_SERVICE_UNAVAILABLE, + VM_SERVICE_UNAVAILABLE, HEADER_TIMESTAMP, HEADER_HOST_ID,
--- a/client/heapdumper/src/main/java/com/redhat/thermostat/client/heap/cli/ListHeapDumpsCommand.java Wed Oct 03 18:05:54 2012 -0400 +++ b/client/heapdumper/src/main/java/com/redhat/thermostat/client/heap/cli/ListHeapDumpsCommand.java Wed Oct 03 18:05:54 2012 -0400 @@ -96,10 +96,13 @@ DAOFactory daoFactory = ApplicationContext.getInstance().getDAOFactory(); HostInfoDAO hostDAO = serviceProvider.getServiceAllowNull(HostInfoDAO.class); if (hostDAO == null) { - throw new CommandException(Translate.localize(LocaleResources.HOST_SERVICE_UNAVAILALBE)); + throw new CommandException(Translate.localize(LocaleResources.HOST_SERVICE_UNAVAILABLE)); } - VmInfoDAO vmDAO = daoFactory.getVmInfoDAO(); + VmInfoDAO vmDAO = serviceProvider.getServiceAllowNull(VmInfoDAO.class); + if (vmDAO == null) { + throw new CommandException(Translate.localize(LocaleResources.VM_SERVICE_UNAVAILABLE)); + } HeapDAO heapDAO = daoFactory.getHeapDAO(); Collection<HostRef> hosts = args.getHost() != null ? Arrays.asList(args.getHost()) : hostDAO.getHosts(); @@ -110,6 +113,7 @@ } } + serviceProvider.ungetService(VmInfoDAO.class, vmDAO); serviceProvider.ungetService(HostInfoDAO.class, hostDAO); renderer.render(ctx.getConsole().getOutput());
--- a/client/heapdumper/src/main/resources/com/redhat/thermostat/client/heap/strings.properties Wed Oct 03 18:05:54 2012 -0400 +++ b/client/heapdumper/src/main/resources/com/redhat/thermostat/client/heap/strings.properties Wed Oct 03 18:05:54 2012 -0400 @@ -1,5 +1,6 @@ MISSING_INFO = Missing Information -HOST_SERVICE_UNAVAILALBE = Unable to access host information (HostInfoDAO unavailable) +HOST_SERVICE_UNAVAILABLE = Unable to access host information (HostInfoDAO unavailable) +VM_SERVICE_UNAVAILABLE = Unable to access vm information (VmInfoDAO unavailable) HEADER_TIMESTAMP = TIMESTAMP HEADER_HOST_ID = HOST ID
--- a/client/heapdumper/src/test/java/com/redhat/thermostat/client/heap/cli/ListHeapDumpsCommandTest.java Wed Oct 03 18:05:54 2012 -0400 +++ b/client/heapdumper/src/test/java/com/redhat/thermostat/client/heap/cli/ListHeapDumpsCommandTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -134,10 +134,10 @@ OSGIUtils serviceProvider = mock(OSGIUtils.class); when(serviceProvider.getServiceAllowNull(HostInfoDAO.class)).thenReturn(hostInfo); + when(serviceProvider.getServiceAllowNull(VmInfoDAO.class)).thenReturn(vmInfo); DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getVmInfoDAO()).thenReturn(vmInfo); - + // FIXME the command queries this for the heap dao (null!) which is never used by this test. ApplicationContext.getInstance().setDAOFactory(daoFactory); Command command = new ListHeapDumpsCommand(serviceProvider); @@ -171,9 +171,9 @@ OSGIUtils serviceProvider = mock(OSGIUtils.class); when(serviceProvider.getServiceAllowNull(HostInfoDAO.class)).thenReturn(hostInfo); + when(serviceProvider.getServiceAllowNull(VmInfoDAO.class)).thenReturn(vmInfo); DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getVmInfoDAO()).thenReturn(vmInfo); when(daoFactory.getHeapDAO()).thenReturn(heapDao); ApplicationContext.getInstance().setDAOFactory(daoFactory); @@ -219,9 +219,9 @@ OSGIUtils serviceProvider = mock(OSGIUtils.class); when(serviceProvider.getServiceAllowNull(HostInfoDAO.class)).thenReturn(hostInfo); + when(serviceProvider.getServiceAllowNull(VmInfoDAO.class)).thenReturn(vmInfo); DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getVmInfoDAO()).thenReturn(vmInfo); when(daoFactory.getHeapDAO()).thenReturn(heapDao); ApplicationContext.getInstance().setDAOFactory(daoFactory); @@ -272,9 +272,9 @@ OSGIUtils serviceProvider = mock(OSGIUtils.class); when(serviceProvider.getServiceAllowNull(HostInfoDAO.class)).thenReturn(hostInfo); + when(serviceProvider.getServiceAllowNull(VmInfoDAO.class)).thenReturn(vmInfo); DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getVmInfoDAO()).thenReturn(vmInfo); when(daoFactory.getHeapDAO()).thenReturn(heapDao); ApplicationContext.getInstance().setDAOFactory(daoFactory);
--- a/tools/src/main/java/com/redhat/thermostat/tools/LocaleResources.java Wed Oct 03 18:05:54 2012 -0400 +++ b/tools/src/main/java/com/redhat/thermostat/tools/LocaleResources.java Wed Oct 03 18:05:54 2012 -0400 @@ -43,6 +43,7 @@ VALUE_AND_UNIT, HOST_SERVICE_UNAVAILABLE, + VM_SERVICE_UNAVAILABLE, COMMAND_CONNECT_ALREADY_CONNECTED, COMMAND_CONNECT_FAILED_TO_CONNECT,
--- a/tools/src/main/java/com/redhat/thermostat/tools/cli/ListVMsCommand.java Wed Oct 03 18:05:54 2012 -0400 +++ b/tools/src/main/java/com/redhat/thermostat/tools/cli/ListVMsCommand.java Wed Oct 03 18:05:54 2012 -0400 @@ -38,11 +38,9 @@ import java.util.Collection; -import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.cli.CommandContext; import com.redhat.thermostat.common.cli.CommandException; import com.redhat.thermostat.common.cli.SimpleCommand; -import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef; import com.redhat.thermostat.common.dao.VmInfoDAO; @@ -69,8 +67,6 @@ @Override public void run(CommandContext ctx) throws CommandException { - DAOFactory daoFactory = ApplicationContext.getInstance().getDAOFactory(); - HostInfoDAO hostsDAO = serviceProvider.getServiceAllowNull(HostInfoDAO.class); if (hostsDAO == null) { throw new CommandException(Translate.localize(LocaleResources.HOST_SERVICE_UNAVAILABLE)); @@ -78,7 +74,10 @@ Collection<HostRef> hosts = hostsDAO.getHosts(); serviceProvider.ungetService(HostInfoDAO.class, hostsDAO); - VmInfoDAO vmsDAO = daoFactory.getVmInfoDAO(); + VmInfoDAO vmsDAO = serviceProvider.getServiceAllowNull(VmInfoDAO.class); + if (vmsDAO == null) { + throw new CommandException(Translate.localize(LocaleResources.VM_SERVICE_UNAVAILABLE)); + } VMListFormatter formatter = new VMListFormatter(); for (HostRef host : hosts) { Collection<VmRef> vms = vmsDAO.getVMs(host); @@ -88,6 +87,8 @@ } } formatter.format(ctx.getConsole().getOutput()); + + serviceProvider.ungetService(VmInfoDAO.class, vmsDAO); } @Override
--- a/tools/src/main/java/com/redhat/thermostat/tools/cli/VMInfoCommand.java Wed Oct 03 18:05:54 2012 -0400 +++ b/tools/src/main/java/com/redhat/thermostat/tools/cli/VMInfoCommand.java Wed Oct 03 18:05:54 2012 -0400 @@ -40,18 +40,17 @@ import java.util.Collection; import java.util.Date; -import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.cli.CommandContext; import com.redhat.thermostat.common.cli.CommandException; import com.redhat.thermostat.common.cli.HostVMArguments; import com.redhat.thermostat.common.cli.SimpleCommand; import com.redhat.thermostat.common.cli.TableRenderer; import com.redhat.thermostat.common.dao.DAOException; -import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostRef; import com.redhat.thermostat.common.dao.VmInfoDAO; import com.redhat.thermostat.common.dao.VmRef; import com.redhat.thermostat.common.model.VmInfo; +import com.redhat.thermostat.common.utils.OSGIUtils; import com.redhat.thermostat.tools.LocaleResources; import com.redhat.thermostat.tools.Translate; @@ -60,10 +59,24 @@ private static final String NAME = "vm-info"; private static final String STILL_ALIVE = Translate.localize(LocaleResources.VM_STOP_TIME_RUNNING); + private OSGIUtils serviceProvider; + + public VMInfoCommand() { + this(OSGIUtils.getInstance()); + } + + /** For tests only */ + VMInfoCommand(OSGIUtils serviceProvider) { + this.serviceProvider = serviceProvider; + } + @Override public void run(CommandContext ctx) throws CommandException { - DAOFactory daoFactory = ApplicationContext.getInstance().getDAOFactory(); - VmInfoDAO vmsDAO = daoFactory.getVmInfoDAO(); + VmInfoDAO vmsDAO = serviceProvider.getServiceAllowNull(VmInfoDAO.class); + if (vmsDAO == null) { + throw new CommandException(Translate.localize(LocaleResources.VM_SERVICE_UNAVAILABLE)); + } + HostVMArguments hostVMArgs = new HostVMArguments(ctx.getArguments(), true, false); HostRef host = hostVMArgs.getHost(); VmRef vm = hostVMArgs.getVM(); @@ -76,6 +89,8 @@ } } catch (DAOException ex) { ctx.getConsole().getError().println(ex.getMessage()); + } finally { + serviceProvider.ungetService(VmInfoDAO.class, vmsDAO); } }
--- a/tools/src/main/resources/com/redhat/thermostat/tools/strings.properties Wed Oct 03 18:05:54 2012 -0400 +++ b/tools/src/main/resources/com/redhat/thermostat/tools/strings.properties Wed Oct 03 18:05:54 2012 -0400 @@ -3,6 +3,7 @@ VALUE_AND_UNIT = {0} {1} HOST_SERVICE_UNAVAILABLE = Unable to get host information (HostInfoDAO is unavailable) +VM_SERVICE_UNAVAILABLE = Unable to get vm information (VmInfoDAO is unavailable) COMMAND_CONNECT_ALREADY_CONNECTED = Already connected to storage. Please use disconnect command to disconnect. COMMAND_CONNECT_FAILED_TO_CONNECT = Could not connect to db {0}
--- a/tools/src/test/java/com/redhat/thermostat/tools/cli/ListVMsCommandTest.java Wed Oct 03 18:05:54 2012 -0400 +++ b/tools/src/test/java/com/redhat/thermostat/tools/cli/ListVMsCommandTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -50,12 +50,9 @@ import org.junit.Before; import org.junit.Test; -import com.redhat.thermostat.common.appctx.ApplicationContext; -import com.redhat.thermostat.common.appctx.ApplicationContextUtil; import com.redhat.thermostat.common.cli.CommandContext; import com.redhat.thermostat.common.cli.CommandException; import com.redhat.thermostat.common.cli.SimpleArguments; -import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef; import com.redhat.thermostat.common.dao.VmInfoDAO; @@ -74,36 +71,28 @@ @Before public void setUp() { - ApplicationContextUtil.resetApplicationContext(); setupCommandContextFactory(); serviceProvider = mock(OSGIUtils.class); cmd = new ListVMsCommand(serviceProvider); - setupDAOs(); + hostsDAO = mock(HostInfoDAO.class); + vmsDAO = mock(VmInfoDAO.class); when(serviceProvider.getServiceAllowNull(HostInfoDAO.class)).thenReturn(hostsDAO); + when(serviceProvider.getServiceAllowNull(VmInfoDAO.class)).thenReturn(vmsDAO); } private void setupCommandContextFactory() { cmdCtxFactory = new TestCommandContextFactory(); } - private void setupDAOs() { - hostsDAO = mock(HostInfoDAO.class); - vmsDAO = mock(VmInfoDAO.class); - DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getVmInfoDAO()).thenReturn(vmsDAO); - ApplicationContext.getInstance().setDAOFactory(daoFactory); - } - @After public void tearDown() { vmsDAO = null; hostsDAO = null; cmdCtxFactory = null; cmd = null; - ApplicationContextUtil.resetApplicationContext(); } @Test
--- a/tools/src/test/java/com/redhat/thermostat/tools/cli/VMInfoCommandTest.java Wed Oct 03 18:05:54 2012 -0400 +++ b/tools/src/test/java/com/redhat/thermostat/tools/cli/VMInfoCommandTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -67,6 +67,7 @@ import com.redhat.thermostat.common.dao.VmInfoDAO; import com.redhat.thermostat.common.dao.VmRef; import com.redhat.thermostat.common.model.VmInfo; +import com.redhat.thermostat.common.utils.OSGIUtils; import com.redhat.thermostat.test.Bug; import com.redhat.thermostat.test.TestCommandContextFactory; @@ -95,10 +96,14 @@ ApplicationContextUtil.resetApplicationContext(); setupCommandContextFactory(); - cmd = new VMInfoCommand(); + vmsDAO = mock(VmInfoDAO.class); + + OSGIUtils serviceProvider = mock(OSGIUtils.class); + when(serviceProvider.getServiceAllowNull(VmInfoDAO.class)).thenReturn(vmsDAO); + + cmd = new VMInfoCommand(serviceProvider); setupDAOs(); - } private void setupCommandContextFactory() { @@ -106,7 +111,6 @@ } private void setupDAOs() { - vmsDAO = mock(VmInfoDAO.class); HostRef host = new HostRef("123", "dummy"); vm = new VmRef(host, 234, "dummy"); Calendar start = Calendar.getInstance(); @@ -117,9 +121,6 @@ when(vmsDAO.getVmInfo(vm)).thenReturn(vmInfo); when(vmsDAO.getVmInfo(new VmRef(host, 9876, "dummy"))).thenThrow(new DAOException("Unknown VM ID: 9876")); when(vmsDAO.getVMs(host)).thenReturn(Arrays.asList(vm)); - DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getVmInfoDAO()).thenReturn(vmsDAO); - ApplicationContext.getInstance().setDAOFactory(daoFactory); }