Search in sources :

Example 31 with Element

use of org.apache.felix.ipojo.metadata.Element in project felix by apache.

the class ControllerHandler method initializeComponentFactory.

/**
 * Initialize the component factory.
 * The controller field is checked to avoid configure check.
 * @param desc : component description
 * @param metadata : component type metadata
 * @throws ConfigurationException : occurs if the controller field is not in the POJO class or is not a boolean.
 * @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 {
    String field = null;
    Element[] controller = metadata.getElements("controller");
    // Use only the first controller
    field = controller[0].getAttribute("field");
    if (field == null) {
        throw new ConfigurationException("Lifecycle controller : the controller element needs to contain a field attribute");
    }
    PojoMetadata method = getFactory().getPojoMetadata();
    FieldMetadata fieldMetadata = method.getField(field);
    if (fieldMetadata == null) {
        throw new ConfigurationException("Lifecycle controller : The field " + field + " does not exist in the implementation class");
    }
    if (!fieldMetadata.getFieldType().equalsIgnoreCase("boolean")) {
        throw new ConfigurationException("Lifecycle controller : The field " + field + " must be a boolean (" + fieldMetadata.getFieldType() + " found)");
    }
}
Also used : FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) Element(org.apache.felix.ipojo.metadata.Element) PojoMetadata(org.apache.felix.ipojo.parser.PojoMetadata)

Example 32 with Element

use of org.apache.felix.ipojo.metadata.Element in project felix by apache.

the class ProvidedServiceHandler method configure.

/**
 * Configure the handler.
 * @param componentMetadata : the component type metadata
 * @param configuration : the instance configuration
 * @throws ConfigurationException : the metadata are not correct.
 * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
 */
public void configure(Element componentMetadata, Dictionary configuration) throws ConfigurationException {
    m_providedServices.clear();
    // Create the dependency according to the component metadata
    Element[] providedServices = componentMetadata.getElements("Provides");
    for (Element providedService : providedServices) {
        // Set by the initialize component factory.
        String[] serviceSpecifications = ParseUtils.parseArrays(providedService.getAttribute("specifications"));
        // Get the factory policy
        int factory = ProvidedService.SINGLETON_STRATEGY;
        Class custom = null;
        String strategy = providedService.getAttribute("strategy");
        if (strategy == null) {
            strategy = providedService.getAttribute("factory");
        }
        if (strategy != null) {
            if ("singleton".equalsIgnoreCase(strategy)) {
                factory = ProvidedService.SINGLETON_STRATEGY;
            } else if ("service".equalsIgnoreCase(strategy)) {
                factory = ProvidedService.SERVICE_STRATEGY;
            } else if ("method".equalsIgnoreCase(strategy)) {
                factory = ProvidedService.STATIC_STRATEGY;
            } else if ("instance".equalsIgnoreCase(strategy)) {
                factory = ProvidedService.INSTANCE_STRATEGY;
            } else {
                // Customized policy
                try {
                    custom = getInstanceManager().getContext().getBundle().loadClass(strategy);
                    if (!CreationStrategy.class.isAssignableFrom(custom)) {
                        throw new ConfigurationException("The custom creation policy class " + custom.getName() + " does not implement " + CreationStrategy.class.getName());
                    }
                } catch (ClassNotFoundException e) {
                    throw new ConfigurationException("The custom creation policy class " + strategy + " cannot be loaded ", e);
                }
            }
        }
        // Then create the provided service
        ProvidedService svc = new ProvidedService(this, serviceSpecifications, factory, custom, configuration);
        // Post-Registration callback
        String post = providedService.getAttribute("post-registration");
        if (post != null) {
            Callback cb = new Callback(post, new Class[] { ServiceReference.class }, false, getInstanceManager());
            svc.setPostRegistrationCallback(cb);
        }
        post = providedService.getAttribute("post-unregistration");
        if (post != null) {
            // TODO Can we really send the service reference here ?
            Callback cb = new Callback(post, new Class[] { ServiceReference.class }, false, getInstanceManager());
            svc.setPostUnregistrationCallback(cb);
        }
        Element[] props = providedService.getElements("Property");
        if (props != null) {
            // Property[] properties = new Property[props.length];
            Property[] properties = new Property[props.length];
            for (int j = 0; 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");
                Property prop = new Property(name, field, null, value, type, getInstanceManager(), this);
                properties[j] = prop;
                // Check if the instance configuration has a value for this property
                Object object = configuration.get(prop.getName());
                if (object != null) {
                    prop.setValue(object);
                }
                if (field != null) {
                    getInstanceManager().register(new FieldMetadata(field, type), this);
                // Cannot register the property as the interception is necessary
                // to deal with registration update.
                }
            }
            // Attach to properties to the provided service
            svc.setProperties(properties);
        }
        Element[] controllers = providedService.getElements("Controller");
        if (controllers != null) {
            for (Element controller : controllers) {
                String field = controller.getAttribute("field");
                if (field == null) {
                    throw new ConfigurationException("The field attribute of a controller is mandatory");
                }
                String v = controller.getAttribute("value");
                boolean value = !(v != null && v.equalsIgnoreCase("false"));
                String s = controller.getAttribute("specification");
                if (s == null) {
                    s = "ALL";
                }
                svc.setController(field, value, s);
                getInstanceManager().register(new FieldMetadata(field, "boolean"), this);
            }
        }
        if (checkProvidedService(svc)) {
            m_providedServices.add(svc);
        } else {
            StringBuilder itfs = new StringBuilder();
            for (String serviceSpecification : serviceSpecifications) {
                itfs.append(' ');
                itfs.append(serviceSpecification);
            }
            throw new ConfigurationException("The provided service" + itfs + " is not valid");
        }
        // Initialize the description.
        m_description = new ProvidedServiceHandlerDescription(this, getProvidedServices());
    }
}
Also used : FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) Element(org.apache.felix.ipojo.metadata.Element) Callback(org.apache.felix.ipojo.util.Callback) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) Property(org.apache.felix.ipojo.util.Property)

Example 33 with Element

use of org.apache.felix.ipojo.metadata.Element in project felix by apache.

the class ManifestMetadataParser method parseInstance.

/**
 * Parses an Element to create an instance configuration dictionary.
 * The 'name' attribute of the instance declaration is mapped
 * to the 'instance.name' property.
 * @param instance the Element describing an instance.
 * @return the resulting dictionary
 * @throws ParseException if a configuration cannot be parse correctly.
 */
private Dictionary parseInstance(Element instance) throws ParseException {
    Dictionary dict = new Properties();
    String name = instance.getAttribute("name");
    String comp = instance.getAttribute("component");
    String version = instance.getAttribute("version");
    if (name != null) {
        dict.put(Factory.INSTANCE_NAME_PROPERTY, instance.getAttribute("name"));
    }
    if (comp == null) {
        throw new ParseException("An instance does not have the 'component' attribute");
    }
    dict.put("component", comp);
    if (version != null) {
        dict.put(Factory.FACTORY_VERSION_PROPERTY, version);
    }
    Element[] props = instance.getElements("property");
    for (int i = 0; props != null && i < props.length; i++) {
        parseProperty(props[i], dict);
    }
    return dict;
}
Also used : Dictionary(java.util.Dictionary) Element(org.apache.felix.ipojo.metadata.Element) Properties(java.util.Properties)

Example 34 with Element

use of org.apache.felix.ipojo.metadata.Element in project felix by apache.

the class ManifestMetadataParser method parseHeaderMetadata.

/**
 * Parses the metadata from the given header string.
 * This method creates a new {@link ManifestMetadataParser} object and then
 * creates the <code>iPOJO</code> root element, parses content elements
 * (component types and instances declarations), and returns the resulting
 * {@link Element} / {@link Attribute} structure. The parsed string
 * must be a tree (only one root element).
 * @param header the header to parse
 * @return Element the root element resulting of the parsing
 * @throws ParseException if any error occurs
 */
public static Element parseHeaderMetadata(String header) throws ParseException {
    ManifestMetadataParser parser = new ManifestMetadataParser();
    parser.addElement(new Element("iPOJO", ""));
    parser.parseElements(header);
    if (parser.m_elements.length != 1) {
        throw new ParseException("Error in parsing, root element not found : " + header);
    }
    return parser.m_elements[0];
}
Also used : Element(org.apache.felix.ipojo.metadata.Element)

Example 35 with Element

use of org.apache.felix.ipojo.metadata.Element in project felix by apache.

the class ManifestMetadataParser method parseElements.

/**
 * Parses the given string.
 * This methods populates the {@link ManifestMetadataParser#m_elements}
 * list.
 * @param elems the string to parse
 */
private void parseElements(String elems) {
    char[] string = elems.toCharArray();
    for (int i = 0; i < string.length; i++) {
        char current = string[i];
        switch(// NOPMD
        current) {
            // Beginning of an attribute.
            case '$':
                StringBuffer attName = new StringBuffer();
                StringBuffer attValue = new StringBuffer();
                StringBuffer attNs = null;
                i++;
                // Increment and get the new current char.
                current = string[i];
                while (current != '=') {
                    if (current == ':') {
                        attNs = attName;
                        attName = new StringBuffer();
                    } else {
                        attName.append(current);
                    }
                    i++;
                    current = string[i];
                }
                // skip ="
                i = i + 2;
                current = string[i];
                while (current != '"') {
                    attValue.append(current);
                    i++;
                    // Increment and get the new current char.
                    current = string[i];
                }
                // skip "
                i++;
                current = string[i];
                Attribute att = null;
                if (attNs == null) {
                    att = new Attribute(attName.toString(), attValue.toString());
                } else {
                    att = new Attribute(attName.toString(), attNs.toString(), attValue.toString());
                }
                m_elements[m_elements.length - 1].addAttribute(att);
                break;
            // End of an element
            case '}':
                Element lastElement = removeLastElement();
                if (m_elements.length == 0) {
                    addElement(lastElement);
                } else {
                    Element newQueue = m_elements[m_elements.length - 1];
                    newQueue.addElement(lastElement);
                }
                break;
            // Space
            case ' ':
                // do nothing;
                break;
            // Default case
            default:
                StringBuffer qname = new StringBuffer();
                current = string[i];
                while (current != ' ') {
                    qname.append(current);
                    i++;
                    // Increment and get the new current char.
                    current = string[i];
                }
                // Skip spaces
                while (string[i] == ' ') {
                    i = i + 1;
                }
                // skip {
                i = i + 1;
                Element elem = null;
                // Parse the qname
                String n = qname.toString();
                if (n.indexOf(':') == -1) {
                    // No namespace
                    elem = new Element(n, null);
                } else {
                    // The namespace ends on the first ':'
                    int last = n.lastIndexOf(':');
                    String ns = n.substring(0, last);
                    String name = n.substring(last + 1);
                    elem = new Element(name.toString(), ns.toString());
                }
                addElement(elem);
                break;
        }
    }
}
Also used : Attribute(org.apache.felix.ipojo.metadata.Attribute) Element(org.apache.felix.ipojo.metadata.Element)

Aggregations

Element (org.apache.felix.ipojo.metadata.Element)400 Test (org.junit.Test)126 Attribute (org.apache.felix.ipojo.metadata.Attribute)109 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)22 ParseException (org.apache.felix.ipojo.parser.ParseException)14 PojoMetadata (org.apache.felix.ipojo.parser.PojoMetadata)14 ArrayList (java.util.ArrayList)13 Reporter (org.apache.felix.ipojo.manipulator.Reporter)12 FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)12 Dictionary (java.util.Dictionary)10 Properties (java.util.Properties)10 MethodMetadata (org.apache.felix.ipojo.parser.MethodMetadata)10 BaseTest (org.ow2.chameleon.testing.helpers.BaseTest)10 PropertyDescription (org.apache.felix.ipojo.architecture.PropertyDescription)9 IOException (java.io.IOException)8 List (java.util.List)8 Before (org.junit.Before)7 Enumeration (java.util.Enumeration)6 ComponentWorkbench (org.apache.felix.ipojo.manipulator.metadata.annotation.ComponentWorkbench)6 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)6