ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1514593 - in /ace/trunk/org.apache.ace.agent: ./ src/org/apache/ace/agent/ src/org/apache/ace/agent/impl/ src/org/apache/ace/agent/updater/
Date Fri, 16 Aug 2013 06:55:36 GMT
Author: marrs
Date: Fri Aug 16 06:55:36 2013
New Revision: 1514593

URL: http://svn.apache.org/r1514593
Log:
ACE-342 First part of implementation.

Added:
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentUpdateHandler.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentUpdateHandlerImpl.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/UpdateHandlerBase.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/updater/
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/updater/Activator.java
Modified:
    ace/trunk/org.apache.ace.agent/bnd.bnd
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentControl.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentContext.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentControlImpl.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DeploymentHandlerImpl.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/IdentificationHandlerImpl.java

Modified: ace/trunk/org.apache.ace.agent/bnd.bnd
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/bnd.bnd?rev=1514593&r1=1514592&r2=1514593&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/bnd.bnd (original)
+++ ace/trunk/org.apache.ace.agent/bnd.bnd Fri Aug 16 06:55:36 2013
@@ -5,6 +5,7 @@ Bundle-Activator: org.apache.ace.agent.i
 
 Private-Package: \
 	org.apache.ace.agent.impl,\
+	org.apache.ace.agent.updater,\
     org.apache.commons.codec,\
 	org.apache.commons.codec.binary,\
 	org.apache.commons.codec.digest,\
@@ -18,15 +19,19 @@ Private-Package: \
 	org.apache.felix.dm.impl.index,\
 	org.apache.felix.dm.impl.metatype,\
 	org.apache.felix.dm.tracker,\
+	org.osgi.service.metatype,\
+	org.osgi.service.cm,\
 	org.osgi.util.tracker
 
 Import-Package: \
     !org.osgi.service.cm,\
 	!org.osgi.service.metatype,\
-	org.osgi.service.event;resolution:=optional,\
-	org.osgi.service.log;resolution:=optional,\
 	*
-	
+
+DynamicImport-Package: \
+	org.osgi.service.event,\
+	org.osgi.service.log
+
 Export-Package: \
     org.apache.ace.agent,\
 	org.osgi.service.deploymentadmin;-split-package:=merge-last,\

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentControl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentControl.java?rev=1514593&r1=1514592&r2=1514593&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentControl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentControl.java Fri Aug 16 06:55:36
2013
@@ -39,6 +39,5 @@ public interface AgentControl {
 
     DeploymentHandler getDeploymentHandler();
 
-    // TODO
-    // AgentUpdateHandler getAgentUpdateHandler();
+    AgentUpdateHandler getAgentUpdateHandler();
 }

Added: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentUpdateHandler.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentUpdateHandler.java?rev=1514593&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentUpdateHandler.java (added)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentUpdateHandler.java Fri Aug
16 06:55:36 2013
@@ -0,0 +1,27 @@
+package org.apache.ace.agent;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.SortedSet;
+
+import org.osgi.framework.Version;
+
+public interface AgentUpdateHandler {
+    /** Returns the locally installed version of the agent. */
+    Version getInstalledVersion();
+   
+    /** Returns the versions available on the server. */
+    SortedSet<Version> getAvailableVersions() throws RetryAfterException, IOException;
+   
+    /** Returns an input stream for the update of the agent. */
+    InputStream getInputStream(Version version) throws RetryAfterException, IOException;
+   
+    /** Returns a download handle to download the update of the agent. */
+    DownloadHandle getDownloadHandle(Version version) throws RetryAfterException, IOException;
+   
+    /** Returns the size of the update of the agent. */
+    long getSize(Version version) throws RetryAfterException, IOException;
+   
+    /** Installs the update of the agent. */
+    void install(InputStream stream) throws IOException;
+}

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java?rev=1514593&r1=1514592&r2=1514593&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java Fri Aug 16
06:55:36 2013
@@ -22,6 +22,7 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 
 import org.apache.ace.agent.AgentControl;
+import org.apache.ace.agent.AgentUpdateHandler;
 import org.apache.ace.agent.ConfigurationHandler;
 import org.apache.ace.agent.ConnectionHandler;
 import org.apache.ace.agent.DeploymentHandler;
@@ -33,7 +34,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.
+// TODO Decouple from DM to save 170k in agent size. Or: just include what we use
 public class Activator extends DependencyActivatorBase implements AgentContext {
 
     private volatile ConfigurationHandler m_configurationHandler;
@@ -44,6 +45,7 @@ public class Activator extends Dependenc
     private volatile ConnectionHandler m_connectionHandler;
     private volatile ScheduledExecutorService m_executorService;
     private volatile AgentControlImpl m_agentControl;
+    private volatile AgentUpdateHandler m_agentUpdateHandler;
 
     private volatile DefaultController m_controller;
 
@@ -60,6 +62,7 @@ public class Activator extends Dependenc
         m_deploymentHandler = new DeploymentHandlerImpl(this);
         m_downloadHandler = new DownloadHandlerImpl(this);
         m_agentControl = new AgentControlImpl(this);
+        m_agentUpdateHandler = new AgentUpdateHandlerImpl(this, context);
 
         Component service = createComponent().setImplementation(this)
             .setCallbacks("initAgent", "startAgent", "stopAgent", "destroyAgent")
@@ -149,4 +152,9 @@ public class Activator extends Dependenc
     public DownloadHandler getDownloadHandler() {
         return m_downloadHandler;
     }
+    
+    @Override
+    public AgentUpdateHandler getAgentUpdateHandler() {
+        return m_agentUpdateHandler;
+    }
 }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentContext.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentContext.java?rev=1514593&r1=1514592&r2=1514593&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentContext.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentContext.java Fri Aug
16 06:55:36 2013
@@ -20,6 +20,7 @@ package org.apache.ace.agent.impl;
 
 import java.util.concurrent.ScheduledExecutorService;
 
+import org.apache.ace.agent.AgentUpdateHandler;
 import org.apache.ace.agent.ConfigurationHandler;
 import org.apache.ace.agent.ConnectionHandler;
 import org.apache.ace.agent.DeploymentHandler;
@@ -42,4 +43,6 @@ public interface AgentContext {
     ScheduledExecutorService getExecutorService();
     
     ConfigurationHandler getConfigurationHandler();
+
+    AgentUpdateHandler getAgentUpdateHandler();
 }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentControlImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentControlImpl.java?rev=1514593&r1=1514592&r2=1514593&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentControlImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentControlImpl.java Fri
Aug 16 06:55:36 2013
@@ -21,6 +21,7 @@ package org.apache.ace.agent.impl;
 import java.util.List;
 
 import org.apache.ace.agent.AgentControl;
+import org.apache.ace.agent.AgentUpdateHandler;
 import org.apache.ace.agent.ConfigurationHandler;
 import org.apache.ace.agent.DeploymentHandler;
 import org.apache.ace.agent.DownloadHandler;
@@ -60,4 +61,9 @@ public class AgentControlImpl implements
         // TODO Auto-generated method stub
         return null;
     }
+    
+    @Override
+    public AgentUpdateHandler getAgentUpdateHandler() {
+        return m_agentContext.getAgentUpdateHandler();
+    }
 }

Added: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentUpdateHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentUpdateHandlerImpl.java?rev=1514593&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentUpdateHandlerImpl.java
(added)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentUpdateHandlerImpl.java
Fri Aug 16 06:55:36 2013
@@ -0,0 +1,142 @@
+/*
+ * 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 java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.SortedSet;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+
+import org.apache.ace.agent.AgentUpdateHandler;
+import org.apache.ace.agent.DownloadHandle;
+import org.apache.ace.agent.RetryAfterException;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Version;
+import org.osgi.util.tracker.ServiceTracker;
+
+public class AgentUpdateHandlerImpl extends UpdateHandlerBase implements AgentUpdateHandler
{
+    private static final String UPDATER_VERSION = "1.0.0";
+    private static final String UPDATER_SYMBOLICNAME = "org.apache.ace.agent.updater";
+    private BundleContext m_bundleContext;
+
+    public AgentUpdateHandlerImpl(AgentContext agentContext, BundleContext bundleContext)
{
+        super(agentContext);
+        m_bundleContext = bundleContext;
+    }
+
+    @Override
+    public Version getInstalledVersion() {
+        return m_bundleContext.getBundle().getVersion();
+    }
+
+    @Override
+    public SortedSet<Version> getAvailableVersions() throws RetryAfterException, IOException
{
+        return getAvailableVersions(getEndpoint(getServerURL(), getIdentification()));
+    }
+
+    @Override
+    public InputStream getInputStream(Version version) throws RetryAfterException, IOException
{
+        return getInputStream(getEndpoint(getServerURL(), getIdentification(), version));
+    }
+
+    @Override
+    public DownloadHandle getDownloadHandle(Version version) throws RetryAfterException,
IOException {
+        return getDownloadHandle(getEndpoint(getServerURL(), getIdentification(), version));
+    }
+
+    @Override
+    public long getSize(Version version) throws RetryAfterException, IOException {
+        return getPackageSize(getEndpoint(getServerURL(), getIdentification(), version));
+    }
+
+    @Override
+    public void install(InputStream stream) throws IOException {
+        try {
+            InputStream currentBundleVersion = null;
+            Bundle bundle = m_bundleContext.installBundle("agent-updater", generateBundle());
+            bundle.start();
+            ServiceTracker st = new ServiceTracker(m_bundleContext, m_bundleContext.createFilter("("
+ Constants.OBJECTCLASS + "=org.apache.ace.agent.updater.Activator)"), null);
+            st.open();
+            Object service = st.waitForService(60000);
+            Method method = service.getClass().getMethod("update", InputStream.class, InputStream.class);
+            method.invoke(m_bundleContext.getBundle(), currentBundleVersion, stream);
+        }
+        catch (Exception e) {
+        }
+    }
+
+    /** Generates an input stream that contains a complete bundle containing our update code
for the agent. */
+    private InputStream generateBundle() throws IOException {
+        InputStream is = null;
+        JarOutputStream jos = null;
+        ByteArrayOutputStream baos;
+        try {
+            baos = new ByteArrayOutputStream();
+            Manifest manifest = new Manifest();
+            Attributes main = manifest.getMainAttributes();
+            main.put(Attributes.Name.MANIFEST_VERSION, "1.0");
+            main.put(new Attributes.Name("Bundle-SymbolicName"), UPDATER_SYMBOLICNAME);
+            main.put(new Attributes.Name("Bundle-Version"), UPDATER_VERSION);
+            main.put(new Attributes.Name("Bundle-Activator"), "org.apache.ace.agent.updater.Activator");
+            main.put(new Attributes.Name("Bundle-ManifestVersion"), "2");
+            jos = new JarOutputStream(baos, manifest);
+            jos.putNextEntry(new JarEntry("org.apache.ace.agent.updater.Activator.class"));
+            is = getClass().getResourceAsStream("org/apache/ace/agent/updater/Activator.class");
+            byte[] buffer = new byte[1024];
+            int bytes;
+            while ((bytes = is.read(buffer)) != -1) {
+                jos.write(buffer, 0, bytes);
+            }
+            jos.closeEntry();
+        }
+        finally {
+            if (is != null) {
+                is.close();
+            }
+            if (jos != null) {
+                jos.close();
+            }
+        }
+        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+        return bais;
+    }
+
+    private URL getEndpoint(URL serverURL, String identification) {
+        return getEndpoint(serverURL, identification, null);
+    }
+
+    private URL getEndpoint(URL serverURL, String identification, Version version) {
+        try {
+            return new URL(serverURL, "agent/" + identification + "/" + m_bundleContext.getBundle().getSymbolicName()
+ "/versions/" + (version == null ? "" : version.toString()));
+        }
+        catch (MalformedURLException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+}

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DeploymentHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DeploymentHandlerImpl.java?rev=1514593&r1=1514592&r2=1514593&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DeploymentHandlerImpl.java
(original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/DeploymentHandlerImpl.java
Fri Aug 16 06:55:36 2013
@@ -18,18 +18,12 @@
  */
 package org.apache.ace.agent.impl;
 
-import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.net.URLConnection;
 import java.util.SortedSet;
-import java.util.TreeSet;
 
-import org.apache.ace.agent.AgentConstants;
 import org.apache.ace.agent.DeploymentHandler;
 import org.apache.ace.agent.DownloadHandle;
 import org.apache.ace.agent.RetryAfterException;
@@ -39,16 +33,16 @@ import org.osgi.service.deploymentadmin.
 import org.osgi.service.deploymentadmin.DeploymentException;
 import org.osgi.service.deploymentadmin.DeploymentPackage;
 
-public class DeploymentHandlerImpl implements DeploymentHandler {
-
-    private final AgentContext m_agentContext;
+public class DeploymentHandlerImpl extends UpdateHandlerBase implements DeploymentHandler
{
     private DeploymentAdmin m_deploymentAdmin;
 
     public DeploymentHandlerImpl(AgentContext agentContext) {
+        // TODO that DeploymentAdminImpl needs to be injected with several services for it
to work
         this(agentContext, new DeploymentAdminImpl());
     }
 
     public DeploymentHandlerImpl(AgentContext agentContext, DeploymentAdmin deploymentAdmin)
{
+        super(agentContext);
         m_agentContext = agentContext;
         m_deploymentAdmin = deploymentAdmin;
     }
@@ -67,116 +61,36 @@ public class DeploymentHandlerImpl imple
     }
 
     @Override
-    public SortedSet<Version> getAvailableVersions() throws RetryAfterException, IOException
{
-
-        SortedSet<Version> versions = new TreeSet<Version>();
-
-        URL endpoint = getEndpoint(getServerURL(), getIdentification());
-        URLConnection connection = null;
-        BufferedReader reader = null;
+    public void deployPackage(InputStream inputStream) {
+        // FIXME exceptions
         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;
+            m_deploymentAdmin.installDeploymentPackage(inputStream);
         }
-        finally {
-            if (connection != null && connection instanceof HttpURLConnection)
-                ((HttpURLConnection) connection).disconnect();
-            if (reader != null)
-                reader.close();
+        catch (DeploymentException e) {
+            e.printStackTrace();
         }
     }
-
+    
     @Override
     public long getPackageSize(Version version, boolean fixPackage) throws RetryAfterException,
IOException {
-
-        URL url = getPackageURL(version, fixPackage);
-        long packageSize = -1l;
-
-        URLConnection urlConnection = null;
-        InputStream inputStream = null;
-        try {
-            urlConnection = url.openConnection();
-            if (urlConnection instanceof HttpURLConnection)
-                ((HttpURLConnection) urlConnection).setRequestMethod("HEAD");
-
-            String dpSizeHeader = urlConnection.getHeaderField(AgentConstants.HEADER_DPSIZE);
-            if (dpSizeHeader != null)
-                try {
-                    packageSize = Long.parseLong(dpSizeHeader);
-                }
-                catch (NumberFormatException e) {
-                    // ignore
-                }
-            return packageSize;
-        }
-        finally {
-            if (urlConnection != null && urlConnection instanceof HttpURLConnection)
-                ((HttpURLConnection) urlConnection).disconnect();
-            if (inputStream != null)
-                try {
-                    inputStream.close();
-                }
-                catch (IOException e) {
-                    // ignore
-                }
-        }
-    }
+        return getPackageSize(getPackageURL(version, fixPackage));
+    };
 
     @Override
     public InputStream getInputStream(Version version, boolean fixPackage) throws RetryAfterException,
IOException {
-        URL packageURL = getPackageURL(version, fixPackage);
-        URLConnection urlConnection = null;
-        InputStream inputStream = null;
-        try {
-            // TODO handle problems and retries
-            urlConnection = packageURL.openConnection();
-            inputStream = urlConnection.getInputStream();
-            return inputStream;
-        }
-        finally {
-            if (urlConnection != null && urlConnection instanceof HttpURLConnection)
-                ((HttpURLConnection) urlConnection).disconnect();
-            if (inputStream != null)
-                try {
-                    inputStream.close();
-                }
-                catch (IOException e) {
-                    e.printStackTrace();
-                }
-        }
-    }
-
+        return getInputStream(getPackageURL(version, fixPackage));
+    };
+    
     @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 {
-            m_deploymentAdmin.installDeploymentPackage(inputStream);
-        }
-        catch (DeploymentException e) {
-            e.printStackTrace();
-        }
-    }
-
+        return getDownloadHandle(getPackageURL(version, fixPackage));
+    };
+    
+    @Override
+    public SortedSet<Version> getAvailableVersions() throws RetryAfterException ,IOException
{
+        return getAvailableVersions(getEndpoint(getServerURL(), getIdentification()));
+    };
+    
     private URL getPackageURL(Version version, boolean fixPackage) {
         URL url = null;
         if (fixPackage) {
@@ -188,19 +102,7 @@ public class DeploymentHandlerImpl imple
         return url;
     }
 
-    private String getIdentification() {
-        return m_agentContext.getIdentificationHandler().getIdentification();
-    }
-
-    private URL getServerURL() {
-        return m_agentContext.getDiscoveryHandler().getServerUrl();
-    }
-
-    private URLConnection getConnection(URL url) throws IOException {
-        return m_agentContext.getConnectionHandler().getConnection(url);
-    }
-
-    private static URL getEndpoint(URL serverURL, String identification) {
+    private URL getEndpoint(URL serverURL, String identification) {
         try {
             return new URL(serverURL, "deployment/" + identification + "/versions/");
         }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/IdentificationHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/IdentificationHandlerImpl.java?rev=1514593&r1=1514592&r2=1514593&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/IdentificationHandlerImpl.java
(original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/IdentificationHandlerImpl.java
Fri Aug 16 06:55:36 2013
@@ -1,3 +1,21 @@
+/*
+ * 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 org.apache.ace.agent.IdentificationHandler;

Added: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/UpdateHandlerBase.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/UpdateHandlerBase.java?rev=1514593&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/UpdateHandlerBase.java (added)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/UpdateHandlerBase.java Fri
Aug 16 06:55:36 2013
@@ -0,0 +1,144 @@
+/*
+ * 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 java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import org.apache.ace.agent.AgentConstants;
+import org.apache.ace.agent.DownloadHandle;
+import org.apache.ace.agent.RetryAfterException;
+import org.osgi.framework.Version;
+
+public class UpdateHandlerBase {
+    protected AgentContext m_agentContext;
+    public UpdateHandlerBase(AgentContext agentContext) {
+        m_agentContext = agentContext;
+    }
+    
+    protected SortedSet<Version> getAvailableVersions(URL endpoint) throws RetryAfterException,
IOException {
+    
+        SortedSet<Version> versions = new TreeSet<Version>();
+    
+        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;
+        }
+        finally {
+            if (connection != null && connection instanceof HttpURLConnection)
+                ((HttpURLConnection) connection).disconnect();
+            if (reader != null)
+                reader.close();
+        }
+    }
+
+    protected long getPackageSize(URL url) throws RetryAfterException, IOException {
+        long packageSize = -1l;
+    
+        URLConnection urlConnection = null;
+        InputStream inputStream = null;
+        try {
+            urlConnection = url.openConnection();
+            if (urlConnection instanceof HttpURLConnection)
+                ((HttpURLConnection) urlConnection).setRequestMethod("HEAD");
+    
+            String dpSizeHeader = urlConnection.getHeaderField(AgentConstants.HEADER_DPSIZE);
+            if (dpSizeHeader != null)
+                try {
+                    packageSize = Long.parseLong(dpSizeHeader);
+                }
+                catch (NumberFormatException e) {
+                    // ignore
+                }
+            return packageSize;
+        }
+        finally {
+            if (urlConnection != null && urlConnection instanceof HttpURLConnection)
+                ((HttpURLConnection) urlConnection).disconnect();
+            if (inputStream != null)
+                try {
+                    inputStream.close();
+                }
+                catch (IOException e) {
+                    // ignore
+                }
+        }
+    }
+
+    protected InputStream getInputStream(URL packageURL) throws RetryAfterException, IOException
{
+        URLConnection urlConnection = null;
+        InputStream inputStream = null;
+        try {
+            // TODO handle problems and retries
+            urlConnection = packageURL.openConnection();
+            inputStream = urlConnection.getInputStream();
+            return inputStream;
+        }
+        finally {
+            if (urlConnection != null && urlConnection instanceof HttpURLConnection)
+                ((HttpURLConnection) urlConnection).disconnect();
+            if (inputStream != null)
+                try {
+                    inputStream.close();
+                }
+                catch (IOException e) {
+                    e.printStackTrace();
+                }
+        }
+    }
+
+    protected DownloadHandle getDownloadHandle(URL packageURL) {
+        return m_agentContext.getDownloadHandler().getHandle(packageURL);
+    }
+
+    protected String getIdentification() {
+        return m_agentContext.getIdentificationHandler().getIdentification();
+    }
+
+    protected URL getServerURL() {
+        return m_agentContext.getDiscoveryHandler().getServerUrl();
+    }
+
+    private URLConnection getConnection(URL url) throws IOException {
+        return m_agentContext.getConnectionHandler().getConnection(url);
+    }
+
+}

Added: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/updater/Activator.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/updater/Activator.java?rev=1514593&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/updater/Activator.java (added)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/updater/Activator.java Fri Aug
16 06:55:36 2013
@@ -0,0 +1,85 @@
+/*
+ * 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.updater;
+
+import java.io.InputStream;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+
+/**
+ * This class is dynamically deployed as part of the updater bundle that executes the
+ * update of the management agent. It is both the bundle activator and the interface and
+ * implementation of a service that is published and invoked by the management agent.
+ * Care was taken to not create import dependencies on anything other than the core
+ * framework. Also, no inner classes are used, to keep all the code in a single class file.
+ */
+public class Activator implements BundleActivator, Runnable {
+    private Object LOCK = new Object();
+    private BundleContext m_context;
+    private Thread m_updaterThread;
+    private InputStream m_oldStream;
+    private InputStream m_newStream;
+    private Bundle m_agent;
+
+    @Override
+    public void start(BundleContext context) throws Exception {
+        m_context = context;
+        m_context.registerService(Activator.class.getName(), this, null);
+    }
+
+    @Override
+    public void stop(BundleContext context) throws Exception {
+        Thread thread;
+        synchronized (LOCK) {
+            thread = m_updaterThread;
+        }    
+        if (thread != null) {
+            thread.join(60000);
+        }
+    }
+    
+    public void update(Bundle agent, InputStream oldStream, InputStream newStream) {
+        synchronized (LOCK) {
+            m_updaterThread = new Thread(this, "Apache ACE Management Agent Updater");
+            m_agent = agent;
+            m_oldStream = oldStream;
+            m_newStream = newStream;
+        }
+        m_updaterThread.start();
+    }
+    
+    @Override
+    public void run() {
+        try {
+            m_agent.update(m_newStream);
+        }
+        catch (BundleException e) {
+            try {
+                m_agent.update(m_oldStream);
+            }
+            catch (BundleException e1) {
+                // at this point we simply give up
+                e1.printStackTrace();
+            }
+        }
+    }
+}



Mime
View raw message