RE: How do I disable a component on a service-builder bundle?

Christopher Virtucio, modified 6 Years ago. Junior Member Posts: 33 Join Date: 9/26/17 Recent Posts
Hi,

I'm trying to override com-liferay-calendar's EmailNotificationSender(https://github.com/liferay/com-liferay-calendar/blob/b08808950ca37bfbe57913db198335afe88ab236/calendar-service/src/main/java/com/liferay/calendar/notification/impl/EmailNotificationSender.java). I used an OSGi Fragment Bundle (https://liferay.dev/blogs/-/blogs/osgi-fragment-bundles) to do this. However, whenever I deploy this fragment, I get the ff. exception:

2019-07-10 17:07:14.706 ERROR [Start Level: Equinox Container: 00748f1f-35a3-0019-1fe6-d9dd9d798b39][com_liferay_calendar_service:103] [com.liferay.calendar.notification.impl.EmailNotificationSender] Cannot register Component
org.osgi.service.component.ComponentException: The component name 'com.liferay.calendar.notification.impl.EmailNotificationSender' has already been registered by Bundle 152 (com.liferay.calendar.service) as Component of Class com.liferay.calendar.notification.impl.EmailNotificationSender

This makes sense, since the linked blog does state that fragments can only append files, not replace them. The next logical step appears to be ensuring that the original NotificationSender is unregistered, before attempting to register EmailNotificationSender. Is this possible? If so, how do I go about doing this? If not, what alternative can I pursue?
thumbnail
Amos Fong, modified 6 Years ago. Liferay Legend Posts: 2047 Join Date: 10/7/08 Recent Posts
I think you can do this another way.
You might need to dig through the source code to see how it's referenced where you want to override it. If it's done with a @Reference, you should be able to get your component in there with a high service.ranking on your osgi component.
If it's retrieved from NotificationSenderFactory, you could override the original one by making sure your component activates after the original so it will get replaced in the hashmap.
thumbnail
David H Nebinger, modified 6 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
I thought I was clear in my blog that OSGi fragment bundles could not replace classes from the host bundle, only jsp files or add new files. Existing classes can't be touched because the default order of class loading comes from the host bundle first, the fragment bundle second.
thumbnail
Minhchau Dang, modified 6 Years ago. Liferay Master Posts: 598 Join Date: 10/22/07 Recent Posts
The next logical step appears to be ensuring that the original NotificationSender is unregistered, before attempting to register EmailNotificationSender. Is this possible? If so, how do I go about doing this? If not, what alternative can I pursue?

In an ideal world, you'd be on 7.0.6 GA5 or later, or you're on 7.1 or 7.2, and you'd just blacklist the original NotificationSender component.

If you're on 7.0.5 GA4 or earlier, the process is more complicated, because you'll need to use the ServiceComponentRuntime in order to lookup the ComponentDescriptionDTO to disable the component. This process is described in an earlier forum thread.