Search in sources :

Example 6 with TargetedPID

use of org.apache.felix.scr.impl.metadata.TargetedPID in project felix by apache.

the class ConfigurableComponentHolder method configurationUpdated.

/**
 * Configures a component with the given configuration. This configuration
 * update may happen in various situations:
 * <ul>
 * <li>The <code>pid</code> equals the component name. Hence we have a
 * singleton configuration for the single component held by this holder</li>
 * <li>The configuration is a factory configuration and is the first
 * configuration provided. In this case the single component is provided
 * with the configuration and also stored in the map.</li>
 * <li>The configuration is a factory configuration but not the first. In
 * this case a new component is created, configured and stored in the map</li>
 * </ul>
 * @return true if a new configuration was created, false otherwise. //TODO there are now 3 states..... still not satisfied, existing, and new
 */
public boolean configurationUpdated(TargetedPID pid, TargetedPID factoryPid, final Dictionary<String, Object> props, long changeCount) {
    log(LogService.LOG_DEBUG, "ConfigurableComponentHolder configuration updated for pid {0} with properties {1} and change count {2}", new Object[] { pid, props, changeCount }, null);
    // component to update or create
    final Map<AbstractComponentManager<S>, Map<String, Object>> scms = new HashMap<AbstractComponentManager<S>, Map<String, Object>>();
    boolean created = false;
    synchronized (m_components) {
        // Find or create the component manager, or return if not satisfied.
        if (factoryPid != null) {
            checkFactoryPidIndex(factoryPid);
            Long oldChangeCount = m_factoryChangeCount.get(pid.getServicePid());
            TargetedPID oldTargetedPID = m_factoryTargetedPids.get(pid.getServicePid());
            if (oldChangeCount != null && changeCount <= oldChangeCount && factoryPid.equals(oldTargetedPID)) {
                return false;
            }
            m_factoryChangeCount.put(pid.getServicePid(), changeCount);
            m_factoryConfigurations.put(pid.getServicePid(), props);
            m_factoryTargetedPids.put(pid.getServicePid(), factoryPid);
            if (m_enabled && isSatisfied()) {
                if (m_singleComponent != null && !m_componentMetadata.isObsoleteFactoryComponentFactory()) {
                    AbstractComponentManager<S> scm = m_singleComponent;
                    scms.put(scm, mergeProperties(pid.getServicePid()));
                    m_singleComponent = null;
                    m_components.put(pid.getServicePid(), scm);
                } else if (m_components.containsKey(pid.getServicePid())) {
                    scms.put(m_components.get(pid.getServicePid()), mergeProperties(pid.getServicePid()));
                } else {
                    AbstractComponentManager<S> scm = createComponentManager(true);
                    m_components.put(pid.getServicePid(), scm);
                    scms.put(scm, mergeProperties(pid.getServicePid()));
                    created = true;
                }
            } else {
                return false;
            }
        } else {
            // singleton pid
            int index = getSingletonPidIndex(pid);
            if (m_changeCount[index] != null && changeCount <= m_changeCount[index] && pid.equals(m_targetedPids[index])) {
                return false;
            }
            m_changeCount[index] = changeCount;
            m_targetedPids[index] = pid;
            m_configurations[index] = props;
            if (m_enabled && isSatisfied()) {
                if (m_singleComponent != null) {
                    scms.put(m_singleComponent, mergeProperties(pid.getServicePid()));
                } else if (m_factoryPidIndex != null) {
                    for (Map.Entry<String, AbstractComponentManager<S>> entry : m_components.entrySet()) {
                        scms.put(entry.getValue(), mergeProperties(entry.getKey()));
                    }
                } else {
                    m_singleComponent = createComponentManager(false);
                    scms.put(m_singleComponent, mergeProperties(pid.getServicePid()));
                    created = true;
                }
            } else {
                return false;
            }
        }
    }
    // we have the icm.
    // properties is all the configs merged together (without any possible component factory info.
    // TODO WTF?? && getComponentMetadata().isEnabled();
    final boolean enable = created && m_enabled;
    for (Map.Entry<AbstractComponentManager<S>, Map<String, Object>> entry : scms.entrySet()) {
        // configure the component
        entry.getKey().reconfigure(entry.getValue(), false, factoryPid);
        log(LogService.LOG_DEBUG, "ImmediateComponentHolder Finished configuring the dependency managers for component for pid {0} ", new Object[] { pid }, null);
        if (enable) {
            entry.getKey().enable(false);
            log(LogService.LOG_DEBUG, "ImmediateComponentHolder Finished enabling component for pid {0} ", new Object[] { pid }, null);
        } else {
            log(LogService.LOG_DEBUG, "ImmediateComponentHolder Will not enable component for pid {0}: holder enabled state: {1}, metadata enabled: {2} ", new Object[] { pid, m_enabled, m_componentMetadata.isEnabled() }, null);
        }
    }
    return created;
}
Also used : HashMap(java.util.HashMap) TargetedPID(org.apache.felix.scr.impl.metadata.TargetedPID) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

TargetedPID (org.apache.felix.scr.impl.metadata.TargetedPID)6 Dictionary (java.util.Dictionary)2 Hashtable (java.util.Hashtable)2 Map (java.util.Map)2 ComponentMetadata (org.apache.felix.scr.impl.metadata.ComponentMetadata)2 BundleContext (org.osgi.framework.BundleContext)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Configuration (org.osgi.service.cm.Configuration)1 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)1 Deferred (org.osgi.util.promise.Deferred)1 Promise (org.osgi.util.promise.Promise)1