You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by Steinar Bang <sb...@dod.no> on 2024/03/15 16:37:45 UTC

Programmatically restart bundle and/or component?

I have this bundle:
 https://github.com/steinarb/oldalbum/tree/master/oldalbum.web.security

which contains two DS/SCR components:

 1. A component implementing ServletContextHelper used to define the
    servlet context
     https://github.com/steinarb/oldalbum/blob/master/oldalbum.web.security/src/main/java/no/priv/bang/oldalbum/web/security/OldAlbumServletContextHelper.java

 2. A component implementing a Shiro servlet Filter
     https://github.com/steinarb/oldalbum/blob/master/oldalbum.web.security/src/main/java/no/priv/bang/oldalbum/web/security/OldAlbumShiroFilter.java#L35

I would like to reload the shiro servlet filter programmatically when
its configuration changes.

What's the best way of doing that?

Reload the entire bundle?

Reload just the filter component?

Make the filter reinitialize itself without reloading?

Has anyone done anything similar?

Suggestions? Ideas? All are welcome!

Thanks!


- Steinar


Re: Programmatically restart bundle and/or component?

Posted by Steinar Bang <sb...@dod.no>.
>>>>> Jean-Baptiste Onofré <jb...@nanthrax.net>:

> You can create a controller for instance by leveraging event admin (to
> send an event triggering the reload, and the bundle decides what he
> wants to reload). Pros, you control exactly what you want to reload.
> Cons, you need to implement your Event handler.
> Another option would be to define a dependency between components (via
> @Reference). That's probably the cleaner option, but it will do a full
> refresh.

Thanks for the pointer, JB!


Re: Programmatically restart bundle and/or component?

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
Hi Steinar,

You can create a controller for instance by leveraging event admin (to
send an event triggering the reload, and the bundle decides what he
wants to reload). Pros, you control exactly what you want to reload.
Cons, you need to implement your Event handler.
Another option would be to define a dependency between components (via
@Reference). That's probably the cleaner option, but it will do a full
refresh.

Regards
JB

On Fri, Mar 15, 2024 at 5:37 PM Steinar Bang <sb...@dod.no> wrote:
>
> I have this bundle:
>  https://github.com/steinarb/oldalbum/tree/master/oldalbum.web.security
>
> which contains two DS/SCR components:
>
>  1. A component implementing ServletContextHelper used to define the
>     servlet context
>      https://github.com/steinarb/oldalbum/blob/master/oldalbum.web.security/src/main/java/no/priv/bang/oldalbum/web/security/OldAlbumServletContextHelper.java
>
>  2. A component implementing a Shiro servlet Filter
>      https://github.com/steinarb/oldalbum/blob/master/oldalbum.web.security/src/main/java/no/priv/bang/oldalbum/web/security/OldAlbumShiroFilter.java#L35
>
> I would like to reload the shiro servlet filter programmatically when
> its configuration changes.
>
> What's the best way of doing that?
>
> Reload the entire bundle?
>
> Reload just the filter component?
>
> Make the filter reinitialize itself without reloading?
>
> Has anyone done anything similar?
>
> Suggestions? Ideas? All are welcome!
>
> Thanks!
>
>
> - Steinar
>

Re: Programmatically restart bundle and/or component?

Posted by Steinar Bang <sb...@dod.no>.
>>>>> Steinar Bang <sb...@dod.no>:

>> The version of the filter does runtime reconfiguration hasn't been
>> pushed yet, but it will look like this:
>>  https://gist.github.com/steinarb/1c16c1ce4f6148b47581019de1d23e40#file-oldalbumshirofilter-java-L66

>> Here an OldalbumService is injected and is used as a constructor
>> argument to an OldAlbumWebEnvironment, which will look like this (not
>> pushed yet):
>>  https://gist.github.com/steinarb/a7b4bff8786d9e0e37a8925e19090068#file-oldalbumwebenvironment-java-L23

> Will it be enough to run the activate method again, creating a new
> WebEnvironment and use it to set up things?

Answering my question here: Yes, unless I'm fooling myself during early
testing, it was enough to run the contents of the activate method again:

I was able to open an URL in the shiro filter at runtime.

Now just a teensy, weensy bit of cleanup remains (I just did the
quickest hackup I could do to create a new OSGi service, make the OSGi
service reload the config, expose the service all the way to the REST
API and add a button to trigger the reload in the react frontend).

Here is what I did (in non-cleanup form):

 1. Create an interface for a new service
     https://gist.github.com/steinarb/47b6de3f504e1fc8a570a26689644219

 2. Expose the service from the shiro filter and use it to do a reload
    of the shiro config
     https://gist.github.com/steinarb/39e9b4de1ee2ab6da57eea4c7df9e5b5#file-oldalbumshirofilter-java-L39

 3. Make a REST endpoint
     https://gist.github.com/steinarb/8a7b9e209a493f9d167924e58f3efde7#file-routesresource-java-L62

 4. Create a redux saga to call the REST endpoint
     https://gist.github.com/steinarb/dd72a9b095a5f48f6483ac12669a5124

 5. Create a menu entry to trigger the reload
     https://gist.github.com/steinarb/34339a67cb34742f2f89860bb585bf6c


Re: Programmatically restart bundle and/or component?

Posted by Steinar Bang <sb...@dod.no>.
>>>>> Steinar Bang <sb...@dod.no>:

> The version of the filter does runtime reconfiguration hasn't been
> pushed yet, but it will look like this:
>  https://gist.github.com/steinarb/1c16c1ce4f6148b47581019de1d23e40#file-oldalbumshirofilter-java-L66

> Here an OldalbumService is injected and is used as a constructor
> argument to an OldAlbumWebEnvironment, which will look like this (not
> pushed yet):
>  https://gist.github.com/steinarb/a7b4bff8786d9e0e37a8925e19090068#file-oldalbumwebenvironment-java-L23

Will it be enough to run the activate method again, creating a new
WebEnvironment and use it to set up things?


Re: Programmatically restart bundle and/or component?

Posted by Steinar Bang <sb...@dod.no>.
>>>>> João Assunção <jo...@exploitsys.com>:

> Hi Steinar
> A few years ago I implemented a servlet filter to handle CORS. The filter
> was a DS component and it accepted a configuration via activate. Everytime
> the config was changed, it would restart and update the filter.

You wouldn't happen to be able to share the shiro filter reload code, by
any chance...? :-)

(I understand if the answer is "no", but it doesn't hurt to ask...:-) )

> You can also have the DS component implement the filter interface and an
> auxiliary service interface with a method to force a reload of the config
> without restarting.
> I guess it depends on how you supply the configuration to the filter.

The filter is here:
 https://github.com/steinarb/oldalbum/blob/master/oldalbum.web.security/src/main/java/no/priv/bang/oldalbum/web/security/OldAlbumShiroFilter.java#L35

The version of the filter does runtime reconfiguration hasn't been
pushed yet, but it will look like this:
 https://gist.github.com/steinarb/1c16c1ce4f6148b47581019de1d23e40#file-oldalbumshirofilter-java-L66

Here an OldalbumService is injected and is used as a constructor
argument to an OldAlbumWebEnvironment, which will look like this (not
pushed yet):
 https://gist.github.com/steinarb/a7b4bff8786d9e0e37a8925e19090068#file-oldalbumwebenvironment-java-L23

The OldAlbumWebEnvironment in the constructor creates a custom Ini
object with an "urls" section containing paths retrieved from the
OldAlbumService.

So no notification implemented yet, just reading urls paths at startup,
in addition to what's read from the shiro.ini
 https://github.com/steinarb/oldalbum/blob/master/oldalbum.web.security/src/main/resources/shiro.ini


Re: Programmatically restart bundle and/or component?

Posted by João Assunção <jo...@exploitsys.com>.
Hi Steinar

A few years ago I implemented a servlet filter to handle CORS. The filter
was a DS component and it accepted a configuration via activate. Everytime
the config was changed, it would restart and update the filter.
You can also have the DS component implement the filter interface and an
auxiliary service interface with a method to force a reload of the config
without restarting.
I guess it depends on how you supply the configuration to the filter.

Regards,
João Assunção





On Fri, Mar 15, 2024 at 4:38 PM Steinar Bang <sb...@dod.no> wrote:

> I have this bundle:
>  https://github.com/steinarb/oldalbum/tree/master/oldalbum.web.security
>
> which contains two DS/SCR components:
>
>  1. A component implementing ServletContextHelper used to define the
>     servlet context
>
> https://github.com/steinarb/oldalbum/blob/master/oldalbum.web.security/src/main/java/no/priv/bang/oldalbum/web/security/OldAlbumServletContextHelper.java
>
>  2. A component implementing a Shiro servlet Filter
>
> https://github.com/steinarb/oldalbum/blob/master/oldalbum.web.security/src/main/java/no/priv/bang/oldalbum/web/security/OldAlbumShiroFilter.java#L35
>
> I would like to reload the shiro servlet filter programmatically when
> its configuration changes.
>
> What's the best way of doing that?
>
> Reload the entire bundle?
>
> Reload just the filter component?
>
> Make the filter reinitialize itself without reloading?
>
> Has anyone done anything similar?
>
> Suggestions? Ideas? All are welcome!
>
> Thanks!
>
>
> - Steinar
>
>