Search in sources :

Example 1 with DefaultInstanceDeclaration

use of org.apache.felix.ipojo.extender.internal.declaration.DefaultInstanceDeclaration in project felix by apache.

the class DefaultInstanceBuilderTestCase method testDeclarationIsNotAutomaticallyStarted.

public void testDeclarationIsNotAutomaticallyStarted() throws Exception {
    InstanceBuilder builder = new DefaultInstanceBuilder(m_bundleContext, "type");
    DeclarationHandle handle = builder.build();
    DefaultInstanceDeclaration did = (DefaultInstanceDeclaration) handle;
    assertFalse(did.isRegistered());
}
Also used : DefaultInstanceDeclaration(org.apache.felix.ipojo.extender.internal.declaration.DefaultInstanceDeclaration) DeclarationHandle(org.apache.felix.ipojo.extender.DeclarationHandle) InstanceBuilder(org.apache.felix.ipojo.extender.InstanceBuilder)

Example 2 with DefaultInstanceDeclaration

use of org.apache.felix.ipojo.extender.internal.declaration.DefaultInstanceDeclaration 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)

Example 3 with DefaultInstanceDeclaration

use of org.apache.felix.ipojo.extender.internal.declaration.DefaultInstanceDeclaration in project felix by apache.

the class ComponentsBundleProcessor method handleInstanceDeclaration.

/**
 * Extracts and builds the declaration attached to an instance.
 *
 * @param bundle   the bundle declaring the instance
 * @param instance the instance configuration (parsed from the header)
 */
private void handleInstanceDeclaration(Bundle bundle, Dictionary instance) {
    String component = (String) instance.get(COMPONENT_INSTANCE_ATTRIBUTE);
    // String v = (String) instance.get(Factory.FACTORY_VERSION_PROPERTY); //TODO CES to GSA, why this is commented ?
    DefaultInstanceDeclaration declaration = new DefaultInstanceDeclaration(bundle.getBundleContext(), component, instance);
    declaration.start();
    getComponentsAndInstances(bundle).m_instances.add(declaration);
}
Also used : DefaultInstanceDeclaration(org.apache.felix.ipojo.extender.internal.declaration.DefaultInstanceDeclaration)

Aggregations

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