Extract environment name i.e. DEV/UAT/STAGING in model class or servlet?
Hi,
I have to dynamically extract the environment on which the code is currently running?
Any pointers on how to do it?
Regards,
Shalul Rohilla
Hi,
I have to dynamically extract the environment on which the code is currently running?
Any pointers on how to do it?
Regards,
Shalul Rohilla
Hi Shalul,
It depends a bit on how you are defining the environment name. If you are using Sling runmodes, then you would use the SlingSettingsService (http://dev.day.com/docs/en/cq/current/javadoc/org/apache/sling/settings/SlingSettingsService.html) to get the current set of runmodes.
However, I'd suggest trying to avoid this. Typically, it is better to write your code against named configuration variables and then have those variables change per envrionment.
For example, let's say that you have a service which needs to send an email, but only in stage and production. One way would be to write:
Set<String> runmodes = slingSettingsService.getRunModes(); if (runmodes.contains(PRODUCTION) || runmodes.contains(STAGE)) { sendEmail(); }Better would be:
if (shouldSendEmail) { sendEmail(); }where shouldSendEmail is an OSGi-configured property which defaults to false. To set it in production and stage, you'd create /apps/myapp/config.production/<serviceclass> and /apps/myapp/config.stage/<serviceclass>.
This isn't appropriate 100% of the time, but it is appropriate far more frequently than manually checking for the runmodes.
HTH,
Justin
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.