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<String> getChannelNames();
+ Set<String> 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<String> channelNames =
agentControl.getFeedbackHandler().getChannelNames();
+ Set<String> 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<String, String> 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<String, FeedbackChannelImpl> m_feedbackChannels = new
HashMap<String, FeedbackChannelImpl>();
+ 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<String, FeedbackChannelImpl> m_channels = new HashMap<String,
FeedbackChannelImpl>();
+ private Set<String> 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<String> getChannelNames() {
- List<String> names = new
ArrayList<String>(m_feedbackChannels.keySet());
- return java.util.Collections.unmodifiableList(names);
+ public Set<String> 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<String, FeedbackChannelImpl>();
+ 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<String> getConfigurationValues(String value) {
+ Set<String> trimmedValues = new HashSet<String>();
+ 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<String> 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"));
+ }
+}