ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r1513569 - in /ace/sandbox/bramk/org.apache.ace.agent: src/org/apache/ace/agent/ src/org/apache/ace/agent/connection/ src/org/apache/ace/agent/deployment/ src/org/apache/ace/agent/deployment/impl/ src/org/apache/ace/agent/discovery/ src/org...
Date Tue, 13 Aug 2013 16:00:20 GMT
Author: bramk
Date: Tue Aug 13 16:00:20 2013
New Revision: 1513569

URL: http://svn.apache.org/r1513569
Log:
[sandbox] Agent API draft wip

Modified:
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/AgentControl.java
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/FeedbackChannel.java
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/connection/ConnectionHandler.java
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/DeploymentHandler.java
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/impl/DeploymentHandlerImpl.java
    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/download/DownloadHandler.java
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/download/DownloadResult.java
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentControlImpl.java
    ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DefaultController.java
    ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/deployment/impl/DeploymentHandlerTest.java
    ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/AgentControl.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/AgentControl.java?rev=1513569&r1=1513568&r2=1513569&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/AgentControl.java (original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/AgentControl.java Tue
Aug 13 16:00:20 2013
@@ -16,12 +16,14 @@ public interface AgentControl {
     ConfigurationHandler getConfiguration();
 
     /** access to the feedback channels */
-    List<FeedbackChannel> getFeedbackChannels();
+    List<String> getFeedbackChannelNames();
 
-    /** access to the a named feedback channel */
     FeedbackChannel getFeedbackChannel(String name);
 
     DownloadHandler getDownloadHandler();
 
     DeploymentHandler getDeploymentHandler();
+
+    // TODO
+    // AgentUpdateHandler getAgentUpdateHandler();
 }

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/FeedbackChannel.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/FeedbackChannel.java?rev=1513569&r1=1513568&r2=1513569&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/FeedbackChannel.java (original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/FeedbackChannel.java Tue
Aug 13 16:00:20 2013
@@ -8,16 +8,6 @@ import org.apache.ace.agent.deployment.R
 public interface FeedbackChannel {
 
     /**
-     * Returns the name of the channel.
-     */
-    String getName();
-
-    /**
-     * Returns <code>true</code> when new feedback has become available since
the last synchronization.
-     */
-    boolean feedbackAvailable();
-
-    /**
      * Synchronizes the feedback with the current server. Ensures the server has at least
as much feedback data as we
      * do.
      */

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/connection/ConnectionHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/connection/ConnectionHandler.java?rev=1513569&r1=1513568&r2=1513569&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/connection/ConnectionHandler.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/connection/ConnectionHandler.java
Tue Aug 13 16:00:20 2013
@@ -4,7 +4,18 @@ import java.io.IOException;
 import java.net.URL;
 import java.net.URLConnection;
 
+/**
+ * Agent control delegate interface that is responsible for opening connection.
+ * 
+ */
 public interface ConnectionHandler {
 
+    /**
+     * Return a connection for the specified url.
+     * 
+     * @param url The URL
+     * @return The connection
+     * @throws IOException If opening the connection fails
+     */
     URLConnection getConnection(URL url) throws IOException;
 }

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/DeploymentHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/DeploymentHandler.java?rev=1513569&r1=1513568&r2=1513569&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/DeploymentHandler.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/DeploymentHandler.java
Tue Aug 13 16:00:20 2013
@@ -2,28 +2,70 @@ package org.apache.ace.agent.deployment;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.URL;
 import java.util.SortedSet;
 
+import org.apache.ace.agent.download.DownloadHandle;
 import org.osgi.framework.Version;
 
 /**
- * Agent control delegate interface that provides the deployment handles.
+ * Agent control delegate interface that provides the deployment functions.
  * 
  */
 public interface DeploymentHandler {
 
-    SortedSet<Version> getInstalledVersions() throws RetryAfterException, IOException;
-
+    /**
+     * Return the installed deployment package version for this agent.
+     * 
+     * @return The installed version, {@link Version.emptyVersion} if no packages have been
installed
+     */
+    Version getInstalledVersion();
+
+    /**
+     * Return the sorted set of available deployment package versions as reported by the
server.
+     * 
+     * @return The sorted set of versions, may be empty
+     * @throws RetryAfterException If the server indicates it is too busy with a Retry-After
header
+     * @throws IOException If the connection to the server fails
+     */
     SortedSet<Version> getAvailableVersions() throws RetryAfterException, IOException;
 
-    URL getPackageURL(Version version, boolean fixPackage) throws RetryAfterException, IOException;
-
+    /**
+     * Return the estimated size for a deployment package as reported by the server.
+     * 
+     * @param version The version of the package
+     * @param fixPackage Request the server for a fix-package
+     * @return The estimated size in bytes, <code>-1</code> indicates the size
is unknown
+     * @throws RetryAfterException If the server indicates it is too busy with a Retry-After
header
+     * @throws IOException If the connection to the server fails
+     */
     long getPackageSize(Version version, boolean fixPackage) throws RetryAfterException,
IOException;
 
-    void deployPackage(Version version, boolean fixPackage) throws RetryAfterException, IOException;
-
-    void deployPackage(InputStream inputStream) throws RetryAfterException, IOException;
-
-    void deployPackage(URL url) throws RetryAfterException, IOException;
+    /**
+     * Returns the {@link InputStream} for a deployment package.
+     * 
+     * @param version The version of the deployment package
+     * @param fixPackage Request the server for a fix-package
+     * @return The input-stream for the deployment package
+     * @throws RetryAfterException If the server indicates it is too busy with a Retry-After
header
+     * @throws IOException If the connection to the server fails
+     */
+    InputStream getInputStream(Version version, boolean fixPackage) throws RetryAfterException,
IOException;
+
+    /**
+     * Return the {@link DownloadHandle} for a deployment package.
+     * 
+     * @param version The version of the deployment package
+     * @param fixPackage Request the server for a fix-package
+     * @return The download handle
+     */
+    DownloadHandle getDownloadHandle(Version version, boolean fixPackage);
+
+    /**
+     * Install a deployment package from an input stream.
+     * 
+     * @param inputStream The inputStream, not <code>null</code>
+     * @throws IOException If reading the input stream fails.
+     */
+    // TODO deployment exceptions
+    void deployPackage(InputStream inputStream) throws IOException;
 }

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/impl/DeploymentHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/impl/DeploymentHandlerImpl.java?rev=1513569&r1=1513568&r2=1513569&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/impl/DeploymentHandlerImpl.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/impl/DeploymentHandlerImpl.java
Tue Aug 13 16:00:20 2013
@@ -14,6 +14,7 @@ import java.util.TreeSet;
 import org.apache.ace.agent.AgentConstants;
 import org.apache.ace.agent.deployment.DeploymentHandler;
 import org.apache.ace.agent.deployment.RetryAfterException;
+import org.apache.ace.agent.download.DownloadHandle;
 import org.apache.ace.agent.impl.AgentContext;
 import org.apache.felix.deploymentadmin.DeploymentAdminImpl;
 import org.osgi.framework.Version;
@@ -36,26 +37,49 @@ public class DeploymentHandlerImpl imple
     }
 
     @Override
-    public SortedSet<Version> getInstalledVersions() throws RetryAfterException, IOException
{
-        return listInstalledVersions();
+    public Version getInstalledVersion() {
+        Version highestVersion = Version.emptyVersion;
+        DeploymentPackage[] installedPackages = m_deploymentAdmin.listDeploymentPackages();
+        for (DeploymentPackage installedPackage : installedPackages) {
+            if (installedPackage.getName().equals(getIdentification())
+                && installedPackage.getVersion().compareTo(highestVersion) > 0)
{
+                highestVersion = installedPackage.getVersion();
+            }
+        }
+        return highestVersion;
     }
 
     @Override
     public SortedSet<Version> getAvailableVersions() throws RetryAfterException, IOException
{
-        return listRemoteVersions();
-    }
 
-    @Override
-    public URL getPackageURL(Version version, boolean fixPackage) throws RetryAfterException,
IOException {
-        URL url = null;
-        if (fixPackage) {
-            url = getEndpoint(getServerURL(), getIdentification(),
-                getInstalledVersions().last(), version);
+        SortedSet<Version> versions = new TreeSet<Version>();
+
+        URL endpoint = getEndpoint(getServerURL(), getIdentification());
+        URLConnection connection = null;
+        BufferedReader reader = null;
+        try {
+            connection = getConnection(endpoint);
+
+            // TODO handle problems and retries
+            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+            String versionString;
+            while ((versionString = reader.readLine()) != null) {
+                try {
+                    Version version = Version.parseVersion(versionString);
+                    versions.add(version);
+                }
+                catch (IllegalArgumentException e) {
+                    throw new IOException(e);
+                }
+            }
+            return versions;
         }
-        else {
-            url = getEndpoint(getServerURL(), getIdentification(), version);
+        finally {
+            if (connection != null && connection instanceof HttpURLConnection)
+                ((HttpURLConnection) connection).disconnect();
+            if (reader != null)
+                reader.close();
         }
-        return url;
     }
 
     @Override
@@ -95,23 +119,15 @@ public class DeploymentHandlerImpl imple
     }
 
     @Override
-    public void deployPackage(Version version, boolean fixPackage) throws RetryAfterException,
IOException {
-        URL url = getPackageURL(version, fixPackage);
-        deployPackage(url);
-    }
-
-    @Override
-    public void deployPackage(URL url) {
-        // FIXME exceptions
+    public InputStream getInputStream(Version version, boolean fixPackage) throws RetryAfterException,
IOException {
+        URL packageURL = getPackageURL(version, fixPackage);
         URLConnection urlConnection = null;
         InputStream inputStream = null;
         try {
-            urlConnection = url.openConnection();
+            // TODO handle problems and retries
+            urlConnection = packageURL.openConnection();
             inputStream = urlConnection.getInputStream();
-            deployPackage(inputStream);
-        }
-        catch (Exception e) {
-            e.printStackTrace();
+            return inputStream;
         }
         finally {
             if (urlConnection != null && urlConnection instanceof HttpURLConnection)
@@ -127,6 +143,13 @@ public class DeploymentHandlerImpl imple
     }
 
     @Override
+    public DownloadHandle getDownloadHandle(Version version, boolean fixPackage) {
+        URL packageURL = getPackageURL(version, fixPackage);
+        DownloadHandle downloadHandle = m_agentContext.getDownloadHandler().getHandle(packageURL);
+        return downloadHandle;
+    }
+
+    @Override
     public void deployPackage(InputStream inputStream) {
         // FIXME exceptions
         try {
@@ -137,48 +160,15 @@ public class DeploymentHandlerImpl imple
         }
     }
 
-    private SortedSet<Version> listRemoteVersions() throws IOException {
-
-        SortedSet<Version> versions = new TreeSet<Version>();
-        URL endpoint = getEndpoint(getServerURL(), getIdentification());
-        URLConnection connection = null;
-        BufferedReader reader = null;
-
-        try {
-            connection = m_agentContext.getConnectionHandler().getConnection(endpoint);
-            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
-            String versionString;
-            while ((versionString = reader.readLine()) != null) {
-                try {
-                    Version version = Version.parseVersion(versionString);
-                    if (version != Version.emptyVersion) {
-                        versions.add(version);
-                    }
-                }
-                catch (IllegalArgumentException e) {
-                    throw new IOException(e);
-                }
-            }
-            return versions;
-        }
-        finally {
-            if (connection != null && connection instanceof HttpURLConnection)
-                ((HttpURLConnection) connection).disconnect();
-            if (reader != null)
-                reader.close();
+    private URL getPackageURL(Version version, boolean fixPackage) {
+        URL url = null;
+        if (fixPackage) {
+            url = getEndpoint(getServerURL(), getIdentification(), getInstalledVersion(),
version);
         }
-    }
-
-    private SortedSet<Version> listInstalledVersions() {
-        DeploymentPackage[] installedPackages = m_deploymentAdmin.listDeploymentPackages();
-        SortedSet<Version> versions = new TreeSet<Version>();
-        versions.add(Version.emptyVersion);
-        for (int i = 0; i < installedPackages.length; i++) {
-            if (installedPackages[i].getName().equals(getIdentification())) {
-                versions.add(installedPackages[i].getVersion());
-            }
+        else {
+            url = getEndpoint(getServerURL(), getIdentification(), version);
         }
-        return versions;
+        return url;
     }
 
     private String getIdentification() {
@@ -186,18 +176,37 @@ public class DeploymentHandlerImpl imple
     }
 
     private URL getServerURL() {
-        return m_agentContext.getDiscoveryHandler().getServerUrls()[0];
+        return m_agentContext.getDiscoveryHandler().getServerUrl();
     }
 
-    private static URL getEndpoint(URL serverURL, String identification) throws MalformedURLException
{
-        return new URL(serverURL, "deployment/" + identification + "/versions/");
+    private URLConnection getConnection(URL url) throws IOException {
+        return m_agentContext.getConnectionHandler().getConnection(url);
     }
 
-    private URL getEndpoint(URL serverURL, String identification, Version version) throws
MalformedURLException {
-        return new URL(serverURL, "deployment/" + identification + "/versions/" + version.toString());
+    private static URL getEndpoint(URL serverURL, String identification) {
+        try {
+            return new URL(serverURL, "deployment/" + identification + "/versions/");
+        }
+        catch (MalformedURLException e) {
+            throw new IllegalStateException(e);
+        }
     }
 
-    private URL getEndpoint(URL serverURL, String identification, Version from, Version to)
throws MalformedURLException {
-        return new URL(serverURL, "deployment/" + identification + "/versions/" + to.toString()
+ "?current=" + from);
+    private URL getEndpoint(URL serverURL, String identification, Version version) {
+        try {
+            return new URL(serverURL, "deployment/" + identification + "/versions/" + version.toString());
+        }
+        catch (MalformedURLException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
+    private URL getEndpoint(URL serverURL, String identification, Version from, Version to)
{
+        try {
+            return new URL(serverURL, "deployment/" + identification + "/versions/" + to.toString()
+ "?current=" + from);
+        }
+        catch (MalformedURLException e) {
+            throw new IllegalStateException(e);
+        }
     }
 }

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=1513569&r1=1513568&r2=1513569&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
Tue Aug 13 16:00:20 2013
@@ -2,7 +2,17 @@ package org.apache.ace.agent.discovery;
 
 import java.net.URL;
 
+/**
+ * Agent control delegate interface that is responsible for discovering servers. If the handler
supports dynamic
+ * discovery or multiple URLs it should select the most appropriate candidate.
+ * 
+ */
 public interface DiscoveryHandler {
 
-    URL[] getServerUrls();
+    /**
+     * Return a server base URL.
+     * 
+     * @return a URL
+     */
+    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=1513569&r1=1513568&r2=1513569&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
Tue Aug 13 16:00:20 2013
@@ -5,6 +5,9 @@ import java.net.URL;
 import org.apache.ace.agent.discovery.DiscoveryHandler;
 import org.apache.ace.agent.impl.AgentContext;
 
+/**
+ *
+ */
 public class DiscoveryHandlerImpl implements DiscoveryHandler {
 
     private final AgentContext m_agentContext;
@@ -17,7 +20,7 @@ public class DiscoveryHandlerImpl implem
     }
 
     @Override
-    public URL[] getServerUrls() {
-        return m_urls;
+    public URL getServerUrl() {
+        return m_urls[0];
     }
 }

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/download/DownloadHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/download/DownloadHandler.java?rev=1513569&r1=1513568&r2=1513569&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/download/DownloadHandler.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/download/DownloadHandler.java
Tue Aug 13 16:00:20 2013
@@ -15,5 +15,6 @@ public interface DownloadHandler {
      */
     DownloadHandle getHandle(URL url);
     
+    //TODO named handlers (resume over urls)
     DownloadHandle getHandle(URL url, int readBufferSize);
 }

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/download/DownloadResult.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/download/DownloadResult.java?rev=1513569&r1=1513568&r2=1513569&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/download/DownloadResult.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/download/DownloadResult.java
Tue Aug 13 16:00:20 2013
@@ -22,6 +22,7 @@ public interface DownloadResult {
      * 
      * @return The file, <code>null</code> if the download was unsuccessful
      */
+    //TODO inputstream
     File getFile();
 
     int getCode();

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java?rev=1513569&r1=1513568&r2=1513569&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java (original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java Tue
Aug 13 16:00:20 2013
@@ -21,6 +21,7 @@ import org.apache.felix.dm.DependencyAct
 import org.apache.felix.dm.DependencyManager;
 import org.osgi.framework.BundleContext;
 
+// TODO Decouple from DM to save 170k in agent size.
 public class Activator extends DependencyActivatorBase implements AgentContext {
 
     private volatile ConfigurationHandler m_configurationHandler;

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentControlImpl.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentControlImpl.java?rev=1513569&r1=1513568&r2=1513569&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentControlImpl.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentControlImpl.java
Tue Aug 13 16:00:20 2013
@@ -32,7 +32,7 @@ public class AgentControlImpl implements
     }
 
     @Override
-    public List<FeedbackChannel> getFeedbackChannels() {
+    public List<String> getFeedbackChannelNames() {
         // TODO Auto-generated method stub
         return null;
     }

Modified: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DefaultController.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DefaultController.java?rev=1513569&r1=1513568&r2=1513569&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DefaultController.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DefaultController.java
Tue Aug 13 16:00:20 2013
@@ -1,6 +1,7 @@
 package org.apache.ace.agent.impl;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.SortedSet;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
@@ -55,18 +56,21 @@ public class DefaultController implement
 
         DeploymentHandler deploymentHandler = getDeploymentHandler();
 
-        SortedSet<Version> installed = deploymentHandler.getInstalledVersions();
-        Version current = Version.emptyVersion;
-        if (installed != null && !installed.isEmpty())
-            current = installed.last();
-
+        Version current = deploymentHandler.getInstalledVersion();
         SortedSet<Version> available = deploymentHandler.getAvailableVersions();
         Version highest = Version.emptyVersion;
         if (available != null && !available.isEmpty())
             highest = available.last();
 
-        if (highest.compareTo(current) > 1)
-            deploymentHandler.deployPackage(highest, true);
+        if (highest.compareTo(current) > 1) {
+            InputStream inputStream = deploymentHandler.getInputStream(highest, true);
+            try {
+                deploymentHandler.deployPackage(inputStream);
+            }
+            finally {
+                inputStream.close();
+            }
+        }
     }
 
     private void reSchedule(long seconds) {
@@ -74,7 +78,7 @@ public class DefaultController implement
     }
 
     private void unSchedule() {
-        if(m_future != null)
+        if (m_future != null)
             m_future.cancel(true);
     }
 

Modified: ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/deployment/impl/DeploymentHandlerTest.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/deployment/impl/DeploymentHandlerTest.java?rev=1513569&r1=1513568&r2=1513569&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/deployment/impl/DeploymentHandlerTest.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/deployment/impl/DeploymentHandlerTest.java
Tue Aug 13 16:00:20 2013
@@ -117,7 +117,7 @@ public class DeploymentHandlerTest exten
         expect(identificationHandler.getIdentification()).andReturn(identification).anyTimes();
 
         DiscoveryHandler discoveryHandler = createMock(DiscoveryHandler.class);
-        expect(discoveryHandler.getServerUrls()).andReturn(new URL[] { serverURL }).anyTimes();
+        expect(discoveryHandler.getServerUrl()).andReturn(serverURL).anyTimes();
 
         DeploymentAdmin deploymentAdmin = createMock(DeploymentAdmin.class);
         expect(deploymentAdmin.listDeploymentPackages()).andReturn(
@@ -137,10 +137,9 @@ public class DeploymentHandlerTest exten
 
     @Test
     public void testCurrentVersion() throws Exception {
-        SortedSet<Version> installed = m_deploymentHandler.getInstalledVersions();
-        assertNotNull(installed);
-        assertFalse(installed.isEmpty());
-        assertEquals(installed.last(), version2);
+        Version current = m_deploymentHandler.getInstalledVersion();
+        assertNotNull(current);
+        assertEquals(current, version2);
     }
 
     @Test
@@ -163,6 +162,12 @@ public class DeploymentHandlerTest exten
 
     @Test
     public void testDeployPackage() throws Exception {
-        m_deploymentHandler.deployPackage(version3, true);
+        InputStream inputStream = m_deploymentHandler.getInputStream(version3, true);
+        try {
+            m_deploymentHandler.deployPackage(inputStream);
+        }
+        finally {
+            inputStream.close();
+        }
     }
 }

Modified: ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java?rev=1513569&r1=1513568&r2=1513569&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java
(original)
+++ ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java
Tue Aug 13 16:00:20 2013
@@ -1,123 +1,134 @@
 package org.apache.ace.agent.impl;
 
 import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.eq;
 import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.notNull;
 import static org.easymock.EasyMock.replay;
-import static org.testng.Assert.assertTrue;
+import static org.easymock.EasyMock.verify;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.PrintWriter;
+import java.io.InputStream;
 import java.net.URL;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
 
-import org.apache.ace.agent.AgentConstants;
 import org.apache.ace.agent.AgentControl;
-import org.apache.ace.agent.connection.ConnectionHandler;
-import org.apache.ace.agent.connection.impl.ConnectionHandlerImpl;
 import org.apache.ace.agent.deployment.DeploymentHandler;
-import org.apache.ace.agent.deployment.impl.DeploymentHandlerImpl;
-import org.apache.ace.agent.discovery.DiscoveryHandler;
-import org.apache.ace.agent.download.DownloadHandler;
-import org.apache.ace.agent.download.impl.DownloadHandlerImpl;
-import org.apache.ace.agent.identification.IdentificationHandler;
-import org.apache.ace.agent.testutil.TestWebServer;
-import org.osgi.service.deploymentadmin.DeploymentAdmin;
-import org.testng.annotations.BeforeClass;
+import org.apache.ace.agent.download.DownloadHandle;
+import org.apache.ace.agent.download.DownloadResult;
+import org.apache.ace.agent.download.DownloadState;
+import org.osgi.framework.Version;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.BeforeTest;
 import org.testng.annotations.Test;
 
 public class CustomControllerTest {
 
-    static class TestDeploymentServlet extends HttpServlet {
-        private static final long serialVersionUID = 1L;
+    AgentControl m_agentControl;
 
-        private Map<String, File> m_packages = new HashMap<String, File>();
+    Version v1 = Version.parseVersion("1.0.0");
+    Version v2 = Version.parseVersion("2.0.0");
+    Version v3 = Version.parseVersion("3.0.0");
 
-        @Override
-        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
+    SortedSet<Version> availableVersion = new TreeSet<Version>();
 
-            String pathInfo = req.getPathInfo();
-            if (pathInfo != null && pathInfo.startsWith("/"))
-                pathInfo = pathInfo.substring(1);
-
-            if (pathInfo != null && m_packages.containsKey(pathInfo)) {
-                File file = m_packages.get(pathInfo);
-                resp.setHeader(AgentConstants.HEADER_DPSIZE, "" + file.length());
-                FileInputStream fis = new FileInputStream(file);
-                OutputStream os = resp.getOutputStream();
-                int b;
-                while ((b = fis.read()) != -1) {
-                    os.write(b);
-                }
-                fis.close();
-                os.close();
-            }
-            else {
-                PrintWriter writer = resp.getWriter();
-                for (String version : m_packages.keySet()) {
-                    writer.println(version);
-                }
-                writer.close();
-            }
-            resp.setStatus(200, "voila");
-        }
+    File dummyFile;
+    URL dummyFileUrl;
+    InputStream dummyInputStream;
 
-        void addPackage(String version, File file) {
-            m_packages.put(version, file);
-        }
+    Set<Object> m_mocks = new HashSet<Object>();
+
+    @BeforeTest
+    public void setUpOnceAgain() throws Exception {
+
+        availableVersion.add(v1);
+        availableVersion.add(v2);
+        availableVersion.add(v3);
+
+        dummyFile = File.createTempFile("mock", ".txt");
+        dummyFile.deleteOnExit();
+        dummyFileUrl = dummyFile.toURI().toURL();
     }
 
-    TestWebServer m_webServer;
-    AgentControl m_agentControl;
+    @BeforeMethod
+    public void setUpAgain() throws Exception {
 
-    @BeforeClass
-    public void setUpOnceAgain() throws Exception {
+        dummyInputStream = new FileInputStream(dummyFile);
 
-        String identification = "agent007";
-        int port = 8888;
+        DownloadResult downloadResult = addTestMock(DownloadResult.class);
+        expect(downloadResult.getState()).andReturn(DownloadState.SUCCESSFUL).anyTimes();
+        expect(downloadResult.getFile()).andReturn(dummyFile).anyTimes();
+
+        DownloadHandle downloadHandle = addTestMock(DownloadHandle.class);
+        expect(downloadHandle.start()).andReturn(downloadHandle).anyTimes();
+        expect(downloadHandle.result()).andReturn(downloadResult).anyTimes();
+
+        DeploymentHandler deploymentHandler = addTestMock(DeploymentHandler.class);
+        expect(deploymentHandler.getInstalledVersion()).andReturn(v2).anyTimes();
+        expect(deploymentHandler.getAvailableVersions()).andReturn(availableVersion).anyTimes();
+        expect(deploymentHandler.getDownloadHandle(eq(v3), eq(true))).andReturn(downloadHandle).once();
+        deploymentHandler.deployPackage(notNull(InputStream.class));
+        expectLastCall().once();
 
-        m_webServer = new TestWebServer(port, "/", "generated");
-        m_webServer.addServlet(new TestDeploymentServlet(), "/deployment/" + identification
+ "/versions/*");
+        AgentContext agentContext = addTestMock(AgentContext.class);
+        expect(agentContext.getDeploymentHandler()).andReturn(deploymentHandler).anyTimes();
 
-        AgentContext agentContext = createMock("ctx", AgentContext.class);
+        replayTestMocks();
+        m_agentControl = new AgentControlImpl(agentContext);
+    }
 
-        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
-        ConnectionHandler connectionHandler = new ConnectionHandlerImpl(agentContext);
-        DownloadHandler downloadHandler = new DownloadHandlerImpl(agentContext);
+    @AfterMethod
+    public void tearDownOnceAgain() throws Exception {
+        verifyTestMocks();
+        clearTestMocks();
+        dummyInputStream.close();
+    }
 
-        DeploymentAdmin deploymentAdmin = createNiceMock("da", DeploymentAdmin.class);
-        DeploymentHandler deploymentHandler = new DeploymentHandlerImpl(agentContext, deploymentAdmin);
+    @Test
+    public void testDowlownloading() throws Exception {
 
-        IdentificationHandler identificationHandler = createMock(IdentificationHandler.class);
-        expect(identificationHandler.getIdentification()).andReturn(identification).anyTimes();
+        Version current = m_agentControl.getDeploymentHandler().getInstalledVersion();
+        Version highest = m_agentControl.getDeploymentHandler().getAvailableVersions().last();
+        if (highest.compareTo(current) > 0) {
+
+            DownloadHandle handle = m_agentControl.getDeploymentHandler().getDownloadHandle(highest,
true);
+            DownloadResult result = handle.start().result();
+
+            if (result.getState() == DownloadState.SUCCESSFUL) {
+                InputStream inputStream = new FileInputStream(result.getFile());
+                try {
+                    m_agentControl.getDeploymentHandler().deployPackage(inputStream);
+                }
+                finally {
+                    inputStream.close();
+                }
+            }
+        }
+    }
 
-        DiscoveryHandler discoveryHandler = createMock(DiscoveryHandler.class);
-        expect(discoveryHandler.getServerUrls()).andReturn(new URL[] { new URL("http://localhost:"
+ port) }).anyTimes();
+    private <T extends Object> T addTestMock(Class<T> clazz) {
+        T mock = createMock(clazz);
+        m_mocks.add(mock);
+        return mock;
+    }
 
-        expect(agentContext.getExecutorService()).andReturn(executorService).anyTimes();
-        expect(agentContext.getIdentificationHandler()).andReturn(identificationHandler).anyTimes();
-        expect(agentContext.getDiscoveryHandler()).andReturn(discoveryHandler).anyTimes();
-        expect(agentContext.getDownloadHandler()).andReturn(downloadHandler).anyTimes();
-        expect(agentContext.getDeploymentHandler()).andReturn(deploymentHandler).anyTimes();
-        expect(agentContext.getConnectionHandler()).andReturn(connectionHandler).anyTimes();
+    private void replayTestMocks() {
+        for (Object mock : m_mocks)
+            replay(mock);
+    }
 
-        replay(agentContext, identificationHandler, discoveryHandler, deploymentAdmin);
-        m_agentControl = new AgentControlImpl(agentContext);
+    private void verifyTestMocks() {
+        for (Object mock : m_mocks)
+            verify(mock);
     }
 
-    @Test
-    public void testTrue() throws Exception {
-        assertTrue(true);
+    private void clearTestMocks() {
+        m_mocks.clear();
     }
 }



Mime
View raw message