Search in sources :

Example 6 with Callback

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

the class ConfigurationHandler method configure.

/**
 * Configures the handler.
 * Access to field does not require synchronization as this method is executed
 * before any thread access to this object.
 *
 * @param metadata      the metadata of the component
 * @param configuration the instance configuration
 * @throws ConfigurationException one property metadata is not correct
 * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
 */
public void configure(Element metadata, Dictionary configuration) throws ConfigurationException {
    // Build the map
    Element[] confs = metadata.getElements("Properties", "");
    Element[] configurables = confs[0].getElements("Property");
    // Check if the component is dynamically configurable
    // Propagation enabled by default.
    m_mustPropagate = true;
    // We must create a copy as the Config Admin dictionary has some limitations
    m_toPropagate = new Hashtable<String, Object>();
    if (configuration != null) {
        Enumeration keys = configuration.keys();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            // we don't propagate properties starting with .
            if (!excluded(key)) {
                m_toPropagate.put(key, configuration.get(key));
            }
        }
    }
    String propa = confs[0].getAttribute("propagation");
    if (propa != null && propa.equalsIgnoreCase("false")) {
        m_mustPropagate = false;
        m_toPropagate = null;
    }
    // Check if the component support ConfigurationADmin reconfiguration
    // Look inside the component type description
    m_managedServicePID = confs[0].getAttribute("pid");
    // Look inside the instance configuration.
    String instanceMSPID = (String) configuration.get(MANAGED_SERVICE_PID);
    if (instanceMSPID != null) {
        m_managedServicePID = instanceMSPID;
    }
    // updated method
    String upd = confs[0].getAttribute("updated");
    if (upd != null) {
        MethodMetadata method = getPojoMetadata().getMethod(upd);
        if (method == null) {
            throw new ConfigurationException("The updated method is not found in the class " + getInstanceManager().getClassName());
        } else if (method.getMethodArguments().length == 0) {
            m_updated = new Callback(upd, new Class[0], false, getInstanceManager());
        } else if (method.getMethodArguments().length == 1 && method.getMethodArguments()[0].equals(Dictionary.class.getName())) {
            m_updated = new Callback(upd, new Class[] { Dictionary.class }, false, getInstanceManager());
        } else {
            throw new ConfigurationException("The updated method is found in the class " + getInstanceManager().getClassName() + " must have either no argument or a Dictionary");
        }
    }
    for (int i = 0; configurables != null && i < configurables.length; i++) {
        String fieldName = configurables[i].getAttribute("field");
        String methodName = configurables[i].getAttribute("method");
        String paramIndex = configurables[i].getAttribute("constructor-parameter");
        int index = -1;
        // The initialize method has fixed the property name.
        String name = configurables[i].getAttribute("name");
        String value = configurables[i].getAttribute("value");
        // The initialize method has fixed the property name.
        String type = configurables[i].getAttribute("type");
        Property prop;
        if (paramIndex == null) {
            prop = new Property(name, fieldName, methodName, value, type, getInstanceManager(), this);
        } else {
            index = Integer.parseInt(paramIndex);
            prop = new Property(name, fieldName, methodName, index, value, type, getInstanceManager(), this);
        }
        addProperty(prop);
        // Check if the instance configuration contains value for the current property :
        if (configuration.get(name) == null) {
            if (fieldName != null && configuration.get(fieldName) != null) {
                prop.setValue(configuration.get(fieldName));
            }
        } else {
            prop.setValue(configuration.get(name));
        }
        if (fieldName != null) {
            FieldMetadata field = new FieldMetadata(fieldName, type);
            getInstanceManager().register(field, prop);
        }
        if (index != -1) {
            getInstanceManager().register(index, prop);
        }
    }
    m_description = new ConfigurationHandlerDescription(this, m_configurableProperties, m_managedServicePID);
}
Also used : FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) Element(org.apache.felix.ipojo.metadata.Element) Callback(org.apache.felix.ipojo.util.Callback) MethodMetadata(org.apache.felix.ipojo.parser.MethodMetadata) Property(org.apache.felix.ipojo.util.Property)

Example 7 with Callback

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

the class BundleContextHandler method configure.

/**
 * Configures the handler.
 * This method collects all `context` element.
 *
 * @param metadata      the metadata of the component
 * @param configuration the instance configuration
 * @throws org.apache.felix.ipojo.ConfigurationException if the metadata are not correct.
 */
@Override
public void configure(Element metadata, Dictionary configuration) throws ConfigurationException {
    Element[] contexts = metadata.getElements("context");
    for (Element element : contexts) {
        BundleContext bc = getBundleContextForConfiguration(element);
        if (element.containsAttribute("constructor-parameter")) {
            String idx = element.getAttribute("constructor-parameter");
            int index = Integer.parseInt(idx);
            final BundleContext injected = bc;
            getLogger().log(Log.DEBUG, "Registering bundle context injection for index " + index + " for instance" + " " + getInstanceManager().getInstanceName());
            getInstanceManager().register(index, new ConstructorInjector() {

                public Object getConstructorParameter(int index) {
                    return injected;
                }

                public Class getConstructorParameterType(int index) {
                    return BundleContext.class;
                }
            });
        } else if (element.containsAttribute("field")) {
            String field = element.getAttribute("field");
            final BundleContext injected = bc;
            FieldMetadata fm = getFactory().getPojoMetadata().getField(field);
            if (fm == null) {
                throw new ConfigurationException("Cannot inject the bundle context in the field " + field + " - " + "reason: the field does not exist in " + getInstanceManager().getClassName());
            }
            if (!BundleContext.class.getName().equals(fm.getFieldType())) {
                throw new ConfigurationException("Cannot inject the bundle context in the field " + field + " - " + "reason: the field " + field + " from " + getInstanceManager().getClassName() + " is not " + "from the BundleContext type");
            }
            getInstanceManager().register(fm, new FieldInterceptor() {

                public void onSet(Object pojo, String fieldName, Object value) {
                // Do nothing.
                }

                public Object onGet(Object pojo, String fieldName, Object value) {
                    return injected;
                }
            });
        } else if (element.containsAttribute("method")) {
            String method = element.getAttribute("method");
            MethodMetadata mm = getFactory().getPojoMetadata().getMethod(method, new String[] { BundleContext.class.getName() });
            if (mm == null) {
                getLogger().log(Log.WARNING, "Cannot find the method " + method + " in the class " + getInstanceManager().getClassName() + ", super classes lookup will be attempted");
            }
            Callback callback = new Callback(method, new Class[] { BundleContext.class }, false, getInstanceManager());
            m_methods.add(new BundleCallback(callback, bc));
        }
    }
}
Also used : FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) Element(org.apache.felix.ipojo.metadata.Element) FieldInterceptor(org.apache.felix.ipojo.FieldInterceptor) Callback(org.apache.felix.ipojo.util.Callback) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) ConstructorInjector(org.apache.felix.ipojo.ConstructorInjector) MethodMetadata(org.apache.felix.ipojo.parser.MethodMetadata) BundleContext(org.osgi.framework.BundleContext)

Aggregations

Callback (org.apache.felix.ipojo.util.Callback)7 Element (org.apache.felix.ipojo.metadata.Element)5 MethodMetadata (org.apache.felix.ipojo.parser.MethodMetadata)5 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)4 FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Property (org.apache.felix.ipojo.util.Property)2 ArrayList (java.util.ArrayList)1 MBeanException (javax.management.MBeanException)1 ReflectionException (javax.management.ReflectionException)1 ConstructorInjector (org.apache.felix.ipojo.ConstructorInjector)1 FieldInterceptor (org.apache.felix.ipojo.FieldInterceptor)1 PojoMetadata (org.apache.felix.ipojo.parser.PojoMetadata)1 BundleContext (org.osgi.framework.BundleContext)1 Filter (org.osgi.framework.Filter)1 Event (org.osgi.service.event.Event)1