Package org.apache.felix.ipojo.extender

Examples of org.apache.felix.ipojo.extender.InstanceDeclaration


    private class InstanceSupport implements ServiceTrackerCustomizer {
        public Object addingService(final ServiceReference reference) {
            Object service = m_bundleContext.getService(reference);
            if (service instanceof InstanceDeclaration) {
                final InstanceDeclaration instanceDeclaration = (InstanceDeclaration) service;

                // Check that instance is not already bound
                if (instanceDeclaration.getStatus().isBound()) {
                    return null;
                }

                // Handle visibility (private/public factories)
                if (!m_declaration.isPublic()) {
                    if (!reference.getBundle().equals(m_bundleContext.getBundle())) {
                        Bundle origin = m_bundleContext.getBundle();
                        instanceDeclaration.unbind(
                                format("Component '%s/%s' is private. It only accept instances " +
                                               "from bundle %s/%s [%d] (instance bundle origin: %d)",
                                       m_declaration.getComponentName(),
                                       m_declaration.getComponentVersion(),
                                       origin.getSymbolicName(),
                                       origin.getVersion(),
                                       origin.getBundleId(),
                                       reference.getBundle().getBundleId())
                        );
                        return null;
                    }
                }

                return m_queueService.submit(new DefaultJob<ComponentInstance>(reference.getBundle(), INSTANCE_STARTUP_JOB_TYPE) {
                    public ComponentInstance call() throws Exception {
                        try {
                            // Create the component's instance
                            // It is automatically started
                            // Future.get should never be null since this tracker is started when the factory has been created
                            ComponentInstance instance = m_future.get().createComponentInstance(instanceDeclaration.getConfiguration());
                            if (instance instanceof InstanceBundleContextAware) {
                                ((InstanceBundleContextAware) instance).setInstanceBundleContext(instanceDeclaration
                                        .getBundle().getBundleContext());
                            }

                            // Notify the declaration that everything is fine
                            instanceDeclaration.bind();

                            return instance;
                        } catch (UnacceptableConfiguration c) {
                            instanceDeclaration.unbind(format("Instance configuration is invalid (component:%s/%s, bundle:%d)",
                                                              m_declaration.getComponentName(),
                                                              m_declaration.getComponentVersion(),
                                                              reference.getBundle().getBundleId()),
                                    c);
                        } catch (MissingHandlerException e) {
                            instanceDeclaration.unbind(
                                    format(
                                            "Component '%s/%s' (required for instance creation) is missing some handlers",
                                            m_declaration.getComponentName(),
                                            m_declaration.getComponentVersion()
                                    ),
                                    e);
                        } catch (ConfigurationException e) {
                            instanceDeclaration.unbind(
                                    format(
                                            "Instance configuration is incorrect for component '%s/%s'",
                                            m_declaration.getComponentName(),
                                            m_declaration.getComponentVersion()),
                                    e);
View Full Code Here


        public void modifiedService(ServiceReference reference, Object o) {
        }

        public void removedService(ServiceReference reference, Object o) {
            InstanceDeclaration instanceDeclaration = (InstanceDeclaration) m_bundleContext.getService(reference);
            Future<ComponentInstance> future = (Future<ComponentInstance>) o;
            ComponentInstance instance;
            try {
                instance = future.get();
                // It is possible that the instance couldn't be created
                if (instance != null) {
                    String message = format("Factory for Component '%s/%s' is missing",
                                            instance.getFactory().getName(),
                                            m_declaration.getComponentVersion());
                    instanceDeclaration.unbind(message);

                    instance.stop();
                    instance.dispose();
                }

            } catch (InterruptedException e) {
                instanceDeclaration.unbind("Could not create ComponentInstance", e);
            } catch (ExecutionException e) {
                instanceDeclaration.unbind("ComponentInstance creation throw an Exception", e);
            }
        }
View Full Code Here

    }

    public void testNoConfiguration() throws Exception {
        InstanceBuilder builder = new DefaultInstanceBuilder(m_bundleContext, "type");
        DeclarationHandle handle = builder.build();
        InstanceDeclaration did = (InstanceDeclaration) handle;

        assertEquals("type", did.getComponentName());
        assertEquals(InstanceDeclaration.UNNAMED_INSTANCE, did.getInstanceName());
        assertNull(did.getComponentVersion());

        Dictionary<String,Object> configuration = did.getConfiguration();
        assertTrue(configuration.isEmpty());
    }
View Full Code Here

    public void testNameConfiguration() throws Exception {
        InstanceBuilder builder = new DefaultInstanceBuilder(m_bundleContext, "type").name("John");

        DeclarationHandle handle = builder.build();
        InstanceDeclaration did = (InstanceDeclaration) handle;

        assertEquals("type", did.getComponentName());
        assertEquals("John", did.getInstanceName());
        assertNull(did.getComponentVersion());

        Dictionary<String,Object> configuration = did.getConfiguration();
        assertEquals("John", configuration.get(Factory.INSTANCE_NAME_PROPERTY));
    }
View Full Code Here

    public void testVersionConfiguration() throws Exception {
        InstanceBuilder builder = new DefaultInstanceBuilder(m_bundleContext, "type").name("John").version("1.0");

        DeclarationHandle handle = builder.build();
        InstanceDeclaration did = (InstanceDeclaration) handle;

        assertEquals("type", did.getComponentName());
        assertEquals("John", did.getInstanceName());
        assertEquals("1.0", did.getComponentVersion());

        Dictionary<String,Object> configuration = did.getConfiguration();
        assertEquals("John", configuration.get(Factory.INSTANCE_NAME_PROPERTY));
        assertEquals("1.0", configuration.get(Factory.FACTORY_VERSION_PROPERTY));
    }
View Full Code Here

    }

    public void testPropertyAddition() throws Exception {
        DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(parent);
        builder.property("a", "b");
        InstanceDeclaration declaration = (InstanceDeclaration) builder.build();
        assertEquals(declaration.getConfiguration().get("a"), "b");
    }
View Full Code Here

    public void testPropertyAdditionReUse() throws Exception {
        DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(parent);
        builder.property("a", "b");

        // First built instance
        InstanceDeclaration declaration = (InstanceDeclaration) builder.build();
        assertEquals(declaration.getConfiguration().get("a"), "b");

        // Second built instance
        builder.property("c", "d");
        InstanceDeclaration declaration2 = (InstanceDeclaration) builder.build();
        assertEquals(declaration2.getConfiguration().get("a"), "b");
        assertEquals(declaration2.getConfiguration().get("c"), "d");

        // Verify that first instance is not modified
        assertNull(declaration.getConfiguration().get("c"));

    }
View Full Code Here

    public void testPropertyRemoval() throws Exception {
        DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(parent);
        builder.property("a", "b");

        InstanceDeclaration declaration = (InstanceDeclaration) builder.build();
        assertEquals(declaration.getConfiguration().get("a"), "b");

        builder.remove("a");

        InstanceDeclaration declaration2 = (InstanceDeclaration) builder.build();
        assertNull(declaration2.getConfiguration().get("a"));

    }
View Full Code Here

    public void testClear() throws Exception {
        DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(parent);
        builder.property("a", "b");
        builder.property("c", "d");

        InstanceDeclaration declaration = (InstanceDeclaration) builder.build();
        assertEquals(declaration.getConfiguration().get("a"), "b");

        builder.clear();

        InstanceDeclaration declaration2 = (InstanceDeclaration) builder.build();
        assertTrue(declaration2.getConfiguration().isEmpty());

    }
View Full Code Here

TOP

Related Classes of org.apache.felix.ipojo.extender.InstanceDeclaration

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.