Page tree
Skip to end of metadata
Go to start of metadata

This page explains how to configure modules in Magnolia. Module configuration data is useful because:

  • It can be accessed programmatically inside the same module and from other modules.
  • It can be changed on a running system.

The explanation on this page applies to Magnolia Maven modules with a module class.

How it works

A Magnolia Maven module may contain a module class. The class needs to be specified in the module descriptor. See, for example, line 6 below:

Module descriptor (src/main/resources/META-INF/magnolia/another-module.xml)
<!DOCTYPE module SYSTEM "module.dtd" >
<module>
  <name>another-module</name>
  <displayName>${project.name}</displayName>
  <description>${project.description}</description>
  <class>com.example.magnolia.anothermodule.AnotherModule</class>
  <versionHandler>com.example.magnolia.anothermodule.setup.AnotherModuleVersionHandler</versionHandler>
  <version>${project.version}</version>
  <dependencies>
    <dependency>
      <name>core</name>
      <version>6.1/*</version>
    </dependency>
  </dependencies>
</module>
On the module class, you define module configuration properties as Java bean properties. The property values the class defines may be default and are persisted in the configuration data. On module start-up or when configuration data changes, the system calls the properties setter methods to set the new values on the instance of the module class, using the values from the configuration data. Module configuration items are registered in the ModuleConfigurationRegistry. On a running system, you can use the Definitions app to check modules' configuration data. 

Changing the configuration on a running system

If you change configuration data on a running system, Magnolia is notified about it. When the configuration data changes, the properties on the module class are reset accordingly and the configuration item is re-registered in the module configuration registry (ModuleConfigurationRegistry).

If your module requires a specific re-initialization for some custom components, implement ModuleLifecycle and add module-specific code in the #start and #stop interface methods. The system calls these methods, if it detects changes in the configuration data.

Typically, when changing configuration data of modules provided by Magnolia, there is no need to restart the instance.

Configuration data

Configuration data can be stored either in a YAML file or under a JCR node in the config workspace. We recommend using a YAML file.

The structure of the configuration data depends on the bean properties defined on the module class.

The next subsection shows an example of configuration data for a hypothetical module class called com.example.magnolia.anothermodule.AnotherModule:

src/main/java/com/example/magnolia/anothermodule/AnotherModule.java
package com.example.magnolia.anothermodule;

import info.magnolia.module.ModuleLifecycle;
import info.magnolia.module.ModuleLifecycleContext;

import java.util.List;

public class AnotherModule implements ModuleLifecycle {

    private int maxSize;
    private String welcomeMessage;
    private FooBar fooBar;

    public String getWelcomeMessage() {
        return welcomeMessage;
    }

    public void setWelcomeMessage(String welcomeMessage) {
        this.welcomeMessage = welcomeMessage;
    }

    public FooBar getFooBar() {
        return fooBar;
    }

    public void setFooBar(FooBar fooBar) {
        this.fooBar = fooBar;
    }

    public int getMaxSize() {
        return maxSize;
    }


    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    @Override
    public void start(ModuleLifecycleContext moduleLifecycleContext) {
        // do here whatever is required on module start.
    }

    @Override
    public void stop(ModuleLifecycleContext moduleLifecycleContext) {
        // do here whatever is required on module stop.
    }


    public class FooBar {

        private List<String> colors;
        private boolean colorsEnabled;

        public boolean isColorsEnabled() {
            return colorsEnabled;
        }

        public void setColorsEnabled(boolean colorsEnabled) {
            this.colorsEnabled = colorsEnabled;
        }

        public List<String> getColors() {
            return colors;
        }

        public void setColors(List<String> colors) {
            this.colors = colors;
        }
    }

}

The config.yaml file

The YAML file containing module configuration data has to reside at src/main/resources/<module-name>/config.yaml.

Example:

src/main/resources/another-module/config.yaml
maxSize: 25
welcomeMessage: Hello world
fooBar:
  colors: [red, green, blue]

Changing module configuration

There are several ways to change an original configuration.

Original configuration
In YAML fileIn the JCR config workspace
  • Through decoration definition.
  • By applying a hotfix to the resources workspace.
  • Through decoration definition.
  • By editing directly in the config workspace.

(warning) If the original configuration is a YAML file, do not create a node in the JCR config workspace.

Decorating

Decorating means adapting the current configuration. A decorator file can reside in any Magnolia Maven module or light module. In the example below, we create a decorator file in a light module called test-module. In this module, create the decorations/another-module/config.fooBar.yaml file:

<magnolia.resources.dir>/test-module/decorations/another-module/config.fooBar.yaml
colors: [cyan, yellow, magenta, black]
To learn more about decoration, see Definition decoration concept.

Hotfixing

Creating a hotfix means utilizing the Magnolia Resources framework's capability which allows a resource to have different origins, see Resources - Origins and loading order. A hotfix:

  • Is a representation of a resource in the JCR resources workspace.
  • Has precedence over the YAML file in the jar file of the deployed module.

Utilizing the JCR resources workspace can be useful for an administrator to apply a hotfix that is urgent. From a long-term perspective, however, we recommend that you keep your configuration and configuration changes in YAML files. This way they may become part of source control and are easier for large developer teams to work with.

To create a hotfix, follow these steps:

  1. Open the Resource Files app.
  2. Browse to <your-module> and select the config.yaml resource.
  3. In the action bar, click Edit file. The Resource Files app creates a copy of the currently used configuration and stores it in the JCR resources workspace.
  4. Edit the file as necessary.
  5. Click Save changes.
    (You may have to publish the resource node created to your author instances.)

In the config workspace

Even though we recommend using YAML files for configuration data, there are still a lot of modules whose configuration is stored in the JCR config workspace. Below is an example using the ui-admincentral module, in which we change the defaultGroup of the appLauncherLayout

  1. Open the Configuration app.
  2. Go to the /modules/ui-admincentral/config/appLauncherLayout node.
  3. Change the value of the defaultGroup property. (Change other subnodes and properties as necessary.)

    You also can add new nodes and properties, if they match with the defined bean properties of the module class. 
    (info) Changes are applied automatically when you leave a field. (You may have to publish the node you changed to your author instances.)

Accessing configuration data programmatically

To access module configuration in a Java class, use javax.inject.Provider to access the sole instance of your module class. On the module class instance, you can call the getter methods of the module bean properties.

Example:

import javax.inject.Inject;
import javax.inject.Provider;
import com.example.magnolia.anothermodule.AnotherModule;

/**
 * An example class to demonstrate how to access the bean properties (= module configuration) of a module class.
 */
public class GarplyUtils {

    private final Provider<AnotherModule> anotherModuleProvider;

    @Inject
    public GarplyUtils(Provider<AnotherModule> anotherModuleProvider) {
        this.anotherModuleProvider = anotherModuleProvider;
    }

    public void doSomething() {

        // get the (singleton) instance of the module
        AnotherModule moduleInstance = anotherModuleProvider.get();

        // access the modules bean properties
        String welcomeMessage = moduleInstance.getWelcomeMessage();
        int maxSize = moduleInstance.getMaxSize();
        AnotherModule.FooBar fooBar = moduleInstance.getFooBar();

        // do whatever you want to do here
        // ...
    }

}

  • Line 10: Make the javax.inject.Provider a private final field of your class.
  • Lines 12-14: Inject the Provider via a constructor.
  • Line 20: Get the singleton instance of your AnotherModule module class.
  • Lines 23-25: Access the bean properties of your module.