MdsOsgiBundleApplicationContextListener implements OsgiBundleApplicationContextListener { @Override public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent event) { if (!settingsService.getModuleSettings().isRestartModuleAfterTimeout()) { return; } String symbolicName = event.getBundle().getSymbolicName(); if (event instanceof OsgiBundleContextFailedEvent) { if (!restartedBundles.contains(symbolicName)) { OsgiBundleContextFailedEvent failedEvent = (OsgiBundleContextFailedEvent) event; final String failureCauseMsg = failedEvent.getFailureCause().getMessage(); Bundle bundle = failedEvent.getBundle(); if (!failureCauseMsg.startsWith(String.format(TIMEOUT_EXCEPTION_MESSAGE, symbolicName))) { return; } LOGGER.error("Received context failed event {} from {}", event, symbolicName); LOGGER.error("{} failed to start due to {}", symbolicName, failureCauseMsg); logFailReport(symbolicName, failureCauseMsg, BundleRestartStatus.IN_PROGRESS); restartedBundles.add(symbolicName); LOGGER.info("Trying to restart {} bundle", symbolicName); try { bundle.stop(); bundle.start(); } catch (BundleException e) { LOGGER.error("Cannot restart {} bundle due to {}", symbolicName, e); } } else { LOGGER.error("cannot restart {} bundle since it we already attempted it.", symbolicName); sendMessage("CRITICAL", String.format(RESTART_FAILURE_CRIT_MSG, symbolicName)); updateReportStatus(symbolicName, BundleRestartStatus.ERROR); } } else if (event instanceof OsgiBundleContextRefreshedEvent && restartedBundles.contains(symbolicName) ) { sendMessage("WARN", String.format(RESTART_SUCCESS_WARN_MSG, symbolicName)); updateReportStatus(symbolicName, BundleRestartStatus.SUCCESS); restartedBundles.remove(symbolicName); } }  @Override void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent event); void clearBundlesSet(); Set<String> getRestartedBundles(); @Autowired void setAllBundleFailureReports(AllBundleFailureReports allBundleFailureReports); @Autowired void setTransactionManager(JdoTransactionManager transactionManager); @Autowired void setOsgiEventProxy(OsgiEventProxy osgiEventProxy); @Autowired void setSettingsService(SettingsService settingsService); static final String MESSAGE_SUBJECT; static final String MESSAGE_KEY; static final String LEVEL_KEY; static final String MODULE_NAME_KEY; static final String TIMEOUT_EXCEPTION_MESSAGE; }
@Test public void shouldRestartBundle() throws BundleException { mdsOsgiBundleApplicationContextListener.onOsgiApplicationEvent(new OsgiBundleContextFailedEvent(source, bundle, throwable)); verify(allBundleFailureReports).create(bundleFailsReportArgumentCaptor.capture()); BundleFailureReport failsReport = bundleFailsReportArgumentCaptor.getValue(); assertEquals(SAMPLE_SYMBOLIC_NAME, failsReport.getBundleSymbolicName()); assertEquals(FAILURE_MESSAGE_1, failsReport.getErrorMessage()); assertEquals(BundleRestartStatus.IN_PROGRESS, failsReport.getBundleRestartStatus()); verify(bundle).start(); verify(bundle).stop(); mdsOsgiBundleApplicationContextListener.onOsgiApplicationEvent(new OsgiBundleContextRefreshedEvent(source, bundle)); verify(report).setBundleRestartStatus(BundleRestartStatus.SUCCESS); verify(allBundleFailureReports).update(report); verify(osgiEventProxy).sendEvent(eq(MdsOsgiBundleApplicationContextListener.MESSAGE_SUBJECT), paramsArgumentCaptor.capture()); Map<String, Object> params = paramsArgumentCaptor.getValue(); assertEquals("WARN", params.get(MdsOsgiBundleApplicationContextListener.LEVEL_KEY)); assertEquals("mds", params.get(MdsOsgiBundleApplicationContextListener.MODULE_NAME_KEY)); }
@Test public void shouldSetErrorStatus() throws BundleException { mdsOsgiBundleApplicationContextListener.onOsgiApplicationEvent(new OsgiBundleContextFailedEvent(source, bundle, throwable)); verify(allBundleFailureReports).create(bundleFailsReportArgumentCaptor.capture()); BundleFailureReport failsReport = bundleFailsReportArgumentCaptor.getValue(); assertEquals(SAMPLE_SYMBOLIC_NAME, failsReport.getBundleSymbolicName()); assertEquals(FAILURE_MESSAGE_1, failsReport.getErrorMessage()); assertEquals(BundleRestartStatus.IN_PROGRESS, failsReport.getBundleRestartStatus()); verify(bundle).start(); verify(bundle).stop(); mdsOsgiBundleApplicationContextListener.onOsgiApplicationEvent(new OsgiBundleContextFailedEvent(source, bundle, throwable)); verify(report).setBundleRestartStatus(BundleRestartStatus.ERROR); verify(allBundleFailureReports).update(report); verify(osgiEventProxy).sendEvent(eq(MdsOsgiBundleApplicationContextListener.MESSAGE_SUBJECT), paramsArgumentCaptor.capture()); Map<String, Object> params = paramsArgumentCaptor.getValue(); assertEquals("CRITICAL", params.get(MdsOsgiBundleApplicationContextListener.LEVEL_KEY)); assertEquals("mds", params.get(MdsOsgiBundleApplicationContextListener.MODULE_NAME_KEY)); }