Search in sources :

Example 1 with UnacceptableConfiguration

use of org.apache.felix.ipojo.UnacceptableConfiguration in project felix by apache.

the class TestInstances method testOneDuplicateInstance.

@Test
public void testOneDuplicateInstance() {
    Factory factory = ipojoHelper.getFactory("OneDuplicateInstance");
    assertNotNull(factory);
    // Create one instance
    ComponentInstance instance = createInstanceOrFail(factory, "OneDuplicateInstance-0");
    assertNotNull(factory.getInstancesNames());
    assertNotNull(factory.getInstances());
    assertEquals(1, factory.getInstancesNames().size());
    assertEquals(1, factory.getInstances().size());
    assertContains("", factory.getInstancesNames().toArray(new String[1]), "OneDuplicateInstance-0");
    // The instance should not be created, and we should have only the first instance
    try {
        createInstance(factory, "OneDuplicateInstance-0");
        fail("The instance OneDuplicateInstance-0 has been created with factory " + factory.getName() + ". " + "It's shouldn't have been created has it's a duplicate instance");
    } catch (UnacceptableConfiguration e) {
    // expected Exception
    } catch (Exception e) {
        fail("Cannot create instance OneDuplicateInstance-0 with factory " + factory.getName() + ". " + "The wrong exception has been raised : " + e.toString());
    }
    assertNotNull(factory.getInstancesNames());
    assertNotNull(factory.getInstances());
    assertEquals(1, factory.getInstancesNames().size());
    assertEquals(1, factory.getInstances().size());
    // Dispose of the instance
    instance.dispose();
    assertNotNull(factory.getInstancesNames());
    assertNotNull(factory.getInstances());
    assertEquals(0, factory.getInstancesNames().size());
    assertEquals(0, factory.getInstances().size());
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) UnacceptableConfiguration(org.apache.felix.ipojo.UnacceptableConfiguration) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) MissingHandlerException(org.apache.felix.ipojo.MissingHandlerException) Test(org.junit.Test)

Example 2 with UnacceptableConfiguration

use of org.apache.felix.ipojo.UnacceptableConfiguration in project felix by apache.

the class CompositeFactory method reconfigure.

/**
 * Reconfigure an existing instance.
 * @param properties : the new configuration to push.
 * @throws UnacceptableConfiguration : occurs if the new configuration is
 * not consistent with the component type.
 * @throws MissingHandlerException : occurs when an handler is unavailable when creating the instance.
 * @see org.apache.felix.ipojo.Factory#reconfigure(java.util.Dictionary)
 */
public synchronized void reconfigure(Dictionary properties) throws UnacceptableConfiguration, MissingHandlerException {
    if (properties == null || (properties.get("instance.name") == null && properties.get("name") == null)) {
        // Support both instance.name and name
        throw new UnacceptableConfiguration("The configuration does not contains the \"instance.name\" property");
    }
    String name = (String) properties.get("instance.name");
    if (name == null) {
        name = (String) properties.get("name");
    }
    ComponentInstance instance = m_componentInstances.get(name);
    if (instance == null) {
        // The instance does not exists.
        return;
    }
    // re-configure the component
    instance.reconfigure(properties);
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) UnacceptableConfiguration(org.apache.felix.ipojo.UnacceptableConfiguration)

Example 3 with UnacceptableConfiguration

use of org.apache.felix.ipojo.UnacceptableConfiguration in project felix by apache.

the class InstanceHandler method createInstance.

/**
 * Create an instance using the given factory and the given configuration.
 *
 * @param fact : the factory name to used.
 * @param config : the configuration.
 */
private void createInstance(Factory fact, ManagedConfiguration config) {
    Dictionary conf = config.getConfiguration();
    try {
        config.setInstance(fact.createComponentInstance(conf, m_scope));
        config.setFactory(fact.getName());
        config.getInstance().addInstanceStateListener(this);
    } catch (UnacceptableConfiguration e) {
        error("A factory is available for the configuration but the configuration is not acceptable", e);
    } catch (MissingHandlerException e) {
        error("The instance creation has failed, at least one handler is missing", e);
    } catch (ConfigurationException e) {
        error("The instance creation has failed, an error during the configuration has occured", e);
    }
}
Also used : Dictionary(java.util.Dictionary) MissingHandlerException(org.apache.felix.ipojo.MissingHandlerException) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) UnacceptableConfiguration(org.apache.felix.ipojo.UnacceptableConfiguration)

Example 4 with UnacceptableConfiguration

use of org.apache.felix.ipojo.UnacceptableConfiguration in project felix by apache.

the class SvcInstance method onServiceArrival.

/**
 * A new service is injected.
 * This method create the sub-service instance in the composite.
 * @param ref : service reference.
 * @see org.apache.felix.ipojo.util.DependencyModel#onServiceArrival(org.osgi.framework.ServiceReference)
 */
public void onServiceArrival(ServiceReference ref) {
    // The given factory matches.
    try {
        Factory fact = (Factory) getService(ref);
        if (m_factories.get(ref) == null) {
            ComponentInstance instance = createInstance(fact);
            m_factories.put(ref, instance);
        } else {
            m_handler.info("An arriving factory is already used, ignore the creation : " + fact.getName());
        }
    } catch (UnacceptableConfiguration e) {
        m_handler.error("A matching factory refuses the actual configuration : " + e.getMessage());
        m_handler.getCompositeManager().stop();
    } catch (MissingHandlerException e) {
        m_handler.error("A matching factory is no more valid : " + e.getMessage());
        m_handler.getCompositeManager().stop();
    } catch (ConfigurationException e) {
        m_handler.error("A matching configuration is refused by the instance : " + e.getMessage());
        m_handler.getCompositeManager().stop();
    }
}
Also used : MissingHandlerException(org.apache.felix.ipojo.MissingHandlerException) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) UnacceptableConfiguration(org.apache.felix.ipojo.UnacceptableConfiguration)

Example 5 with UnacceptableConfiguration

use of org.apache.felix.ipojo.UnacceptableConfiguration in project felix by apache.

the class ProvidedService method register.

/**
 * Register the exposed service.
 */
public void register() {
    Properties props = new Properties();
    props.put("instance.name", m_instanceName);
    List fields = m_composition.getFieldList();
    for (int i = 0; i < fields.size(); i++) {
        FieldMetadata field = (FieldMetadata) fields.get(i);
        if (field.isUseful() && !field.getSpecification().isInterface()) {
            String type = field.getSpecification().getComponentType();
            Object pojo = getObjectByType(type);
            props.put(field.getName(), pojo);
        }
    }
    if (m_instance == null) {
        // Else we have to create the instance
        try {
            m_instance = m_factory.createComponentInstance(props, m_manager.getServiceContext());
        } catch (UnacceptableConfiguration e) {
            throw new IllegalStateException("Cannot create the service implementation", e);
        } catch (MissingHandlerException e) {
            throw new IllegalStateException("Cannot create the service implementation", e);
        } catch (ConfigurationException e) {
            throw new IllegalStateException("Cannot create the service implementation", e);
        }
    } else {
        // We have to reconfigure the instance in order to inject up to date glue component instance.
        m_instance.reconfigure(props);
    }
    m_exports.start();
}
Also used : MissingHandlerException(org.apache.felix.ipojo.MissingHandlerException) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) List(java.util.List) Properties(java.util.Properties) UnacceptableConfiguration(org.apache.felix.ipojo.UnacceptableConfiguration)

Aggregations

UnacceptableConfiguration (org.apache.felix.ipojo.UnacceptableConfiguration)6 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)5 MissingHandlerException (org.apache.felix.ipojo.MissingHandlerException)5 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)3 Factory (org.apache.felix.ipojo.Factory)3 Properties (java.util.Properties)2 Dictionary (java.util.Dictionary)1 List (java.util.List)1 HandlerFactory (org.apache.felix.ipojo.HandlerFactory)1 HandlerManager (org.apache.felix.ipojo.HandlerManager)1 PolicyServiceContext (org.apache.felix.ipojo.PolicyServiceContext)1 ServiceDependencyHandler (org.apache.felix.ipojo.composite.service.instantiator.ServiceDependencyHandler)1 ServiceImporter (org.apache.felix.ipojo.composite.service.instantiator.ServiceImporter)1 Element (org.apache.felix.ipojo.metadata.Element)1 Test (org.junit.Test)1 BundleContext (org.osgi.framework.BundleContext)1 Filter (org.osgi.framework.Filter)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1