FELIX-3660 Applied the supplied patch.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1391225 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/deviceaccess/src/main/java/org/apache/felix/das/Activator.java b/deviceaccess/src/main/java/org/apache/felix/das/Activator.java
index 1bd6698..c7f918a 100644
--- a/deviceaccess/src/main/java/org/apache/felix/das/Activator.java
+++ b/deviceaccess/src/main/java/org/apache/felix/das/Activator.java
@@ -41,10 +41,10 @@
 {
 
     /** the bundle context */
-    private BundleContext m_context;
+    private volatile BundleContext m_context;
 
     /** the dependency manager */
-    private DependencyManager m_manager;
+    private volatile DependencyManager m_manager;
 
     /** the device manager */
     private DeviceManager m_deviceManager;
@@ -96,7 +96,7 @@
             .setCallbacks( "locatorAdded", "locatorRemoved" ) );
         
         svc.add( createServiceDependency().setService( Driver.class, driverFilter ).setRequired( false )
-    		.setCallbacks( "driverAdded", "driverRemoved" ) );
+    		.setCallbacks( "driverAdded", "driverModified", "driverRemoved" ) );
         
         svc.add( createServiceDependency().setService( deviceFilter ).setRequired( false )
     		.setCallbacks( "deviceAdded", "deviceModified", "deviceRemoved" ) );
diff --git a/deviceaccess/src/main/java/org/apache/felix/das/DeviceManager.java b/deviceaccess/src/main/java/org/apache/felix/das/DeviceManager.java
index 928278a..ed7159a 100644
--- a/deviceaccess/src/main/java/org/apache/felix/das/DeviceManager.java
+++ b/deviceaccess/src/main/java/org/apache/felix/das/DeviceManager.java
@@ -211,15 +211,26 @@
         debug( "driver appeared: " + Util.showDriver( ref ) );
         
         //immediately check for idle devices
-//        submit( new CheckForIdleDevices() );
+        submit( new CheckForIdleDevices() );
     }
 
+    public void driverModified( ServiceReference ref, Object obj )
+    {
+        final Driver driver = Driver.class.cast( obj );
+        
+        debug( "driver modified: " + Util.showDriver( ref ) );
+        m_drivers.remove( ref );
+        m_drivers.put( ref , new DriverAttributes( ref, driver ) );
+
+        // check if devices have become idle
+        // after some time
+        schedule( new CheckForIdleDevices() );
+    }
 
     public void driverRemoved( ServiceReference ref )
     {
-        String driverId = String.class.cast( ref.getProperty( Constants.DRIVER_ID ) );
         debug( "driver lost: " + Util.showDriver( ref ) );
-        m_drivers.remove( driverId );
+        m_drivers.remove( ref );
 
         // check if devices have become idle
         // after some time
@@ -464,6 +475,11 @@
     private boolean isDriverBundle( Bundle bundle )
     {
         ServiceReference[] refs = bundle.getRegisteredServices();
+        
+        if (refs == null) {
+            return false;
+        }
+        
         for ( ServiceReference ref : refs )
         {
             if ( m_driverImplFilter.match( ref ) )
@@ -491,7 +507,14 @@
             {
                 // just call the tryUninstall; the da itself
                 // will know if it should really uninstall the driver.
-                da.tryUninstall();
+                try 
+                { 
+                	da.tryUninstall(); 
+                }
+                catch (Exception e) 
+                {
+                	debug(da.getDriverId() + " uninstall failed");
+                }
             }
 
             return null;
@@ -557,7 +580,7 @@
 
             // first find matching driver bundles
             // if there are no driver locators
-            // we'll have to do with the drivers that where
+            // we'll have to do with the drivers that were
             // added 'manually'
             Set<String> driverIds = m_driverLoader.findDrivers( m_locators, dict );
 
@@ -673,8 +696,7 @@
                 // its a referral
                 info( "attach led to a referral to: " + newDriverId );
                 m_excluded.add( m_finalDriver );
-                return driverAttachment( dict, new String[]
-                    { newDriverId } );
+                return driverAttachment( dict, new String[]{ newDriverId } );
             }
             catch ( Throwable t )
             {
diff --git a/deviceaccess/src/main/java/org/apache/felix/das/util/DriverMatcher.java b/deviceaccess/src/main/java/org/apache/felix/das/util/DriverMatcher.java
index 7bc8eea..f97c974 100644
--- a/deviceaccess/src/main/java/org/apache/felix/das/util/DriverMatcher.java
+++ b/deviceaccess/src/main/java/org/apache/felix/das/util/DriverMatcher.java
@@ -27,6 +27,7 @@
 
 import org.apache.felix.das.DriverAttributes;
 import org.apache.felix.das.Log;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.device.Constants;
 import org.osgi.service.device.DriverSelector;
@@ -109,9 +110,20 @@
 
     public Match selectBestMatch( ServiceReference deviceRef, DriverSelector selector )
     {
-        Match[] matches = m_matches.toArray( new Match[0] );
+//        Match[] matches = m_matches.toArray( new Match[0] );
+        
+        //(re)check bundle status
+        List<Match> activeMatches = new ArrayList<Match>();
+        for (Match match : m_matches) {
+            if (match.getDriver().getBundle().getState() == Bundle.ACTIVE) {
+                activeMatches.add(match);
+            } else {
+                m_log.debug("skipping: " + match + ", it's bundle is: " + match.getDriver().getBundle().getState());
+            }
+        }
         try
         {
+          Match[] matches = activeMatches.toArray( new Match[0] );
             int index = selector.select( deviceRef, matches );
             if ( index != DriverSelector.SELECT_NONE && index >= 0 && index < matches.length )
             {