You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2012/05/08 22:12:53 UTC

svn commit: r1335739 - in /geronimo/server/branches/3.0-beta/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries: ApplicationGBean.java ApplicationUpdateHelper.java BundleGraph.java

Author: gawor
Date: Tue May  8 20:12:53 2012
New Revision: 1335739

URL: http://svn.apache.org/viewvc?rev=1335739&view=rev
Log:
1) configure bundle refresh timeout, 2) use Bundle/Framework Wiring API more

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

Modified: geronimo/server/branches/3.0-beta/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationGBean.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/3.0-beta/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationGBean.java?rev=1335739&r1=1335738&r2=1335739&view=diff
==============================================================================
--- geronimo/server/branches/3.0-beta/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationGBean.java (original)
+++ geronimo/server/branches/3.0-beta/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationGBean.java Tue May  8 20:12:53 2012
@@ -20,11 +20,9 @@ import java.io.File;
 import java.io.IOException;
 import java.text.MessageFormat;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
 
 import org.apache.aries.application.ApplicationMetadataFactory;
@@ -38,7 +36,6 @@ import org.apache.aries.application.mana
 import org.apache.aries.application.management.BundleInfo;
 import org.apache.aries.application.management.ManagementException;
 import org.apache.aries.application.management.ResolverException;
-import org.apache.geronimo.aries.BundleGraph.BundleNode;
 import org.apache.geronimo.gbean.GBeanLifecycle;
 import org.apache.geronimo.gbean.annotation.GBean;
 import org.apache.geronimo.gbean.annotation.ParamAttribute;
@@ -55,7 +52,10 @@ import org.osgi.framework.BundleExceptio
 import org.osgi.framework.ServiceException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.Version;
-import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.framework.wiring.BundleRevision;
+import org.osgi.framework.wiring.BundleWire;
+import org.osgi.framework.wiring.BundleWiring;
+import org.osgi.framework.wiring.FrameworkWiring;
 import org.osgi.service.packageadmin.PackageAdmin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -68,6 +68,8 @@ public class ApplicationGBean implements
         
     private static final Logger LOG = LoggerFactory.getLogger(ApplicationGBean.class);
     
+    private static final long applicationStartTimeout = getApplicationStartTimeout();
+    
     private final Bundle bundle;
     private final ApplicationInstaller installer;
     private final Artifact configId;
@@ -75,7 +77,6 @@ public class ApplicationGBean implements
     private GeronimoApplication application;
     private ApplicationState applicationState;
     private Set<Bundle> applicationBundles;
-    private BundleGraph bundleGraph;
     
     public ApplicationGBean(@ParamSpecial(type = SpecialAttributeType.kernel) Kernel kernel,
                             @ParamSpecial(type = SpecialAttributeType.bundle) Bundle bundle,
@@ -375,21 +376,16 @@ public class ApplicationGBean implements
         }        
     }    
     
-    @SuppressWarnings("deprecation")
     private void startApplicationBundles() throws Exception {
+        BundleContext context = bundle.getBundleContext();
+        FrameworkWiring wiring = context.getBundle(0).adapt(FrameworkWiring.class);
+        if (!wiring.resolveBundles(applicationBundles)) {
+            throw new BundleException("One or more bundles in " + getApplicationName() + " application could not be resolved.");
+        }
         
-        PackageAdmin packageAdmin = null;
-        ServiceReference packageAdminRef = bundle.getBundleContext().getServiceReference(PackageAdmin.class.getName());
         List<Bundle> bundlesWeStarted = new ArrayList<Bundle>();
-        
         try {
-            
-            packageAdmin = getService(packageAdminRef, PackageAdmin.class);
-            Set<Bundle> sortedBundles = new LinkedHashSet<Bundle>();
-            
-            calculateBundleDependencies(packageAdmin);
-            sortedBundles.addAll(bundleGraph.getOrderedBundles());
-            
+            Set<Bundle> sortedBundles = getSortedBundles();
             for (Bundle b : sortedBundles) {
                 if (BundleUtils.canStart(b)) {
                     LOG.debug("Starting {} application bundle.", b);
@@ -411,83 +407,52 @@ public class ApplicationGBean implements
                 }
             }
             throw be;
-        } finally {
-            
-            if (packageAdmin != null) {
-                bundle.getBundleContext().ungetService(packageAdminRef);
-            }
         }
     }
+
+    /*
+     * Sorts bundles in bundle dependency order (i.e. Import-Package, Require-Bundle order).
+     */
+    private LinkedHashSet<Bundle> getSortedBundles() {
+        LinkedHashSet<Bundle> orderedBundles = new LinkedHashSet<Bundle>();
+        for (Bundle bundle : applicationBundles) {
+            sortDependentBundles(bundle, orderedBundles);
+        }
+        return orderedBundles;
+    }
     
-    
-    @SuppressWarnings("deprecation")
-    private void calculateBundleDependencies(PackageAdmin packageAdmin) throws BundleException {
-        if(! packageAdmin.resolveBundles(applicationBundles.toArray(new Bundle[applicationBundles.size()]))) {
-            throw new BundleException("The bundles in " + application.getApplicationMetadata().getApplicationSymbolicName() + 
-                    ":" + application.getApplicationMetadata().getApplicationVersion() + " could not be resolved");
+    private void sortDependentBundles(Bundle bundle, LinkedHashSet<Bundle> sortedBundles) {
+        if (sortedBundles.contains(bundle)) {
+            return;
         }
         
-        Map<String, BundleNode> nodesMap = new HashMap<String, BundleNode>();
-        
-        for(Bundle currentBundle : applicationBundles) {
-            
-            BundleNode currentNode = getBundleNode(currentBundle, nodesMap);
-            
-            Bundle[] requiringBundles = getRequiringBundles(currentBundle, packageAdmin);
-            for(Bundle rBundle : requiringBundles) {
-                BundleNode rBundleNode = getBundleNode(rBundle, nodesMap);
-                rBundleNode.getRequiredBundles().add(currentNode); 
+        BundleWiring wiring = bundle.adapt(BundleWiring.class);
+        List<BundleWire> wires;
+        wires = wiring.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
+        for (BundleWire wire : wires) {
+            Bundle wiredBundle = wire.getProviderWiring().getBundle();
+            if (applicationBundles.contains(wiredBundle)) {
+                sortDependentBundles(wiredBundle, sortedBundles);
             }
         }
-        
-        bundleGraph = new BundleGraph(nodesMap.values());
-    }
-    
-    /**
-     * Get the requiring bundles which require the target bundle
-     * 
-     * @param targetBundle the target bundle
-     * @param packageAdmin
-     * @return
-     */
-    @SuppressWarnings("deprecation")
-    private Bundle[] getRequiringBundles(Bundle targetBundle, PackageAdmin packageAdmin) {
-        ExportedPackage[] ePackages = packageAdmin.getExportedPackages(targetBundle);
-        if(ePackages == null || ePackages.length == 0) return new Bundle[]{};
-        
-        Set<Bundle> requiringBundles = new HashSet<Bundle>();
-        
-        for(ExportedPackage ePackage : ePackages) {
-            Bundle[] importingBundles = ePackage.getImportingBundles();
-            if(importingBundles == null) continue;
-            
-            for(Bundle iBundle : importingBundles) {
-                if(! targetBundle.equals(iBundle) && applicationBundles.contains(iBundle)) {
-                    requiringBundles.add(iBundle);
-                }
+        wires = wiring.getRequiredWires(BundleRevision.BUNDLE_NAMESPACE);
+        for (BundleWire wire : wires) {
+            Bundle wiredBundle = wire.getProviderWiring().getBundle();
+            if (applicationBundles.contains(wiredBundle)) {
+                sortDependentBundles(wiredBundle, sortedBundles);
             }
         }
         
-        return requiringBundles.toArray(new Bundle[requiringBundles.size()]);
-    }
-    
-    private BundleNode getBundleNode(Bundle bundle, Map<String, BundleNode> nodesMap) {
-        BundleNode node = nodesMap.get(bundle.getSymbolicName() + bundle.getVersion());
-        if(node == null) {
-            node = new BundleNode(bundle);
-        }
-        nodesMap.put(bundle.getSymbolicName() + bundle.getVersion(), node);
-        return node;
+        sortedBundles.add(bundle);
     }
     
-	
     private static long getApplicationStartTimeout() {
         String property = System.getProperty("org.apache.geronimo.aries.applicationStartTimeout", String.valueOf(5 * 60 * 1000));
         return Long.parseLong(property);
     }
     
     private void waitForStart() {
-        waitForStart(getApplicationStartTimeout());
+        waitForStart(applicationStartTimeout);
     }
     
     private void waitForStart(long timeout) {

Modified: geronimo/server/branches/3.0-beta/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationUpdateHelper.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/3.0-beta/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationUpdateHelper.java?rev=1335739&r1=1335738&r2=1335739&view=diff
==============================================================================
--- geronimo/server/branches/3.0-beta/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationUpdateHelper.java (original)
+++ geronimo/server/branches/3.0-beta/plugins/aries/geronimo-aries/src/main/java/org/apache/geronimo/aries/ApplicationUpdateHelper.java Tue May  8 20:12:53 2012
@@ -29,11 +29,9 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Enumeration;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.jar.JarEntry;
@@ -57,9 +55,7 @@ import org.osgi.framework.BundleExceptio
 import org.osgi.framework.Constants;
 import org.osgi.framework.FrameworkEvent;
 import org.osgi.framework.FrameworkListener;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.packageadmin.ExportedPackage;
-import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.framework.wiring.FrameworkWiring;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -70,12 +66,19 @@ public class ApplicationUpdateHelper {
         
     private static final Logger LOG = LoggerFactory.getLogger(ApplicationUpdateHelper.class);
     
+    private static final long bundleRefreshTimeout = getBundleRefreshTimeout();
+    
     private final ApplicationGBean applicationGBean;
     
     public ApplicationUpdateHelper(ApplicationGBean applicationGBean) {
         this.applicationGBean = applicationGBean;
     }
     
+    private static long getBundleRefreshTimeout() {
+        String property = System.getProperty("org.apache.geronimo.aries.bundleRefreshTimeout", String.valueOf(5 * 60 * 1000));
+        return Long.parseLong(property);
+    }
+    
     public void updateBundle(Bundle targetBundle, File bundleFile) throws Exception {
         
         String applicationName = applicationGBean.getApplicationName();
@@ -85,8 +88,6 @@ public class ApplicationUpdateHelper {
                 
         BundleContext context = applicationGBean.getBundle().getBundleContext();
 
-        ServiceReference reference = null;
-        RefreshListener refreshListener = null;
         try {
             // stop the bundle
             targetBundle.stop();
@@ -100,18 +101,18 @@ public class ApplicationUpdateHelper {
                 close(fi);
             }
 
-            reference = context.getServiceReference(PackageAdmin.class.getName());
-            PackageAdmin packageAdmin = (PackageAdmin) context.getService(reference);
+            FrameworkWiring wiring = context.getBundle(0).adapt(FrameworkWiring.class);
+            
+            Collection<Bundle> bundles = Arrays.asList(targetBundle);
             
-            Bundle[] bundles = new Bundle [] { targetBundle };
             // resolve the bundle
-            if (!packageAdmin.resolveBundles(bundles)) {
+            if (!wiring.resolveBundles(bundles)) {
                 StringBuilder builder = new StringBuilder();
                 builder.append("Updated ").append(bundleName).append(" bundle cannot be resolved.");
                 
                 // check for resolver errors
                 ResolverErrorAnalyzer errorAnalyzer = new ResolverErrorAnalyzer(context);
-                String resolverErrors = errorAnalyzer.getErrorsAsString(Arrays.asList(bundles));
+                String resolverErrors = errorAnalyzer.getErrorsAsString(bundles);
                 if (resolverErrors != null) {
                     builder.append(" ").append(resolverErrors);
                 }
@@ -119,19 +120,19 @@ public class ApplicationUpdateHelper {
                 throw new BundleException(builder.toString());
             }
             
-            Set<Bundle> dependents = new HashSet<Bundle>();
-            collectDependentBundles(packageAdmin, dependents, targetBundle);
+            // 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);
             }
             
             // install listener for package refresh
-            refreshListener = new RefreshListener();
-            context.addFrameworkListener(refreshListener);
+            RefreshListener refreshListener = new RefreshListener();
 
             // refresh the bundle - this happens asynchronously
-            packageAdmin.refreshPackages(bundles);
+            wiring.refreshBundles(bundles, refreshListener);
 
             // update application archive
             try {
@@ -142,7 +143,7 @@ public class ApplicationUpdateHelper {
             }
 
             // wait for package refresh to finish
-            refreshListener.waitForRefresh(10 * 1000);
+            refreshListener.waitForRefresh(bundleRefreshTimeout);
 
             // start the bundle
             if (BundleUtils.canStart(targetBundle)) {
@@ -154,13 +155,6 @@ public class ApplicationUpdateHelper {
         } catch (Exception e) {
             LOG.error("Error updating " + bundleName + " bundle in " + applicationName + " application", e);
             throw new Exception("Error updating application: " + e.getMessage());
-        } finally {
-            if (refreshListener != null) {
-                context.removeFrameworkListener(refreshListener);
-            }
-            if (reference != null) {
-                context.ungetService(reference);
-            }
         }
     }
     
@@ -306,7 +300,7 @@ public class ApplicationUpdateHelper {
             }
         }
 
-        public void waitForRefresh(int timeout) {
+        public void waitForRefresh(long timeout) {
             try {
                 latch.await(timeout, TimeUnit.MILLISECONDS);
             } catch (InterruptedException e) {
@@ -314,23 +308,6 @@ public class ApplicationUpdateHelper {
             }
         }
     }
-
-    private void collectDependentBundles(PackageAdmin packageAdmin, Set<Bundle> dependents, Bundle bundle) {
-        ExportedPackage[] exportedPackages = packageAdmin.getExportedPackages(bundle);
-        if (exportedPackages != null) {
-            for (ExportedPackage exportedPackage : exportedPackages) {
-                Bundle[] importingBundles = exportedPackage.getImportingBundles();
-                if (importingBundles != null) {
-                    for (Bundle importingBundle : importingBundles) {
-                        if (!dependents.contains(importingBundle)) {
-                            dependents.add(importingBundle);
-                            collectDependentBundles(packageAdmin, dependents, importingBundle);
-                        }
-                    }
-                }
-            }
-        }
-    }
     
     private static String bundleCollectionToString(Collection<Bundle> bundles) {
         StringBuilder builder = new StringBuilder();