You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by pd...@apache.org on 2014/10/04 18:35:19 UTC

svn commit: r1629410 - in /felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl: ComponentImpl.java ConfigurationDependencyImpl.java FactoryConfigurationAdapterImpl.java

Author: pderop
Date: Sat Oct  4 16:35:18 2014
New Revision: 1629410

URL: http://svn.apache.org/r1629410
Log:
- Injects autoconfig fields before calling required dependency callbacks.
- Log exceptions caught when handling Configurarion Update.
- Removed some Eclipse warn.


Modified:
    felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ComponentImpl.java
    felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ConfigurationDependencyImpl.java
    felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/FactoryConfigurationAdapterImpl.java

Modified: felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ComponentImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ComponentImpl.java?rev=1629410&r1=1629409&r2=1629410&view=diff
==============================================================================
--- felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ComponentImpl.java (original)
+++ felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ComponentImpl.java Sat Oct  4 16:35:18 2014
@@ -24,12 +24,10 @@ import static org.apache.felix.dm.Compon
 import static org.apache.felix.dm.ComponentState.WAITING_FOR_REQUIRED;
 
 import java.lang.reflect.Constructor;
-import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -63,7 +61,7 @@ public class ComponentImpl implements Co
 			.newProxyInstance(ComponentImpl.class.getClassLoader(),
 					new Class[] { ServiceRegistration.class },
 					new DefaultNullObject());
-    private static final Class[] VOID = new Class[] {};
+    private static final Class<?>[] VOID = new Class[] {};
 	private volatile Executor m_executor = new SerialExecutor(new Logger(null));
 	private ComponentState m_state = ComponentState.INACTIVE;
 	private final CopyOnWriteArrayList<DependencyContext> m_dependencies = new CopyOnWriteArrayList<>();
@@ -75,10 +73,10 @@ public class ComponentImpl implements Co
     private Object m_componentDefinition;
 	private Object m_componentInstance;
     private volatile Object m_serviceName;
-    private volatile Dictionary m_serviceProperties;
+    private volatile Dictionary<String, ?> m_serviceProperties;
     private volatile ServiceRegistration m_registration;
-    private final Map m_autoConfig = new ConcurrentHashMap();
-    private final Map m_autoConfigInstance = new ConcurrentHashMap();
+    private final Map<Class<?>, Boolean> m_autoConfig = new ConcurrentHashMap<>();
+    private final Map<Class<?>, String> m_autoConfigInstance = new ConcurrentHashMap<>();
     private final long m_id;
     private static AtomicLong m_idGenerator = new AtomicLong();
     // Holds all the services of a given dependency context. Caution: the last entry in the skiplist is the highest ranked service.
@@ -166,7 +164,7 @@ public class ComponentImpl implements Co
 		getExecutor().execute(new Runnable() {
 			@Override
 			public void run() {
-				List<DependencyContext> instanceBoundDeps = new ArrayList();
+				List<DependencyContext> instanceBoundDeps = new ArrayList<>();
 				for (Dependency d : dependencies) {
 					DependencyContext dc = (DependencyContext) d;
 					m_dependencyEvents.put(dc,  new ConcurrentSkipListSet<Event>());
@@ -405,23 +403,23 @@ public class ComponentImpl implements Co
         return m_dependencyEvents.get(dc);
     }
 
-    public Component setAutoConfig(Class clazz, boolean autoConfig) {
+    public Component setAutoConfig(Class<?> clazz, boolean autoConfig) {
         m_autoConfig.put(clazz, Boolean.valueOf(autoConfig));
         return this;
     }
     
-    public Component setAutoConfig(Class clazz, String instanceName) {
+    public Component setAutoConfig(Class<?> clazz, String instanceName) {
         m_autoConfig.put(clazz, Boolean.valueOf(instanceName != null));
         m_autoConfigInstance.put(clazz, instanceName);
         return this;
     }
     
-    public boolean getAutoConfig(Class clazz) {
+    public boolean getAutoConfig(Class<?> clazz) {
         Boolean result = (Boolean) m_autoConfig.get(clazz);
         return (result != null && result.booleanValue());
     }
     
-    public String getAutoConfigInstance(Class clazz) {
+    public String getAutoConfigInstance(Class<?> clazz) {
         return (String) m_autoConfigInstance.get(clazz);
     }
 
@@ -494,8 +492,8 @@ public class ComponentImpl implements Co
 				System.out.println("*" + debugKey + " T" + Thread.currentThread().getId() + " instantiate!");
 			}
 			instantiateComponent();
+            invokeAutoConfigDependencies();
 			invokeAddRequiredDependencies();
-			invokeAutoConfigDependencies();
 			ComponentState stateBeforeCallingInit = m_state;
 	        invoke(m_callbackInit); 
 	        if (stateBeforeCallingInit == m_state) {
@@ -504,8 +502,8 @@ public class ComponentImpl implements Co
 			return true;
 		}
 		if (oldState == ComponentState.INSTANTIATED_AND_WAITING_FOR_REQUIRED && newState == ComponentState.TRACKING_OPTIONAL) {
+            invokeAutoConfigInstanceBoundDependencies();
 			invokeAddRequiredInstanceBoundDependencies();
-			invokeAutoConfigInstanceBoundDependencies();
 			invoke(m_callbackStart);
 			invokeAddOptionalDependencies();
 			registerService();
@@ -596,7 +594,7 @@ public class ComponentImpl implements Co
     	if (debug) {
     		System.out.println("*" + debugKey + " T" + Thread.currentThread().getId() + " startDependencies.");
     	}
-        List<DependencyContext> requiredDeps = new ArrayList();
+        List<DependencyContext> requiredDeps = new ArrayList<>();
         for (DependencyContext d : dependencies) {
             if (d.isRequired()) {
                 requiredDeps.add(d);
@@ -636,7 +634,7 @@ public class ComponentImpl implements Co
             ServiceRegistration registration;
 
             // determine service properties
-            Dictionary properties = calculateServiceProperties();
+            Dictionary<?,?> properties = calculateServiceProperties();
 
             // register the service
             try {
@@ -872,7 +870,7 @@ public class ComponentImpl implements Co
     	return getCompositionInstances();
     }
     
-    public void invokeCallbackMethod(Object[] instances, String methodName, Class[][] signatures, Object[][] parameters) {
+    public void invokeCallbackMethod(Object[] instances, String methodName, Class<?>[][] signatures, Object[][] parameters) {
         for (int i = 0; i < instances.length; i++) {
             try {
                 InvocationUtil.invokeCallbackMethod(instances[i], methodName, signatures, parameters);
@@ -890,8 +888,8 @@ public class ComponentImpl implements Co
         }
     }
 
-    private Object createInstance(Class clazz) throws SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
-		Constructor constructor = clazz.getConstructor(VOID);
+    private Object createInstance(Class<?> clazz) throws SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+		Constructor<?> constructor = clazz.getConstructor(VOID);
 		constructor.setAccessible(true);
         return constructor.newInstance(null);
     }
@@ -902,10 +900,6 @@ public class ComponentImpl implements Co
 		}
 	}
 	
-	private void notifyListener(ComponentStateListener l, ComponentState state) {
-	    l.changed(this, state);
-	}
-
 	public boolean isAvailable() {
 		return m_state == TRACKING_OPTIONAL;
 	}
@@ -922,7 +916,8 @@ public class ComponentImpl implements Co
 	    return this;
 	}
 
-	@Override
+	@SuppressWarnings("unchecked")
+    @Override
 	public List<DependencyContext> getDependencies() {
 		return (List<DependencyContext>) m_dependencies.clone();
 	}
@@ -933,7 +928,7 @@ public class ComponentImpl implements Co
 		return this;
 	}
 	
-    private void configureImplementation(Class clazz, Object instance) {
+    private void configureImplementation(Class<?> clazz, Object instance) {
         configureImplementation(clazz, instance, null);
     }
 
@@ -946,12 +941,12 @@ public class ComponentImpl implements Co
      * @param inject the object to fill in the implementation class(es) field
      * @param instanceName the name of the instance to fill in, or <code>null</code> if not used
      */
-    private void configureImplementation(Class clazz, Object inject, String fieldName) {
+    private void configureImplementation(Class<?> clazz, Object inject, String fieldName) {
         Object[] targets = getInstances();
         FieldUtil.injectField(targets, fieldName, clazz, inject, m_logger);
     }
 
-    private void configureImplementation(Class clazz, DependencyContext dc, String fieldName) {
+    private void configureImplementation(Class<?> clazz, DependencyContext dc, String fieldName) {
         Object[] targets = getInstances();
         FieldUtil.injectDependencyField(targets, fieldName, clazz, dc, m_logger);
     }
@@ -968,7 +963,7 @@ public class ComponentImpl implements Co
      * @param add true if the dependency service has been added, false if it has been removed. This flag is 
      *        ignored if the "update" flag is true (because the dependency properties are just being updated).
      */
-    private void updateImplementation(Class clazz, DependencyContext dc, String fieldName, Event event, boolean update,
+    private void updateImplementation(Class<?> clazz, DependencyContext dc, String fieldName, Event event, boolean update,
         boolean add)
     {
         Object[] targets = getInstances();
@@ -1083,7 +1078,7 @@ public class ComponentImpl implements Co
 	}
 	
     public ComponentDependencyDeclaration[] getComponentDependencies() {
-        List deps = getDependencies();
+        List<DependencyContext> deps = getDependencies();
         if (deps != null) {
             ComponentDependencyDeclaration[] result = new ComponentDependencyDeclaration[deps.size()];
             for (int i = 0; i < result.length; i++) {
@@ -1119,7 +1114,7 @@ public class ComponentImpl implements Co
             Object implementation = m_componentDefinition;
             if (implementation != null) {
                 if (implementation instanceof Class) {
-                    sb.append(((Class) implementation).getName());
+                    sb.append(((Class<?>) implementation).getName());
                 } else {
                     // If the implementation instance does not override "toString", just display
                     // the class name, else display the component using its toString method
@@ -1146,7 +1141,7 @@ public class ComponentImpl implements Co
         Dictionary properties = calculateServiceProperties();
         if (properties != null) {
             result.append("(");
-            Enumeration enumeration = properties.keys();
+            Enumeration<?> enumeration = properties.keys();
             while (enumeration.hasMoreElements()) {
                 Object key = enumeration.nextElement();
                 result.append(key.toString());
@@ -1243,4 +1238,8 @@ public class ComponentImpl implements Co
         m_executor = new DispatchExecutor(threadPool, m_logger);
         return this;
     }
+    
+    public Logger getLogger() {
+        return m_logger;
+    }
 }

Modified: felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ConfigurationDependencyImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ConfigurationDependencyImpl.java?rev=1629410&r1=1629409&r2=1629410&view=diff
==============================================================================
--- felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ConfigurationDependencyImpl.java (original)
+++ felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ConfigurationDependencyImpl.java Sat Oct  4 16:35:18 2014
@@ -21,7 +21,6 @@ package org.apache.felix.dm.impl;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Dictionary;
 import java.util.Properties;
-import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.felix.dm.ConfigurationDependency;
@@ -173,7 +172,12 @@ public class ConfigurationDependencyImpl
         if (settings != null) {
             Object[] instances = m_component.getInstances();
             if (instances != null) {
-                invokeUpdated(settings);
+                try {
+                    invokeUpdated(settings);
+                } catch (ConfigurationException e) {
+                    logConfigurationException(e);
+                    throw e;
+                }
             }
         }
         
@@ -203,7 +207,7 @@ public class ConfigurationDependencyImpl
 		try {
 			invokeUpdated(m_settings);
 		} catch (ConfigurationException e) {
-			e.printStackTrace(); // FIXME use a LogService
+            logConfigurationException(e);
 		}
     }
 
@@ -219,7 +223,7 @@ public class ConfigurationDependencyImpl
         	m_updateInvokedCache.set(false);
             invokeUpdated(null);
         } catch (ConfigurationException e) {
-            e.printStackTrace(); // FIXME use a LogService
+            logConfigurationException(e);
         } finally {
         	// Reset for the next time the state machine calls invokeAdd
         	m_updateInvokedCache.set(false);
@@ -270,4 +274,10 @@ public class ConfigurationDependencyImpl
             m_metaType = new MetaTypeProviderImpl(getName(), m_context, m_logger, this, null);
         }
     }
+    
+    private void logConfigurationException(ConfigurationException e) {
+        if (m_logger != null) {
+            m_logger.log(Logger.LOG_ERROR, "Got exception while handling configuration update for pid " + m_pid, e);
+        }
+    }
 }

Modified: felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/FactoryConfigurationAdapterImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/FactoryConfigurationAdapterImpl.java?rev=1629410&r1=1629409&r2=1629410&view=diff
==============================================================================
--- felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/FactoryConfigurationAdapterImpl.java (original)
+++ felix/sandbox/pderop/dependencymanager-prototype/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/FactoryConfigurationAdapterImpl.java Sat Oct  4 16:35:18 2014
@@ -46,9 +46,13 @@ public class FactoryConfigurationAdapter
     // Our Managed Service Factory PID
     protected final String m_factoryPid;
     
+    // Our logger
+    protected final Logger m_logger;
+    
     public FactoryConfigurationAdapterImpl(DependencyManager dm, String factoryPid, String update, boolean propagate) {
         super(dm.createComponent()); // This service will be filtered by our super class, allowing us to take control.
         m_factoryPid = factoryPid;
+        m_logger = ((ComponentImpl) m_component).getLogger();
 
         Hashtable props = new Hashtable();
         props.put(Constants.SERVICE_PID, factoryPid);
@@ -62,6 +66,7 @@ public class FactoryConfigurationAdapter
         BundleContext bctx, Logger logger, String heading, String description, String localization, PropertyMetaData[] properyMetaData) {
         super(dm.createComponent()); // This service will be filtered by our super class, allowing us to take control.
         m_factoryPid = factoryPid;
+        m_logger = logger;
         Hashtable props = new Hashtable();
         props.put(Constants.SERVICE_PID, factoryPid);
         m_component
@@ -159,7 +164,7 @@ public class FactoryConfigurationAdapter
             Dictionary cmSettings = (Dictionary) properties[0];
             Component service = (Component) properties[1];
             Object impl = service.getInstances()[0];
-           
+
             try {
                 InvocationUtil.invokeCallbackMethod(impl, m_update, 
                     new Class[][] {{ Dictionary.class }, {}}, 
@@ -244,6 +249,7 @@ public class FactoryConfigurationAdapter
         }
     
         private void handleException(Throwable t) {
+            m_logger.log(Logger.LOG_ERROR, "Got exception while handling configuration update for factory pid " + m_factoryPid, t);
             if (t instanceof InvocationTargetException) {
                 // Our super class will check if the target exception is itself a ConfigurationException.
                 // In this case, it will simply re-thrown.