Search in sources :

Example 11 with MethodMetadata

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

the class DependencyConfigurationChecker method ensureThatTheConstructorParameterIsCoherent.

/**
 * Checks whether the constructor parameter injection is suitable. this check verified that the constructor has
 * enough parameter.
 * @param dependency the dependency
 * @param manipulation the manipulation metadata
 * @throws ConfigurationException if the constructor is not suitable
 */
private static void ensureThatTheConstructorParameterIsCoherent(Dependency dependency, PojoMetadata manipulation) throws ConfigurationException {
    if (dependency.getConstructorParameterIndex() != -1) {
        MethodMetadata[] constructors = manipulation.getConstructors();
        if (constructors == null || constructors.length == 0) {
            throw new ConfigurationException("The constructor parameter attribute of " + DependencyHandler.getDependencyIdentifier(dependency) + " is inconsistent - reason: there is no constructor in" + " the component class (" + dependency.getHandler().getInstanceManager().getClassName() + ")");
        }
        // TODO Consider only the first constructor. This is a limitation we should think about,
        // how to determine which constructor to use. Only one constructor should have annotations,
        // it could be use as hint.
        MethodMetadata constructor = constructors[0];
        if (!(constructor.getMethodArguments().length > dependency.getConstructorParameterIndex())) {
            throw new ConfigurationException("The constructor parameter attribute of " + DependencyHandler.getDependencyIdentifier(dependency) + " is inconsistent - reason: the constructor with the " + "signature " + Arrays.toString(constructor.getMethodArguments()) + " has not enough " + "parameters");
        }
    }
}
Also used : ConfigurationException(org.apache.felix.ipojo.ConfigurationException) MethodMetadata(org.apache.felix.ipojo.parser.MethodMetadata)

Example 12 with MethodMetadata

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

the class DependencyHandler method configure.

/**
 * Configure the handler.
 *
 * @param componentMetadata : the component type metadata
 * @param configuration     : the instance configuration
 * @throws ConfigurationException : one dependency metadata is 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 {
    PojoMetadata manipulation = getFactory().getPojoMetadata();
    boolean atLeastOneField = false;
    // Create the dependency according to the component metadata
    Element[] deps = componentMetadata.getElements("Requires");
    // Get instance filters.
    Dictionary filtersConfiguration = getRequiresFilters(configuration.get("requires.filters"));
    Dictionary fromConfiguration = (Dictionary) configuration.get("requires.from");
    for (int i = 0; deps != null && i < deps.length; i++) {
        // Create the dependency metadata
        final Element dependencyElement = deps[i];
        String field = dependencyElement.getAttribute("field");
        String serviceSpecification = getServiceSpecificationAttribute(dependencyElement);
        String opt = dependencyElement.getAttribute("optional");
        boolean optional = opt != null && opt.equalsIgnoreCase("true");
        String defaultImpl = dependencyElement.getAttribute("default-implementation");
        String exception = dependencyElement.getAttribute("exception");
        String to = dependencyElement.getAttribute("timeout");
        int timeout = 0;
        if (to != null) {
            timeout = Integer.parseInt(to);
        }
        String agg = dependencyElement.getAttribute("aggregate");
        boolean aggregate = agg != null && agg.equalsIgnoreCase("true");
        String identity = dependencyElement.getAttribute("id");
        String nul = dependencyElement.getAttribute("nullable");
        boolean nullable = nul == null || nul.equalsIgnoreCase("true");
        boolean isProxy = isProxy(dependencyElement);
        BundleContext context = getFacetedBundleContext(dependencyElement);
        String filter = computeFilter(dependencyElement, filtersConfiguration, fromConfiguration, aggregate, identity);
        Filter fil = createAndCheckFilter(filter);
        Class spec = null;
        if (serviceSpecification != null) {
            spec = DependencyMetadataHelper.loadSpecification(serviceSpecification, getInstanceManager().getContext());
        }
        int policy = DependencyMetadataHelper.getPolicy(dependencyElement);
        Comparator cmp = DependencyMetadataHelper.getComparator(dependencyElement, getInstanceManager().getGlobalContext());
        Dependency dep = new Dependency(this, field, spec, fil, optional, aggregate, nullable, isProxy, identity, context, policy, cmp, defaultImpl, exception);
        dep.setTimeout(timeout);
        // Look for dependency callback :
        addCallbacksToDependency(dependencyElement, dep);
        // Add the constructor parameter if needed
        String paramIndex = dependencyElement.getAttribute("constructor-parameter");
        if (paramIndex != null) {
            int index = Integer.parseInt(paramIndex);
            dep.addConstructorInjection(index);
        }
        // Check the dependency, throws an exception on error.
        DependencyConfigurationChecker.ensure(dep, dependencyElement, manipulation);
        m_dependencies.add(dep);
        if (dep.getField() != null) {
            getInstanceManager().register(manipulation.getField(dep.getField()), dep);
            atLeastOneField = true;
        }
    }
    if (atLeastOneField) {
        // Does register only if we have fields
        MethodMetadata[] methods = manipulation.getMethods();
        for (MethodMetadata method : methods) {
            for (Dependency dep : m_dependencies) {
                getInstanceManager().register(method, dep);
            }
        }
        // Also track the inner class methods
        for (String inner : manipulation.getInnerClasses()) {
            MethodMetadata[] meths = manipulation.getMethodsFromInnerClass(inner);
            if (meths != null) {
                for (MethodMetadata method : meths) {
                    for (Dependency dep : m_dependencies) {
                        getInstanceManager().register(method, inner, dep);
                    }
                }
            }
        }
    }
    // Initialize the description.
    m_description = new DependencyHandlerDescription(this, getDependencies());
    manageContextSources(configuration);
}
Also used : Element(org.apache.felix.ipojo.metadata.Element) Filter(org.osgi.framework.Filter) PojoMetadata(org.apache.felix.ipojo.parser.PojoMetadata) MethodMetadata(org.apache.felix.ipojo.parser.MethodMetadata) BundleContext(org.osgi.framework.BundleContext)

Example 13 with MethodMetadata

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

the class LifecycleCallbackHandler method configure.

/**
 * Configure the handler.
 * @param metadata : the component type metadata
 * @param configuration : the instance configuration
 * @throws ConfigurationException : one callback metadata is not correct (either the transition or the method are 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 {
    m_callbacks = new LifecycleCallback[0];
    String imm = metadata.getAttribute("immediate");
    m_immediate = imm != null && imm.equalsIgnoreCase("true");
    PojoMetadata meta = getFactory().getPojoMetadata();
    Element[] hooksMetadata = metadata.getElements("callback");
    for (int i = 0; hooksMetadata != null && i < hooksMetadata.length; i++) {
        String method = hooksMetadata[i].getAttribute("method");
        if (method == null) {
            throw new ConfigurationException("Lifecycle callback : A callback needs to contain a method attribute");
        }
        MethodMetadata met = meta.getMethod(method, new String[0]);
        int transition = -1;
        String trans = hooksMetadata[i].getAttribute("transition");
        if (trans == null) {
            throw new ConfigurationException("Lifecycle callback : the transition attribute is missing");
        } else {
            if (trans.equalsIgnoreCase("validate")) {
                transition = LifecycleCallback.VALIDATE;
            } else if (trans.equalsIgnoreCase("invalidate")) {
                transition = LifecycleCallback.INVALIDATE;
            } else {
                throw new ConfigurationException("Lifecycle callback : Unknown or malformed transition : " + trans);
            }
        }
        LifecycleCallback callback = null;
        if (met == null) {
            callback = new LifecycleCallback(this, transition, method);
        } else {
            callback = new LifecycleCallback(this, transition, met);
        }
        addCallback(callback);
    }
}
Also used : ConfigurationException(org.apache.felix.ipojo.ConfigurationException) Element(org.apache.felix.ipojo.metadata.Element) PojoMetadata(org.apache.felix.ipojo.parser.PojoMetadata) MethodMetadata(org.apache.felix.ipojo.parser.MethodMetadata)

Aggregations

MethodMetadata (org.apache.felix.ipojo.parser.MethodMetadata)13 Element (org.apache.felix.ipojo.metadata.Element)10 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)6 PojoMetadata (org.apache.felix.ipojo.parser.PojoMetadata)6 FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)5 Callback (org.apache.felix.ipojo.util.Callback)5 BundleContext (org.osgi.framework.BundleContext)4 PropertyDescription (org.apache.felix.ipojo.architecture.PropertyDescription)2 Attribute (org.apache.felix.ipojo.metadata.Attribute)2 Property (org.apache.felix.ipojo.util.Property)2 Filter (org.osgi.framework.Filter)2 Event (org.osgi.service.event.Event)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Member (java.lang.reflect.Member)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 MBeanException (javax.management.MBeanException)1 ReflectionException (javax.management.ReflectionException)1