Search in sources :

Example 1 with Attribute

use of org.apache.felix.metatype.Attribute in project felix by apache.

the class MetaTypeUtil method getProperties.

/**
 * Determines the actual configuration data based on the specified designate and object class definition
 *
 * @param designate The designate object containing the values for the properties
 * @param ocd The object class definition
 * @return A dictionary containing data as described in the designate and ocd objects, or <code>null</code> if the designate does not match it's
 * definition and the designate was marked as optional.
 * @throws ResourceProcessorException If the designate does not match the ocd and the designate is not marked as optional.
 */
public static Dictionary getProperties(Designate designate, ObjectClassDefinition ocd) throws ResourceProcessorException {
    Dictionary properties = new Hashtable();
    AttributeDefinition[] attributeDefs = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL);
    List<Attribute> attributes = designate.getObject().getAttributes();
    for (Attribute attribute : attributes) {
        String adRef = attribute.getAdRef();
        boolean found = false;
        for (int j = 0; j < attributeDefs.length; j++) {
            AttributeDefinition ad = attributeDefs[j];
            if (adRef.equals(ad.getID())) {
                // found attribute definition
                Object value = getValue(attribute, ad);
                if (value == null) {
                    if (designate.isOptional()) {
                        properties = null;
                        break;
                    } else {
                        throw new ResourceProcessorException(CODE_OTHER_ERROR, "Could not match attribute to it's definition: adref=" + adRef);
                    }
                }
                properties.put(adRef, value);
                found = true;
                break;
            }
        }
        if (!found) {
            if (designate.isOptional()) {
                properties = null;
                break;
            } else {
                throw new ResourceProcessorException(CODE_OTHER_ERROR, "Could not find attribute definition: adref=" + adRef);
            }
        }
    }
    return properties;
}
Also used : Dictionary(java.util.Dictionary) ResourceProcessorException(org.osgi.service.deploymentadmin.spi.ResourceProcessorException) Attribute(org.apache.felix.metatype.Attribute) Hashtable(java.util.Hashtable) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition)

Aggregations

Dictionary (java.util.Dictionary)1 Hashtable (java.util.Hashtable)1 Attribute (org.apache.felix.metatype.Attribute)1 ResourceProcessorException (org.osgi.service.deploymentadmin.spi.ResourceProcessorException)1 AttributeDefinition (org.osgi.service.metatype.AttributeDefinition)1