Search in sources :

Example 1 with InvocationResult

use of org.apache.felix.ipojo.util.InvocationResult in project felix by apache.

the class ConfigurationProcessor method instantiateAndDeclareInstances.

private void instantiateAndDeclareInstances(Bundle bundle, String resource, ClassLoader classLoader) {
    String classname = getClassNameFromResource(resource);
    List<Instance> instances = new ArrayList<Instance>();
    try {
        Class clazz = classLoader.loadClass(classname);
        Object configuration = clazz.newInstance();
        // Collect fields
        Map<Field, Instance> fields = fields().ofType(Instance.class).in(configuration).map();
        for (Map.Entry<Field, Instance> entry : fields.entrySet()) {
            Instance instance = entry.getValue();
            instance.nameIfUnnamed(entry.getKey().getName()).with("instance.bundle.context").setto(bundle.getBundleContext());
            instances.add(instance);
        }
        // Collect methods with Bundle Context as argument
        Map<Method, InvocationResult<Instance>> methods = methods().in(configuration).ofReturnType(Instance.class).withParameter(BundleContext.class).map(bundle.getBundleContext());
        // Collect methods without arguments
        methods.putAll(methods().in(configuration).ofReturnType(Instance.class).map());
        for (Map.Entry<Method, InvocationResult<Instance>> entry : methods.entrySet()) {
            Instance instance = entry.getValue().get();
            if (instance == null) {
                m_logger.log(Log.ERROR, "The Instance declaration creation failed because the method " + entry.getKey().getName() + " of class " + entry.getKey().getDeclaringClass() + " threw an " + "exception", entry.getValue().error());
            } else {
                instance.nameIfUnnamed(entry.getKey().getName()).with("instance.bundle.context").setto(bundle.getBundleContext());
                instances.add(instance);
            }
        }
    } catch (ClassNotFoundException e) {
        m_logger.log(Log.ERROR, "Cannot load class " + classname + " despite it was considered as a " + "configuration", e);
        return;
    } catch (InstantiationException e) {
        m_logger.log(Log.ERROR, "Cannot instantiate class " + classname + " despite it was considered as a " + "configuration", e);
        return;
    } catch (IllegalAccessException e) {
        m_logger.log(Log.ERROR, "Cannot instantiate class " + classname + " despite it was considered as a " + "configuration", e);
        return;
    }
    m_logger.log(Log.WARNING, instances.size() + " instances found in class " + classname);
    // Build and enqueue declaration
    for (Instance instance : instances) {
        DefaultInstanceDeclaration declaration = new DefaultInstanceDeclaration(bundle.getBundleContext(), instance.factory(), instance.configuration());
        declaration.start();
        getComponentsAndInstances(bundle).m_instances.add(declaration);
    }
}
Also used : Instance(org.apache.felix.ipojo.configuration.Instance) DefaultInstanceDeclaration(org.apache.felix.ipojo.extender.internal.declaration.DefaultInstanceDeclaration) Method(java.lang.reflect.Method) InvocationResult(org.apache.felix.ipojo.util.InvocationResult) Field(java.lang.reflect.Field) BundleContext(org.osgi.framework.BundleContext)

Aggregations

Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Instance (org.apache.felix.ipojo.configuration.Instance)1 DefaultInstanceDeclaration (org.apache.felix.ipojo.extender.internal.declaration.DefaultInstanceDeclaration)1 InvocationResult (org.apache.felix.ipojo.util.InvocationResult)1 BundleContext (org.osgi.framework.BundleContext)1