Search in sources :

Example 1 with PropertyDescription

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

the class EventAdminSubscriberHandler method initializeComponentFactory.

/**
 * Initializes the component type.
 *
 * @param cd component type description to populate.
 * @param metadata component type metadata.
 * @throws ConfigurationException if the metadata are incorrect.
 * @see org.apache.felix.ipojo.Handler#initializeComponentFactory(
 * org.apache.felix.ipojo.architecture.ComponentTypeDescription, org.apache.felix.ipojo.metadata.Element)
 */
public void initializeComponentFactory(ComponentTypeDescription cd, Element metadata) throws ConfigurationException {
    // Update the current component description
    Dictionary dict = new Properties();
    cd.addProperty(new PropertyDescription(TOPICS_PROPERTY, Dictionary.class.getName(), dict.toString()));
    dict = new Properties();
    cd.addProperty(new PropertyDescription(FILTER_PROPERTY, Dictionary.class.getName(), dict.toString()));
    // Get Metadata subscribers
    Element[] subscribers = metadata.getElements("subscriber", NAMESPACE);
    if (subscribers != null) {
        // Maps used to check name and field are unique
        Set nameSet = new HashSet();
        Set callbackSet = new HashSet();
        // Check all subscribers are well formed
        for (int i = 0; i < subscribers.length; i++) {
            // Check the subscriber configuration is correct by creating an
            // unused subscriber metadata
            EventAdminSubscriberMetadata subscriberMetadata = new EventAdminSubscriberMetadata(getFactory().getBundleContext(), subscribers[i]);
            String name = subscriberMetadata.getName();
            info(LOG_PREFIX + "Checking subscriber " + name);
            // Determine the event callback prototype
            PojoMetadata pojoMetadata = getPojoMetadata();
            String callbackType;
            if (subscriberMetadata.getDataKey() == null) {
                callbackType = Event.class.getName();
            } else {
                callbackType = subscriberMetadata.getDataType().getName();
            }
            // Check the event callback method is present
            MethodMetadata methodMetadata = pojoMetadata.getMethod(subscriberMetadata.getCallback(), new String[] { callbackType });
            String callbackSignature = subscriberMetadata.getCallback() + "(" + callbackType + ")";
            if (methodMetadata == null) {
                throw new ConfigurationException("Cannot find callback method " + callbackSignature);
            }
            // Warn if the same callback is used by several subscribers
            if (callbackSet.contains(callbackSignature)) {
                warn("The callback method is already used by another subscriber : " + callbackSignature);
            } else {
                callbackSet.add(callbackSignature);
            }
            // Check name is unique
            if (nameSet.contains(name)) {
                throw new ConfigurationException("A subscriber with the same name already exists : " + name);
            }
            nameSet.add(name);
        }
        m_description = new EventAdminSubscriberHandlerDescription(this, subscribers);
    } else {
        info(LOG_PREFIX + "No subscriber to check");
    }
}
Also used : Element(org.apache.felix.ipojo.metadata.Element) PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) PojoMetadata(org.apache.felix.ipojo.parser.PojoMetadata) Event(org.osgi.service.event.Event) MethodMetadata(org.apache.felix.ipojo.parser.MethodMetadata)

Example 2 with PropertyDescription

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

the class EventAdminPublisherHandler method initializeComponentFactory.

/**
 * Initializes the component type.
 *
 * @param cd       the component type description to populate
 * @param metadata the component type metadata
 * @throws ConfigurationException if the given metadata is incorrect.
 * @see org.apache.felix.ipojo.Handler#initializeComponentFactory(
 * org.apache.felix.ipojo.architecture.ComponentTypeDescription, org.apache.felix.ipojo.metadata.Element)
 */
public void initializeComponentFactory(ComponentTypeDescription cd, Element metadata) throws ConfigurationException {
    // Update the current component description
    Dictionary dict = new Properties();
    PropertyDescription pd = new PropertyDescription(TOPICS_PROPERTY, Dictionary.class.getName(), dict.toString());
    cd.addProperty(pd);
    // Get Metadata publishers
    Element[] publishers = metadata.getElements("publisher", NAMESPACE);
    // if publisher is null, look for 'publishes' elements
    if (publishers == null || publishers.length == 0) {
        publishers = metadata.getElements("publishes", NAMESPACE);
    }
    if (publishers != null) {
        // Maps used to check name and field are unique
        Set nameSet = new HashSet();
        Set fieldSet = new HashSet();
        // Check all publishers are well formed
        for (int i = 0; i < publishers.length; i++) {
            // Check the publisher configuration is correct by creating an
            // unused publisher metadata
            EventAdminPublisherMetadata publisherMetadata = new EventAdminPublisherMetadata(publishers[i]);
            String name = publisherMetadata.getName();
            info(LOG_PREFIX + "Checking publisher " + name);
            // Check field existence and type
            String field = publisherMetadata.getField();
            FieldMetadata fieldMetadata = getPojoMetadata().getField(publisherMetadata.getField(), Publisher.class.getName());
            if (fieldMetadata == null) {
                throw new ConfigurationException("Field not found in the component : " + Publisher.class.getName() + " " + field);
            }
            // Check name and field are unique
            if (nameSet.contains(name)) {
                throw new ConfigurationException("A publisher with the same name already exists : " + name);
            } else if (fieldSet.contains(field)) {
                throw new ConfigurationException("The field " + field + " is already associated to a publisher");
            }
            nameSet.add(name);
            fieldSet.add(field);
        }
    } else {
        info(LOG_PREFIX + "No publisher to check");
    }
}
Also used : Dictionary(java.util.Dictionary) Set(java.util.Set) HashSet(java.util.HashSet) FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) Element(org.apache.felix.ipojo.metadata.Element) Properties(java.util.Properties) PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) HashSet(java.util.HashSet)

Example 3 with PropertyDescription

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

the class TestFactoryProperties method testProps.

@Test
public void testProps() {
    ServiceReference ref1 = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "Factories-FooProviderType-Dyn2");
    assertNotNull("The factory is available", ref1);
    PropertyDescription[] pd = (PropertyDescription[]) ref1.getProperty("component.properties");
    assertEquals("Check property list size", pd.length, 5);
    // P0
    assertEquals("0) Check name", "int", pd[0].getName());
    assertEquals("0) Check type", "int", pd[0].getType());
    assertEquals("0) Check value", "4", pd[0].getValue());
    // P1
    assertEquals("1) Check name", "boolean", pd[1].getName());
    assertEquals("1) Check type", "boolean", pd[1].getType());
    assertNull("1) Check value", pd[1].getValue());
    // P2
    assertEquals("2) Check name", "string", pd[2].getName());
    assertEquals("2) Check type", String.class.getName(), pd[2].getType());
    assertNull("2) Check value", pd[2].getValue());
    // P3
    assertEquals("3) Check name", "strAProp", pd[3].getName());
    assertEquals("3) Check type", "java.lang.String[]", pd[3].getType());
    assertNull("3) Check value", pd[3].getValue());
    // P4
    assertEquals("4) Check name", "intAProp", pd[4].getName());
    assertEquals("4) Check type", "int[]", pd[4].getType());
}
Also used : PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription) Factory(org.apache.felix.ipojo.Factory) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 4 with PropertyDescription

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

the class CheckServiceAndFieldInterceptorHandler method initializeComponentFactory.

public void initializeComponentFactory(ComponentTypeDescription cd, Element metadata) {
    cd.addProperty(new PropertyDescription("csh.simple", "java.lang.String", null));
    cd.addProperty(new PropertyDescription("csh.map", "java.util.Dictionary", null));
}
Also used : PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription)

Example 5 with PropertyDescription

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

the class CheckServiceHandler method initializeComponentFactory.

public void initializeComponentFactory(ComponentTypeDescription cd, Element metadata) {
    cd.addProperty(new PropertyDescription("csh.simple", "java.lang.String", null));
    cd.addProperty(new PropertyDescription("csh.map", "java.util.Dictionary", null));
}
Also used : PropertyDescription(org.apache.felix.ipojo.architecture.PropertyDescription)

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