Search in sources :

Example 11 with Property

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

the class ConfigurationHandler method start.

/**
 * Start method.
 * This method is synchronized to avoid the config admin pushing a configuration before ending the method.
 * Propagate properties if the propagation is activated.
 *
 * @see org.apache.felix.ipojo.Handler#start()
 */
public synchronized void start() {
    // Get the provided service handler :
    m_providedServiceHandler = (ProvidedServiceHandler) getHandler(HandlerFactory.IPOJO_NAMESPACE + ":provides");
    // Propagation
    if (m_mustPropagate) {
        for (Property prop : m_configurableProperties) {
            if (prop.getValue() != Property.NO_VALUE && prop.getValue() != null) {
                // No injected value, or null
                m_toPropagate.put(prop.getName(), prop.getValue());
            }
        }
        // We cannot use the reconfigure method directly, as there are no real changes.
        Properties extra = reconfigureProperties(m_toPropagate);
        propagate(extra, m_propagatedFromInstance);
        m_propagatedFromInstance = extra;
        if (getInstanceManager().getPojoObjects() != null) {
            try {
                notifyUpdated(null);
            } catch (Throwable e) {
                error("Cannot call the updated method : " + e.getMessage(), e);
            }
        }
    }
    // Give initial values and reset the 'invoked' flag.
    for (Property prop : m_configurableProperties) {
        // Clear the invoked flag.
        prop.reset();
        if (prop.hasField() && prop.getValue() != Property.NO_VALUE && prop.getValue() != null) {
            getInstanceManager().onSet(null, prop.getField(), prop.getValue());
        }
    }
    if (m_managedServicePID != null && m_sr == null) {
        Hashtable<String, Object> props = new Hashtable<String, Object>();
        props.put(Constants.SERVICE_PID, m_managedServicePID);
        props.put(Factory.INSTANCE_NAME_PROPERTY, getInstanceManager().getInstanceName());
        props.put("factory.name", getInstanceManager().getFactory().getFactoryName());
        // Security Check
        if (SecurityHelper.hasPermissionToRegisterService(ManagedService.class.getName(), getInstanceManager().getContext()) && SecurityHelper.canRegisterService(getInstanceManager().getContext())) {
            m_sr = getInstanceManager().getContext().registerService(ManagedService.class.getName(), this, props);
        } else {
            error("Cannot register the ManagedService - The bundle " + getInstanceManager().getContext().getBundle().getBundleId() + " does not have the permission to register the service");
        }
    }
}
Also used : Property(org.apache.felix.ipojo.util.Property)

Example 12 with Property

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

the class ConfigurationHandler method reconfigure.

/**
 * Reconfigure the component instance.
 * Check if the new configuration modifies the current configuration.
 * Invokes the updated method if needed.
 *
 * @param configuration : the new configuration
 * @see org.apache.felix.ipojo.Handler#reconfigure(java.util.Dictionary)
 */
public void reconfigure(Dictionary configuration) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    boolean changed = false;
    synchronized (this) {
        info(getInstanceManager().getInstanceName() + " is reconfiguring the properties : " + configuration);
        // Is there any changes ?
        changed = detectConfigurationChanges(configuration);
        if (changed) {
            Properties extra = reconfigureProperties(configuration);
            propagate(extra, m_propagatedFromInstance);
            m_propagatedFromInstance = extra;
            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());
            }
        }
    }
    if (changed) {
        notifyListeners(map);
    }
}
Also used : Property(org.apache.felix.ipojo.util.Property)

Example 13 with Property

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

the class ProvidedService method addProperties.

/**
 * Add properties to the list.
 *
 * @param props : properties to add
 */
protected void addProperties(Dictionary props) {
    Enumeration keys = props.keys();
    boolean updated = false;
    while (keys.hasMoreElements()) {
        String key = (String) keys.nextElement();
        Object value = props.get(key);
        // m_properties can be modified by another thread, we need to make a stack confinement
        Property property;
        synchronized (this) {
            property = m_properties.get(key);
        }
        if (property != null) {
            // Already existing property
            if (property.getValue() == null || !value.equals(property.getValue())) {
                property.setValue(value);
                updated = true;
            }
        } else {
            try {
                // Create the property.
                property = new Property(key, null, null, value, getInstanceManager(), m_handler);
                addProperty(property);
                updated = true;
            } catch (ConfigurationException e) {
                m_handler.error("The propagated property " + key + " cannot be created correctly : " + e.getMessage());
            }
        }
    }
    if (updated) {
        m_handler.info("Update triggered by adding properties " + props);
        update();
    }
}
Also used : Property(org.apache.felix.ipojo.util.Property)

Example 14 with Property

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

the class ProvidedServiceHandler method reconfigure.

/**
 * Reconfigure provided service.
 * @param dict : the new instance configuration.
 * @see org.apache.felix.ipojo.Handler#reconfigure(java.util.Dictionary)
 */
public void reconfigure(Dictionary dict) {
    for (int j = 0; j < getProvidedServices().length; j++) {
        ProvidedService svc = getProvidedServices()[j];
        Property[] props = svc.getProperties();
        boolean update = false;
        for (Property prop : props) {
            final Object receivedValue = dict.get(prop.getName());
            if (receivedValue != null) {
                update = true;
                prop.setValue(receivedValue);
            }
        }
        if (update) {
            svc.update();
        }
    }
}
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