String excludeEventsString = payload.get(CONFIG_LOGGING_EXCLUDE_EVENTS); if (excludeEventsString != null && !"".equals(excludeEventsString.trim())) { logDebug(CONFIG_LOGGING_EXCLUDE_EVENTS + " configuration changed to " + excludeEventsString); Set<Integer> excludeEvents = new HashSet<>(); for(String s:excludeEventsString.trim().split("\\s*,\\s*")) { excludeEvents.add(Integer.parseInt(s)); } catch (NumberFormatException nfe) { logWarning("Unable to parse " + CONFIG_LOGGING_EXCLUDE_EVENTS, nfe); eventType = AuditEvent.DEPLOYMENTADMIN_COMPLETE; props.put(AuditEvent.KEY_NAME, payload.get("deploymentpackage.name")); props.put(AuditEvent.KEY_VERSION, getDeploymentHandler().getInstalledVersion().toString()); props.put(AuditEvent.KEY_SUCCESS, payload.get("successful")); writeAuditEvent(eventType, props);
@Override protected void onInit() throws Exception { getEventsHandler().addListener(this); }
private void writeAuditEvent(int eventType, Map<String, String> payload) { try { FeedbackChannel channel = getFeedbackHandler().getChannel(EVENTLOGGER_FEEDBACKCHANNEL); boolean isExcluded; synchronized(m_excludeEventList) { isExcluded = m_excludeEventList.contains(eventType); } if (channel != null && !isExcluded) { channel.write(eventType, payload); } } catch (IOException e) { logWarning("Failed to write feedback event!", e); } } }
@BeforeMethod(alwaysRun = true) public void reset() throws Exception { // create a new eventlogger for every test m_eventLogger = new EventLoggerImpl(mockBundleContext()); m_eventLogger.init(m_agentContext); m_eventsHandler.addListener(m_eventLogger); m_eventLogger.start(m_agentContext); // reset the previously logged data in our mock feedbackchannel. No need to create a new one here FeedbackHandler feedbackHandler = m_agentContext.getHandler(FeedbackHandler.class); TestFeedbackChannel channel = (TestFeedbackChannel) feedbackHandler.getChannel("auditlog"); channel.reset(); }
/** * Called by our {@link DependencyTrackerImpl} when all dependencies are satisfied. */ public void componentStarted(BundleContext context) throws Exception { final File bundleDataArea = context.getDataFile(""); m_agentContext = new AgentContextImpl(bundleDataArea); m_agentContext.setHandler(LoggingHandler.class, new LoggingHandlerImpl(context)); m_agentContext.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl(context)); m_agentContext.setHandler(EventsHandler.class, new EventsHandlerImpl(context)); m_agentContext.setHandler(ScheduledExecutorService.class, m_executorService); m_agentContext.setHandler(DownloadHandler.class, new DownloadHandlerImpl(bundleDataArea)); m_agentContext.setHandler(DeploymentHandler.class, new DeploymentHandlerImpl(context, m_packageAdmin)); m_agentContext.setHandler(AgentUpdateHandler.class, new AgentUpdateHandlerImpl(context)); m_agentContext.setHandler(FeedbackHandler.class, new FeedbackHandlerImpl()); IdentificationHandler identificationHandler = (m_identificationHandler != null) ? m_identificationHandler : new IdentificationHandlerImpl(); m_agentContext.setHandler(IdentificationHandler.class, identificationHandler); DiscoveryHandler discoveryHandler = (m_discoveryHandler != null) ? m_discoveryHandler : new DiscoveryHandlerImpl(); m_agentContext.setHandler(DiscoveryHandler.class, discoveryHandler); ConnectionHandler connectionHandler = (m_connectionHandler != null) ? m_connectionHandler : new ConnectionHandlerImpl(); m_agentContext.setHandler(ConnectionHandler.class, connectionHandler); m_agentContext.addComponent(new EventLoggerImpl(context)); // Lastly, inject the (custom) controller for this agent... m_agentContext.setController(m_controller); m_agentContext.start(); m_agentControlRegistration = context.registerService(AgentControl.class.getName(), new AgentControlImpl(m_agentContext), null); }
@SuppressWarnings("deprecation") @Test public void testWriteEvent() throws Exception { FrameworkEvent event = new FrameworkEvent(32, new Object()); m_eventLogger.frameworkEvent(event); FeedbackHandler feedbackHandler = m_agentContext.getHandler(FeedbackHandler.class); TestFeedbackChannel channel = (TestFeedbackChannel) feedbackHandler.getChannel("auditlog"); assertEquals(channel.getLastTtype(), 1001); }
break; writeAuditEvent(eventType, props);
@SuppressWarnings("deprecation") @Test public void testExcludeEvent() throws Exception { ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class); configureAgent(configurationHandler, AgentConstants.CONFIG_LOGGING_EXCLUDE_EVENTS, "1001,1002"); FrameworkEvent event = new FrameworkEvent(32, new Object()); FeedbackHandler feedbackHandler = m_agentContext.getHandler(FeedbackHandler.class); TestFeedbackChannel channel = (TestFeedbackChannel) feedbackHandler.getChannel("auditlog"); // make sure the configuration is written to the channel assertEquals(channel.getLastTtype(), 2000); m_eventLogger.frameworkEvent(event); // make sure nothing is written to the channel assertEquals(channel.getLastTtype(), 2000); } }
break; writeAuditEvent(eventType, props);
@Override protected void onStop() throws Exception { if (m_isStarted.compareAndSet(true, false)) { getEventsHandler().removeListener(this); m_bundleContext.removeBundleListener(this); m_bundleContext.removeFrameworkListener(this); } }