ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1515450 - in /ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent: impl/Activator.java impl/AgentUpdateHandlerImpl.java impl/UpdateHandlerBase.java updater/Activator.java
Date Mon, 19 Aug 2013 14:43:29 GMT
Author: marrs
Date: Mon Aug 19 14:43:28 2013
New Revision: 1515450

URL: http://svn.apache.org/r1515450
Log:
ACE-342 Cleaned up the code, some bugfixes, and now caches agents in the bundle cache before
attempting an update.

Modified:
    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/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/Activator.java

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=1515450&r1=1515449&r2=1515450&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 Mon Aug 19
14:43:28 2013
@@ -93,7 +93,7 @@ public class Activator extends Dependenc
         }
 
         if (Boolean.parseBoolean(System.getProperty("agent.connectionhandler.disabled")))
{
-            service.add(createServiceDependency().setService(DiscoveryHandler.class).setRequired(true));
+            service.add(createServiceDependency().setService(ConnectionHandler.class).setRequired(true));
         }
         else {
             m_connectionHandler = new ConnectionHandlerImpl(this);

Modified: 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=1515450&r1=1515449&r2=1515450&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentUpdateHandlerImpl.java
(original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentUpdateHandlerImpl.java
Mon Aug 19 14:43:28 2013
@@ -22,6 +22,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -42,6 +43,7 @@ import org.osgi.framework.Version;
 import org.osgi.util.tracker.ServiceTracker;
 
 public class AgentUpdateHandlerImpl extends UpdateHandlerBase implements AgentUpdateHandler
{
+    private static final int TIMEOUT = 15000;
     private static final String UPDATER_VERSION = "1.0.0";
     private static final String UPDATER_SYMBOLICNAME = "org.apache.ace.agent.updater";
     private BundleContext m_bundleContext;
@@ -93,23 +95,31 @@ public class AgentUpdateHandlerImpl exte
     @Override
     public void install(InputStream stream) throws IOException {
         try {
-            InputStream currentBundleVersion = new ByteArrayInputStream(new byte[0]);
+            InputStream currentBundleVersion = getInputStream(m_bundleContext.getBundle().getVersion());
             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(true);
-            Object service = st.waitForService(3000);
+            Object service = st.waitForService(TIMEOUT);
             if (service != null) {
                 Method method = service.getClass().getMethod("update", Bundle.class, InputStream.class,
InputStream.class);
                 System.out.println("Method: " + method);
-                method.invoke(service, m_bundleContext.getBundle(), currentBundleVersion,
stream);
+                try {
+                    method.invoke(service, m_bundleContext.getBundle(), currentBundleVersion,
stream);
+                }
+                catch (InvocationTargetException e) {
+                    bundle.uninstall();
+                }
+                finally {
+                    st.close();
+                }
             }
             else {
-                System.out.println("Error: no service!");
+                throw new IOException("No update service found after launching temporary
bundle.");
             }
         }
         catch (Exception e) {
-            e.printStackTrace();
+            throw new IOException("Could not update management agent.", e);
         }
     }
 
@@ -140,10 +150,16 @@ public class AgentUpdateHandlerImpl exte
         }
         finally {
             if (is != null) {
-                is.close();
+                try {
+                    is.close();
+                }
+                catch (IOException e) {}
             }
             if (jos != null) {
-                jos.close();
+                try {
+                    jos.close();
+                }
+                catch (IOException e) {}
             }
         }
         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

Modified: 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=1515450&r1=1515449&r2=1515450&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/UpdateHandlerBase.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/UpdateHandlerBase.java Mon
Aug 19 14:43:28 2013
@@ -31,25 +31,21 @@ 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.BundleException;
-import org.osgi.framework.FrameworkUtil;
 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;
@@ -65,43 +61,48 @@ public class UpdateHandlerBase {
             return versions;
         }
         finally {
-            if (connection != null && connection instanceof HttpURLConnection)
+            if (connection != null && connection instanceof HttpURLConnection) {
                 ((HttpURLConnection) connection).disconnect();
-            if (reader != null)
+            }
+            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)
+            if (urlConnection instanceof HttpURLConnection) {
                 ((HttpURLConnection) urlConnection).setRequestMethod("HEAD");
+            }
     
             String dpSizeHeader = urlConnection.getHeaderField(AgentConstants.HEADER_DPSIZE);
-            if (dpSizeHeader != null)
+            if (dpSizeHeader != null) {
                 try {
                     packageSize = Long.parseLong(dpSizeHeader);
                 }
                 catch (NumberFormatException e) {
                     // ignore
                 }
+            }
             return packageSize;
         }
         finally {
-            if (urlConnection != null && urlConnection instanceof HttpURLConnection)
+            if (urlConnection != null && urlConnection instanceof HttpURLConnection)
{
                 ((HttpURLConnection) urlConnection).disconnect();
-            if (inputStream != null)
+            }
+            if (inputStream != null) {
                 try {
                     inputStream.close();
                 }
                 catch (IOException e) {
                     // ignore
                 }
+            }
         }
     }
 
@@ -129,5 +130,4 @@ public class UpdateHandlerBase {
     private URLConnection getConnection(URL url) throws IOException {
         return m_agentContext.getConnectionHandler().getConnection(url);
     }
-
 }

Modified: 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=1515450&r1=1515449&r2=1515450&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/updater/Activator.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/updater/Activator.java Mon Aug
19 14:43:28 2013
@@ -18,7 +18,12 @@
  */
 package org.apache.ace.agent.updater;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleActivator;
@@ -33,43 +38,62 @@ import org.osgi.framework.BundleExceptio
  * framework. Also, no inner classes are used, to keep all the code in a single class file.
  */
 public class Activator implements BundleActivator, Runnable {
+    private static final int BUFFER_SIZE = 4096;
     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;
+    private File m_oldFile;
+    private File m_newFile;
 
     @Override
     public void start(BundleContext context) throws Exception {
         m_context = context;
         m_context.registerService(Activator.class.getName(), this, null);
+        m_oldFile = m_context.getDataFile("old.jar");
+        m_newFile = m_context.getDataFile("new.jar");
     }
 
     @Override
     public void stop(BundleContext context) throws Exception {
-//        Thread thread;
-//        synchronized (LOCK) {
-//            thread = m_updaterThread;
-//        }    
-//        if (thread != null) {
-//            thread.join(10000);
-//        }
     }
     
-    public void update(Bundle agent, InputStream oldStream, InputStream newStream) {
+    public void update(Bundle agent, InputStream oldStream, InputStream newStream) throws
IOException {
         synchronized (LOCK) {
             m_updaterThread = new Thread(this, "Apache ACE Management Agent Updater");
             m_agent = agent;
-            m_oldStream = oldStream;
-            m_newStream = newStream;
+            copy(oldStream, new FileOutputStream(m_oldFile));
+            copy(newStream, new FileOutputStream(m_newFile));
+            m_oldStream = new FileInputStream(m_oldFile);
+            m_newStream = new FileInputStream(m_newFile);
         }
         m_updaterThread.start();
     }
     
+    public void copy(InputStream in, OutputStream out) throws IOException {
+        byte[] buffer = new byte[BUFFER_SIZE];
+        int length;
+        try {
+            while ((length = in.read(buffer)) != -1) {
+                out.write(buffer, 0, length);
+            }
+        }
+        finally {
+            try {
+                in.close();
+            }
+            catch (IOException e) {}
+            try {
+                out.close();
+            }
+            catch (IOException e) {}
+        }
+    }
+    
     @Override
     public void run() {
-        // TODO First fetch both streams and store them in a file inside the bundle cache.
         try {
             System.out.println("Updating to " + m_newStream);
             m_agent.update(m_newStream);
@@ -78,6 +102,7 @@ public class Activator implements Bundle
             try {
                 System.out.println("Reverting to " + m_oldStream);
                 m_agent.update(m_oldStream);
+                m_agent.start();
             }
             catch (BundleException e1) {
                 // at this point we simply give up



Mime
View raw message