Skip to content

Descriptor Loader

papousek edited this page Apr 26, 2012 · 3 revisions

This extensions loads configuration file shared among all extensions. To set set the path to configuration file use parasim.config.file system property. Default value is set to parasim.xml.

Configuration File Format

Parasim uses XML file as a configuration file. The root element of this XML file is <parasim></parasim>. To configure extension use <extension></extension> element. The example follows:

<parasim>
    <extension qualifier="extensionA">
        <property name="configProperty">value</property>
        <property name="configPropertyArray">
            <value>1</value>
            <value>2</value>
            <value>3</value>
        </property>
    </extension>
</parasim>

extensionA is qualifier of the extension you want to configure.

The file is loaded to org.sybila.parasim.core.extension.configuration.api.ParasimDescriptor object. To retrieve configuration for your extension use getExtensionDescriptor() method.

This extension also provides an easy way to map Parasim configuration objects to your configuration beans. This functionality is provided by org.sybila.parasim.core.extension.configuration.api.ExtensionDescriptorMapper object.

Configuring through System Properties

The configuration from XML file can be overridden by system properties. If you want use this functionality, you mast use dots in the property name (instead of camel case) and prefix parasim.extensionname. To override property configProperty from the example above use parasim.extension.a.config.property.

Events

This extension fires org.sybila.parasim.core.extension.configuration.api.event.ConfigurationLoaded. This event is fired before org.sybila.parasim.core.event.ManagerStarted, so extensions loaded by Extension Loader can't observe it.

Example

Configuration Bean

public class ConfigurationBean {
    private Integer[] ports = new Integer[] {4444, 5555}; // WARNING: atomic types can't be used when array is needed
    private int timout = 10000;
    private Color color;

    public Color getColor() {
        return color;
    }

    public int getTimout() {
        return timout;
    }

    public int getPort(int index) {
        return ports[index];
    }

    public int getNumberOfPorts() {
        return ports.length;
    }

    public void validate() {
        if (ports == null) {
            throw new IllegalStateException("The property [ports] is not defined");
        }
        if (timout <= 0) {
            throw new IllegalStateException("The property [timout] has to be a positive number.");
        }
        if (color == null) {
            throw new IllegalStateException("The property [color] is not defined.");
        }
    }
}

Configuration File

<parasim>
    <extension qualifier="myextension">
        <property name="timout">5000<property>
        <property name="ports">
            <value>8080</value>
            <value>8081</value>
            <value>8082</value>
        </property>
        <property name="color">black</property>
    </extension>
</parasim>

Mapping

public class MyExtension {
    @Inject
    private Instance<ConfigurationBean> conf;

    public void prepareConfiguration(@Observes ConfigurationLoaded event, ParasimDescriptor descriptor, ExtensionDescriptorMapper mapper) {
        ConfigurationBean configBean = new ConfigurationBean();
        ExtensionDescriptor extensionDescriptor = descriptor.getExtensionDescriptor("myextension");
        if (extensionDescriptor != null) {
            mapper.map(extensionDescriptor, configBean);
        }
        configBean.validate();
        conf.set(configBean);
    }
}