Search in sources :

Example 1 with Property

use of org.apache.felix.ipojo.util.Property in project felix by apache.

the class ConfigurationHandler method onCreation.

/**
 * Handler createInstance method.
 * This method is override to allow delayed callback invocation.
 * Invokes the updated method if needed.
 *
 * @param instance : the created object
 * @see org.apache.felix.ipojo.PrimitiveHandler#onCreation(Object)
 */
public void onCreation(Object instance) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    for (Property prop : m_configurableProperties) {
        if (prop.hasMethod()) {
            prop.invoke(instance);
        }
        // Fill the snapshot copy while calling callbacks
        map.put(prop.getName(), prop.getValue());
    }
    try {
        notifyUpdated(instance);
    } catch (Throwable e) {
        error("Cannot call the updated method : " + e.getMessage(), e);
    }
    notifyListeners(map);
}
Also used : Property(org.apache.felix.ipojo.util.Property)

Example 2 with Property

use of org.apache.felix.ipojo.util.Property in project felix by apache.

the class ConfigurationHandler method reconfigureProperties.

/**
 * Reconfigured configuration properties and returns non matching properties.
 * When called, it must hold the monitor lock.
 *
 * @param configuration : new configuration
 * @return the properties that does not match with configuration properties
 */
private Properties reconfigureProperties(Dictionary configuration) {
    Properties toPropagate = new Properties();
    Enumeration keysEnumeration = configuration.keys();
    while (keysEnumeration.hasMoreElements()) {
        String name = (String) keysEnumeration.nextElement();
        Object value = configuration.get(name);
        boolean found = false;
        // Check if the name is a configurable property
        for (Property prop : m_configurableProperties) {
            if (prop.getName().equals(name)) {
                Object v = reconfigureProperty(prop, value);
                found = true;
                if (m_mustPropagate && !excluded(name)) {
                    toPropagate.put(name, v);
                }
                // Exit the search loop
                break;
            }
        }
        if (!found && m_mustPropagate && !excluded(name)) {
            toPropagate.put(name, value);
        }
    }
    // Every removed configurable property gets reset to its default value
    for (Property prop : m_configurableProperties) {
        if (configuration.get(prop.getName()) == null) {
            reconfigureProperty(prop, prop.getDefaultValue());
        }
    }
    return toPropagate;
}
Also used : Property(org.apache.felix.ipojo.util.Property)

Example 3 with Property

use of org.apache.felix.ipojo.util.Property in project felix by apache.

the class ConfigurationHandler method detectConfigurationChanges.

private boolean detectConfigurationChanges(Dictionary configuration) {
    Enumeration keysEnumeration = configuration.keys();
    while (keysEnumeration.hasMoreElements()) {
        String name = (String) keysEnumeration.nextElement();
        Object value = configuration.get(name);
        // Some properties are skipped
        if (name.equals(Factory.INSTANCE_NAME_PROPERTY) || name.equals(Constants.SERVICE_PID) || name.equals(MANAGED_SERVICE_PID)) {
            continue;
        }
        // Do we have a property.
        Property p = getPropertyByName(name);
        if (p != null) {
            // Change detection based on the value.
            if (p.getValue() == null) {
                return true;
            } else if (!p.getValue().equals(value)) {
                return true;
            }
        } else {
            // Was it propagated ?
            if (m_propagatedFromCA != null) {
                Object v = m_propagatedFromCA.get(name);
                if (v == null || !v.equals(value)) {
                    return true;
                }
            }
            if (m_propagatedFromInstance != null) {
                Object v = m_propagatedFromInstance.get(name);
                if (v == null || !v.equals(value)) {
                    return true;
                }
            }
        }
    }
    // A propagated property may have been removed.
    if (m_propagatedFromCA != null) {
        Enumeration enumeration = m_propagatedFromCA.keys();
        while (enumeration.hasMoreElements()) {
            String k = (String) enumeration.nextElement();
            if (configuration.get(k) == null) {
                return true;
            }
        }
    }
    if (m_propagatedFromInstance != null) {
        Enumeration enumeration = m_propagatedFromInstance.keys();
        while (enumeration.hasMoreElements()) {
            String k = (String) enumeration.nextElement();
            if (configuration.get(k) == null) {
                return true;
            }
        }
    }
    return false;
}
Also used : Property(org.apache.felix.ipojo.util.Property)

Example 4 with Property

use of org.apache.felix.ipojo.util.Property in project felix by apache.

the class ConfigurationHandler method updated.

/**
 * Managed Service method.
 * This method is called when the instance is reconfigured by the ConfigurationAdmin.
 * When called, it must hold the monitor lock.
 *
 * @param conf : pushed configuration.
 * @throws org.osgi.service.cm.ConfigurationException
 *          the reconfiguration failed.
 * @see org.osgi.service.cm.ManagedService#updated(java.util.Dictionary)
 */
public void updated(Dictionary conf) throws org.osgi.service.cm.ConfigurationException {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    synchronized (this) {
        if (conf == null && !m_configurationAlreadyPushed) {
            // First call
            return;
        } else if (conf != null) {
            // Configuration push
            Properties props = reconfigureProperties(conf);
            propagate(props, m_propagatedFromCA);
            m_propagatedFromCA = props;
            m_configurationAlreadyPushed = true;
        } else if (m_configurationAlreadyPushed) {
            // Configuration deletion
            propagate(null, m_propagatedFromCA);
            m_propagatedFromCA = null;
            m_configurationAlreadyPushed = false;
        }
        if (getInstanceManager().getPojoObjects() != null) {
            try {
                notifyUpdated(null);
            } catch (Throwable e) {
                error("Cannot call the updated method : " + e.getMessage(), e);
            }
        }
        // Make a snapshot of the current configuration
        for (Property p : m_configurableProperties) {
            map.put(p.getName(), p.getValue());
        }
    }
    notifyListeners(map);
}
Also used : Property(org.apache.felix.ipojo.util.Property)

Example 5 with Property

use of org.apache.felix.ipojo.util.Property in project felix by apache.

the class ConfigurationHandler method notifyUpdated.

/**
 * Invokes the updated method.
 * This method build the dictionary containing all valued properties,
 * as well as properties propagated to the provided service handler (
 * only if the propagation is enabled).
 *
 * @param instance the instance on which the callback must be called.
 *                 If <code>null</code> the callback is called on all the existing
 *                 object.
 */
private void notifyUpdated(Object instance) {
    if (m_updated == null) {
        return;
    }
    if (m_updated.getArguments().length == 0) {
        // we just call the callback.
        try {
            if (instance == null) {
                m_updated.call(new Object[0]);
            } else {
                m_updated.call(instance, new Object[0]);
            }
        } catch (Exception e) {
            error("Cannot call the updated method " + m_updated.getMethod() + " : " + e.getMessage());
        }
        return;
    }
    // Else we must compute the properties.
    Properties props = new Properties();
    for (Property property : m_configurableProperties) {
        String n = property.getName();
        Object v = property.getValue();
        if (v != Property.NO_VALUE) {
            props.put(n, v);
        }
    }
    // add propagated properties to the list if propagation is enabled
    if (m_mustPropagate) {
        // Start by properties from the configuration admin,
        if (m_propagatedFromCA != null) {
            Enumeration e = m_propagatedFromCA.keys();
            while (e.hasMoreElements()) {
                String k = (String) e.nextElement();
                if (!k.equals(Factory.INSTANCE_NAME_PROPERTY)) {
                    props.put(k, m_propagatedFromCA.get(k));
                }
            }
        }
        // Do also the one from the instance configuration
        if (m_propagatedFromInstance != null) {
            Enumeration e = m_propagatedFromInstance.keys();
            while (e.hasMoreElements()) {
                String k = (String) e.nextElement();
                if (!k.equals(Factory.INSTANCE_NAME_PROPERTY)) {
                    // Skip instance.name
                    props.put(k, m_propagatedFromInstance.get(k));
                }
            }
        }
    }
    try {
        if (instance == null) {
            m_updated.call(new Object[] { props });
        } else {
            m_updated.call(instance, new Object[] { props });
        }
    } catch (Exception e) {
        error("Cannot call the updated method " + m_updated.getMethod() + " : " + e.getMessage());
    }
}
Also used : Property(org.apache.felix.ipojo.util.Property)

Aggregations

Property (org.apache.felix.ipojo.util.Property)14 Element (org.apache.felix.ipojo.metadata.Element)2 FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)2 MethodMetadata (org.apache.felix.ipojo.parser.MethodMetadata)2 Callback (org.apache.felix.ipojo.util.Callback)2 Properties (java.util.Properties)1 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)1 ServiceController (org.apache.felix.ipojo.handlers.providedservice.ProvidedService.ServiceController)1 BundleContext (org.osgi.framework.BundleContext)1