Search in sources :

Example 11 with PropertyDescription

use of org.apache.felix.ipojo.architecture.PropertyDescription in project felix by apache.

the class Property method getPropertyDescription.

/**
 * Gets the property description for the current property.
 * @param instance the component instance on which looking for the property.
 * @return the property description associated with the current property
 * or <code>null</code> if not found.
 */
public PropertyDescription getPropertyDescription(ComponentInstance instance) {
    PrimitiveInstanceDescription desc = (PrimitiveInstanceDescription) instance.getInstanceDescription();
    PropertyDescription[] props = desc.getProperties();
    for (int i = 0; i < props.length; i++) {
        if ((m_name != null && m_name.equals(props[i].getName())) || (m_field != null && m_field.equals(props[i].getName()))) {
            return props[i];
        }
    }
    return null;
}
Also used : PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription)

Example 12 with PropertyDescription

use of org.apache.felix.ipojo.architecture.PropertyDescription in project felix by apache.

the class SvcInstance method match.

/**
 * Does the service instance match with the given factory ?
 * @param fact : the factory to test.
 * @return true if the factory match, false otherwise.
 */
public boolean match(ServiceReference fact) {
    // Check if the factory can provide the specification
    ComponentTypeDescription desc = (ComponentTypeDescription) fact.getProperty("component.description");
    if (desc == null) {
        // No component type description.
        return false;
    }
    String[] provides = desc.getprovidedServiceSpecification();
    for (int i = 0; provides != null && i < provides.length; i++) {
        if (provides[i].equals(m_specification)) {
            // Check that the factory needs every properties contained in
            // the configuration
            PropertyDescription[] props = desc.getProperties();
            Properties conf = new Properties();
            Enumeration keys = m_configuration.keys();
            while (keys.hasMoreElements()) {
                String key = (String) keys.nextElement();
                if (!containsProperty(key, props)) {
                    return false;
                }
                conf.put(key, m_configuration.get(key));
            }
            Factory factory = (Factory) getService(fact);
            return factory.isAcceptable(conf);
        }
    }
    return false;
}
Also used : PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription) ComponentTypeDescription(org.apache.felix.ipojo.architecture.ComponentTypeDescription) Enumeration(java.util.Enumeration) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties)

Example 13 with PropertyDescription

use of org.apache.felix.ipojo.architecture.PropertyDescription in project felix by apache.

the class TestPropagation method testPropagationFromConfigurationAdminWhenCreatingTheInstance.

@Test
public void testPropagationFromConfigurationAdminWhenCreatingTheInstance() throws IOException {
    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put("message", "message");
    props.put("propagated", "propagated");
    props.put(".private", "wow");
    Configuration configuration = admin.createFactoryConfiguration(factoryName, "?");
    configuration.update(props);
    ServiceReference ref = osgiHelper.waitForService(FooService.class.getName(), "(instance.name=" + configuration.getPid() + ")", 1000);
    // Check the propagation
    assertEquals(ref.getProperty("propagated"), "propagated");
    assertEquals(ref.getProperty("message"), "message");
    assertNull(ref.getProperty(".private"));
    assertNull(ref.getProperty("private"));
    // Check the the .private property has the right value
    ConfigurationHandlerDescription desc = (ConfigurationHandlerDescription) ipojoHelper.getArchitectureByName(configuration.getPid()).getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:properties");
    PropertyDescription prop = desc.getPropertyByName(".private");
    assertEquals(prop.getValue(), "wow");
    // Update the property
    props.put("message", "message2");
    props.put("propagated", "propagated2");
    props.put(".private", "wow2");
    configuration.update(props);
    grace();
    ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), configuration.getPid());
    // Check the propagation
    assertEquals(ref.getProperty("propagated"), "propagated2");
    assertEquals(ref.getProperty("message"), "message2");
    desc = (ConfigurationHandlerDescription) ipojoHelper.getArchitectureByName(configuration.getPid()).getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:properties");
    prop = desc.getPropertyByName(".private");
    assertEquals(prop.getValue(), "wow2");
    configuration.delete();
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription) Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) ConfigurationHandlerDescription(org.apache.felix.ipojo.handlers.configuration.ConfigurationHandlerDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 14 with PropertyDescription

use of org.apache.felix.ipojo.architecture.PropertyDescription in project felix by apache.

the class TestArchitecture method testIntrospection.

/**
 * Checks the introspection possibilities before and after object creation.
 * Especially check the 'unvalued' case.
 */
@Test
public void testIntrospection() {
    Dictionary<String, String> configuration = new Hashtable<String, String>();
    configuration.put("p5", "v5i");
    configuration.put("p7", "v7i");
    configuration.put("p9", "v9i");
    ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.components.arch.MyComponentToIntrospect", configuration);
    // We don't get the service object until we finished the pre-instantiation tests.
    ServiceReference reference = osgiHelper.waitForService(CheckService.class.getName(), "(instance.name=" + instance.getInstanceName() + ")", 1000);
    assertNotNull(reference);
    PropertyDescription[] properties = ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getProperties();
    assertNotNull(properties);
    // Check the properties.
    assertEquals(getProperty(properties, "p1").getValue(), Property.UNVALUED);
    assertEquals(getProperty(properties, "p2").getValue(), Property.UNVALUED);
    // Default value
    assertEquals(getProperty(properties, "p3").getValue(), "v3");
    // Default value
    assertEquals(getProperty(properties, "p4").getValue(), "v4");
    // Instance value
    assertEquals(getProperty(properties, "p5").getValue(), "v5i");
    assertEquals(getProperty(properties, "p6").getValue(), Property.UNVALUED);
    assertEquals(getProperty(properties, "p6").getValue(), Property.UNVALUED);
    // Instance value
    assertEquals(getProperty(properties, "p7").getValue(), "v7i");
    assertEquals(getProperty(properties, "p8").getValue(), Property.UNVALUED);
    // Instance value
    assertEquals(getProperty(properties, "p9").getValue(), "v9i");
    // Check the propagation
    assertNull(reference.getProperty("p1"));
    assertNull(reference.getProperty("p2"));
    assertEquals(reference.getProperty("p3"), "v3");
    assertEquals(reference.getProperty("p4"), "v4");
    assertEquals(reference.getProperty("p5"), "v5i");
    assertNull(reference.getProperty("p6"));
    assertNull(reference.getProperty("p62"));
    assertEquals(reference.getProperty("p7"), "v7i");
    assertNull(reference.getProperty("p8"));
    assertEquals(reference.getProperty("p9"), "v9i");
    // Trigger instantiation
    assertTrue(((CheckService) context.getService(reference)).check());
    // Check new value.
    assertEquals(getProperty(properties, "p1").getValue(), "v1");
    assertEquals(getProperty(properties, "p2").getValue(), "v2");
    // Default value
    assertEquals(getProperty(properties, "p3").getValue(), "v3");
    // Default value
    assertEquals(getProperty(properties, "p4").getValue(), "v42");
    // Field value
    assertEquals(getProperty(properties, "p5").getValue(), "v52");
    // Specific value used for null
    assertEquals(getProperty(properties, "p6").getValue(), Property.UNVALUED);
    // Specific value used for null
    assertEquals(getProperty(properties, "p62").getValue(), "null");
    // Instance value
    assertEquals(getProperty(properties, "p7").getValue(), "v7i");
    assertEquals(getProperty(properties, "p8").getValue(), Property.UNVALUED);
    // Instance value
    assertEquals(getProperty(properties, "p9").getValue(), "v9i");
// New valued properties are not propagated.
// It avoids having fluctuation ins the service registrations
// To enable this propagation use @ServiceProperty
}
Also used : PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription) Hashtable(java.util.Hashtable) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 15 with PropertyDescription

use of org.apache.felix.ipojo.architecture.PropertyDescription in project felix by apache.

the class IPOJOPlugin method getFactoryDetail.

/**
 * Writes the JSON object containing details about a specific factory.
 * @param pw the writer
 * @param name the factory name
 * @throws IOException if the json object cannot be written.
 */
private void getFactoryDetail(JSONWriter pw, String name) throws IOException {
    // Find the factory
    Factory factory = null;
    for (Factory fact : m_factories) {
        if (fact.getName().equals(name)) {
            factory = fact;
        }
    }
    if (factory == null) {
        // This will be used a error message (cannot be interpreted as json)
        pw.value("The factory " + name + " does not exist or is private");
        return;
    }
    pw.object();
    // Statline.
    pw.key("count");
    pw.value(m_factories.size());
    pw.key("valid_count");
    pw.value(StateUtils.getValidFactoriesCount(m_factories));
    pw.key("invalid_count");
    pw.value(StateUtils.getInvalidFactoriesCount(m_factories));
    // End of the statline
    // Factory object
    pw.key("data");
    pw.object();
    pw.key("name");
    pw.value(factory.getName());
    pw.key("state");
    pw.value(StateUtils.getFactoryState(factory.getState()));
    String bundle = factory.getBundleContext().getBundle().getSymbolicName() + " (" + factory.getBundleContext().getBundle().getBundleId() + ")";
    pw.key("bundle");
    pw.value(bundle);
    // Provided service specifications
    if (factory.getComponentDescription().getprovidedServiceSpecification().length != 0) {
        pw.key("services");
        pw.value(factory.getComponentDescription().getprovidedServiceSpecification());
    }
    // Properties
    PropertyDescription[] props = factory.getComponentDescription().getProperties();
    if (props != null && props.length != 0) {
        pw.key("properties");
        pw.array();
        for (int i = 0; i < props.length; i++) {
            pw.object();
            pw.key("name");
            pw.value(props[i].getName());
            pw.key("type");
            pw.value(props[i].getType());
            pw.key("mandatory");
            pw.value(props[i].isMandatory());
            pw.key("immutable");
            pw.value(props[i].isImmutable());
            if (props[i].getValue() != null) {
                pw.key("value");
                pw.value(props[i].getValue());
            }
            pw.endObject();
        }
        pw.key("properties");
        pw.array();
        pw.endArray();
    }
    if (!factory.getRequiredHandlers().isEmpty()) {
        pw.key("requiredHandlers");
        pw.value(factory.getRequiredHandlers());
    }
    if (!factory.getMissingHandlers().isEmpty()) {
        pw.key("missingHandlers");
        pw.value(factory.getMissingHandlers());
    }
    List<?> instances = StateUtils.getInstanceList(m_archs, name);
    if (!instances.isEmpty()) {
        pw.key("instances");
        pw.value(instances);
    }
    pw.key("architecture");
    pw.value(factory.getDescription().toString());
    pw.endObject();
    pw.endObject();
}
Also used : PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription) HandlerFactory(org.apache.felix.ipojo.HandlerFactory) Factory(org.apache.felix.ipojo.Factory)

Aggregations

PropertyDescription (org.apache.felix.ipojo.architecture.PropertyDescription)17 Element (org.apache.felix.ipojo.metadata.Element)9 Test (org.junit.Test)8 ComponentTypeDescription (org.apache.felix.ipojo.architecture.ComponentTypeDescription)6 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)6 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)3 Factory (org.apache.felix.ipojo.Factory)3 FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)3 PojoMetadata (org.apache.felix.ipojo.parser.PojoMetadata)3 ServiceReference (org.osgi.framework.ServiceReference)3 Hashtable (java.util.Hashtable)2 Properties (java.util.Properties)2 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)2 Attribute (org.apache.felix.ipojo.metadata.Attribute)2 MethodMetadata (org.apache.felix.ipojo.parser.MethodMetadata)2 Dictionary (java.util.Dictionary)1 Enumeration (java.util.Enumeration)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)1