Updated Branches:
  refs/heads/master 9c8f53375 -> 3af0088bc

[KARAF-2637] Optimize the kar deployer to not read/write the status every time 
a bundle changes


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/3af0088b
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/3af0088b
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/3af0088b

Branch: refs/heads/master
Commit: 3af0088bcfe1565b2ba2197c4282abfa3386ad32
Parents: 9c8f533
Author: Guillaume Nodet <[email protected]>
Authored: Wed Dec 18 22:25:13 2013 +0100
Committer: Guillaume Nodet <[email protected]>
Committed: Wed Dec 18 22:29:03 2013 +0100

----------------------------------------------------------------------
 .../features/FeatureDeploymentListener.java     | 142 ++++++++++---------
 1 file changed, 77 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/3af0088b/deployer/features/src/main/java/org/apache/karaf/deployer/features/FeatureDeploymentListener.java
----------------------------------------------------------------------
diff --git 
a/deployer/features/src/main/java/org/apache/karaf/deployer/features/FeatureDeploymentListener.java
 
b/deployer/features/src/main/java/org/apache/karaf/deployer/features/FeatureDeploymentListener.java
index f0b1193..1ef86c3 100644
--- 
a/deployer/features/src/main/java/org/apache/karaf/deployer/features/FeatureDeploymentListener.java
+++ 
b/deployer/features/src/main/java/org/apache/karaf/deployer/features/FeatureDeploymentListener.java
@@ -19,12 +19,20 @@ package org.apache.karaf.deployer.features;
 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 java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.EnumSet;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -42,6 +50,9 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
 import org.osgi.framework.BundleListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
 import org.xml.sax.ErrorHandler;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
@@ -58,6 +69,7 @@ public class FeatureDeploymentListener implements 
ArtifactUrlTransformer, Bundle
     private DocumentBuilderFactory dbf;
     private FeaturesService featuresService;
     private BundleContext bundleContext;
+    private Properties properties = new Properties();
 
     public void setFeaturesService(FeaturesService featuresService) {
         this.featuresService = featuresService;
@@ -77,6 +89,8 @@ public class FeatureDeploymentListener implements 
ArtifactUrlTransformer, Bundle
 
     public void init() throws Exception {
         bundleContext.addBundleListener(this);
+        loadProperties();
+        // Scan bundles
         for (Bundle bundle : bundleContext.getBundles()) {
             if (bundle.getState() == Bundle.RESOLVED || bundle.getState() == 
Bundle.STARTING
                     || bundle.getState() == Bundle.ACTIVE)
@@ -109,7 +123,38 @@ public class FeatureDeploymentListener implements 
ArtifactUrlTransformer, Bundle
        }
        return false;
     }
-    
+
+    private void loadProperties() throws IOException {
+        // Load properties
+        File file = getPropertiesFile();
+        if (file != null) {
+            if (file.exists()) {
+                InputStream input = new FileInputStream(file);
+                try {
+                    properties.load(input);
+                } finally {
+                    input.close();
+                }
+            }
+        }
+    }
+
+    private void saveProperties() throws IOException {
+        File file = getPropertiesFile();
+        if (file != null) {
+            OutputStream output = new FileOutputStream(file);
+            try {
+                properties.store(output, null);
+            } finally {
+                output.close();
+            }
+        }
+    }
+
+    private File getPropertiesFile() {
+        return bundleContext.getDataFile("FeatureDeploymentListener.cfg");
+    }
+
     public boolean canHandle(File artifact) {
         try {
             if (artifact.isFile() && artifact.getName().endsWith(".xml")) {
@@ -189,28 +234,17 @@ public class FeatureDeploymentListener implements 
ArtifactUrlTransformer, Bundle
                         }
                     }
                     synchronized (this) {
-                        File file = 
bundleContext.getDataFile("FeatureDeploymentListener.cfg");
-                        if (file != null) {
-                            Properties props = new Properties();
-                            if (file.exists()) {
-                                InputStream input = new FileInputStream(file);
-                                try {
-                                    props.load(input);
-                                } finally {
-                                    input.close();
-                                }
-                            }
-                            String prefix = bundle.getSymbolicName() + "-" + 
bundle.getVersion();
-                            props.put(prefix + ".count", 
Integer.toString(urls.size()));
+                        String prefix = bundle.getSymbolicName() + "-" + 
bundle.getVersion();
+                        String old = (String) properties.get(prefix + 
".count");
+                        if (old != null && urls.isEmpty()) {
+                            properties.remove(prefix + ".count");
+                            saveProperties();
+                        } else if (!urls.isEmpty()) {
+                            properties.put(prefix + ".count", 
Integer.toString(urls.size()));
                             for (int i = 0; i < urls.size(); i++) {
-                                props.put(prefix + ".url." + i, 
urls.get(i).toExternalForm());
-                            }
-                            OutputStream output = new FileOutputStream(file);
-                            try {
-                                props.store(output, null);
-                            } finally {
-                                output.close();
+                                properties.put(prefix + ".url." + i, 
urls.get(i).toExternalForm());
                             }
+                            saveProperties();
                         }
                     }
                 } catch (Exception e) {
@@ -219,56 +253,34 @@ public class FeatureDeploymentListener implements 
ArtifactUrlTransformer, Bundle
             } else if (bundleEvent.getType() == BundleEvent.UNINSTALLED) {
                 try {
                     synchronized (this) {
-                        File file = 
bundleContext.getDataFile("FeatureDeploymentListener.cfg");
-                        if (file != null) {
-                            Properties props = new Properties();
-                            if (file.exists()) {
-                                InputStream input = new FileInputStream(file);
-                                try {
-                                    props.load(input);
-                                } finally {
-                                    input.close();
-                                }
-                            }
-                            String prefix = bundle.getSymbolicName() + "-" + 
bundle.getVersion();
-                            String countStr = (String) props.get(prefix + 
".count");
-                            if (countStr != null) {
-                                int count = Integer.parseInt(countStr);
-                                for (int i = 0; i < count; i++) {
-                                    URL url = new URL((String) 
props.get(prefix + ".url." + i));
-                                    for (Repository repo : 
featuresService.listRepositories()) {
-                                        try {
-                                            if 
(repo.getURI().equals(url.toURI())) {
-                                                for (Feature f : 
repo.getFeatures()) {
-                                                    try {
-                                                        
featuresService.uninstallFeature(f.getName(), f.getVersion());
-                                                    } catch (Exception e) {
-                                                        logger.error("Unable 
to uninstall feature: " + f.getName(), e);
-                                                    }
+                        String prefix = bundle.getSymbolicName() + "-" + 
bundle.getVersion();
+                        String countStr = (String) properties.remove(prefix + 
".count");
+                        if (countStr != null) {
+                            int count = Integer.parseInt(countStr);
+                            for (int i = 0; i < count; i++) {
+                                URL url = new URL((String) 
properties.remove(prefix + ".url." + i));
+                                for (Repository repo : 
featuresService.listRepositories()) {
+                                    try {
+                                        if (repo.getURI().equals(url.toURI())) 
{
+                                            for (Feature f : 
repo.getFeatures()) {
+                                                try {
+                                                    
featuresService.uninstallFeature(f.getName(), f.getVersion());
+                                                } catch (Exception e) {
+                                                    logger.error("Unable to 
uninstall feature: " + f.getName(), e);
                                                 }
                                             }
-                                        } catch (Exception e) {
-                                            logger.error("Unable to uninstall 
features: " + url, e);
                                         }
-                                    }
-                                    try {
-                                        
featuresService.removeRepository(url.toURI());
-                                    } catch (URISyntaxException e) {
-                                        logger.error("Unable to remove 
repository: " + url, e);
+                                    } catch (Exception e) {
+                                        logger.error("Unable to uninstall 
features: " + url, e);
                                     }
                                 }
-                            }
-                            for (Iterator<Object> it = 
props.keySet().iterator(); it.hasNext();) {
-                                if (it.next().toString().startsWith(prefix + 
".")) {
-                                    it.remove();
+                                try {
+                                    
featuresService.removeRepository(url.toURI());
+                                } catch (URISyntaxException e) {
+                                    logger.error("Unable to remove repository: 
" + url, e);
                                 }
                             }
-                            OutputStream output = new FileOutputStream(file);
-                            try {
-                                props.store(output, null);
-                            } finally {
-                                output.close();
-                            }
+                            saveProperties();
                         }
                     }
                 } catch (Exception e) {

Reply via email to