incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mahrw...@apache.org
Subject svn commit: r901691 - in /incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker: AbstractBundleTrackerCustomizer.java AriesBundleTrackerCustomizer.java MultiBundleTracker.java
Date Thu, 21 Jan 2010 13:12:11 GMT
Author: mahrwald
Date: Thu Jan 21 13:12:11 2010
New Revision: 901691

URL: http://svn.apache.org/viewvc?rev=901691&view=rev
Log:
ARIES-125 Commit Brian's changes (slightly modified)

Added:
    incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AbstractBundleTrackerCustomizer.java
    incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/MultiBundleTracker.java
Modified:
    incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AriesBundleTrackerCustomizer.java

Added: incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AbstractBundleTrackerCustomizer.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AbstractBundleTrackerCustomizer.java?rev=901691&view=auto
==============================================================================
--- incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AbstractBundleTrackerCustomizer.java
(added)
+++ incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AbstractBundleTrackerCustomizer.java
Thu Jan 21 13:12:11 2010
@@ -0,0 +1,68 @@
+package org.apache.aries.util.tracker;
+
+import java.util.List;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.service.framework.CompositeBundle;
+import org.osgi.util.tracker.BundleTracker;
+import org.osgi.util.tracker.BundleTrackerCustomizer;
+
+/**
+ * This class provides code to track <code>CompositeBundle></code>s.
+ * 
+ * @author pradine
+ *
+ */
+public abstract class AbstractBundleTrackerCustomizer implements BundleTrackerCustomizer
{
+
+    public AbstractBundleTrackerCustomizer() {
+        super();
+    }
+
+    protected void customizedProcessBundle(BundleTrackerCustomizer btc, Bundle b, BundleEvent
event, int stateMask) {
+        if (b instanceof CompositeBundle) {
+            // check if the compositeBundle is already tracked in the
+            // BundleTrackerFactory
+            String bundleScope = b.getSymbolicName() + "_"
+            + b.getVersion().toString();
+            List<BundleTracker> btList = BundleTrackerFactory
+            .getBundleTrackerList(bundleScope);
+
+            // bundle is already active and there is no event associated
+            // this can happen when bundle is first time added to the tracker
+            if (event == null) {
+                if (b.getState() == Bundle.ACTIVE) {
+                    openTracker(btc, b, bundleScope, stateMask);
+                }
+            } else {
+                if (event.getType() == BundleEvent.STOPPING) {
+                    // if CompositeBundle is being stopped, let's remove the bundle
+                    // tracker(s) associated with the composite bundle
+                    if (btList != null) {
+                        // unregister the bundlescope off the factory and close
+                        // bundle trackers
+                        BundleTrackerFactory
+                        .unregisterAndCloseBundleTracker(bundleScope);
+                    }
+                } else if (event.getType() == BundleEvent.STARTING) {
+                    openTracker(btc, b, bundleScope, stateMask);
+                }
+            }
+        }
+    }
+
+    private void openTracker(BundleTrackerCustomizer btc, Bundle b, String bundleScope, int
stateMask) {
+         // let's process each of the bundle in the CompositeBundle
+         CompositeBundle cb = (CompositeBundle) b;
+         BundleContext compositeBundleContext = cb
+                 .getCompositeFramework().getBundleContext();
+    
+         // let's track each of the bundle in the CompositeBundle
+         BundleTracker bt = new BundleTracker(compositeBundleContext, stateMask, btc);
+         bt.open();
+         BundleTrackerFactory.registerBundleTracker(bundleScope, bt);
+     }
+
+}
\ No newline at end of file

Modified: incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AriesBundleTrackerCustomizer.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AriesBundleTrackerCustomizer.java?rev=901691&r1=901690&r2=901691&view=diff
==============================================================================
--- incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AriesBundleTrackerCustomizer.java
(original)
+++ incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AriesBundleTrackerCustomizer.java
Thu Jan 21 13:12:11 2010
@@ -18,77 +18,23 @@
  */
 package org.apache.aries.util.tracker;
 
-import java.util.List;
-
 import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
-import org.osgi.service.framework.CompositeBundle;
-import org.osgi.util.tracker.BundleTracker;
-import org.osgi.util.tracker.BundleTrackerCustomizer;
 
-public abstract class AriesBundleTrackerCustomizer implements
-        BundleTrackerCustomizer {
+public abstract class AriesBundleTrackerCustomizer extends AbstractBundleTrackerCustomizer
{
 
     public AriesBundleTrackerCustomizer() {
     }
 
     public Object addingBundle(Bundle b, BundleEvent event) {
-        customizedProcessBundle(b, event, Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING);
+        customizedProcessBundle(this, b, event, Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING);
         return b;
     }
 
     public void modifiedBundle(Bundle b, BundleEvent event, Object arg2) {
-        customizedProcessBundle(b, event, Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING);
+        customizedProcessBundle(this, b, event, Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING);
     }
 
     public void removedBundle(Bundle b, BundleEvent event, Object arg2) {
     }
-
-    protected void customizedProcessBundle(Bundle b, BundleEvent event,
-            int stateMask) {
-        if (b instanceof CompositeBundle) {
-            // check if the compositeBundle is already tracked in the
-            // BundleTrackerFactory
-            String bundleScope = b.getSymbolicName() + "_"
-                    + b.getVersion().toString();
-            List<BundleTracker> btList = BundleTrackerFactory
-                    .getBundleTrackerList(bundleScope);
-
-            // bundle is already active and there is no event associated
-            // this can happen when bundle is first time added to the tracker
-            if (event == null) {
-                if (b.getState() == Bundle.ACTIVE) {
-                    openTracker(b, bundleScope, stateMask);
-                }
-            } else {
-                if (event.getType() == BundleEvent.STOPPING) {
-                    // if CompositeBundle is being stopped, let's remove the bundle
-                    // tracker(s) associated with the composite bundle
-                    if (btList != null) {
-                        // unregister the bundlescope off the factory and close
-                        // bundle trackers
-                        BundleTrackerFactory
-                                .unregisterAndCloseBundleTracker(bundleScope);
-                    }
-                } else if (event.getType() == BundleEvent.STARTING) {
-                    openTracker(b, bundleScope, stateMask);
-                }
-            }
-        }
-    }
-        
-     private void openTracker(Bundle b, String bundleScope, int stateMask) {
-         // let's process each of the bundle in the CompositeBundle
-         CompositeBundle cb = (CompositeBundle) b;
-         BundleContext compositeBundleContext = cb
-                 .getCompositeFramework().getBundleContext();
-
-         // let's track each of the bundle in the CompositeBundle
-         BundleTracker bt = new BundleTracker(compositeBundleContext,
-                 stateMask, this);
-         bt.open();
-         BundleTrackerFactory.registerBundleTracker(bundleScope, bt);
-     }
-     
 }

Added: incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/MultiBundleTracker.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/MultiBundleTracker.java?rev=901691&view=auto
==============================================================================
--- incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/MultiBundleTracker.java
(added)
+++ incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/MultiBundleTracker.java
Thu Jan 21 13:12:11 2010
@@ -0,0 +1,121 @@
+package org.apache.aries.util.tracker;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.framework.CompositeBundle;
+import org.osgi.util.tracker.BundleTracker;
+
+/**
+ * <p>This class supports the tracking of composite bundles. It allows clients to ignore
any
+ * events related to framework bundles, as it will automatically handle these events. In
+ * order to use this class clients must create a subclass and implement the methods of the
+ * <code>BundleTrackerCustomizer</code> interface. In spite of this, instances
of this class
+ * MUST NOT be passed as a parameter to any <code>BundleTracker</code>.</p>

+ * 
+ * @author pradine
+ *
+ */
+public abstract class MultiBundleTracker extends AbstractBundleTrackerCustomizer {
+    private static final int COMPOSITE_BUNDLE_MASK =
+        Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING;
+    
+    private final BundleTracker tracker;
+        
+    /**
+     * Constructor
+     * 
+     * @param context - The <code>BundleContext</code> against which the tracking
is done.
+     * @param stateMask - The bit mask of the ORing of the bundle states to be tracked. The
+     * mask must contain the flags <code>Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING</code>
+     * as a minimum.
+     * @throws IllegalArgumentException - If the provided bit mask does not contain required
+     * flags
+     */
+    public MultiBundleTracker(BundleContext context, int stateMask) {
+        if ((stateMask & COMPOSITE_BUNDLE_MASK) != COMPOSITE_BUNDLE_MASK)
+            throw new IllegalArgumentException();
+        
+        if (areMultipleFrameworksAvailable(context)) {
+          tracker = new InternalBundleTracker(context, stateMask);
+        } else {
+          tracker = new BundleTracker(context, stateMask, this);
+        }
+    }
+    
+    private static boolean areMultipleFrameworksAvailable(BundleContext context) {
+      ServiceReference sr = context.getServiceReference("org.osgi.service.framework.CompositeBundleFactory");
+      return sr != null;
+    }
+    
+    /**
+     * Start tracking bundles that match the bit mask provided at creation time.
+     * 
+     * @see BundleTracker#open()
+     */
+    public void open() {
+        tracker.open();
+    }
+    
+    /**
+     * Stop the tracking of bundles
+     * 
+     * @see BundleTracker#close()
+     */
+    public void close() {
+        tracker.close();
+    }
+
+    //This implementation of a BundleTracker is based on the implementation
+    //in org.apache.aries.util.tracker.AriesBundleTrackerCustomizer
+    private class InternalBundleTracker extends BundleTracker {
+        private final int mask;
+        
+        public InternalBundleTracker(BundleContext context, int stateMask) {
+            super(context, stateMask, null);
+            
+            mask = stateMask;
+        }
+
+        /*
+         * (non-Javadoc)
+         * @see org.osgi.util.tracker.BundleTracker#addingBundle(org.osgi.framework.Bundle,
org.osgi.framework.BundleEvent)
+         */
+        public Object addingBundle(Bundle b, BundleEvent event) {
+            Object o = null;
+            
+            if (b instanceof CompositeBundle) {
+                customizedProcessBundle(this, b, event, mask);
+                o = b;
+            }
+            else {
+                o = MultiBundleTracker.this.addingBundle(b, event);
+            }
+            
+            return o;
+        }
+
+        /*
+         * (non-Javadoc)
+         * @see org.osgi.util.tracker.BundleTracker#modifiedBundle(org.osgi.framework.Bundle,
org.osgi.framework.BundleEvent, java.lang.Object)
+         */
+        public void modifiedBundle(Bundle b, BundleEvent event, Object object) {
+            if (b instanceof CompositeBundle) {
+                customizedProcessBundle(this, b, event, mask);
+            }
+            else {
+                MultiBundleTracker.this.modifiedBundle(b, event, object);
+            }
+        }
+
+        /*
+         * (non-Javadoc)
+         * @see org.osgi.util.tracker.BundleTracker#removedBundle(org.osgi.framework.Bundle,
org.osgi.framework.BundleEvent, java.lang.Object)
+         */
+        public void removedBundle(Bundle b, BundleEvent event, Object object) {
+            if (!(b instanceof CompositeBundle))
+                MultiBundleTracker.this.removedBundle(b, event, object);
+        }
+    }
+}



Mime
View raw message