Some type safety in ManagedService[Factory] service configuration

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@602844 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/configadmin/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java b/configadmin/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java
index c59cc80..c9ee8b6 100644
--- a/configadmin/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java
+++ b/configadmin/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java
@@ -1,4 +1,4 @@
-/* 
+/*
  * 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
@@ -86,7 +86,7 @@
  * this property is not set the <code>config</code> directory in the current
  * working directory as specified in the <code>user.dir</code> system property
  * is used.
- * 
+ *
  * @author fmeschbe
  */
 public class ConfigurationManager implements BundleActivator, BundleListener
@@ -95,14 +95,14 @@
     /**
      * The name of the bundle context property defining the location for the
      * configuration files (value is "felix.cm.dir").
-     * 
+     *
      * @see #FilePersistenceManager(BundleContext)
      */
     public static final String CM_CONFIG_DIR = "felix.cm.dir";
 
     // The name of the LogService (not using the class, which might be missing)
     private static final String LOG_SERVICE_NAME = "org.osgi.service.log.LogService";
-    
+
     // random number generator to create configuration PIDs for factory
     // configurations
     private static SecureRandom numberGenerator;
@@ -115,7 +115,7 @@
 
     // the service registration of the configuration admin
     private ServiceRegistration configurationAdminRegistration;
-    
+
     // the service reference to the ConfigurationAdmin service
     private ServiceReference configurationAdminReference;
 
@@ -221,7 +221,7 @@
         // immediately unregister the Configuration Admin before cleaning up
         configurationAdminRegistration.unregister();
         configurationAdminRegistration = null;
-        
+
         // stop handling ManagedService[Factory] services
         managedServiceFactoryTracker.close();
         managedServiceTracker.close();
@@ -312,7 +312,7 @@
 
     /*
      * (non-Javadoc)
-     * 
+     *
      * @see org.osgi.service.cm.ConfigurationAdmin#createFactoryConfiguration(java.lang.String)
      */
     ConfigurationImpl createFactoryConfiguration( ConfigurationAdminImpl configurationAdmin, String factoryPid )
@@ -341,7 +341,7 @@
 
     /*
      * (non-Javadoc)
-     * 
+     *
      * @see org.osgi.service.cm.ConfigurationAdmin#createFactoryConfiguration(java.lang.String,
      *      java.lang.String)
      */
@@ -367,7 +367,7 @@
         {
             return config;
         }
-        
+
         PersistenceManager[] pmList = getPersistenceManagers();
         for ( int i = 0; i < pmList.length; i++ )
         {
@@ -378,12 +378,12 @@
                 return cacheConfiguration( config );
             }
         }
-        
+
         // neither the cache nor any persistence manager has configuration
         return null;
     }
-    
-    
+
+
     ConfigurationImpl getConfiguration( String pid, String bundleLocation ) throws IOException
     {
         // check for existing (cached or persistent) configuration
@@ -652,7 +652,7 @@
      * Calls the registered configuration plugins on the given configuration
      * object unless the configuration has just been created and not been
      * updated yet.
-     * 
+     *
      * @param sr The service reference of the managed service (factory) which
      *            is to be updated with configuration
      * @param cfg The configuration object whose properties have to be passed
@@ -664,7 +664,7 @@
     private Dictionary callPlugins( ServiceReference sr, ConfigurationImpl cfg )
     {
         Dictionary props = cfg.getProperties();
-        
+
         // guard against NPE for new configuration never updated
         if (props == null) {
             return null;
@@ -723,7 +723,7 @@
 
     /**
      * Creates a PID for the given factoryPid
-     * 
+     *
      * @param factoryPid
      * @return
      */
@@ -833,7 +833,7 @@
                 log( LogService.LOG_ERROR, "Error loading configuration for " + pid, ioe );
                 return;
             }
-            
+
             // this will be set below to be given to the service
             Dictionary dictionary;
 
@@ -1251,7 +1251,7 @@
         private int type;
 
         private String pid;
-        
+
         private String factoryPid;
 
 
@@ -1325,15 +1325,19 @@
 
         public Object addingService( ServiceReference reference )
         {
-            ManagedService service = ( ManagedService ) super.addingService( reference );
+            Object serviceObject = super.addingService( reference );
 
             // configure the managed service
-            if ( service != null )
+            if ( serviceObject instanceof ManagedService )
             {
-                configure( reference, service );
+                configure( reference, ( ManagedService ) serviceObject );
+            }
+            else
+            {
+                log( LogService.LOG_WARNING, "Service " + serviceObject + " is not a ManagedService", null );
             }
 
-            return service;
+            return serviceObject;
         }
     }
 
@@ -1347,15 +1351,19 @@
 
         public Object addingService( ServiceReference reference )
         {
-            ManagedServiceFactory service = ( ManagedServiceFactory ) super.addingService( reference );
+            Object serviceObject = super.addingService( reference );
 
-            // configure the managed service
-            if ( service != null )
+            // configure the managed service factory
+            if ( serviceObject instanceof ManagedServiceFactory )
             {
-                configure( reference, service );
+                configure( reference, ( ManagedServiceFactory ) serviceObject );
+            }
+            else
+            {
+                log( LogService.LOG_WARNING, "Service " + serviceObject + " is not a ManagedServiceFactory", null );
             }
 
-            return service;
+            return serviceObject;
         }
     }
 }