You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by li...@apache.org on 2009/12/01 19:30:29 UTC

svn commit: r885857 - in /incubator/aries/trunk/util: pom.xml src/main/java/org/apache/aries/util/tracker/ src/main/java/org/apache/aries/util/tracker/AriesBundleTrackerCustomizer.java src/main/java/org/apache/aries/util/tracker/BundleTrackerFactory.java

Author: linsun
Date: Tue Dec  1 18:30:29 2009
New Revision: 885857

URL: http://svn.apache.org/viewvc?rev=885857&view=rev
Log:
ARIES-61 provide a common bundle tracker customizer and bundle tracker factory that can be used by extenders

Added:
    incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/
    incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AriesBundleTrackerCustomizer.java   (with props)
    incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/BundleTrackerFactory.java   (with props)
Modified:
    incubator/aries/trunk/util/pom.xml

Modified: incubator/aries/trunk/util/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/util/pom.xml?rev=885857&r1=885856&r2=885857&view=diff
==============================================================================
--- incubator/aries/trunk/util/pom.xml (original)
+++ incubator/aries/trunk/util/pom.xml Tue Dec  1 18:30:29 2009
@@ -39,6 +39,18 @@
           <scope>provided</scope>
       </dependency>
       <dependency>
+          <groupId>org.osgi</groupId>
+          <artifactId>org.osgi.compendium</artifactId>
+          <version>4.2.0</version>
+          <scope>provided</scope>
+      </dependency>
+      <dependency>
+          <groupId>org.eclipse</groupId>
+          <artifactId>osgi</artifactId>
+          <version>3.5.0.v20090520</version>
+          <scope>provided</scope>
+      </dependency>      
+      <dependency>
           <groupId>org.apache.aries.testsupport</groupId>
           <artifactId>aries-testsupport-unit</artifactId>
           <version>${version}</version>
@@ -62,9 +74,11 @@
                     <instructions>
                         <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
                         <Export-Package>
-                            org.apache.aries.util;version="${pom.version}"
+                            org.apache.aries.util*;version="${pom.version}"
                         </Export-Package>
-                        <Import-Package>!org.apache.aries.util*,*</Import-Package>
+                        <!--  let's make org.osgi.service.framework optional as this is proposed API 
+                        may not be avail depends on the OSGi framework -->
+                        <Import-Package>!org.apache.aries.util*,org.osgi.service.framework;resolution:=optional,*</Import-Package>
                         <_versionpolicy>[$(version;==;$(@)),$(version;+;$(@)))</_versionpolicy>
                         <_removeheaders>Ignore-Package,Include-Resource,Private-Package,Bundle-DocURL</_removeheaders>
                     </instructions>

Added: 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=885857&view=auto
==============================================================================
--- 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/AriesBundleTrackerCustomizer.java Tue Dec  1 18:30:29 2009
@@ -0,0 +1,78 @@
+/**
+ * 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.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 AriesBundleTrackerCustomizer() {
+  }
+  
+  public Object addingBundle(Bundle b, BundleEvent event)
+  {
+    customizedProcessBundle(b, event, Bundle.STARTING | Bundle.STOPPING);
+    return null;   
+  }
+  
+  public void modifiedBundle(Bundle b, BundleEvent event, Object arg2)
+  {
+    // we are only interested in uninstalled bundle state for composite bundles
+    // as we need to remove the bt off the bt factory
+    if (event.getType() == BundleEvent.STOPPING) {
+      customizedProcessBundle(b, event, 0);
+    }
+  }
+
+  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);
+      
+      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) {
+        // 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);
+      }
+    }
+  }  
+}

Propchange: incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AriesBundleTrackerCustomizer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AriesBundleTrackerCustomizer.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/AriesBundleTrackerCustomizer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/BundleTrackerFactory.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/BundleTrackerFactory.java?rev=885857&view=auto
==============================================================================
--- incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/BundleTrackerFactory.java (added)
+++ incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/BundleTrackerFactory.java Tue Dec  1 18:30:29 2009
@@ -0,0 +1,97 @@
+/**
+ * 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.aries.util.tracker;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.osgi.framework.Version;
+import org.osgi.util.tracker.BundleTracker;
+
+/**
+ * this is the factory for BundleTracker
+ */
+public class BundleTrackerFactory
+{
+  private static ConcurrentHashMap<String, List<BundleTracker>> btMap = new ConcurrentHashMap<String, List<BundleTracker>>();
+
+  /**
+   * get bundle tracker based on bundle name and version
+   * @param bundleScope        composite bundle's - SymbolicName_Version
+   * @return                   the list of bundle tracker associated with the bundle scope
+   */
+  public static List<BundleTracker> getBundleTrackerList(String bundleScope)
+  {
+    return (List<BundleTracker>) btMap.get(bundleScope);
+  }
+  
+  /**
+   * get bundle tracker based on composite bundle's symbolicName and version
+   * @param bundleSymbolicName  composite bundle's symbolicName 
+   * @param bundleVersion       composite bundle's version
+   * @return                    the list of bundle tracker associated with the bundle scope
+   */
+  public static List<BundleTracker> getBundleTrackerList(String symbolicName, Version version)
+  {
+    return (List<BundleTracker>) btMap.get(symbolicName + "_" + version.toString());
+  }
+
+  /**
+   * get all bundle tracker registered in this factory
+   * @return
+   */
+  public static Collection<List<BundleTracker>> getAllBundleTracker()
+  {
+    return btMap.values();
+  }
+  
+  /**
+   * register the bundle tracker
+   * @param bundleScope    composite bundle's SymbolicName_Version
+   * @param bt          the bundle tracker to be registered
+   */
+  public static void registerBundleTracker(String bundleScope, BundleTracker bt)
+  {
+    List<BundleTracker> list = btMap.get(bundleScope);
+    if (list == null) {
+      list = new ArrayList<BundleTracker>();
+    } 
+    list.add(bt);
+    btMap.putIfAbsent(bundleScope, list);
+  }
+  
+  /**
+   * unregister and close the bundle tracker(s) associated with composite bundle's - SymbolicName_Version
+   * @param bundleScope  composite bundle's - SymbolicName_Version
+   */
+  public static void unregisterAndCloseBundleTracker(String bundleScope)
+  {
+    List<BundleTracker> list = btMap.get(bundleScope);
+    if (list == null) {
+      return;
+    } else {
+      for (BundleTracker bt : list) {
+        bt.close();
+      }
+    }
+    btMap.remove(bundleScope);
+  }
+}
\ No newline at end of file

Propchange: incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/BundleTrackerFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/BundleTrackerFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: incubator/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/BundleTrackerFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain