Search in sources :

Example 11 with FieldMetadata

use of org.apache.felix.ipojo.parser.FieldMetadata 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)

Example 12 with FieldMetadata

use of org.apache.felix.ipojo.parser.FieldMetadata in project felix by apache.

the class ControllerHandler method configure.

/**
 * Configure method.
 * Look for the first 'controller' element.
 * @param metadata : metadata
 * @param configuration : configuration
 * @throws ConfigurationException : the field attribute is missing or does not exist in the class.
 * @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 {
    Element[] controller = metadata.getElements("controller");
    m_field = controller[0].getAttribute("field");
    getInstanceManager().register(new FieldMetadata(m_field, "boolean"), this);
}
Also used : FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) Element(org.apache.felix.ipojo.metadata.Element)

Example 13 with FieldMetadata

use of org.apache.felix.ipojo.parser.FieldMetadata in project felix by apache.

the class ProvidedServiceHandler method initializeComponentFactory.

/**
 * Initialize the component type.
 * @param desc : component type description to populate.
 * @param metadata : component type metadata.
 * @throws ConfigurationException : occurs when the POJO does not implement any interfaces.
 * @see org.apache.felix.ipojo.Handler#initializeComponentFactory(org.apache.felix.ipojo.architecture.ComponentTypeDescription, org.apache.felix.ipojo.metadata.Element)
 */
public void initializeComponentFactory(ComponentTypeDescription desc, Element metadata) throws ConfigurationException {
    // Change ComponentInfo
    Element[] provides = metadata.getElements("provides");
    PojoMetadata manipulation = getFactory().getPojoMetadata();
    for (Element provide : provides) {
        // First : create the serviceSpecification list
        String[] serviceSpecification = manipulation.getInterfaces();
        String parent = manipulation.getSuperClass();
        Set<String> interfaces = new HashSet<String>();
        Set<String> parentClasses = new HashSet<String>();
        try {
            computeInterfacesAndSuperClasses(serviceSpecification, parent, desc.getBundleContext().getBundle(), interfaces, parentClasses);
            getLogger().log(Logger.INFO, "Collected interfaces from " + metadata.getAttribute("classname") + " : " + interfaces);
            getLogger().log(Logger.INFO, "Collected super classes from " + metadata.getAttribute("classname") + " : " + parentClasses);
        } catch (ClassNotFoundException e) {
            throw new ConfigurationException("An interface or parent class cannot be loaded", e);
        }
        String serviceSpecificationStr = provide.getAttribute("specifications");
        if (serviceSpecificationStr == null) {
            serviceSpecificationStr = provide.getAttribute("interface");
            if (serviceSpecificationStr != null) {
                warn("The 'interface' attribute is deprecated, use the 'specifications' attribute instead of 'interface'");
            }
        }
        if (serviceSpecificationStr != null) {
            List<String> itfs = ParseUtils.parseArraysAsList(serviceSpecificationStr);
            for (String itf : itfs) if (!interfaces.contains(itf) && !parentClasses.contains(itf) && !desc.getFactory().getClassName().equals(itf)) {
                desc.getFactory().getLogger().log(Logger.ERROR, "The specification " + itf + " is not implemented by " + metadata.getAttribute("classname"));
            }
            interfaces.clear();
            interfaces.addAll(itfs);
        }
        if (interfaces.isEmpty()) {
            warn("No service interface found in the class hierarchy, use the implementation class");
            interfaces.add(desc.getFactory().getClassName());
        }
        StringBuffer specs = null;
        Set<String> set = new HashSet<String>(interfaces);
        // Remove POJO.
        set.remove(Pojo.class.getName());
        for (String spec : set) {
            desc.addProvidedServiceSpecification(spec);
            if (specs == null) {
                specs = new StringBuffer("{");
                specs.append(spec);
            } else {
                specs.append(',');
                specs.append(spec);
            }
        }
        specs.append('}');
        // Add interface attribute to avoid checking in the configure method
        provide.addAttribute(new Attribute("specifications", specs.toString()));
        Element[] props = provide.getElements("property");
        for (int j = 0; props != null && j < props.length; j++) {
            String name = props[j].getAttribute("name");
            String value = props[j].getAttribute("value");
            String type = props[j].getAttribute("type");
            String field = props[j].getAttribute("field");
            // Get property name :
            if (field != null && name == null) {
                name = field;
            }
            // Check type if not already set
            if (type == null) {
                if (field == null) {
                    throw new ConfigurationException("The property " + name + " has neither type nor field.");
                }
                FieldMetadata fieldMeta = manipulation.getField(field);
                if (fieldMeta == null) {
                    throw new ConfigurationException("A declared property was not found in the implementation class : " + field);
                }
                type = fieldMeta.getFieldType();
                props[j].addAttribute(new Attribute("type", type));
            }
            // Is the property set to immutable
            boolean immutable = false;
            String imm = props[j].getAttribute("immutable");
            if (imm != null && imm.equalsIgnoreCase("true")) {
                immutable = true;
            }
            PropertyDescription pd = new PropertyDescription(name, type, value, immutable);
            desc.addProperty(pd);
            String man = props[j].getAttribute("mandatory");
            if (man != null && man.equalsIgnoreCase("true")) {
                pd.setMandatory();
            }
        }
    }
}
Also used : Pojo(org.apache.felix.ipojo.Pojo) FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) Attribute(org.apache.felix.ipojo.metadata.Attribute) Element(org.apache.felix.ipojo.metadata.Element) PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) PojoMetadata(org.apache.felix.ipojo.parser.PojoMetadata)

Aggregations

FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)13 Element (org.apache.felix.ipojo.metadata.Element)12 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)9 MethodMetadata (org.apache.felix.ipojo.parser.MethodMetadata)5 PojoMetadata (org.apache.felix.ipojo.parser.PojoMetadata)5 Callback (org.apache.felix.ipojo.util.Callback)4 PropertyDescription (org.apache.felix.ipojo.architecture.PropertyDescription)3 Dictionary (java.util.Dictionary)2 Attribute (org.apache.felix.ipojo.metadata.Attribute)2 Property (org.apache.felix.ipojo.util.Property)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 Set (java.util.Set)1 ConstructorInjector (org.apache.felix.ipojo.ConstructorInjector)1 FieldInterceptor (org.apache.felix.ipojo.FieldInterceptor)1 Pojo (org.apache.felix.ipojo.Pojo)1 BundleContext (org.osgi.framework.BundleContext)1