Return-Path: X-Original-To: apmail-ace-commits-archive@www.apache.org Delivered-To: apmail-ace-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D811D10943 for ; Thu, 22 Aug 2013 12:05:10 +0000 (UTC) Received: (qmail 96817 invoked by uid 500); 22 Aug 2013 12:05:10 -0000 Delivered-To: apmail-ace-commits-archive@ace.apache.org Received: (qmail 96797 invoked by uid 500); 22 Aug 2013 12:05:09 -0000 Mailing-List: contact commits-help@ace.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ace.apache.org Delivered-To: mailing list commits@ace.apache.org Received: (qmail 96788 invoked by uid 99); 22 Aug 2013 12:05:08 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 22 Aug 2013 12:05:08 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 22 Aug 2013 12:05:04 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 29BA8238889B; Thu, 22 Aug 2013 12:04:43 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1516425 - in /ace/trunk/org.apache.ace.agent: src/org/apache/ace/agent/ src/org/apache/ace/agent/impl/ test/org/apache/ace/agent/impl/ Date: Thu, 22 Aug 2013 12:04:42 -0000 To: commits@ace.apache.org From: bramk@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20130822120443.29BA8238889B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: bramk Date: Thu Aug 22 12:04:42 2013 New Revision: 1516425 URL: http://svn.apache.org/r1516425 Log: ACE-347 Implemented configurable FeedbackHandler Added: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/FeedbackHandler.java ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DefaultController.java ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DiscoveryHandlerImpl.java ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventLoggerImpl.java ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackHandlerImpl.java Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/FeedbackHandler.java URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/FeedbackHandler.java?rev=1516425&r1=1516424&r2=1516425&view=diff ============================================================================== --- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/FeedbackHandler.java (original) +++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/FeedbackHandler.java Thu Aug 22 12:04:42 2013 @@ -18,7 +18,8 @@ */ package org.apache.ace.agent; -import java.util.List; +import java.io.IOException; +import java.util.Set; /** * Agent control delegate interface that is responsible for handling feedback channels. @@ -26,8 +27,8 @@ import java.util.List; public interface FeedbackHandler { /** Returns the feedback channels names */ - List getChannelNames(); + Set getChannelNames() throws IOException; /** Returns the feedback channel for a name */ - FeedbackChannel getChannel(String name); + FeedbackChannel getChannel(String name) throws IOException; } Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DefaultController.java URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DefaultController.java?rev=1516425&r1=1516424&r2=1516425&view=diff ============================================================================== --- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DefaultController.java (original) +++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DefaultController.java Thu Aug 22 12:04:42 2013 @@ -20,7 +20,7 @@ package org.apache.ace.agent.impl; import java.io.IOException; import java.io.InputStream; -import java.util.List; +import java.util.Set; import java.util.SortedSet; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -194,7 +194,7 @@ public class DefaultController implement AgentControl agentControl = m_agentContext.getAgentControl(); m_agentContext.logDebug(COMPONENT_IDENTIFIER, "Synchronizing feedback channels"); - List channelNames = agentControl.getFeedbackHandler().getChannelNames(); + Set channelNames = agentControl.getFeedbackHandler().getChannelNames(); for (String channelName : channelNames) { FeedbackChannel channel = agentControl.getFeedbackHandler().getChannel(channelName); if (channel != null) { Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DiscoveryHandlerImpl.java URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DiscoveryHandlerImpl.java?rev=1516425&r1=1516424&r2=1516425&view=diff ============================================================================== --- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DiscoveryHandlerImpl.java (original) +++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DiscoveryHandlerImpl.java Thu Aug 22 12:04:42 2013 @@ -37,10 +37,6 @@ public class DiscoveryHandlerImpl extend public static final String COMPONENT_IDENTIFIER = "discovery"; public static final String CONFIG_KEY_BASE = ConfigurationHandlerImpl.CONFIG_KEY_NAMESPACE + "." + COMPONENT_IDENTIFIER; - public DiscoveryHandlerImpl() { - super(COMPONENT_IDENTIFIER); - } - /** * Configuration key for the default discovery handler. The value must be a comma-separated list of valid base * server URLs. @@ -48,6 +44,11 @@ public class DiscoveryHandlerImpl extend public static final String CONFIG_KEY_SERVERURLS = CONFIG_KEY_BASE + ".serverUrls"; public static final String CONFIG_DEFAULT_SERVERURLS = "http://localhost:8080"; + public DiscoveryHandlerImpl() { + super(COMPONENT_IDENTIFIER); + } + + // TODO Pretty naive implementation below. It always takes the first configured URL it can connect to and is not // thread-safe. @Override 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=1516425&r1=1516424&r2=1516425&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 Thu Aug 22 12:04:42 2013 @@ -236,16 +236,16 @@ public class EventLoggerImpl implements } private void writeEvent(int eventType, Map payload) { - FeedbackChannel channel = m_agentControl.getFeedbackHandler() - .getChannel(EVENTLOGGER_FEEDBACKCHANNEL); - if (channel != null) { - try { + try { + FeedbackChannel channel = m_agentControl.getFeedbackHandler() + .getChannel(EVENTLOGGER_FEEDBACKCHANNEL); + if (channel != null) { channel.write(eventType, payload); } - catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } } } Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackHandlerImpl.java URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackHandlerImpl.java?rev=1516425&r1=1516424&r2=1516425&view=diff ============================================================================== --- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackHandlerImpl.java (original) +++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackHandlerImpl.java Thu Aug 22 12:04:42 2013 @@ -18,10 +18,12 @@ */ package org.apache.ace.agent.impl; -import java.util.ArrayList; +import java.io.IOException; +import java.util.Collections; import java.util.HashMap; -import java.util.List; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import org.apache.ace.agent.FeedbackChannel; import org.apache.ace.agent.FeedbackHandler; @@ -31,31 +33,83 @@ import org.apache.ace.agent.FeedbackHand */ public class FeedbackHandlerImpl extends HandlerBase implements FeedbackHandler { - private final Map m_feedbackChannels = new HashMap(); + public static final String COMPONENT_IDENTIFIER = "feedback"; + public static final String CONFIG_KEY_BASE = ConfigurationHandlerImpl.CONFIG_KEY_NAMESPACE + "." + COMPONENT_IDENTIFIER; + + /** + * Configuration key for the default discovery handler. The value must be a comma-separated list of valid base + * server URLs. + */ + public static final String CONFIG_KEY_CHANNELS = CONFIG_KEY_BASE + ".channels"; + public static final String CONFIG_DEFAULT_CHANNELS = "auditlog"; + + private Map m_channels = new HashMap(); + private Set m_channelNames; + private String m_channelNamesConfig; public FeedbackHandlerImpl() { - super("feedback"); + super(COMPONENT_IDENTIFIER); } @Override protected void onStart() throws Exception { - // TODO get from configuration - m_feedbackChannels.put("auditlog", new FeedbackChannelImpl(getAgentContext(), "auditlog")); + synchronized (m_channels) { + ensureChannels(); // fail fast + } } @Override protected void onStop() throws Exception { - m_feedbackChannels.clear(); + synchronized (m_channels) { + clearChannels(); + } } @Override - public List getChannelNames() { - List names = new ArrayList(m_feedbackChannels.keySet()); - return java.util.Collections.unmodifiableList(names); + public Set getChannelNames() throws IOException { + synchronized (m_channels) { + ensureChannels(); + return m_channelNames; + } } @Override - public FeedbackChannel getChannel(String name) { - return m_feedbackChannels.get(name); + public FeedbackChannel getChannel(String name) throws IOException { + synchronized (m_channels) { + ensureChannels(); + return m_channels.get(name); + } + } + + private void ensureChannels() throws IOException { + String channelNamesConfig = getAgentContext().getConfigurationHandler().get(CONFIG_KEY_CHANNELS, CONFIG_DEFAULT_CHANNELS); + if (m_channelNamesConfig != null && m_channelNamesConfig.equals(channelNamesConfig)) { + return; + } + + m_channelNamesConfig = channelNamesConfig; + m_channelNames = Collections.unmodifiableSet(getConfigurationValues(channelNamesConfig)); + m_channels = new HashMap(); + for (String channelName : m_channelNames) { + m_channels.put(channelName, new FeedbackChannelImpl(getAgentContext(), channelName)); + } + } + + private void clearChannels() { + m_channelNamesConfig = null; + m_channelNames = null; + m_channels = null; + } + + // TODO move to util or configurationhandler + private static Set getConfigurationValues(String value) { + Set trimmedValues = new HashSet(); + if(value != null){ + String[] rawValues = value.split(","); + for (String rawValue : rawValues) { + trimmedValues.add(rawValue.trim()); + } + } + return trimmedValues; } } Added: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java?rev=1516425&view=auto ============================================================================== --- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java (added) +++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java Thu Aug 22 12:04:42 2013 @@ -0,0 +1,99 @@ +/* + * 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.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.reset; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +import java.io.File; +import java.lang.reflect.Method; +import java.util.Set; + +import org.apache.ace.agent.AgentContext; +import org.apache.ace.agent.ConfigurationHandler; +import org.apache.ace.agent.FeedbackHandler; +import org.apache.ace.agent.testutil.BaseAgentTest; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +/** + * Testing {@link FeedbackHandlerImplTest}. + */ +public class FeedbackHandlerImplTest extends BaseAgentTest { + + private AgentContext m_agentContext; + private ConfigurationHandler m_configurationHandler; + + @BeforeMethod + public void setUpAgain(Method method) throws Exception { + File methodDir = new File(new File(getWorkDir(), FeedbackHandlerImplTest.class.getName()), method.getName()); + methodDir.mkdirs(); + cleanDir(methodDir); + + m_agentContext = addTestMock(AgentContext.class); + m_configurationHandler = addTestMock(ConfigurationHandler.class); + expect(m_agentContext.getWorkDir()).andReturn(methodDir).anyTimes(); + expect(m_agentContext.getConfigurationHandler()).andReturn(m_configurationHandler).anyTimes(); + replayTestMocks(); + } + + @AfterMethod + public void tearDownAgain(Method method) throws Exception { + verifyTestMocks(); + clearTestMocks(); + } + + @Test + public void testFeedbackChannelConfig() throws Exception { + + reset(m_configurationHandler); + expect(m_configurationHandler.get(eq(FeedbackHandlerImpl.CONFIG_KEY_CHANNELS), anyObject(String.class))).andReturn("auditlog").anyTimes(); + replay(m_configurationHandler); + + FeedbackHandler feedbackHandler = new FeedbackHandlerImpl(); + startHandler(feedbackHandler, m_agentContext); + + Set names = feedbackHandler.getChannelNames(); + assertNotNull(names); + assertTrue(names.size() == 1); + assertTrue(names.contains("auditlog")); + assertNotNull(feedbackHandler.getChannel("auditlog")); + assertNull(feedbackHandler.getChannel("QQQ")); + + reset(m_configurationHandler); + expect(m_configurationHandler.get(eq(FeedbackHandlerImpl.CONFIG_KEY_CHANNELS), anyObject(String.class))).andReturn("auditlog, customchannel").anyTimes(); + replay(m_configurationHandler); + + names = feedbackHandler.getChannelNames(); + assertNotNull(names); + assertTrue(names.size() == 2); + assertTrue(names.contains("auditlog")); + assertTrue(names.contains("customchannel")); + assertNotNull(feedbackHandler.getChannel("auditlog")); + assertNotNull(feedbackHandler.getChannel("customchannel")); + assertNull(feedbackHandler.getChannel("QQQ")); + } +}