geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r1361316 - /geronimo/server/branches/3.0/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationUpdateHelper.java
Date Fri, 13 Jul 2012 18:10:54 GMT
Author: gawor
Date: Fri Jul 13 18:10:54 2012
New Revision: 1361316

URL: http://svn.apache.org/viewvc?rev=1361316&view=rev
Log:
GERONIMO-6370: 1) Deal better when the entire unpacked bundle gets updated, and 2) more efficient
replacement of unpacked bundle

Modified:
    geronimo/server/branches/3.0/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationUpdateHelper.java

Modified: geronimo/server/branches/3.0/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationUpdateHelper.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/3.0/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationUpdateHelper.java?rev=1361316&r1=1361315&r2=1361316&view=diff
==============================================================================
--- geronimo/server/branches/3.0/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationUpdateHelper.java
(original)
+++ geronimo/server/branches/3.0/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationUpdateHelper.java
Fri Jul 13 18:10:54 2012
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.lang.instrument.ClassDefinition;
 import java.net.URI;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -88,22 +89,48 @@ public class ApplicationUpdateHelper {
                 
         BundleContext context = applicationGBean.getBundle().getBundleContext();
 
+        Collection<Bundle> bundles = Arrays.asList(targetBundle);
+        
+        FrameworkWiring wiring = context.getBundle(0).adapt(FrameworkWiring.class);
+
         try {
+            // log dependents
+            Collection<Bundle> dependents = wiring.getDependencyClosure(bundles);
+            dependents.removeAll(bundles);
+            if (!dependents.isEmpty()) {
+                String bundleListString = bundleCollectionToString(dependents);
+                LOG.info("Update of {} bundle will cause the following bundles to be refreshed:
{}", bundleName, bundleListString);
+            }
+        
             // stop the bundle
             targetBundle.stop();
 
+            InputStream in = null;
+            String bundleLocation = targetBundle.getLocation();
+            File location = BundleUtils.toFile(bundleLocation);
+            if (location != null && location.isDirectory()) {
+                // bundle installed from a directory - update bundle directory contents before
calling update()
+                
+                // update bundle directory
+                try {
+                    updateEBA(targetBundle, bundleFile);
+                } catch (Exception e) {
+                    LOG.warn("Error updating application bundle with the new contents.",
e.getMessage());
+                }
+                
+                URL url = new URL(bundleLocation);
+                in = url.openStream();
+            } else {
+                // bundle is NOT installed from a directory - update bundle archive after
calling update()            
+                in = new FileInputStream(bundleFile);
+            }
+            
             // update the bundle
-            FileInputStream fi = null;
             try {
-                fi = new FileInputStream(bundleFile);
-                targetBundle.update(fi);
+                targetBundle.update(in);
             } finally {
-                IOUtils.close(fi);
+                IOUtils.close(in);
             }
-
-            FrameworkWiring wiring = context.getBundle(0).adapt(FrameworkWiring.class);
-            
-            Collection<Bundle> bundles = Arrays.asList(targetBundle);
             
             // resolve the bundle
             if (!wiring.resolveBundles(bundles)) {
@@ -120,20 +147,14 @@ public class ApplicationUpdateHelper {
                 throw new BundleException(builder.toString());
             }
             
-            // log dependents
-            Collection<Bundle> dependents = wiring.getDependencyClosure(bundles);
-            dependents.removeAll(bundles);
-            if (!dependents.isEmpty()) {
-                String bundleListString = bundleCollectionToString(dependents);
-                LOG.info("Update of {} bundle will cause the following bundles to be refreshed:
{}", bundleName, bundleListString);
-            }
-            
-            // update application archive
-            try {
-                updateEBA(targetBundle, bundleFile);
-            } catch (Exception e) {
-                LOG.warn("Error updating application archive with the new contents. " +
-                         "Changes made might be gone next time the application or server
is restarted.", e.getMessage());
+            if (location == null || !location.isDirectory()) {
+                // update application archive
+                try {
+                    updateEBA(targetBundle, bundleFile);
+                } catch (Exception e) {
+                    LOG.warn("Error updating application archive with the new contents. "
+
+                             "Changes made might be gone next time the application or server
is restarted.", e.getMessage());
+                }
             }
             
             // refresh the bundle and its dependents
@@ -188,11 +209,7 @@ public class ApplicationUpdateHelper {
                     } else {
                         // bundle file is a directory - just update class files
                         ZipFile zipFile = new ZipFile(changesFile);
-                        try {
-                            JarUtils.unzipToDirectory(zipFile, bundleFile);
-                        } finally {
-                            JarUtils.close(zipFile);
-                        }
+                        JarUtils.unzipToDirectory(zipFile, bundleFile);
                     }
                 } else {
                     // eba is an file                   
@@ -479,19 +496,54 @@ public class ApplicationUpdateHelper {
                 updateApplicationDirectory(ebaArchive, newBundleFile, bundleNameInApp);
             } else {
                 // bundle file is a directory - replace directory
-                FileUtils.recursiveDelete(bundleFile);
-                ZipFile zipFile = new ZipFile(newBundleFile);
-                try {
-                    JarUtils.unzipToDirectory(zipFile, bundleFile);
-                } finally {
-                    JarUtils.close(zipFile);
-                }
+                ZipFile zipFile;
+                               
+                // overwrite existing files
+                zipFile = new ZipFile(newBundleFile);
+                JarUtils.unzipToDirectory(zipFile, bundleFile);
+                
+                // find & remove deleted files
+                zipFile = new ZipFile(newBundleFile);
+                List<File> filesToDelete = findDeletedFiles(zipFile, bundleFile);
+                List<String> inUseFiles = new ArrayList<String>();
+                for (File file : filesToDelete) {
+                    FileUtils.recursiveDelete(file, inUseFiles);
+                }
+                if (!inUseFiles.isEmpty()) {
+                    LOG.warn("Some files could not be deleted: {}", inUseFiles);
+                }                
             }
         } else {
             // eba is a file
             updateApplicationArchive(ebaArchive, newBundleFile, bundleNameInApp);
         }
     }
+    
+    private List<File> findDeletedFiles(ZipFile zipFile, File directory) {
+        List<File> deletedFiles = new ArrayList<File>();
+        try {
+            collectDeletedFiles(zipFile, directory, "", deletedFiles);
+        } finally {
+            JarUtils.close(zipFile);
+        }
+        return deletedFiles;
+    }
+    
+    private void collectDeletedFiles(ZipFile zipFile, File directory, String baseName, List<File>
deleted) {
+        File[] files = directory.listFiles();
+        if (files != null) {
+            for (int i = 0; i < files.length; i++) {
+                File file = files[i];
+                String name = baseName + file.getName();
+                ZipEntry entry = zipFile.getEntry(name);
+                if (entry == null) {
+                    deleted.add(file);                 
+                } else if (file.isDirectory()) {
+                    collectDeletedFiles(zipFile, file, name + "/", deleted);
+                }
+            }
+        }
+    }
 
     private void updateApplicationDirectory(File ebaArchive, File bundleFile, String bundleNameInApp)
throws IOException {
         File destinationFile = new File(ebaArchive, bundleNameInApp);



Mime
View raw message