felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pde...@apache.org
Subject svn commit: r1573385 - /felix/sandbox/pderop/dependencymanager-prototype/dm/src/dm/impl/ConfigurationDependencyImpl.java
Date Sun, 02 Mar 2014 22:44:16 GMT
Author: pderop
Date: Sun Mar  2 22:44:15 2014
New Revision: 1573385

URL: http://svn.apache.org/r1573385
Log:
Finalized configuration dependency. Also, had to reintroduce the m_updateInvokedCache, because
this is actually necessary.

Modified:
    felix/sandbox/pderop/dependencymanager-prototype/dm/src/dm/impl/ConfigurationDependencyImpl.java

Modified: felix/sandbox/pderop/dependencymanager-prototype/dm/src/dm/impl/ConfigurationDependencyImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-prototype/dm/src/dm/impl/ConfigurationDependencyImpl.java?rev=1573385&r1=1573384&r2=1573385&view=diff
==============================================================================
--- felix/sandbox/pderop/dependencymanager-prototype/dm/src/dm/impl/ConfigurationDependencyImpl.java
(original)
+++ felix/sandbox/pderop/dependencymanager-prototype/dm/src/dm/impl/ConfigurationDependencyImpl.java
Sun Mar  2 22:44:15 2014
@@ -2,18 +2,56 @@ package dm.impl;
 
 import java.lang.reflect.InvocationTargetException;
 import java.util.Dictionary;
+import java.util.Properties;
+import java.util.concurrent.atomic.AtomicBoolean;
 
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.cm.ManagedService;
 
+import dm.ConfigurationDependency;
+import dm.PropertyMetaData;
+import dm.context.DependencyContext;
+import dm.impl.metatype.MetaTypeProviderImpl;
+
 // Todo implements some methods from DependencyImpl (createCopy, etc ...)
-public class ConfigurationDependencyImpl extends DependencyImpl implements ManagedService
{
-    private Dictionary m_settings;
+public class ConfigurationDependencyImpl extends DependencyImpl implements ConfigurationDependency,
ManagedService {
+    private Dictionary<?,?> m_settings;
     private String m_callback = "updated";
+	private final BundleContext m_context;
+	private final Logger m_logger;
+	private String m_pid;
+	private ServiceRegistration m_registration;
+    private MetaTypeProviderImpl m_metaType;
+    private boolean m_propagate;
+	private final AtomicBoolean m_updateInvokedCache = new AtomicBoolean();
 
     public ConfigurationDependencyImpl() {
+    	this(null, null);
+    }
+    
+    public ConfigurationDependencyImpl(BundleContext context, Logger logger) {
+    	super(false /* not autoconfig */);
+    	m_context = context;
+    	m_logger = logger;
         setRequired(true);
     }
+    
+	public ConfigurationDependencyImpl(ConfigurationDependencyImpl prototype) {
+	    super(prototype);
+	    m_context = prototype.m_context;
+	    m_pid = prototype.m_pid;
+	    m_propagate = prototype.m_propagate;
+	    m_callback = prototype.m_callback;
+	    m_logger = prototype.m_logger;
+	}
+	
+	@Override
+	public DependencyContext createCopy() {
+	    return new ConfigurationDependencyImpl(this);
+	}
 
     public ConfigurationDependencyImpl setCallback(String callback) {
         m_callback = callback;
@@ -21,6 +59,11 @@ public class ConfigurationDependencyImpl
     }
 
     @Override
+	public boolean isPropagated() {
+		return m_propagate;
+	}
+
+    @Override
     public boolean needsInstance() {
         return true;
     }
@@ -28,17 +71,94 @@ public class ConfigurationDependencyImpl
     @Override
     public void start() {
         super.start();
-        // Register our managed service in the service registry
+        if (m_context != null) { // If null, we are in a test environment
+	        Properties props = new Properties();
+	        props.put(Constants.SERVICE_PID, m_pid);
+	        ManagedService ms = this;
+	        if (m_metaType != null) {
+	            ms = m_metaType;
+	        }
+	        m_registration = m_context.registerService(ManagedService.class.getName(), ms, props);
+        }
     }
 
     @Override
     public void stop() {
         super.stop();
-        // Unregister our managed service from the service registry
+        if (m_registration != null) {
+        	m_registration.unregister();
+        	m_registration = null;
+        }
+    }
+    
+	public ConfigurationDependency setPid(String pid) {
+		// ensureNotActive(); TODO
+		m_pid = pid;
+		return this;
+	}
+
+	public ConfigurationDependency setPropagate(boolean propagate) {
+		// ensureNotActive(); TODO
+		m_propagate = propagate;
+		return this;
+	}
+		
+    public String toString() {
+    	return "ConfigurationDependency[" + m_pid + "]";
+    }
+    
+    public String getName() {
+        return m_pid;
+    }
+    
+    public String getType() {
+        return "configuration";
     }
 
     @Override
+    protected Object getService() {
+        return m_settings;
+    }
+        
+    public ConfigurationDependency add(PropertyMetaData properties)
+    {
+        createMetaTypeImpl();
+        m_metaType.add(properties);
+       return this;
+    }
+
+    public ConfigurationDependency setDescription(String description)
+    {
+        createMetaTypeImpl();
+        m_metaType.setDescription(description);
+       return this;
+    }
+
+    public ConfigurationDependency setHeading(String heading)
+    {
+        createMetaTypeImpl();
+        m_metaType.setName(heading);
+       return this;
+    }
+    
+    public ConfigurationDependency setLocalization(String path)
+    {
+        createMetaTypeImpl();
+        m_metaType.setLocalization(path);
+        return this;
+    }
+    
+	@Override
+	public Dictionary getProperties() {
+		if (m_settings == null) {
+            throw new IllegalStateException("cannot find configuration");
+		}
+		return m_settings;
+	}
+    
+    @Override
     public void updated(Dictionary settings) throws ConfigurationException {
+    	m_updateInvokedCache.set(false);
         Dictionary<?,?> oldSettings = null;
         synchronized (this) {
             oldSettings = m_settings;
@@ -81,7 +201,11 @@ public class ConfigurationDependencyImpl
     }
 
     public void invokeAdd() {
-        // We already did that synchronously, from our updated method
+		try {
+			invokeUpdated(m_settings);
+		} catch (ConfigurationException e) {
+			e.printStackTrace(); // FIXME use a LogService
+		}
     }
 
     public void invokeChange() {
@@ -89,45 +213,60 @@ public class ConfigurationDependencyImpl
     }
 
     public void invokeRemove() {
-        // The configuration has gone, so, the state machine has stopped the component,
-        // and all required dependencies must now be removed, including our configuration

-        // dependency.
+        // The state machine is stopping us. We have to invoke updated(null).
         try {
+        	m_updateInvokedCache.set(false);
             invokeUpdated(null);
         } catch (ConfigurationException e) {
             e.printStackTrace(); // FIXME use a LogService
+        } finally {
+        	// Reset for the next time the state machine calls invokeAdd
+        	m_updateInvokedCache.set(false);
         }
     }
     
-    private void invokeUpdated(Dictionary settings) throws ConfigurationException {
-        Object[] instances = m_component.getInstances();
-        if (instances != null) {
-            for (int i = 0; i < instances.length; i++) {
-                try {
-                    InvocationUtil.invokeCallbackMethod(instances[i], m_callback, 
-                        new Class[][] { { Dictionary.class }, {} }, 
-                        new Object[][] { { settings }, {} });
-                }
-
-                catch (InvocationTargetException e) {
-                    // The component has thrown an exception during it's callback invocation.
-                    if (e.getTargetException() instanceof ConfigurationException) {
-                        // the callback threw an OSGi ConfigurationException: just re-throw
it.
-                        throw (ConfigurationException) e.getTargetException();
-                    }
-                    else {
-                        // wrap the callback exception into a ConfigurationException.
-                        throw new ConfigurationException(null, "Configuration update failed",
e.getTargetException());
-                    }
-                }
-                catch (NoSuchMethodException e) {
-                    // if the method does not exist, ignore it
-                }
-                catch (Throwable t) {
-                    // wrap any other exception as a ConfigurationException.
-                    throw new ConfigurationException(null, "Configuration update failed",
t);
-                }
-            }
+    private void invokeUpdated(Dictionary<?,?> settings) throws ConfigurationException
{
+    	if (m_updateInvokedCache.compareAndSet(false, true)) {
+			Object[] instances = m_component.getInstances();
+			if (instances != null) {
+				for (int i = 0; i < instances.length; i++) {
+					try {
+						InvocationUtil.invokeCallbackMethod(instances[i],
+								m_callback, new Class[][] {
+										{ Dictionary.class }, {} },
+								new Object[][] { { settings }, {} });
+					}
+
+					catch (InvocationTargetException e) {
+						// The component has thrown an exception during it's
+						// callback invocation.
+						if (e.getTargetException() instanceof ConfigurationException) {
+							// the callback threw an OSGi
+							// ConfigurationException: just re-throw it.
+							throw (ConfigurationException) e
+									.getTargetException();
+						} else {
+							// wrap the callback exception into a
+							// ConfigurationException.
+							throw new ConfigurationException(null,
+									"Configuration update failed",
+									e.getTargetException());
+						}
+					} catch (NoSuchMethodException e) {
+						// if the method does not exist, ignore it
+					} catch (Throwable t) {
+						// wrap any other exception as a ConfigurationException.
+						throw new ConfigurationException(null,
+								"Configuration update failed", t);
+					}
+				}
+			}
+    	}
+    }
+    
+    private synchronized void createMetaTypeImpl() {
+        if (m_metaType == null) {
+            m_metaType = new MetaTypeProviderImpl(getName(), m_context, m_logger, this, null);
         }
     }
 }



Mime
View raw message