You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2010/08/24 14:47:04 UTC

svn commit: r988515 - in /sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl: OsgiInstallerImpl.java tasks/BundleTaskCreator.java tasks/BundleUpdateTask.java tasks/SystemBundleUpdateTask.java

Author: cziegeler
Date: Tue Aug 24 12:47:04 2010
New Revision: 988515

URL: http://svn.apache.org/viewvc?rev=988515&view=rev
Log:
SLING-1663 : OSGi Install should recognize system bundle updates

Added:
    sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SystemBundleUpdateTask.java   (with props)
Modified:
    sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java
    sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleTaskCreator.java
    sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleUpdateTask.java

Modified: sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java?rev=988515&r1=988514&r2=988515&view=diff
==============================================================================
--- sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java (original)
+++ sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java Tue Aug 24 12:47:04 2010
@@ -117,7 +117,7 @@ public class OsgiInstallerImpl
             	}
 
             	retriesScheduled = false;
-                if(executeTasks(tasks) > 0) {
+                if (executeTasks(tasks) > 0) {
                     Logger.logDebug("Tasks have been executed, saving persistentList");
                     persistentList.save();
                 }
@@ -129,7 +129,7 @@ public class OsgiInstallerImpl
             } catch(Exception e) {
                 Logger.logWarn(e.toString(), e);
                 try {
-                    Thread.sleep(1000);
+                    Thread.sleep(1500);
                 } catch(InterruptedException ignored) {
                 }
             }
@@ -338,7 +338,7 @@ public class OsgiInstallerImpl
             }
         };
         int counter = 0;
-        while(!tasks.isEmpty()) {
+        while (!tasks.isEmpty()) {
             OsgiInstallerTask t = null;
             synchronized (tasks) {
                 t = tasks.first();

Modified: sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleTaskCreator.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleTaskCreator.java?rev=988515&r1=988514&r2=988515&view=diff
==============================================================================
--- sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleTaskCreator.java (original)
+++ sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleTaskCreator.java Tue Aug 24 12:47:04 2010
@@ -153,7 +153,7 @@ public class BundleTaskCreator {
 		if (toActivate == null) {
 		    // None of our resources are installable, remove corresponding bundle if present
 		    // and if we installed it
-		    if (getBundleInfo(resources.first()) != null) {
+		    if (firstResource != null && getBundleInfo(firstResource) != null) {
 		        if (this.getInstalledBundleVersion(symbolicName) == null) {
 		            Logger.logInfo("Bundle " + symbolicName
                                 + " was not installed by this module, not removed");
@@ -165,8 +165,6 @@ public class BundleTaskCreator {
 
 		} else {
 			final BundleInfo info = getBundleInfo(toActivate);
-			final Version newVersion = new Version((String)toActivate.getAttributes().get(Constants.BUNDLE_VERSION));
-	        RegisteredResource toUpdate = null;
 			if (info == null) {
 			    // bundle is not installed yet: install and save digest to avoid
 			    // unnecessary updates
@@ -174,15 +172,17 @@ public class BundleTaskCreator {
 				        this));
 				digestToSave = toActivate.getDigest();
 			} else {
+	            RegisteredResource toUpdate = null;
+	            final Version newVersion = new Version((String)toActivate.getAttributes().get(Constants.BUNDLE_VERSION));
 			    final int compare = info.version.compareTo(newVersion);
-                if(compare < 0) {
+                if (compare < 0) {
                     // installed version is lower -> update
                     toUpdate = toActivate;
-                } else if(compare > 0) {
+                } else if (compare > 0) {
 	                // installed version is higher -> downgrade only if
                     // we installed that version
                     final String installedVersion = this.getInstalledBundleVersion(info.symbolicName);
-                    if(info.version.toString().equals(installedVersion)) {
+                    if (info.version.toString().equals(installedVersion)) {
                         toUpdate = toActivate;
                         Logger.logInfo("Bundle " + info.symbolicName + " " + installedVersion
                                     + " was installed by this module, downgrading to " + newVersion);
@@ -190,29 +190,33 @@ public class BundleTaskCreator {
                         Logger.logInfo("Bundle " + info.symbolicName + " " + installedVersion
                                     + " was not installed by this module, not downgraded");
                     }
-			    } else if(compare == 0 && this.isSnapshot(newVersion)){
+			    } else if (compare == 0 && this.isSnapshot(newVersion)){
 			        // installed, same version but SNAPSHOT
                     toUpdate = toActivate;
 			    }
+                // Save the digest of installed and updated resources, keyed by
+                // bundle symbolic name, to avoid unnecessary updates
+                if (toUpdate != null) {
+                    final String previousDigest = digests.get(symbolicName);
+                    if(toUpdate.getDigest().equals(previousDigest)) {
+                        Logger.logDebug("Ignoring update of " + toUpdate + ", digest didn't change");
+                        digestToSave = previousDigest;
+                    } else {
+                        Logger.logDebug("Scheduling update of " + toUpdate + ", digest has changed");
+                        if ( Constants.SYSTEM_BUNDLE_SYMBOLICNAME.equals(symbolicName) ) {
+                            tasks.add(new SystemBundleUpdateTask(toUpdate, this));
+                        } else {
+                            tasks.add(new BundleUpdateTask(toUpdate,
+                                this));
+                        }
+                        digestToSave = toUpdate.getDigest();
+                    }
+                }
 			}
 
-			// Save the digest of installed and updated resources, keyed by
-			// bundle symbolic name, to avoid unnecessary updates
-			if (toUpdate != null) {
-			    final String previousDigest = digests.get(symbolicName);
-			    if(toUpdate.getDigest().equals(previousDigest)) {
-			        Logger.logDebug("Ignoring update of " + toUpdate + ", digest didn't change");
-                    digestToSave = previousDigest;
-			    } else {
-			        Logger.logDebug("Scheduling update of " + toUpdate + ", digest has changed");
-			        tasks.add(new BundleUpdateTask(toUpdate,
-			                this));
-			        digestToSave = toUpdate.getDigest();
-			    }
-			}
 
-			if(digestToSave == null) {
-			    if(symbolicName != null) {
+			if (digestToSave == null) {
+			    if (symbolicName != null) {
 			        digests.remove(symbolicName);
 			    }
 			} else {
@@ -258,7 +262,11 @@ public class BundleTaskCreator {
      */
     public Bundle getMatchingBundle(String bundleSymbolicName) {
         if (bundleSymbolicName != null) {
-            Bundle[] bundles = bundleContext.getBundles();
+            // check if this is the system bundle
+            if ( Constants.SYSTEM_BUNDLE_SYMBOLICNAME.equals(bundleSymbolicName) ) {
+                return bundleContext.getBundle(0);
+            }
+            final Bundle[] bundles = bundleContext.getBundles();
             for (Bundle bundle : bundles) {
                 if (bundleSymbolicName.equals(bundle.getSymbolicName())) {
                     return bundle;

Modified: sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleUpdateTask.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleUpdateTask.java?rev=988515&r1=988514&r2=988515&view=diff
==============================================================================
--- sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleUpdateTask.java (original)
+++ sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/BundleUpdateTask.java Tue Aug 24 12:47:04 2010
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.osgi.installer.impl.tasks;
 
+import java.io.IOException;
 import java.io.InputStream;
 
 import org.apache.sling.osgi.installer.impl.Logger;
@@ -60,16 +61,27 @@ public class BundleUpdateTask extends Os
     public void execute(OsgiInstallerContext ctx) {
         final String symbolicName = (String)resource.getAttributes().get(Constants.BUNDLE_SYMBOLICNAME);
         final Bundle b = this.creator.getMatchingBundle(symbolicName);
-        if(b == null) {
+        if (b == null) {
             throw new IllegalStateException("Bundle to update (" + symbolicName + ") not found");
         }
+        final Version newVersion = new Version((String)resource.getAttributes().get(Constants.BUNDLE_VERSION));
 
+        // check for system bundle update
+        if ( Constants.SYSTEM_BUNDLE_SYMBOLICNAME.equals(symbolicName) ) {
+            logExecution();
+            try {
+                this.creator.saveInstalledBundleInfo(b.getSymbolicName(), resource.getDigest(), newVersion.toString());
+            } catch (final IOException e) {
+                Logger.logWarn("Removing failing tasks - unable to retry: " + this, e);
+                return;
+            }
+        }
         // Do not update if same version, unless snapshot
         boolean snapshot = false;
     	final Version currentVersion = new Version((String)b.getHeaders().get(Constants.BUNDLE_VERSION));
-    	final Version newVersion = new Version((String)resource.getAttributes().get(Constants.BUNDLE_VERSION));
     	snapshot = this.creator.isSnapshot(newVersion);
-    	if(currentVersion.equals(newVersion) && !snapshot) {
+    	if (currentVersion.equals(newVersion) && !snapshot) {
+    	    // TODO : Isn't this already checked in the task creator?
     	    Logger.logDebug("Same version is already installed, and not a snapshot, ignoring update:" + resource);
     		return;
     	}
@@ -78,16 +90,16 @@ public class BundleUpdateTask extends Os
             // If snapshot and ready to update, cancel if digest didn't change - as the list
             // of RegisteredResources is not saved, this might not have been detected earlier,
             // if the snapshot was installed and the installer was later restarted
-            if(snapshot) {
+            if (snapshot) {
                 final String oldDigest = this.creator.getInstalledBundleDigest(b);
-                if(resource.getDigest().equals(oldDigest)) {
+                if (resource.getDigest().equals(oldDigest)) {
                     Logger.logDebug("Snapshot digest did not change, ignoring update:" + resource);
                     return;
                 }
             }
 
             logExecution();
-            if(b.getState() == Bundle.ACTIVE) {
+            if (b.getState() == Bundle.ACTIVE) {
                 // bundle was active before the update - restart it once updated, but
                 // in sequence, not right now
                 ctx.addTaskToCurrentCycle(new BundleStartTask(b.getBundleId(), this.creator));

Added: sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SystemBundleUpdateTask.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SystemBundleUpdateTask.java?rev=988515&view=auto
==============================================================================
--- sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SystemBundleUpdateTask.java (added)
+++ sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SystemBundleUpdateTask.java Tue Aug 24 12:47:04 2010
@@ -0,0 +1,116 @@
+/*
+ * 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.sling.osgi.installer.impl.tasks;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.sling.osgi.installer.impl.Logger;
+import org.apache.sling.osgi.installer.impl.OsgiInstallerContext;
+import org.apache.sling.osgi.installer.impl.OsgiInstallerTask;
+import org.apache.sling.osgi.installer.impl.RegisteredResource;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Version;
+
+/**
+ * Update the system bundle from a RegisteredResource.
+ */
+public class SystemBundleUpdateTask extends OsgiInstallerTask {
+
+    private static final String BUNDLE_UPDATE_ORDER = "99-";
+
+    private final RegisteredResource resource;
+
+    private final BundleTaskCreator creator;
+
+    public SystemBundleUpdateTask(final RegisteredResource r,
+            final BundleTaskCreator creator) {
+        this.creator = creator;
+        this.resource = r;
+    }
+
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + ": " + resource;
+    }
+
+    @Override
+    public void execute(OsgiInstallerContext ctx) {
+        final String symbolicName = (String)resource.getAttributes().get(Constants.BUNDLE_SYMBOLICNAME);
+        final Bundle b = this.creator.getMatchingBundle(symbolicName);
+        if (b == null) {
+            throw new IllegalStateException("Bundle to update (" + symbolicName + ") not found");
+        }
+        final Version newVersion = new Version((String)resource.getAttributes().get(Constants.BUNDLE_VERSION));
+
+        logExecution();
+        InputStream is = null;
+        try {
+            is = resource.getInputStream();
+            if (is == null) {
+                throw new IllegalStateException(
+                        "RegisteredResource provides null InputStream, cannot update bundle: "
+                        + resource);
+            }
+            this.creator.saveInstalledBundleInfo(b.getSymbolicName(), resource.getDigest(), newVersion.toString());
+            // delayed system bundle update
+            final InputStream backgroundIS = is;
+            is = null;
+            final Thread t = new Thread(new Runnable() {
+
+                /**
+                 * @see java.lang.Runnable#run()
+                 */
+                public void run() {
+                    try {
+                        Thread.sleep(800);
+                    } catch(InterruptedException ignored) {
+                    }
+                    try {
+                        b.update(backgroundIS);
+                    } catch (final BundleException be) {
+                        Logger.logWarn("Unable to update system bundle", be);
+                    } finally {
+                        try {
+                            backgroundIS.close();
+                        } catch (IOException ignore) {}
+                    }
+                }
+            });
+            t.setDaemon(true);
+            t.start();
+        } catch (final IOException e) {
+            Logger.logWarn("Removing failing tasks - unable to retry: " + this, e);
+        } finally {
+            if ( is != null ) {
+                try {
+                    is.close();
+                } catch (IOException ignore) {}
+            }
+        }
+    }
+
+    @Override
+    public String getSortKey() {
+        return BUNDLE_UPDATE_ORDER + resource.getURL();
+    }
+
+}
\ No newline at end of file

Propchange: sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SystemBundleUpdateTask.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SystemBundleUpdateTask.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/tasks/SystemBundleUpdateTask.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain