Title: Apache Felix Metatype Service
The OSGi Metatype specification provides a unified way to describe metadata about services and a Java interface to retrieve and manipulate such information. This information can optionally be localized, thus allowing the developer to provide human-readable metadata descriptions in multiple languages. The specification defines a rich dynamic typing system to describe service attributes in a precise way, which makes it possible to dynamically create user interfaces to configure services.
The simplest way to specify service metatype information is to add one or more XML files in the `OSGI-INF/metatype` folder.
For example, the `OSGI-INF/metatype/metatype.xml` file included in the [Apache Felix File Install]({{ refs.apache-felix-file-install.path }}) subproject is as follows:
The main elements of this simple example are:
* *OCD*, Object Class Definition; contains the information about a set of attributes.
* *AD*, Attribute Definition; describes an attribute (name, description, unique id, type, cardinality, default value, ...).
* *Designate*, defines the association between an Object Class Definition and a pid; the pid can be a reference to a factory or a singleton configuration object.
The following types are supported by the specification:
* String
* Long
* Double
* Float
* Integer
* Byte
* Char
* Boolean
* Short
## Localization
As mentioned before, it is possible to localize metatype information. A good example is the [Apache Felix Web Console]({{ refs.apache-felix-web-console.path }}) subproject, which includes the following metatype information:
The key elements to write a localized metatype definition are
* The *localization* attribute, that points to the property file that localize this XML.
* The *%* notation that specifies a localizable string name.
* One or more property files defining localized strings.
The following example shows the localization property file of the Web Console:
manager.name = Apache Felix OSGi Management Console
manager.description = Configuration of the Apache Felix OSGi Management Console.
manager.root.name = Root URI
manager.root.description = The root path to the OSGi Management Console.
realm.name = Realm
realm.description = The name of the HTTP Authentication Realm.
username.name = User Name
username.description = The name of the user allowed to access the OSGi \
Management Console. To disable authentication clear this value.
password.name = Password
password.description = The password for the user allowed to access the OSGi \
Management Console.
default.render.name = Default Page
default.render.description = The name of the default configuration page \
when invoking the OSGi Management console.
## Metatype Service API
Before using the Metatype Service API, it's worth noting that this specification is not part of the OSGi core specification, so it's not included in the standard Felix distribution. It is therefore necessary to download and install the additional Metatype bundle.
Accessing the Metatype service is as simple as performing a lookup in the service registry:
ServiceReference metatypeRef = bundleContext.getServiceReference(MetaTypeService.class.getName());
MetaTypeService service = (MetaTypeService) bundleContext.getService(metatypeRef);
MetaTypeInformation information = service.getMetaTypeInformation(myBundle);
After retrieving the `MetaTypeInformation` object for the required bundle, we have access to all the information contained in the metatype XML definition. In particular, it is possible to access the ObjectClassDefinition (optionally specifying a locale):
ObjectClassDefinition ocd = information.getObjectClassDefinition(pid, locale);
With the `ObjectClassDefinition`, it is possible to retrieve all the attribute definitions:
AttributeDefinition[] attributes = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL);