This code does what it’s supposed to do. But there can be a problem, that you want to run this code not only on authors, but (for whatever reasons) also on publish. Or you don’t want to run the code on UAT authors.
In such cases this code does not work anymore, because it’s not flexible enough (the runmode is hardcoded); any change requires a code change and deployment.

A better way is to reformulate the requirement a bit: “The data import will only run if there is the ‘importActive’ flag set to true”.

If you design this flag “importActive” as an OSGI config, and combine it with runmode dependent configuration, then you can achieve the same behaviour as above, but be much more flexible. You can even disable it (and if only for a certain time).

Now you translate the requirement “imports should only happen on authoring” into a configuration decision, and it’s no longer hardcoded. And that’s why the reason why I will be picky on code reviews about it.

Post navigation

2 thoughts on “AEM coding pattern: Run-mode specific code”

On my project we had similar task: import for pages should be performed only on author instance.
To implement this functionality, we used ConfigurationPolicy.REQUIRED for the service that performed import.
If on author instance there is no config file for this service, service doesn`t exist on author instance, and import is not performed.
In this case there is no need for creating an OSGI config with flag value and add check on Java code.