Extract environment name i.e. DEV/UAT/STAGING in model class or servlet? | Community
Skip to main content
October 16, 2015
Solved

Extract environment name i.e. DEV/UAT/STAGING in model class or servlet?

  • October 16, 2015
  • 1 reply
  • 1207 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by JustinEd3

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 

1 reply

JustinEd3Adobe EmployeeAccepted solution
Adobe Employee
October 16, 2015

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