You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ace.apache.org by an...@apache.org on 2013/10/02 16:53:16 UTC

svn commit: r1528505 - in /ace/trunk/org.apache.ace.agent: src/org/apache/ace/agent/AgentConstants.java src/org/apache/ace/agent/impl/EventLoggerImpl.java test/org/apache/ace/agent/impl/EventLoggerImplTest.java

Author: angelos
Date: Wed Oct  2 14:53:15 2013
New Revision: 1528505

URL: http://svn.apache.org/r1528505
Log:
ACE-383 We can now set agent.logging.events.exclude to exclude auditlog events, as defined by AuditEvent.

Added:
    ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/EventLoggerImplTest.java
Modified:
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentConstants.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventLoggerImpl.java

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentConstants.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentConstants.java?rev=1528505&r1=1528504&r2=1528505&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentConstants.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentConstants.java Wed Oct  2 14:53:15 2013
@@ -18,6 +18,8 @@
  */
 package org.apache.ace.agent;
 
+import org.apache.ace.feedback.AuditEvent;
+
 /**
  * Compile time constants for this package. Includes configuration keys and event topics.
  */
@@ -39,7 +41,13 @@ public interface AgentConstants {
      * , default is <code>INFO</code>.
      */
     String CONFIG_LOGGING_LEVEL = CONFIG_KEY_NAMESPACE + ".logging.level";
-
+    
+    /**
+     * Exclude list for auditlog events. Should be a comma separated list of integers, as defined by {@link AuditEvent}.
+     * Example : '2001,2003,2005,3001'
+     */
+    String CONFIG_LOGGING_EXCLUDE_EVENTS = CONFIG_KEY_NAMESPACE + ".logging.events.exclude";
+    
     /**
      * Configuration option to disable the default identification handler. When set to true some other bundle must
      * provide it as a service. Should be <code>{true,false}</code>, default is <code>false</code>.

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventLoggerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventLoggerImpl.java?rev=1528505&r1=1528504&r2=1528505&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventLoggerImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventLoggerImpl.java Wed Oct  2 14:53:15 2013
@@ -18,9 +18,14 @@
  */
 package org.apache.ace.agent.impl;
 
+import static org.apache.ace.agent.AgentConstants.CONFIG_LOGGING_EXCLUDE_EVENTS;
+import static org.apache.ace.agent.AgentConstants.EVENT_AGENT_CONFIG_CHANGED;
+
 import java.io.IOException;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.ace.agent.AgentConstants;
@@ -48,12 +53,14 @@ public class EventLoggerImpl extends Com
 
     private final BundleContext m_bundleContext;
     private final AtomicBoolean m_isStarted;
+    private final Set<Integer> m_excludeEventList;
 
     public EventLoggerImpl(BundleContext bundleContext) {
         super("auditlogger");
 
         m_bundleContext = bundleContext;
         m_isStarted = new AtomicBoolean(false);
+        m_excludeEventList = new HashSet<Integer>();
     }
 
     @Override
@@ -85,6 +92,26 @@ public class EventLoggerImpl extends Com
             return;
         }
 
+        if (EVENT_AGENT_CONFIG_CHANGED.equals(topic)) {
+            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<Integer>();
+                for(String s:excludeEventsString.trim().split("\\s*,\\s*")) {
+                    try {
+                        excludeEvents.add(Integer.parseInt(s));
+                    } catch (NumberFormatException nfe) {
+                        logWarning("Unable to parse " + CONFIG_LOGGING_EXCLUDE_EVENTS,  nfe);
+                    }
+                }
+                
+                synchronized (m_excludeEventList) {
+                    m_excludeEventList.clear();
+                    m_excludeEventList.addAll(excludeEvents);
+                }
+            }
+        }
+        
         int eventType = AuditEvent.DEPLOYMENTADMIN_BASE;
         Map<String, String> props = new HashMap<String, String>();
 
@@ -228,7 +255,11 @@ public class EventLoggerImpl extends Com
     private void writeAuditEvent(int eventType, Map<String, String> payload) {
         try {
             FeedbackChannel channel = getFeedbackHandler().getChannel(EVENTLOGGER_FEEDBACKCHANNEL);
-            if (channel != null) {
+            boolean isExcluded;
+            synchronized(m_excludeEventList) {
+                isExcluded = m_excludeEventList.contains(eventType);
+            }
+            if (channel != null && !isExcluded) {
                 channel.write(eventType, payload);
             }
         }

Added: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/EventLoggerImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/EventLoggerImplTest.java?rev=1528505&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/EventLoggerImplTest.java (added)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/EventLoggerImplTest.java Wed Oct  2 14:53:15 2013
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.impl;
+
+import static org.testng.Assert.assertEquals;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.ace.agent.AgentConstants;
+import org.apache.ace.agent.ConfigurationHandler;
+import org.apache.ace.agent.EventsHandler;
+import org.apache.ace.agent.FeedbackChannel;
+import org.apache.ace.agent.FeedbackHandler;
+import org.apache.ace.agent.RetryAfterException;
+import org.apache.ace.agent.testutil.BaseAgentTest;
+import org.osgi.framework.FrameworkEvent;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class EventLoggerImplTest extends BaseAgentTest {
+
+    static class TestFeedbackChannel implements FeedbackChannel {
+
+        int m_lastType = 0;
+        
+        @Override
+        public void sendFeedback() throws RetryAfterException, IOException {
+        }
+
+        @Override
+        public void write(int type, Map<String, String> properties) throws IOException {
+            m_lastType = type;
+        }
+        
+        public int getLastTtype() {
+            return m_lastType;
+        }
+        
+        public void reset() {
+            m_lastType = 0;
+        }
+    }
+    
+    static class TestFeedbackHandler implements FeedbackHandler {
+
+        Map<String, FeedbackChannel> channels = new HashMap<String, FeedbackChannel>();
+        
+        TestFeedbackHandler() {
+            channels.put("auditlog", new TestFeedbackChannel());
+        }
+        
+        @Override
+        public Set<String> getChannelNames() throws IOException {
+            return channels.keySet();
+        }
+
+        @Override
+        public FeedbackChannel getChannel(String name) throws IOException {
+            return channels.get("auditlog");
+        }
+        
+    }
+    
+    private AgentContextImpl m_agentContext;
+    private EventLoggerImpl m_eventLogger;
+    private EventsHandler m_eventsHandler;
+
+    @BeforeClass
+    public void setUpOnceAgain() throws Exception {
+        m_agentContext = mockAgentContext();
+
+        m_eventsHandler = new EventsHandlerImpl(mockBundleContext());
+        m_agentContext.setHandler(EventsHandler.class, m_eventsHandler);
+        m_agentContext.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl());
+        m_agentContext.setHandler(FeedbackHandler.class, new TestFeedbackHandler());
+
+        replayTestMocks();
+        m_agentContext.start();
+
+    }
+    
+    @AfterClass
+    public void tearDownOnceAgain() throws Exception {
+        m_agentContext.stop();
+        verifyTestMocks();
+        clearTestMocks();
+    }
+
+    @BeforeMethod
+    public void reset() throws Exception {
+        // create a new eventlogger for every test
+        m_eventLogger = new EventLoggerImpl(mockBundleContext());
+        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();
+    }
+    
+    @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);
+        
+    }
+    
+    @SuppressWarnings("deprecation")
+    @Test
+    public void testExcludeEvent() throws Exception {
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        configurationHandler.put(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);
+    }
+}