Enable Run-Mode Specific Servlets in AEM
Servlets are one of the important building blocks of the AEM system to enable the AEM system interacts with external applications, servlets can expose data with external applications also data to be consumed by the Font-End layer.
Some of the time, we may have the use case to enable the servlets based on the run mode(author, publish, etc)of the AEM server, for example, the servlet should be enabled only in the servers with run mode publish or should be enabled only in servers with run mode dev.
In this tutorial, let us see the details on enabling the Servlets in AEM based on the run mode.
The ConfigurationPolicy helps to set the policy for the components through DS annotation.
The configurationPolicy controls whether component configurations must be satisfied depending on the presence of a corresponding Configuration object in the OSGi Configuration Admin service(osgi:config). A corresponding configuration is a Configuration object where the PID is the name of the component.
ConfigurationPolicy.IGNORE — Always allow the component configuration to be satisfied and do not use the corresponding Configuration object even if it is present.
ConfigurationPolicy.OPTIONAL — Use the corresponding Configuration object if present but allow the component to be satisfied even if the corresponding Configuration object is not present.
ConfigurationPolicy.REQUIRE — There must be a corresponding Configuration object for the component configuration to become satisfied.
By specifying the configuration policy as REQUIRE the component will be enabled only when the configuration(osgi:config) is available for the specific run mode otherwise the component will be unsatisfied.
By following this, we should be able to enable the Servlets on specific run mode, create the configuration(osgi:config) for the servlet PID on required run modes so the servlet will be enabled for those specific run modes and disabled on other run modes.