Search in sources :

Example 6 with PojoMetadata

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

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

the class TemporalHandler method configure.

/**
 * Configure method. Creates managed dependencies.
 *
 * @param meta       the component type metadata.
 * @param dictionary the instance configuration.
 * @throws ConfigurationException if the dependency is not configured correctly
 * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
 */
public void configure(Element meta, Dictionary dictionary) throws ConfigurationException {
    PojoMetadata manipulation = getFactory().getPojoMetadata();
    Element[] deps = meta.getElements("requires", NAMESPACE);
    // Also check with temporal is no requires.
    if (deps == null || deps.length == 0) {
        deps = meta.getElements("temporal", NAMESPACE);
    }
    // Get instance filters.
    Dictionary filtersConfiguration = getRequiresFilters(dictionary.get("temporal.filters"));
    if (filtersConfiguration == null || filtersConfiguration.isEmpty()) {
        // Fall back on the Requires handler configuration, if any
        filtersConfiguration = getRequiresFilters(dictionary.get("requires.filters"));
    }
    // Get from filters if any.
    Dictionary fromConfiguration = getRequiresFilters(dictionary.get("temporal.from"));
    if (fromConfiguration == null || fromConfiguration.isEmpty()) {
        // Fall back on the Requires handler configuration, if any
        fromConfiguration = getRequiresFilters(dictionary.get("requires.from"));
    }
    for (int i = 0; i < deps.length; i++) {
        if (!deps[i].containsAttribute("field") || m_dependencies.contains(deps[i].getAttribute("field"))) {
            error("One temporal dependency must be attached to a field or the field is already used");
            return;
        }
        String field = deps[i].getAttribute("field");
        String id = field;
        if (deps[i].containsAttribute("id")) {
            id = deps[i].getAttribute("id");
        }
        FieldMetadata fieldmeta = manipulation.getField(field);
        if (fieldmeta == null) {
            error("The field " + field + " does not exist in the class " + getInstanceManager().getClassName());
            return;
        }
        boolean agg = false;
        boolean collection = false;
        String spec = fieldmeta.getFieldType();
        if (spec.endsWith("[]")) {
            agg = true;
            spec = spec.substring(0, spec.length() - 2);
        } else if (Collection.class.getName().equals(spec)) {
            agg = true;
            collection = true;
            // Collection detected. Check for the specification attribute
            spec = deps[i].getAttribute("specification");
            if (spec == null) {
                error("A dependency injected inside a Collection must contain the 'specification' attribute");
            }
        }
        // Determine the filter
        String fil = deps[i].getAttribute("filter");
        // Override the filter if filter configuration if available in the instance configuration
        if (filtersConfiguration != null && id != null && filtersConfiguration.get(id) != null) {
            fil = (String) filtersConfiguration.get(id);
        }
        // Check the from attribute
        String from = deps[i].getAttribute("from");
        if (fromConfiguration != null && id != null && fromConfiguration.get(id) != null) {
            from = (String) fromConfiguration.get(id);
        }
        if (from != null) {
            String fromFilter = "(|(instance.name=" + from + ")(service.pid=" + from + "))";
            if (agg) {
                warn("The 'from' attribute is incompatible with aggregate requirements: only one provider will " + "match : " + fromFilter);
            }
            if (fil != null) {
                // Append the two filters
                fil = "(&" + fromFilter + fil + ")";
            } else {
                fil = fromFilter;
            }
        }
        Filter filter = null;
        if (fil != null) {
            try {
                filter = getInstanceManager().getContext().createFilter(fil);
            } catch (InvalidSyntaxException e) {
                throw new ConfigurationException("A requirement filter is invalid : " + filter, e);
            }
        }
        String prox = deps[i].getAttribute("proxy");
        // Use proxy by default except for array:
        boolean proxy = prox == null || prox.equals("true");
        if (prox == null && proxy) {
            // Proxy set because of the default.
            if (agg && !collection) {
                // Aggregate and array
                proxy = false;
            }
        }
        if (proxy && agg) {
            if (!collection) {
                error("Proxied aggregate temporal dependencies cannot be an array. Only collections are supported");
            }
        }
        long timeout = DEFAULT_TIMEOUT;
        if (deps[i].containsAttribute("timeout")) {
            String to = deps[i].getAttribute("timeout");
            if (to.equalsIgnoreCase("infinite") || to.equalsIgnoreCase("-1")) {
                // Infinite wait time ...
                timeout = Long.MAX_VALUE;
            } else {
                timeout = new Long(deps[i].getAttribute("timeout")).longValue();
            }
        }
        int policy = NO_POLICY;
        String di = null;
        String onTimeout = deps[i].getAttribute("onTimeout");
        if (onTimeout != null) {
            if (onTimeout.equalsIgnoreCase("nullable")) {
                policy = NULLABLE;
            } else if (onTimeout.equalsIgnoreCase("empty-array") || onTimeout.equalsIgnoreCase("empty")) {
                policy = EMPTY;
                if (!agg) {
                    // The empty array policy can only be used on aggregate dependencies
                    error("Cannot use the empty array policy for " + field + " : non aggregate dependency.");
                }
            } else if (onTimeout.equalsIgnoreCase("null")) {
                policy = NULL;
            } else if (onTimeout.length() > 0) {
                di = onTimeout;
                policy = DEFAULT_IMPLEMENTATION;
            }
        }
        Class specification = DependencyMetadataHelper.loadSpecification(spec, getInstanceManager().getContext());
        TemporalDependency dep = new TemporalDependency(specification, agg, collection, proxy, filter, getInstanceManager().getContext(), timeout, policy, di, this);
        m_dependencies.add(dep);
        if (!proxy) {
            // Register method interceptor only if are not a proxy
            MethodMetadata[] methods = manipulation.getMethods();
            for (int k = 0; k < methods.length; k++) {
                getInstanceManager().register(methods[k], dep);
            }
        }
        getInstanceManager().register(fieldmeta, dep);
    }
}
Also used : FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) Element(org.apache.felix.ipojo.metadata.Element) Filter(org.osgi.framework.Filter) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) PojoMetadata(org.apache.felix.ipojo.parser.PojoMetadata) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MethodMetadata(org.apache.felix.ipojo.parser.MethodMetadata)

Example 8 with PojoMetadata

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

the class ComponentFactory method check.

/**
 * Allows a factory to check if the given element is well-formed.
 * A component factory metadata is correct if they contain the 'classname' attribute.
 * As this method is called from the (single-threaded) constructor, no synchronization is needed.
 *
 * @param element the metadata describing the component
 * @throws ConfigurationException if the element describing the factory is malformed.
 */
public void check(Element element) throws ConfigurationException {
    m_classname = element.getAttribute("classname");
    if (m_classname == null) {
        throw new ConfigurationException("A component needs a class name : " + element);
    }
    m_manipulation = new PojoMetadata(m_componentMetadata);
}
Also used : PojoMetadata(org.apache.felix.ipojo.parser.PojoMetadata)

Example 9 with PojoMetadata

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

the class ProvidedServiceHandlerTest method testServiceDetectionImplementationClass.

public void testServiceDetectionImplementationClass() throws ConfigurationException {
    String classname = "org.apache.felix.ipojo.handlers.providedservice.ComponentTestWithSuperClass";
    Element metadata = new Element("component", "");
    Element manipulation = new Element("manipulation", "");
    metadata.addAttribute(new Attribute("classname", classname));
    Element provides = new Element("provides", "");
    provides.addAttribute(new Attribute("specifications", classname));
    metadata.addElement(provides);
    metadata.addElement(manipulation);
    manipulation.addAttribute(new Attribute("classname", classname));
    manipulation.addAttribute(new Attribute("super", "java.beans.SimpleBeanInfo"));
    Mockito.when(factory.getPojoMetadata()).thenReturn(new PojoMetadata(metadata));
    Mockito.when(factory.getClassName()).thenReturn(classname);
    handler.initializeComponentFactory(desc, metadata);
    System.out.println(metadata);
}
Also used : Attribute(org.apache.felix.ipojo.metadata.Attribute) Element(org.apache.felix.ipojo.metadata.Element) PojoMetadata(org.apache.felix.ipojo.parser.PojoMetadata)

Example 10 with PojoMetadata

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

the class ProvidedServiceHandlerTest method testServiceDetectionNoInterface.

public void testServiceDetectionNoInterface() throws ConfigurationException {
    String classname = "org.apache.felix.ipojo.handlers.providedservice.ComponentTest";
    Element metadata = new Element("component", "");
    Element manipulation = new Element("manipulation", "");
    metadata.addAttribute(new Attribute("classname", classname));
    metadata.addElement(new Element("provides", ""));
    metadata.addElement(manipulation);
    manipulation.addAttribute(new Attribute("classname", classname));
    Mockito.when(factory.getPojoMetadata()).thenReturn(new PojoMetadata(metadata));
    Mockito.when(factory.getClassName()).thenReturn(classname);
    handler.initializeComponentFactory(desc, metadata);
    // Expected behavior: the implementation classname
    Assert.assertEquals("{org.apache.felix.ipojo.handlers.providedservice.ComponentTest}", metadata.getElements("provides")[0].getAttribute("specifications"));
}
Also used : Attribute(org.apache.felix.ipojo.metadata.Attribute) Element(org.apache.felix.ipojo.metadata.Element) PojoMetadata(org.apache.felix.ipojo.parser.PojoMetadata)

Aggregations

PojoMetadata (org.apache.felix.ipojo.parser.PojoMetadata)15 Element (org.apache.felix.ipojo.metadata.Element)14 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)6 Attribute (org.apache.felix.ipojo.metadata.Attribute)6 MethodMetadata (org.apache.felix.ipojo.parser.MethodMetadata)6 FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)5 PropertyDescription (org.apache.felix.ipojo.architecture.PropertyDescription)3 Filter (org.osgi.framework.Filter)2 Event (org.osgi.service.event.Event)2 IOException (java.io.IOException)1 Enumeration (java.util.Enumeration)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 Pojo (org.apache.felix.ipojo.Pojo)1 Callback (org.apache.felix.ipojo.util.Callback)1 BundleContext (org.osgi.framework.BundleContext)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1