Search in sources :

Example 1 with FieldInterceptor

use of org.apache.felix.ipojo.FieldInterceptor 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

ConfigurationException (org.apache.felix.ipojo.ConfigurationException)1 ConstructorInjector (org.apache.felix.ipojo.ConstructorInjector)1 FieldInterceptor (org.apache.felix.ipojo.FieldInterceptor)1 Element (org.apache.felix.ipojo.metadata.Element)1 FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)1 MethodMetadata (org.apache.felix.ipojo.parser.MethodMetadata)1 Callback (org.apache.felix.ipojo.util.Callback)1 BundleContext (org.osgi.framework.BundleContext)1