I am having issues trying to access a service of mine in a BundleListener. The service (LanguageService) works in different parts of my code outside of the BundleListener (for example my rest endpoints).
Activator:
public class Activator implements BundleActivator {
@Override
public void start(BundleContext bundleContext) throws Exception {
bundleContext.addBundleListener(new PlatformCoreBundleListener());
}
...
}
BundleListener:
@Component(immediate = true)
public class PlatformCoreBundleListener implements BundleListener {
private static final Logger LOG = LoggerFactory.getLogger("AsAdventure");
@Reference
private LanguageService languageService;
@Override
public void bundleChanged(BundleEvent bundleEvent) {
String symbolicName = bundleEvent.getBundle().getSymbolicName();
if (symbolicName.equalsIgnoreCase(BundleConstants.SYMBOLIC_AEM_PLATFORM_CORE_BUNDLE)) {
if (bundleEvent.getType() == BundleEvent.STARTED) {
LOG.info("[PlatformCoreBundleListener]: " + BundleConstants.SYMBOLIC_AEM_PLATFORM_CORE_BUNDLE + " has started.");
}
}
}
}
The languageService is always null even when I take a look at it in debug mode while the bundleEvent has a type of 'STARTED'.
What am I doing wrong? Thanks in advance!
Solved! Go to Solution.
Hi,
In the Activator class you create a PlatformCoreBundleListener through the use of the constructor ("new ..."). In that case the object is not constructed by the SCR runtime, which would handle all the injections.
You should get a reference to the PlatformCoreBundleListener instead.
But:I am not sure if this works either. IIRC the bundle activator is called by the OSGI framework itself and not by instantiated by the SCR framework. And then the @Reference injections are not handled as well.
That's the reason why I hardly use Bundle activators.
regards,
Jörg
are all the bundles active?
Views
Replies
Total Likes
Yes they are.
Example of me disabling the acs commons bundle while in debug mode:
Views
Replies
Total Likes
Can you go into "components" of the system/console and lookup your component?
There should be the reason displayed why the service is null
Views
Replies
Total Likes
com.company.core.service.PlatformCoreBundleListener
|
Everything seems to be ok here.
Btw my original use case was to provide a set of permission for the System-user.
Views
Replies
Total Likes
Hi,
In the Activator class you create a PlatformCoreBundleListener through the use of the constructor ("new ..."). In that case the object is not constructed by the SCR runtime, which would handle all the injections.
You should get a reference to the PlatformCoreBundleListener instead.
But:I am not sure if this works either. IIRC the bundle activator is called by the OSGI framework itself and not by instantiated by the SCR framework. And then the @Reference injections are not handled as well.
That's the reason why I hardly use Bundle activators.
regards,
Jörg
Thanks! Explains a lot. Will need to find another method to provide the set of default permission for my system-user.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies