Expand my Community achievements bar.

SOLVED

Disable component on specific run mode

Avatar

Level 2

Hi everyone,

we have two special components in an OSGi Bundle. One should only run on the author instance and the other only on publish. Does anyone of you know how to disable each component on author/publish while activating the bundle?

We've tried this so far:

private static final String AUTHOR_RUN_MODE = "author"; @Activate public void activate(final ComponentContext context) throws Exception { @SuppressWarnings("rawtypes") final Dictionary properties = context.getProperties(); if (!slingSettingsService.getRunModes().contains(AUTHOR_RUN_MODE)) { final String componentName = (String) properties.get(ComponentConstants.COMPONENT_NAME); context.disableComponent(componentName); } }

Thanks in advance!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

a better way would be to use OSGI bool  type property, read the value of that property (which will either be boolean true or false) and accordingly disable the OSGI component in activate method.

Now the value of the Bool property can be driven and forced to true or false as per your AEM run modes.. i.e. using runmode specific environment configuration file.

Follow the mentioned article to understand more on environment specific OSGI configuration - http://www.wemblog.com/2012/10/how-to-work-with-configurations-in-cq.html

Hope it helps.

- Runal

View solution in original post

5 Replies

Avatar

Correct answer by
Community Advisor

a better way would be to use OSGI bool  type property, read the value of that property (which will either be boolean true or false) and accordingly disable the OSGI component in activate method.

Now the value of the Bool property can be driven and forced to true or false as per your AEM run modes.. i.e. using runmode specific environment configuration file.

Follow the mentioned article to understand more on environment specific OSGI configuration - http://www.wemblog.com/2012/10/how-to-work-with-configurations-in-cq.html

Hope it helps.

- Runal

Avatar

Employee

I prefer to do this via configuration, and not via code.

You can use: policy=ConfigurationPolicy.REQUIRE on @Component for this

When this is on your OSGi-component, this component will only be 'active' if there is a config supplied for it.

Avatar

Level 2

Thanks for your help. We didn't want to use a configuration file but it looks like that this is the best way so far to solve the problem.