<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>BundleActivator  implementation and OSGi injection</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=110859918" />
  <subtitle>BundleActivator  implementation and OSGi injection</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=110859918</id>
  <updated>2026-04-05T07:41:18Z</updated>
  <dc:date>2026-04-05T07:41:18Z</dc:date>
  <entry>
    <title>RE: BundleActivator  implementation and OSGi injection</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=110882480" />
    <author>
      <name>Ketan Solanki</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=110882480</id>
    <updated>2018-09-05T01:36:30Z</updated>
    <published>2018-09-05T01:36:30Z</published>
    <summary type="html">&lt;p&gt;Thanks Tomas for your time and response. This statement&lt;/p&gt;
&lt;pre&gt;
group.getExpandoBridge().setAttribute(&amp;quot;RefreshJson&amp;quot;, Boolean.TRUE, &lt;strong&gt;false&lt;/strong&gt;);&lt;/pre&gt;
&lt;p&gt;Has done the magic, along with BundleActivator :) so basically when I
  passed &lt;em&gt;secure: true&lt;/em&gt; as the last parameter, Liferay was
  checking all the permission logic and permission checker wasn't yet
  initialized so it threw exception!&lt;/p&gt;
&lt;p&gt;I tried with BundleTracker but somehow it's overridden methods were
  not printing the logs while deploying the portal. &lt;/p&gt;
&lt;p&gt;Anyways, problem solved - and once again thanks very much for the solution...&lt;/p&gt;</summary>
    <dc:creator>Ketan Solanki</dc:creator>
    <dc:date>2018-09-05T01:36:30Z</dc:date>
  </entry>
  <entry>
    <title>RE: BundleActivator  implementation and OSGi injection</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=110872867" />
    <author>
      <name>Tomáš Polešovský</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=110872867</id>
    <updated>2018-09-04T08:14:16Z</updated>
    <published>2018-09-04T08:14:16Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;what you needed was&amp;nbsp;&lt;code&gt;org.osgi.framework.BundleListener&lt;/code&gt;,
  not BundleActivator. BundleListener receives events about other
  bundles, BundleActivator fires when your module starts. Or better
  use&amp;nbsp;&lt;code&gt;org.osgi.util.tracker.BundleTracker&lt;/code&gt; that is easier
  and simpler to use.&lt;/p&gt;
&lt;p&gt;Related to the PermissionChecker exception: do you need to run the
  code with some signed in user privileges? It looks rather like a
  system job or similar, correct? In such case you can call
  setAttribute(..., false), i.e. something like this:&lt;/p&gt;
&lt;pre&gt;
group.getExpandoBridge().setAttribute("RefreshJson", Boolean.TRUE, &lt;strong&gt;false&lt;/strong&gt;);&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
  &lt;span style="display: none;"&gt;&amp;nbsp;&lt;/span&gt;Another problem is that OSGi
  modules usually start before portal is initialized, that means portal
  Spring services (including Group or Expando services) might not be up.
  It's a good practice to reference directly GroupLocalService from the
  component and don't rely on GroupLocalServiceUtil (static class) to
  make sure portal is up. Otherwise&amp;nbsp;you need to reference this
  "service" to make sure your component activates after&amp;nbsp;portal
  is up:&amp;nbsp;&lt;code&gt;@Reference(target =
    ModuleServiceLifecycle.PORTAL_INITIALIZED, unbind = "-")&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;So please use this:&lt;/p&gt;
&lt;pre&gt;
@Reference
private GroupLocalService groupLocalService;

private void setRefreshJsonTrue() {
    ...
    List&amp;lt;Group&amp;gt; groups = groupLocalService.getGroups(0, 10000);
    ...
}
&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;HTH.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>Tomáš Polešovský</dc:creator>
    <dc:date>2018-09-04T08:14:16Z</dc:date>
  </entry>
  <entry>
    <title>RE: BundleActivator  implementation and OSGi injection</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=110870115" />
    <author>
      <name>Ketan Solanki</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=110870115</id>
    <updated>2018-09-04T03:58:25Z</updated>
    <published>2018-09-04T03:58:25Z</published>
    <summary type="html">&lt;p&gt;Thanks very much Christoph and Olaf for your time and responses. &lt;/p&gt;
&lt;p&gt;@Christoph: I have tried the method you suggested, it gets invoked
  while portlet is starting up but even in that method the
  liferayInstanceService remains NULL. And that's true for the start
  method as well. &lt;/p&gt;
&lt;p&gt;@Olaf: I agree with you that DS and Bundle Activator don't go
  together! DS doesn't work at all on BundleActivator!&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;So I tried something completely different, like wrote the code of
  getting the site, and setting the field from the class itself - as below:&lt;/p&gt;
&lt;pre&gt;
private void setRefreshJsonTrue() {
    try {
        String siteName = ApplicationConfig.getInstance().defaultSiteName();

        List&amp;lt;Group&amp;gt; groups = GroupLocalServiceUtil.getGroups(0, 10000);
        for (Group group : groups) {
            if (group.getDescriptiveName().equalsIgnoreCase(siteName)) {
                &lt;u&gt;&lt;strong&gt;group.getExpandoBridge().setAttribute(&amp;quot;RefreshJson&amp;quot;, Boolean.TRUE, true);&lt;/strong&gt;&lt;/u&gt;
            }
        }
    } catch (PortalException pe) {
        LOG.error(&amp;quot;Error occurred while getting the site, try again!&amp;quot;);
    }
}
&lt;/pre&gt;
&lt;p&gt;I invoked above method from the start() but I got different and,
  obviously, legitimate exception. &lt;/p&gt;
&lt;p&gt;Caused by: java.lang.RuntimeException:
  com.liferay.portal.kernel.security.auth.PrincipalException:
    &lt;strong&gt;PermissionChecker not initialized&lt;/strong&gt;
  &lt;br /&gt;         at com.liferay.portlet.expando.model.impl.ExpandoBridgeImpl.setAttribute(ExpandoBridgeImpl.java:444)&lt;/p&gt;
&lt;p&gt;Because DS runs after bundle activator... &lt;/p&gt;
&lt;p&gt;I am sure someone would have definitely faced an issue like this,
  where a site custom field is changed everytime a deployment is made! &lt;/p&gt;
&lt;p&gt;Please guide me how to resolve this issue. &lt;/p&gt;
&lt;p&gt;Thanks, &lt;/p&gt;</summary>
    <dc:creator>Ketan Solanki</dc:creator>
    <dc:date>2018-09-04T03:58:25Z</dc:date>
  </entry>
  <entry>
    <title>RE: BundleActivator  implementation and OSGi injection</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=110866412" />
    <author>
      <name>Olaf Kock</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=110866412</id>
    <updated>2018-09-03T14:41:03Z</updated>
    <published>2018-09-03T14:41:03Z</published>
    <summary type="html">&lt;blockquote&gt;
  &lt;div class="quote-title"&gt;Ketan Solanki:&lt;/div&gt;
  &lt;div class="quote"&gt;
    &lt;div class="quote-content"&gt;But my primary purpose is to invoke a
      method of some other Component class from this start method.  &lt;pre&gt;
@Component(immediate = true, service = StartupActivator.class)
public class StartupActivator implements BundleActivator {

...
}
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;p&gt;I might be wrong here, but a DS as a Bundle Activator sounds weird to
  me: A BundleActivator is run before the DS initialization is done,
  thus I'd expect you'll have to decide for one: There are DS-Activators
  and BundleActivators.&lt;/p&gt;
&lt;p&gt;And as they're the source of resource- and memory leaks: Make sure
  that you deactivate if you're doing anything else than storing a
  String. Otherwise you might hold on to a service that's long gone and
  prevent its garbage collection.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;</summary>
    <dc:creator>Olaf Kock</dc:creator>
    <dc:date>2018-09-03T14:41:03Z</dc:date>
  </entry>
  <entry>
    <title>RE: BundleActivator  implementation and OSGi injection</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=110862369" />
    <author>
      <name>Christoph Rabel</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=110862369</id>
    <updated>2018-09-03T12:08:50Z</updated>
    <published>2018-09-03T12:08:50Z</published>
    <summary type="html">&lt;p&gt;Not sure if this will help you, but adding this reference here tells
  your module to wait, till the portal is initialized.&lt;/p&gt;
&lt;p&gt;      @Reference(target = ModuleServiceLifecycle.PORTAL_INITIALIZED,
  unbind = &amp;quot;-&amp;quot;)&lt;br /&gt;       protected void
  setModuleServiceLifecycle(ModuleServiceLifecycle
  moduleServiceLifecycle) {&lt;br /&gt;       }&lt;/p&gt;
&lt;p&gt;Please note, that there is something fishy here, I don't think that
  this will resolve the root problem. Are you sure that you don't get a
  reference to LiferayInstanceService?&lt;/p&gt;</summary>
    <dc:creator>Christoph Rabel</dc:creator>
    <dc:date>2018-09-03T12:08:50Z</dc:date>
  </entry>
  <entry>
    <title>BundleActivator  implementation and OSGi injection</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=110859917" />
    <author>
      <name>Ketan Solanki</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=110859917</id>
    <updated>2018-09-03T07:36:17Z</updated>
    <published>2018-09-03T07:36:17Z</published>
    <summary type="html">&lt;p&gt;Hi All,&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;I want to execute a piece of code every time a module is deployed! So
  I searched forums and I got following 2 useful APIs&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;com.liferay.portal.kernel.events.SimpleAction --&amp;gt; This gets
  activated ONLY while Liferay server is starting (&lt;a href="https://community.liferay.com/forums/-/message_boards/message/82509245"&gt;here&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;org.osgi.framework.BundleActivator --&amp;gt; This gets activated every
  time the module is deployed, so this fits (&lt;a href="https://community.liferay.com/forums/-/message_boards/message/82509245"&gt;here&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;If I put the LOG statements in the BundleActivator implementation, it
  gets printed!&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;But my primary purpose is to invoke a method of some other Component
  class from this start method. &lt;/p&gt;
&lt;pre&gt;
@Component(immediate = true, service = StartupActivator.class)
public class StartupActivator implements BundleActivator {

@Reference
private LiferayInstanceService liferayInstanceService;

private static final Log LOG = LogFactoryUtil.getLog(StartupActivator.class);

@Override
public void start(BundleContext context) throws Exception {
    LOG.info(&amp;quot;I am running NOW&amp;quot;);
    this.liferayInstanceService.setSiteCustomField(&amp;quot;RefreshJson&amp;quot;, Boolean.TRUE);
}
&lt;/pre&gt;
&lt;p&gt;Above code in start() method basically sets the
  &amp;quot;RefreshJson&amp;quot; site custom field to TRUE everytime this
  module is deployed! But I am not getting instance of &lt;strong&gt;
    &lt;em&gt;liferayInstanceService &lt;/em&gt;&lt;/strong&gt;injected into the class
  (this injection works fine in all other component classes throughout
  the project)! This class definition is as below...&lt;/p&gt;
&lt;pre&gt;
@Component(immediate = true, service = LiferayInstanceService.class)
public class LiferayInstanceService {
&lt;/pre&gt;
&lt;p&gt;So I guess BundleActivator class is invoked before OSGi initializes
  everything else or may be something else... Is there any way to get
  this instance of the class successfully injected into this? Please let
  me know this or any other way to do so...&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Ketan&lt;/p&gt;</summary>
    <dc:creator>Ketan Solanki</dc:creator>
    <dc:date>2018-09-03T07:36:17Z</dc:date>
  </entry>
</feed>
