ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r1513770 - in /ace/sandbox/bramk/org.apache.ace.agent: src/org/apache/ace/agent/discovery/ src/org/apache/ace/agent/discovery/impl/ src/org/apache/ace/agent/identification/ src/org/apache/ace/agent/identification/impl/ test/org/apache/ace/a...
Date Wed, 14 Aug 2013 09:15:00 GMT
Author: bramk
Date: Wed Aug 14 09:15:00 2013
New Revision: 1513770

URL: http://svn.apache.org/r1513770
Log:
[sandbox] Agent impl wip; default identification/discovery implementation and test

Added:
    ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/discovery/
    ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/discovery/impl/
    ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/discovery/impl/DiscoveryHandlerImplTest.java
    ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/identification/
    ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/identification/impl/
    ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/identification/impl/IdentificationhandlerImplTest.java
    ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseEasyMockTest.java
Modified:
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/discovery/DiscoveryHandler.java
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/discovery/impl/DiscoveryHandlerImpl.java
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/identification/IdentificationHandler.java
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/identification/impl/IdentificationHandlerImpl.java

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/discovery/DiscoveryHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/discovery/DiscoveryHandler.java?rev=1513770&r1=1513769&r2=1513770&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/discovery/DiscoveryHandler.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/discovery/DiscoveryHandler.java
Wed Aug 14 09:15:00 2013
@@ -12,7 +12,7 @@ public interface DiscoveryHandler {
     /**
      * Return a server base URL.
      * 
-     * @return a URL
+     * @return The URL, <code>null</code> if none is available
      */
     URL getServerUrl();
 }

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/discovery/impl/DiscoveryHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/discovery/impl/DiscoveryHandlerImpl.java?rev=1513770&r1=1513769&r2=1513770&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/discovery/impl/DiscoveryHandlerImpl.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/discovery/impl/DiscoveryHandlerImpl.java
Wed Aug 14 09:15:00 2013
@@ -1,26 +1,92 @@
 package org.apache.ace.agent.discovery.impl;
 
+import java.io.IOException;
+import java.net.HttpURLConnection;
 import java.net.URL;
+import java.net.URLConnection;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.apache.ace.agent.discovery.DiscoveryHandler;
 import org.apache.ace.agent.impl.AgentContext;
 
 /**
- *
+ * Default discovery handler that reads the serverURL(s) from the configuration using key
{@link DISCOVERY_CONFIG_KEY}.
+ * 
  */
 public class DiscoveryHandlerImpl implements DiscoveryHandler {
 
-    private final AgentContext m_agentContext;
+    /**
+     * Configuration key for the default discovery handler. The value must be a comma-separated
list of valid base
+     * server URLs.
+     */
+    // TODO move to and validate in config handler?
+    public static final String DISCOVERY_CONFIG_KEY = "agent.discovery";
 
-    private URL[] m_urls;
+    private final AgentContext m_agentContext;
 
     public DiscoveryHandlerImpl(AgentContext agentContext) throws Exception {
         m_agentContext = agentContext;
-        m_urls = new URL[] { new URL("http://localhost:8888") };
     }
 
+    // TODO Pretty naive implementation below. It always takes the first configurred URL
it can connect to and is not
+    // thread-safe.
     @Override
     public URL getServerUrl() {
-        return m_urls[0];
+        String configValue = m_agentContext.getConfiguration().getMap().get(DISCOVERY_CONFIG_KEY);
+        if (configValue == null || configValue.equals(""))
+            return null;
+        if (configValue.indexOf(",") == -1) {
+            return checkURL(configValue.trim());
+        }
+        for (String configValuePart : configValue.split(",")) {
+            URL url = checkURL(configValuePart.trim());
+            if (url != null)
+                return url;
+        }
+        return null;
+    }
+
+    private static final long CACHE_TIME = 1000;
+
+    private static class CheckedURL {
+        URL url;
+        long timestamp;
+
+        public CheckedURL(URL url, long timestamp) {
+            this.url = url;
+            this.timestamp = timestamp;
+        }
+    }
+
+    private final Map<String, CheckedURL> m_checkedURLs = new HashMap<String, DiscoveryHandlerImpl.CheckedURL>();
+
+    private URL checkURL(String serverURL) {
+        CheckedURL checked = m_checkedURLs.get(serverURL);
+        if (checked != null && checked.timestamp > (System.currentTimeMillis()
- CACHE_TIME)) {
+            return checked.url;
+        }
+        try {
+            URL url = new URL(serverURL);
+            tryConnect(url);
+            m_checkedURLs.put(serverURL, new CheckedURL(url, System.currentTimeMillis()));
+            return url;
+        }
+        catch (IOException e) {
+            // TODO log
+            return null;
+        }
+    }
+
+    private void tryConnect(URL serverURL) throws IOException {
+        URLConnection connection = null;
+        try {
+            connection = m_agentContext.getConnectionHandler().getConnection(serverURL);
+            connection.connect();
+        }
+        finally {
+            if (connection != null && connection instanceof HttpURLConnection)
+                ((HttpURLConnection) connection).disconnect();
+        }
     }
 }

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/identification/IdentificationHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/identification/IdentificationHandler.java?rev=1513770&r1=1513769&r2=1513770&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/identification/IdentificationHandler.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/identification/IdentificationHandler.java
Wed Aug 14 09:15:00 2013
@@ -1,6 +1,15 @@
 package org.apache.ace.agent.identification;
 
+/**
+ * Agent control delegate interface that is responsible for target identification.
+ * 
+ */
 public interface IdentificationHandler {
 
+    /**
+     * Return the agent identification.
+     * 
+     * @return The identification, <code>null</code> if none is available.
+     */
     String getIdentification();
 }

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/identification/impl/IdentificationHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/identification/impl/IdentificationHandlerImpl.java?rev=1513770&r1=1513769&r2=1513770&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/identification/impl/IdentificationHandlerImpl.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/identification/impl/IdentificationHandlerImpl.java
Wed Aug 14 09:15:00 2013
@@ -3,16 +3,35 @@ package org.apache.ace.agent.identificat
 import org.apache.ace.agent.identification.IdentificationHandler;
 import org.apache.ace.agent.impl.AgentContext;
 
+/**
+ * Default identification handler that reads the identity from the configuration using key
+ * {@link IDENTIFICATION_CONFIG_KEY}.
+ * 
+ */
 public class IdentificationHandlerImpl implements IdentificationHandler {
 
+    /**
+     * Configuration key for the default identification handler. The value must be a single
file-system and URL safe
+     * string.
+     */
+    // TODO move to and validate in configuration handler?
+    public static final String IDENTIFICATION_CONFIG_KEY = "agent.discovery";
+
     private final AgentContext m_agentContext;
 
     public IdentificationHandlerImpl(AgentContext agentContext) {
         m_agentContext = agentContext;
     }
 
+    // TODO add a default fallback?
     @Override
     public String getIdentification() {
-        return "agent";
+        String configValue = m_agentContext.getConfiguration().getMap().get(IDENTIFICATION_CONFIG_KEY);
+        if (configValue == null)
+            return null;
+        configValue = configValue.trim();
+        if (configValue.equals(""))
+            return null;
+        return configValue;
     }
 }

Added: ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/discovery/impl/DiscoveryHandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/discovery/impl/DiscoveryHandlerImplTest.java?rev=1513770&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/discovery/impl/DiscoveryHandlerImplTest.java
(added)
+++ ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/discovery/impl/DiscoveryHandlerImplTest.java
Wed Aug 14 09:15:00 2013
@@ -0,0 +1,114 @@
+package org.apache.ace.agent.discovery.impl;
+
+import static org.easymock.EasyMock.expect;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
+
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.ace.agent.configuration.ConfigurationHandler;
+import org.apache.ace.agent.connection.ConnectionHandler;
+import org.apache.ace.agent.connection.impl.ConnectionHandlerImpl;
+import org.apache.ace.agent.discovery.DiscoveryHandler;
+import org.apache.ace.agent.impl.AgentContext;
+import org.apache.ace.agent.testutil.BaseEasyMockTest;
+import org.apache.ace.agent.testutil.TestWebServer;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+public class DiscoveryHandlerImplTest extends BaseEasyMockTest {
+
+    Map<String, String> configuration = new HashMap<String, String>();
+
+    DiscoveryHandler m_discoveryHandler;
+
+    TestWebServer m_webServer;
+    TestWebServer m_secondWebServer;
+
+    URL m_availableURL;
+    URL m_unavailableURL;
+
+    @BeforeTest
+    public void setUpAgain() throws Exception {
+
+        m_webServer = new TestWebServer(8888, "/", "generated");
+        m_webServer.start();
+
+        m_availableURL = new URL("http://localhost:8888");
+        m_unavailableURL = new URL("http://localhost:8889");
+
+        AgentContext agentContext = addTestMock(AgentContext.class);
+        m_discoveryHandler = new DiscoveryHandlerImpl(agentContext);
+
+        ConfigurationHandler configurationHandler = addTestMock(ConfigurationHandler.class);
+        expect(configurationHandler.getMap()).andReturn(configuration).anyTimes();
+
+        ConnectionHandler connectionHandler = new ConnectionHandlerImpl(agentContext);
+
+        expect(agentContext.getConfiguration()).andReturn(configurationHandler).anyTimes();
+        expect(agentContext.getConnectionHandler()).andReturn(connectionHandler).anyTimes();
+
+        replayTestMocks();
+    }
+
+    @AfterTest
+    public void tearDownAgain() throws Exception {
+        m_webServer.stop();
+        verifyTestMocks();
+    }
+
+    @Test
+    public void testAvailableURL() throws Exception {
+        configuration.put(DiscoveryHandlerImpl.DISCOVERY_CONFIG_KEY, m_availableURL.toExternalForm());
+        assertEquals(m_discoveryHandler.getServerUrl(), m_availableURL);
+    }
+
+    @Test
+    public void testUnavailableURL_unavailable() throws Exception {
+        configuration.put(DiscoveryHandlerImpl.DISCOVERY_CONFIG_KEY, m_unavailableURL.toExternalForm());
+        assertNull(m_discoveryHandler.getServerUrl());
+    }
+
+    @Test
+    public void testUnavailableAfterConfigUpdate() throws Exception {
+        configuration.put(DiscoveryHandlerImpl.DISCOVERY_CONFIG_KEY, m_availableURL.toExternalForm());
+        assertEquals(m_discoveryHandler.getServerUrl(), m_availableURL);
+        configuration.put(DiscoveryHandlerImpl.DISCOVERY_CONFIG_KEY, m_unavailableURL.toExternalForm());
+        assertNull(m_discoveryHandler.getServerUrl());
+    }
+
+    @Test
+    public void testAvailableAfterConfigUpdate() throws Exception {
+        configuration.put(DiscoveryHandlerImpl.DISCOVERY_CONFIG_KEY, m_unavailableURL.toExternalForm());
+        assertNull(m_discoveryHandler.getServerUrl());
+        configuration.put(DiscoveryHandlerImpl.DISCOVERY_CONFIG_KEY, m_availableURL.toExternalForm());
+        assertEquals(m_discoveryHandler.getServerUrl(), m_availableURL);
+    }
+
+    @Test
+    public void testAvailableAfterUnavailableURL() throws Exception {
+        configuration.put(DiscoveryHandlerImpl.DISCOVERY_CONFIG_KEY, m_unavailableURL.toExternalForm()
+ "," + m_availableURL.toExternalForm());
+        assertEquals(m_discoveryHandler.getServerUrl(), m_availableURL);
+    }
+
+    @Test
+    public void testNoURLConfig() throws Exception {
+        configuration.clear();
+        assertNull(m_discoveryHandler.getServerUrl());
+    }
+
+    @Test
+    public void testEmptyURLConfig() throws Exception {
+        configuration.put(DiscoveryHandlerImpl.DISCOVERY_CONFIG_KEY, "");
+        assertNull(m_discoveryHandler.getServerUrl());
+    }
+
+    @Test
+    public void testBadURLConfig() throws Exception {
+        configuration.put(DiscoveryHandlerImpl.DISCOVERY_CONFIG_KEY, "fooBar");
+        assertNull(m_discoveryHandler.getServerUrl());
+    }
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/identification/impl/IdentificationhandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/identification/impl/IdentificationhandlerImplTest.java?rev=1513770&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/identification/impl/IdentificationhandlerImplTest.java
(added)
+++ ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/identification/impl/IdentificationhandlerImplTest.java
Wed Aug 14 09:15:00 2013
@@ -0,0 +1,64 @@
+package org.apache.ace.agent.identification.impl;
+
+import static org.easymock.EasyMock.expect;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.ace.agent.configuration.ConfigurationHandler;
+import org.apache.ace.agent.identification.IdentificationHandler;
+import org.apache.ace.agent.impl.AgentContext;
+import org.apache.ace.agent.testutil.BaseEasyMockTest;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+public class IdentificationhandlerImplTest extends BaseEasyMockTest {
+
+    Map<String, String> configuration = new HashMap<String, String>();
+
+    IdentificationHandler m_identificationHandler;
+
+    @BeforeTest
+    public void setUpAgain() throws Exception {
+        AgentContext agentContext = addTestMock(AgentContext.class);
+        m_identificationHandler = new IdentificationHandlerImpl(agentContext);
+        ConfigurationHandler configurationHandler = addTestMock(ConfigurationHandler.class);
+        expect(configurationHandler.getMap()).andReturn(configuration).anyTimes();
+        expect(agentContext.getConfiguration()).andReturn(configurationHandler).anyTimes();
+        replayTestMocks();
+    }
+
+    @AfterTest
+    public void tearDownAgain() throws Exception {
+        verifyTestMocks();
+    }
+
+    @Test
+    public void testAvailableIdentification() throws Exception {
+        configuration.put(IdentificationHandlerImpl.IDENTIFICATION_CONFIG_KEY, "qqq");
+        assertEquals(m_identificationHandler.getIdentification(), "qqq");
+    }
+
+    @Test
+    public void testUpdatedIdentification() throws Exception {
+        configuration.put(IdentificationHandlerImpl.IDENTIFICATION_CONFIG_KEY, "qqq");
+        assertEquals(m_identificationHandler.getIdentification(), "qqq");
+        configuration.put(IdentificationHandlerImpl.IDENTIFICATION_CONFIG_KEY, "yyy");
+        assertEquals(m_identificationHandler.getIdentification(), "yyy");
+    }
+
+    @Test
+    public void testNoIdentification() throws Exception {
+        configuration.clear();
+        assertNull(m_identificationHandler.getIdentification());
+    }
+
+    @Test
+    public void testEmptyIdentification() throws Exception {
+        configuration.put(IdentificationHandlerImpl.IDENTIFICATION_CONFIG_KEY, " ");
+        assertNull(m_identificationHandler.getIdentification());
+    }
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseEasyMockTest.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseEasyMockTest.java?rev=1513770&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseEasyMockTest.java
(added)
+++ ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseEasyMockTest.java
Wed Aug 14 09:15:00 2013
@@ -0,0 +1,33 @@
+package org.apache.ace.agent.testutil;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class BaseEasyMockTest {
+
+    Set<Object> m_mocks = new HashSet<Object>();
+
+    protected <T extends Object> T addTestMock(Class<T> clazz) {
+        T mock = createMock(clazz);
+        m_mocks.add(mock);
+        return mock;
+    }
+
+    protected void replayTestMocks() {
+        for (Object mock : m_mocks)
+            replay(mock);
+    }
+
+    protected void verifyTestMocks() {
+        for (Object mock : m_mocks)
+            verify(mock);
+    }
+
+    protected void clearTestMocks() {
+        m_mocks.clear();
+    }
+}



Mime
View raw message