Mercurial > hg > release > thermostat-1.2
changeset 666:fcc75743c583
Use HostInfoDAO as a service
Use HostInfoDAO as a service rather than using it through DaoFactory.
Also fix cli bits to register all DAOs as services too. Cli commands
should work now too - they were broken by the AgentInfoDAO patch.
Reviewed-by: jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-September/003374.html
line wrap: on
line diff
--- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/AgentApplication.java Wed Oct 03 16:21:59 2012 -0400 +++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/AgentApplication.java Wed Oct 03 18:05:54 2012 -0400 @@ -58,25 +58,12 @@ import com.redhat.thermostat.common.cli.CommandContext; import com.redhat.thermostat.common.cli.CommandException; import com.redhat.thermostat.common.config.InvalidConfigurationException; -import com.redhat.thermostat.common.dao.AgentInfoDAO; -import com.redhat.thermostat.common.dao.BackendInfoDAO; -import com.redhat.thermostat.common.dao.CpuStatDAO; import com.redhat.thermostat.common.dao.DAOFactory; -import com.redhat.thermostat.common.dao.HeapDAO; -import com.redhat.thermostat.common.dao.HostInfoDAO; -import com.redhat.thermostat.common.dao.MemoryStatDAO; import com.redhat.thermostat.common.dao.MongoDAOFactory; -import com.redhat.thermostat.common.dao.NetworkInterfaceInfoDAO; -import com.redhat.thermostat.common.dao.VmClassStatDAO; -import com.redhat.thermostat.common.dao.VmCpuStatDAO; -import com.redhat.thermostat.common.dao.VmGcStatDAO; -import com.redhat.thermostat.common.dao.VmInfoDAO; -import com.redhat.thermostat.common.dao.VmMemoryStatDAO; import com.redhat.thermostat.common.storage.Connection; import com.redhat.thermostat.common.storage.Connection.ConnectionListener; import com.redhat.thermostat.common.storage.Connection.ConnectionStatus; import com.redhat.thermostat.common.storage.MongoStorageProvider; -import com.redhat.thermostat.common.storage.Storage; import com.redhat.thermostat.common.storage.StorageProvider; import com.redhat.thermostat.common.tools.BasicCommand; import com.redhat.thermostat.common.utils.LoggingUtils; @@ -132,7 +119,7 @@ break; case CONNECTED: logger.fine("Connected to storage, registering storage as service"); - registerDAOsAndStorageAsOSGiServices(daoFactory); + daoFactory.registerDAOsAndStorageAsOSGiServices(); break; case FAILED_TO_CONNECT: logger.warning("Could not connect to storage."); @@ -186,27 +173,6 @@ return; } } - - private void registerDAOsAndStorageAsOSGiServices(DAOFactory daoFactory) { - OSGIUtils registerer = OSGIUtils.getInstance(); - - registerer.registerService(Storage.class, daoFactory.getStorage()); - - registerer.registerService(AgentInfoDAO.class, daoFactory.getAgentInfoDAO()); - registerer.registerService(BackendInfoDAO.class, daoFactory.getBackendInfoDAO()); - - registerer.registerService(HostInfoDAO.class, daoFactory.getHostInfoDAO()); - registerer.registerService(NetworkInterfaceInfoDAO.class, daoFactory.getNetworkInterfaceInfoDAO()); - registerer.registerService(CpuStatDAO.class, daoFactory.getCpuStatDAO()); - registerer.registerService(MemoryStatDAO.class, daoFactory.getMemoryStatDAO()); - - registerer.registerService(VmInfoDAO.class, daoFactory.getVmInfoDAO()); - registerer.registerService(VmClassStatDAO.class, daoFactory.getVmClassStatsDAO()); - registerer.registerService(VmCpuStatDAO.class, daoFactory.getVmCpuStatDAO()); - registerer.registerService(VmGcStatDAO.class, daoFactory.getVmGcStatDAO()); - registerer.registerService(VmMemoryStatDAO.class, daoFactory.getVmMemoryStatDAO()); - registerer.registerService(HeapDAO.class, daoFactory.getHeapDAO()); - } @Override public void run(CommandContext ctx) throws CommandException {
--- a/client/command/src/main/java/com/redhat/thermostat/client/command/cli/PingCommand.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/command/src/main/java/com/redhat/thermostat/client/command/cli/PingCommand.java Wed Oct 03 18:05:54 2012 -0400 @@ -115,19 +115,24 @@ printCustomMessageWithUsage(out, "Ping command accepts one and only one argument."); return; } - DAOFactory df = ApplicationContext.getInstance().getDAOFactory(); - HostInfoDAO dao = df.getHostInfoDAO(); - HostRef targetHostRef = getHostRef(dao, agentId); + + HostInfoDAO hostInfoDao = serviceProvider.getServiceAllowNull(HostInfoDAO.class); + if (hostInfoDao == null) { + throw new CommandException("Unable to access host information: service not available"); + } + HostRef targetHostRef = getHostRef(hostInfoDao, agentId); + serviceProvider.ungetService(HostInfoDAO.class, hostInfoDao); + if (targetHostRef == null) { printCustomMessageWithUsage(out, "Invalid host ID or agent no longer running. See \'help list-vms to obtain a valid host ID."); return; } - AgentInfoDAO service = serviceProvider.getService(AgentInfoDAO.class); - if (service == null) { + AgentInfoDAO agentInfoDao = serviceProvider.getService(AgentInfoDAO.class); + if (agentInfoDao == null) { throw new CommandException("Unable to access agent information: service not available"); } - String address = service.getAgentInformation(targetHostRef).getConfigListenAddress(); - serviceProvider.ungetService(service); + String address = agentInfoDao.getAgentInformation(targetHostRef).getConfigListenAddress(); + serviceProvider.ungetService(AgentInfoDAO.class, agentInfoDao); String [] host = address.split(":"); InetSocketAddress target = new InetSocketAddress(host[0], Integer.parseInt(host[1]));
--- a/client/core/src/main/java/com/redhat/thermostat/client/internal/HostPanelFacadeImpl.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/internal/HostPanelFacadeImpl.java Wed Oct 03 18:05:54 2012 -0400 @@ -39,6 +39,7 @@ import com.redhat.thermostat.client.ui.HostCpuController; import com.redhat.thermostat.client.ui.HostMemoryController; import com.redhat.thermostat.client.ui.HostOverviewController; +import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef; public class HostPanelFacadeImpl implements HostPanelFacade { @@ -47,10 +48,10 @@ private final HostCpuController cpuController; private final HostMemoryController memoryController; - public HostPanelFacadeImpl(HostRef ref) { - overviewController = new HostOverviewController(ref); - cpuController = new HostCpuController(ref); - memoryController = new HostMemoryController(ref); + public HostPanelFacadeImpl(HostInfoDAO hostInfoDao, HostRef ref) { + overviewController = new HostOverviewController(hostInfoDao, ref); + cpuController = new HostCpuController(hostInfoDao, ref); + memoryController = new HostMemoryController(hostInfoDao, ref); } @Override
--- a/client/core/src/main/java/com/redhat/thermostat/client/internal/Main.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/internal/Main.java Wed Oct 03 18:05:54 2012 -0400 @@ -67,26 +67,13 @@ import com.redhat.thermostat.common.appctx.ApplicationContext; import com.redhat.thermostat.common.config.ClientPreferences; import com.redhat.thermostat.common.config.StartupConfiguration; -import com.redhat.thermostat.common.dao.AgentInfoDAO; -import com.redhat.thermostat.common.dao.BackendInfoDAO; -import com.redhat.thermostat.common.dao.CpuStatDAO; import com.redhat.thermostat.common.dao.DAOFactory; -import com.redhat.thermostat.common.dao.HeapDAO; -import com.redhat.thermostat.common.dao.HostInfoDAO; -import com.redhat.thermostat.common.dao.MemoryStatDAO; import com.redhat.thermostat.common.dao.MongoDAOFactory; -import com.redhat.thermostat.common.dao.NetworkInterfaceInfoDAO; -import com.redhat.thermostat.common.dao.VmClassStatDAO; -import com.redhat.thermostat.common.dao.VmCpuStatDAO; -import com.redhat.thermostat.common.dao.VmGcStatDAO; -import com.redhat.thermostat.common.dao.VmInfoDAO; -import com.redhat.thermostat.common.dao.VmMemoryStatDAO; import com.redhat.thermostat.common.storage.Connection; import com.redhat.thermostat.common.storage.Connection.ConnectionListener; import com.redhat.thermostat.common.storage.Connection.ConnectionStatus; import com.redhat.thermostat.common.storage.Connection.ConnectionType; import com.redhat.thermostat.common.storage.MongoStorageProvider; -import com.redhat.thermostat.common.storage.Storage; import com.redhat.thermostat.common.storage.StorageProvider; import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.common.utils.OSGIUtils; @@ -296,7 +283,8 @@ // register the storage, so other services can request it DAOFactory daoFactory = ApplicationContext.getInstance().getDAOFactory(); - registerDAOsAndStorageAsOSGiServices(daoFactory); + daoFactory.registerDAOsAndStorageAsOSGiServices(); + uiFacadeFactory.setHostInfoDao(daoFactory.getHostInfoDAO()); showMainWindow(); } else if (newStatus == ConnectionStatus.FAILED_TO_CONNECT) { @@ -314,27 +302,7 @@ } } } - - private void registerDAOsAndStorageAsOSGiServices(DAOFactory daoFactory) { - OSGIUtils registerer = OSGIUtils.getInstance(); - registerer.registerService(Storage.class, daoFactory.getStorage()); - - registerer.registerService(AgentInfoDAO.class, daoFactory.getAgentInfoDAO()); - registerer.registerService(BackendInfoDAO.class, daoFactory.getBackendInfoDAO()); - - registerer.registerService(HostInfoDAO.class, daoFactory.getHostInfoDAO()); - registerer.registerService(NetworkInterfaceInfoDAO.class, daoFactory.getNetworkInterfaceInfoDAO()); - registerer.registerService(CpuStatDAO.class, daoFactory.getCpuStatDAO()); - registerer.registerService(MemoryStatDAO.class, daoFactory.getMemoryStatDAO()); - - registerer.registerService(VmInfoDAO.class, daoFactory.getVmInfoDAO()); - registerer.registerService(VmClassStatDAO.class, daoFactory.getVmClassStatsDAO()); - registerer.registerService(VmCpuStatDAO.class, daoFactory.getVmCpuStatDAO()); - registerer.registerService(VmGcStatDAO.class, daoFactory.getVmGcStatDAO()); - registerer.registerService(VmMemoryStatDAO.class, daoFactory.getVmMemoryStatDAO()); - registerer.registerService(HeapDAO.class, daoFactory.getHeapDAO()); - } private void showMainWindow() { SwingUtilities.invokeLater(new ShowMainWindow());
--- a/client/core/src/main/java/com/redhat/thermostat/client/internal/MainWindowControllerImpl.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/internal/MainWindowControllerImpl.java Wed Oct 03 18:05:54 2012 -0400 @@ -162,7 +162,7 @@ private VmInformationControllerProvider vmInfoControllerProvider; - public MainWindowControllerImpl(UiFacadeFactory facadeFactory, MainView view, RegistryFactory registryFactory) + public MainWindowControllerImpl(UiFacadeFactory facadeFactory, MainView view, RegistryFactory registryFactory, HostInfoDAO hostsDao) { try { vmFilterRegistry = registryFactory.createVmFilterRegistry(); @@ -184,7 +184,7 @@ ApplicationContext ctx = ApplicationContext.getInstance(); DAOFactory daoFactory = ctx.getDAOFactory(); - hostsDAO = daoFactory.getHostInfoDAO(); + this.hostsDAO = hostsDao; vmsDAO = daoFactory.getVmInfoDAO(); initView(view); @@ -426,7 +426,7 @@ Ref ref = view.getSelectedHostOrVm(); if (ref == null) { - SummaryController controller = new SummaryController(); + SummaryController controller = new SummaryController(hostsDAO); 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 16:21:59 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/internal/UiFacadeFactoryImpl.java Wed Oct 03 18:05:54 2012 -0400 @@ -50,6 +50,7 @@ import com.redhat.thermostat.client.ui.SummaryController; import com.redhat.thermostat.client.ui.UiFacadeFactory; import com.redhat.thermostat.client.ui.VmInformationController; +import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef; import com.redhat.thermostat.common.dao.VmRef; @@ -61,27 +62,33 @@ private Collection<VMContextAction> contextAction = new ArrayList<>(); private BundleContext context; + private HostInfoDAO hostInfoDao; public UiFacadeFactoryImpl(BundleContext context) { this.context = context; } @Override + public void setHostInfoDao(HostInfoDAO hostInfoDao) { + this.hostInfoDao = hostInfoDao; + } + + @Override public MainWindowController getMainWindow() { MainView mainView = new MainWindow(); RegistryFactory registryFactory = new RegistryFactory(context); - return new MainWindowControllerImpl(this, mainView, registryFactory); + return new MainWindowControllerImpl(this, mainView, registryFactory, hostInfoDao); } @Override public SummaryController getSummary() { - return new SummaryController(); + return new SummaryController(hostInfoDao); } @Override public HostInformationController getHostController(HostRef ref) { - return new HostInformationController(ref); + return new HostInformationController(hostInfoDao, ref); }
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/HostCpuController.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/HostCpuController.java Wed Oct 03 18:05:54 2012 -0400 @@ -70,12 +70,12 @@ private int chartsAdded = 0; private long lastSeenTimeStamp = Long.MIN_VALUE; - public HostCpuController(HostRef ref) { + public HostCpuController(HostInfoDAO hostInfoDao, HostRef ref) { this.ref = ref; view = ApplicationContext.getInstance().getViewFactory().getView(HostCpuView.class); view.clearCpuUsageData(); DAOFactory daos = ApplicationContext.getInstance().getDAOFactory(); - hostInfoDAO = daos.getHostInfoDAO(); + this.hostInfoDAO = hostInfoDao; cpuStatDAO = daos.getCpuStatDAO(); backgroundUpdateTimer = ApplicationContext.getInstance().getTimerFactory().createTimer();
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/HostInformationController.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/HostInformationController.java Wed Oct 03 18:05:54 2012 -0400 @@ -41,6 +41,7 @@ import com.redhat.thermostat.client.locale.LocaleResources; import com.redhat.thermostat.client.osgi.service.BasicView; import com.redhat.thermostat.common.appctx.ApplicationContext; +import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef; public class HostInformationController { @@ -51,10 +52,10 @@ private final HostInformationView view; - public HostInformationController(HostRef ref) { - overviewController = new HostOverviewController(ref); - cpuController = new HostCpuController(ref); - memoryController = new HostMemoryController(ref); + public HostInformationController(HostInfoDAO hostInfoDao, HostRef ref) { + overviewController = new HostOverviewController(hostInfoDao, ref); + cpuController = new HostCpuController(hostInfoDao, ref); + memoryController = new HostMemoryController(hostInfoDao ,ref); view = ApplicationContext.getInstance().getViewFactory().getView(HostInformationView.class);
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/HostMemoryController.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/HostMemoryController.java Wed Oct 03 18:05:54 2012 -0400 @@ -74,10 +74,10 @@ private long lastSeenTimeStamp = Long.MIN_VALUE; - public HostMemoryController(final HostRef ref) { + public HostMemoryController(HostInfoDAO hostInfoDAO, final HostRef ref) { this.ref = ref; DAOFactory daos = ApplicationContext.getInstance().getDAOFactory(); - hostInfoDAO = daos.getHostInfoDAO(); + this.hostInfoDAO = hostInfoDAO; memoryStatDAO = daos.getMemoryStatDAO(); view = ApplicationContext.getInstance().getViewFactory().getView(HostMemoryView.class); @@ -110,7 +110,7 @@ backgroundUpdateTimer.setAction(new Runnable() { @Override public void run() { - long memorySize = hostInfoDAO.getHostInfo(ref).getTotalMemory(); + long memorySize = HostMemoryController.this.hostInfoDAO.getHostInfo(ref).getTotalMemory(); String[] memorySizeParts = DisplayableValues.bytes(memorySize); view.setTotalMemory(localize(LocaleResources.NUMBER_AND_UNIT, memorySizeParts[0], memorySizeParts[1])); doMemoryChartUpdate();
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/HostOverviewController.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/HostOverviewController.java Wed Oct 03 18:05:54 2012 -0400 @@ -75,10 +75,10 @@ private final HostOverviewView view; - public HostOverviewController(final HostRef ref) { + public HostOverviewController(HostInfoDAO hostInfoDAO, final HostRef ref) { this.ref = ref; DAOFactory df = ApplicationContext.getInstance().getDAOFactory(); - hostInfoDAO = df.getHostInfoDAO(); + this.hostInfoDAO = hostInfoDAO; networkInfoDAO = df.getNetworkInterfaceInfoDAO(); final Vector<String> networkTableColumnVector; @@ -91,7 +91,7 @@ backgroundUpdateTimer.setAction(new Runnable() { @Override public void run() { - HostInfo hostInfo = hostInfoDAO.getHostInfo(ref); + HostInfo hostInfo = HostOverviewController.this.hostInfoDAO.getHostInfo(ref); view.setHostName(hostInfo.getHostname()); view.setOsName(hostInfo.getOsName()); view.setOsKernel(hostInfo.getOsKernel());
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/SummaryController.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/SummaryController.java Wed Oct 03 18:05:54 2012 -0400 @@ -59,7 +59,7 @@ private final Timer backgroundUpdateTimer; - public SummaryController() { + public SummaryController(HostInfoDAO hostInfoDao) { ApplicationContext ctx = ApplicationContext.getInstance(); this.view = ctx.getViewFactory().getView(SummaryView.class); @@ -81,7 +81,7 @@ }); DAOFactory daoFactory = ctx.getDAOFactory(); - hostsDAO = daoFactory.getHostInfoDAO(); + hostsDAO = hostInfoDao; vmsDAO = daoFactory.getVmInfoDAO(); backgroundUpdateTimer = ApplicationContext.getInstance().getTimerFactory().createTimer();
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/UiFacadeFactory.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/UiFacadeFactory.java Wed Oct 03 18:05:54 2012 -0400 @@ -40,11 +40,14 @@ import com.redhat.thermostat.client.osgi.service.VMContextAction; import com.redhat.thermostat.client.osgi.service.VmInformationService; +import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef; import com.redhat.thermostat.common.dao.VmRef; public interface UiFacadeFactory { + void setHostInfoDao(HostInfoDAO hostInfoDao); + public MainWindowController getMainWindow(); public SummaryController getSummary();
--- a/client/core/src/test/java/com/redhat/thermostat/client/internal/MainWindowControllerImplTest.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/test/java/com/redhat/thermostat/client/internal/MainWindowControllerImplTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -185,7 +185,7 @@ setUpVMContextActions(); - controller = new MainWindowControllerImpl(uiFacadeFactory, view, registryFactory); + controller = new MainWindowControllerImpl(uiFacadeFactory, view, registryFactory, mockHostsDAO); l = grabListener.getValue(); hostFiltersListener = grabHostFiltersListener.getValue(); @@ -222,7 +222,6 @@ mockVmsDAO = mock(VmInfoDAO.class); DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getHostInfoDAO()).thenReturn(mockHostsDAO); when(daoFactory.getVmInfoDAO()).thenReturn(mockVmsDAO); ApplicationContext.getInstance().setDAOFactory(daoFactory);
--- a/client/core/src/test/java/com/redhat/thermostat/client/ui/HostCpuControllerTest.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/test/java/com/redhat/thermostat/client/ui/HostCpuControllerTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -107,7 +107,6 @@ when(cpuStatDAO.getLatestCpuStats(any(HostRef.class), anyLong())).thenReturn(Arrays.asList(cpuStat1, cpuStat2)); DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getHostInfoDAO()).thenReturn(hostInfoDAO); when(daoFactory.getCpuStatDAO()).thenReturn(cpuStatDAO); ApplicationContext.getInstance().setDAOFactory(daoFactory); @@ -122,7 +121,7 @@ ApplicationContext.getInstance().setViewFactory(viewFactory); HostRef host = new HostRef("123", "fluffhost"); - controller = new HostCpuController(host); + controller = new HostCpuController(hostInfoDAO, host); timerAction = actionCaptor.getValue(); viewListener = viewArgumentCaptor.getValue();
--- a/client/core/src/test/java/com/redhat/thermostat/client/ui/HostMemoryControllerTest.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/test/java/com/redhat/thermostat/client/ui/HostMemoryControllerTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -89,7 +89,6 @@ when(memoryStatDAO.getLatestMemoryStats(any(HostRef.class), anyLong())).thenReturn(memoryStats); DAOFactory daoFactory = mock(MongoDAOFactory.class); - when(daoFactory.getHostInfoDAO()).thenReturn(hostInfoDAO); when(daoFactory.getMemoryStatDAO()).thenReturn(memoryStatDAO); ApplicationContext.getInstance().setDAOFactory(daoFactory); @@ -111,7 +110,7 @@ when(viewFactory.getView(eq(HostMemoryView.class))).thenReturn(view); ApplicationContext.getInstance().setViewFactory(viewFactory); - HostMemoryController controller = new HostMemoryController(ref); + HostMemoryController controller = new HostMemoryController(hostInfoDAO, ref); ActionListener<HostMemoryView.Action> l = viewArgumentCaptor.getValue();
--- a/client/core/src/test/java/com/redhat/thermostat/client/ui/HostOverviewControllerTest.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/test/java/com/redhat/thermostat/client/ui/HostOverviewControllerTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -119,7 +119,6 @@ NetworkInterfaceInfoDAO networkInfoDao = mock(NetworkInterfaceInfoDAO.class); when(networkInfoDao.getNetworkInterfaces(any(HostRef.class))).thenReturn(networkInfo); - when(daoFactory.getHostInfoDAO()).thenReturn(hostInfoDao); when(daoFactory.getNetworkInterfaceInfoDAO()).thenReturn(networkInfoDao); ApplicationContext.getInstance().setDAOFactory(daoFactory); @@ -133,7 +132,7 @@ ApplicationContext.getInstance().setViewFactory(viewFactory); - HostOverviewController controller = new HostOverviewController(ref); + HostOverviewController controller = new HostOverviewController(hostInfoDao, ref); listener = listenerCaptor.getValue(); timerAction = timerActionCaptor.getValue();
--- a/client/core/src/test/java/com/redhat/thermostat/client/ui/SummaryControllerTest.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/core/src/test/java/com/redhat/thermostat/client/ui/SummaryControllerTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -95,7 +95,6 @@ when(vDAO.getCount()).thenReturn(42l); DAOFactory daoFactory = mock(MongoDAOFactory.class); - when(daoFactory.getHostInfoDAO()).thenReturn(hDAO); when(daoFactory.getVmInfoDAO()).thenReturn(vDAO); ctx.setDAOFactory(daoFactory); @@ -109,7 +108,7 @@ when(viewFactory.getView(eq(SummaryView.class))).thenReturn(view); ApplicationContext.getInstance().setViewFactory(viewFactory); - SummaryController summaryCtrl = new SummaryController(); + SummaryController summaryCtrl = new SummaryController(hDAO); timerAction = actionCaptor.getValue(); viewListener = viewArgumentCaptor.getValue();
--- a/client/heapdumper/src/main/java/com/redhat/thermostat/client/heap/LocaleResources.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/heapdumper/src/main/java/com/redhat/thermostat/client/heap/LocaleResources.java Wed Oct 03 18:05:54 2012 -0400 @@ -39,6 +39,7 @@ public enum LocaleResources { MISSING_INFO, + HOST_SERVICE_UNAVAILALBE, HEADER_TIMESTAMP, HEADER_HOST_ID,
--- a/client/heapdumper/src/main/java/com/redhat/thermostat/client/heap/cli/DumpHeapCommand.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/heapdumper/src/main/java/com/redhat/thermostat/client/heap/cli/DumpHeapCommand.java Wed Oct 03 18:05:54 2012 -0400 @@ -87,7 +87,7 @@ throw new CommandException("Unable to access agent information"); } implementation.execute(service, args.getVM(), r); - serviceProvider.ungetService(service); + serviceProvider.ungetService(AgentInfoDAO.class, service); try { s.acquire();
--- a/client/heapdumper/src/main/java/com/redhat/thermostat/client/heap/cli/ListHeapDumpsCommand.java Wed Oct 03 16:21:59 2012 -0400 +++ b/client/heapdumper/src/main/java/com/redhat/thermostat/client/heap/cli/ListHeapDumpsCommand.java Wed Oct 03 18:05:54 2012 -0400 @@ -55,6 +55,7 @@ import com.redhat.thermostat.common.dao.VmInfoDAO; import com.redhat.thermostat.common.dao.VmRef; import com.redhat.thermostat.common.model.HeapInfo; +import com.redhat.thermostat.common.utils.OSGIUtils; public class ListHeapDumpsCommand extends SimpleCommand { @@ -68,6 +69,17 @@ Translate.localize(LocaleResources.HEADER_TIMESTAMP), }; + private final OSGIUtils serviceProvider; + + public ListHeapDumpsCommand() { + this(OSGIUtils.getInstance()); + } + + /** For tests only */ + ListHeapDumpsCommand(OSGIUtils serviceProvider) { + this.serviceProvider = serviceProvider; + } + @Override public String getName() { return NAME; @@ -82,7 +94,11 @@ renderer.printLine(COLUMN_NAMES); DAOFactory daoFactory = ApplicationContext.getInstance().getDAOFactory(); - HostInfoDAO hostDAO = daoFactory.getHostInfoDAO(); + HostInfoDAO hostDAO = serviceProvider.getServiceAllowNull(HostInfoDAO.class); + if (hostDAO == null) { + throw new CommandException(Translate.localize(LocaleResources.HOST_SERVICE_UNAVAILALBE)); + } + VmInfoDAO vmDAO = daoFactory.getVmInfoDAO(); HeapDAO heapDAO = daoFactory.getHeapDAO(); @@ -94,6 +110,8 @@ } } + 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 16:21:59 2012 -0400 +++ b/client/heapdumper/src/main/resources/com/redhat/thermostat/client/heap/strings.properties Wed Oct 03 18:05:54 2012 -0400 @@ -1,4 +1,5 @@ MISSING_INFO = Missing Information +HOST_SERVICE_UNAVAILALBE = Unable to access host information (HostInfoDAO 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 16:21:59 2012 -0400 +++ b/client/heapdumper/src/test/java/com/redhat/thermostat/client/heap/cli/ListHeapDumpsCommandTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -66,6 +66,7 @@ import com.redhat.thermostat.common.dao.VmInfoDAO; import com.redhat.thermostat.common.dao.VmRef; import com.redhat.thermostat.common.model.HeapInfo; +import com.redhat.thermostat.common.utils.OSGIUtils; import com.redhat.thermostat.test.TestCommandContextFactory; public class ListHeapDumpsCommandTest { @@ -112,18 +113,34 @@ } @Test + public void verifyFailsWithoutHostDao() throws Exception { + OSGIUtils serviceProvider = mock(OSGIUtils.class); + + Command command = new ListHeapDumpsCommand(serviceProvider); + TestCommandContextFactory factory = new TestCommandContextFactory(); + try { + command.run(factory.createContext(new SimpleArguments())); + } catch (CommandException hostDaoNotAvailableException) { + assertEquals("Unable to access host information (HostInfoDAO unavailable)", + hostDaoNotAvailableException.getMessage()); + } + } + + @Test public void verifyWorksWithoutAnyInformation() throws CommandException { HostInfoDAO hostInfo = mock(HostInfoDAO.class); VmInfoDAO vmInfo = mock(VmInfoDAO.class); + OSGIUtils serviceProvider = mock(OSGIUtils.class); + when(serviceProvider.getServiceAllowNull(HostInfoDAO.class)).thenReturn(hostInfo); + DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getHostInfoDAO()).thenReturn(hostInfo); when(daoFactory.getVmInfoDAO()).thenReturn(vmInfo); ApplicationContext.getInstance().setDAOFactory(daoFactory); - Command command = new ListHeapDumpsCommand(); + Command command = new ListHeapDumpsCommand(serviceProvider); TestCommandContextFactory factory = new TestCommandContextFactory(); command.run(factory.createContext(new SimpleArguments())); assertEquals("HOST ID VM ID HEAP ID TIMESTAMP\n", factory.getOutput()); @@ -152,14 +169,16 @@ when(heapDao.getAllHeapInfo(vmRef)).thenReturn(Arrays.asList(heapInfo)); + OSGIUtils serviceProvider = mock(OSGIUtils.class); + when(serviceProvider.getServiceAllowNull(HostInfoDAO.class)).thenReturn(hostInfo); + DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getHostInfoDAO()).thenReturn(hostInfo); when(daoFactory.getVmInfoDAO()).thenReturn(vmInfo); when(daoFactory.getHeapDAO()).thenReturn(heapDao); ApplicationContext.getInstance().setDAOFactory(daoFactory); - Command command = new ListHeapDumpsCommand(); + Command command = new ListHeapDumpsCommand(serviceProvider); TestCommandContextFactory factory = new TestCommandContextFactory(); command.run(factory.createContext(new SimpleArguments())); @@ -198,14 +217,16 @@ when(heapDao.getAllHeapInfo(vmRef1)).thenReturn(Arrays.asList(heapInfo)); when(heapDao.getAllHeapInfo(vmRef2)).thenReturn(Arrays.asList(heapInfo)); + OSGIUtils serviceProvider = mock(OSGIUtils.class); + when(serviceProvider.getServiceAllowNull(HostInfoDAO.class)).thenReturn(hostInfo); + DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getHostInfoDAO()).thenReturn(hostInfo); when(daoFactory.getVmInfoDAO()).thenReturn(vmInfo); when(daoFactory.getHeapDAO()).thenReturn(heapDao); ApplicationContext.getInstance().setDAOFactory(daoFactory); - Command command = new ListHeapDumpsCommand(); + Command command = new ListHeapDumpsCommand(serviceProvider); TestCommandContextFactory factory = new TestCommandContextFactory(); SimpleArguments args = new SimpleArguments(); @@ -249,14 +270,16 @@ when(heapDao.getAllHeapInfo(vmRef1)).thenReturn(Arrays.asList(heapInfo)); when(heapDao.getAllHeapInfo(vmRef2)).thenReturn(Arrays.asList(heapInfo)); + OSGIUtils serviceProvider = mock(OSGIUtils.class); + when(serviceProvider.getServiceAllowNull(HostInfoDAO.class)).thenReturn(hostInfo); + DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getHostInfoDAO()).thenReturn(hostInfo); when(daoFactory.getVmInfoDAO()).thenReturn(vmInfo); when(daoFactory.getHeapDAO()).thenReturn(heapDao); ApplicationContext.getInstance().setDAOFactory(daoFactory); - Command command = new ListHeapDumpsCommand(); + Command command = new ListHeapDumpsCommand(serviceProvider); TestCommandContextFactory factory = new TestCommandContextFactory(); SimpleArguments args = new SimpleArguments();
--- a/common/core/src/main/java/com/redhat/thermostat/common/dao/DAOFactory.java Wed Oct 03 16:21:59 2012 -0400 +++ b/common/core/src/main/java/com/redhat/thermostat/common/dao/DAOFactory.java Wed Oct 03 18:05:54 2012 -0400 @@ -70,4 +70,6 @@ public HeapDAO getHeapDAO(); + public void registerDAOsAndStorageAsOSGiServices(); + }
--- a/common/core/src/main/java/com/redhat/thermostat/common/dao/MongoDAOFactory.java Wed Oct 03 16:21:59 2012 -0400 +++ b/common/core/src/main/java/com/redhat/thermostat/common/dao/MongoDAOFactory.java Wed Oct 03 18:05:54 2012 -0400 @@ -39,6 +39,7 @@ import com.redhat.thermostat.common.storage.Connection; import com.redhat.thermostat.common.storage.StorageProvider; import com.redhat.thermostat.common.storage.Storage; +import com.redhat.thermostat.common.utils.OSGIUtils; public class MongoDAOFactory implements DAOFactory { @@ -133,4 +134,26 @@ ensureStorageConnected(); return new HeapDAOImpl(storage); } + + @Override + public void registerDAOsAndStorageAsOSGiServices() { + OSGIUtils registerer = OSGIUtils.getInstance(); + + registerer.registerService(Storage.class, getStorage()); + + registerer.registerService(AgentInfoDAO.class, getAgentInfoDAO()); + registerer.registerService(BackendInfoDAO.class, getBackendInfoDAO()); + + registerer.registerService(HostInfoDAO.class, getHostInfoDAO()); + registerer.registerService(NetworkInterfaceInfoDAO.class, getNetworkInterfaceInfoDAO()); + registerer.registerService(CpuStatDAO.class, getCpuStatDAO()); + registerer.registerService(MemoryStatDAO.class, getMemoryStatDAO()); + + registerer.registerService(VmInfoDAO.class, getVmInfoDAO()); + registerer.registerService(VmClassStatDAO.class, getVmClassStatsDAO()); + registerer.registerService(VmCpuStatDAO.class, getVmCpuStatDAO()); + registerer.registerService(VmGcStatDAO.class, getVmGcStatDAO()); + registerer.registerService(VmMemoryStatDAO.class, getVmMemoryStatDAO()); + registerer.registerService(HeapDAO.class, getHeapDAO()); + } }
--- a/common/core/src/main/java/com/redhat/thermostat/common/utils/OSGIUtils.java Wed Oct 03 16:21:59 2012 -0400 +++ b/common/core/src/main/java/com/redhat/thermostat/common/utils/OSGIUtils.java Wed Oct 03 18:05:54 2012 -0400 @@ -85,9 +85,8 @@ return (E) ctx.getService(ref); } - public <E extends Object> void ungetService(Object service) { + public <E extends Object> void ungetService(Class<E> serviceType, Object service) { BundleContext ctx = FrameworkUtil.getBundle(getClass()).getBundleContext(); - Class<?> serviceType = service.getClass(); // FIXME the reference returned now may be different from a previous invocation ServiceReference ref = ctx.getServiceReference(serviceType.getName()); ctx.ungetService(ref);
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/DbServiceImpl.java Wed Oct 03 16:21:59 2012 -0400 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/DbServiceImpl.java Wed Oct 03 18:05:54 2012 -0400 @@ -65,10 +65,6 @@ this.dbUrl = dbUrl; } - /* - * (non-Javadoc) - * @see com.redhat.thermostat.launcher.DbService#connect() - */ public void connect() throws ConnectionException { StartupConfiguration config = new ConnectionConfiguration(dbUrl, username, password); @@ -77,12 +73,10 @@ Connection connection = daoFactory.getConnection(); connection.connect(); ApplicationContext.getInstance().setDAOFactory(daoFactory); + + daoFactory.registerDAOsAndStorageAsOSGiServices(); } - /* - * (non-Javadoc) - * @see com.redhat.thermostat.launcher.DbService#disconnect() - */ public void disconnect() throws ConnectionException { DAOFactory factory = ApplicationContext.getInstance().getDAOFactory(); Connection connection = factory.getConnection(); @@ -90,8 +84,6 @@ ApplicationContext.getInstance().setDAOFactory(null); } - - /** * Factory method for creating a DbService instance. *
--- a/tools/src/main/java/com/redhat/thermostat/tools/LocaleResources.java Wed Oct 03 16:21:59 2012 -0400 +++ b/tools/src/main/java/com/redhat/thermostat/tools/LocaleResources.java Wed Oct 03 18:05:54 2012 -0400 @@ -42,6 +42,8 @@ VALUE_AND_UNIT, + HOST_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 16:21:59 2012 -0400 +++ b/tools/src/main/java/com/redhat/thermostat/tools/cli/ListVMsCommand.java Wed Oct 03 18:05:54 2012 -0400 @@ -48,17 +48,36 @@ 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; public class ListVMsCommand extends SimpleCommand { private static final String NAME = "list-vms"; + private final OSGIUtils serviceProvider; + + public ListVMsCommand() { + this(OSGIUtils.getInstance()); + } + + ListVMsCommand(OSGIUtils serviceProvider) { + this.serviceProvider = serviceProvider; + } + @Override public void run(CommandContext ctx) throws CommandException { DAOFactory daoFactory = ApplicationContext.getInstance().getDAOFactory(); - HostInfoDAO hostsDAO = daoFactory.getHostInfoDAO(); + + HostInfoDAO hostsDAO = serviceProvider.getServiceAllowNull(HostInfoDAO.class); + if (hostsDAO == null) { + throw new CommandException(Translate.localize(LocaleResources.HOST_SERVICE_UNAVAILABLE)); + } Collection<HostRef> hosts = hostsDAO.getHosts(); + serviceProvider.ungetService(HostInfoDAO.class, hostsDAO); + VmInfoDAO vmsDAO = daoFactory.getVmInfoDAO(); VMListFormatter formatter = new VMListFormatter(); for (HostRef host : hosts) {
--- a/tools/src/main/resources/com/redhat/thermostat/tools/strings.properties Wed Oct 03 16:21:59 2012 -0400 +++ b/tools/src/main/resources/com/redhat/thermostat/tools/strings.properties Wed Oct 03 18:05:54 2012 -0400 @@ -2,6 +2,8 @@ VALUE_AND_UNIT = {0} {1} +HOST_SERVICE_UNAVAILABLE = Unable to get host information (HostInfoDAO 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 16:21:59 2012 -0400 +++ b/tools/src/test/java/com/redhat/thermostat/tools/cli/ListVMsCommandTest.java Wed Oct 03 18:05:54 2012 -0400 @@ -61,6 +61,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.TestCommandContextFactory; public class ListVMsCommandTest { @@ -69,16 +70,19 @@ private TestCommandContextFactory cmdCtxFactory; private HostInfoDAO hostsDAO; private VmInfoDAO vmsDAO; + private OSGIUtils serviceProvider; @Before public void setUp() { ApplicationContextUtil.resetApplicationContext(); setupCommandContextFactory(); + serviceProvider = mock(OSGIUtils.class); - cmd = new ListVMsCommand(); + cmd = new ListVMsCommand(serviceProvider); setupDAOs(); + when(serviceProvider.getServiceAllowNull(HostInfoDAO.class)).thenReturn(hostsDAO); } private void setupCommandContextFactory() { @@ -89,7 +93,6 @@ hostsDAO = mock(HostInfoDAO.class); vmsDAO = mock(VmInfoDAO.class); DAOFactory daoFactory = mock(DAOFactory.class); - when(daoFactory.getHostInfoDAO()).thenReturn(hostsDAO); when(daoFactory.getVmInfoDAO()).thenReturn(vmsDAO); ApplicationContext.getInstance().setDAOFactory(daoFactory); }