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 2025/04/11 22:51:44 UTC

code example that saves a value to a cfg file?

Is there a code example somewhere of an SCR component that saves a value
to a cfg file in karaf?

Thanks!


Re: code example that saves a value to a cfg file?

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

I have also tackled this before, and I achieved it using the ConfigAdmin
service. I think it has the advantage of being agnostic of the OSGi
runtime, which might be useful for some. When running in Karaf, the config
will be persisted to the cfg file backing it up. It has the disadvantage of
the properties in the cfg file becoming "typed".
One remark. I consider the cfg files in Karaf as another "interface" to set
configurations (similar to using config:edit), instead of being the files
where the configurations are persisted. The actual configurations are
stored by ConfigAdmin as typed properties inside ConfigAdmin's bundle
directory.

Best regards,
João Assunção




On Sat, Apr 12, 2025 at 8:25 PM Steinar Bang <sb...@dod.no> wrote:

> >>>>> Steinar Bang <sb...@dod.no>:
>
> >>> Is there a code example somewhere of an SCR component that saves a
> >>> value to a cfg file in karaf?
>
> > Looks like injecting a ConfigRepository service may be the way to go...?
> >
> https://github.com/apache/karaf/blob/main/config/core/src/main/java/org/apache/karaf/config/core/ConfigRepository.java#L26
>
> > I will try!
>
> Using ConfigReposity worked.
>
> This code gets config matching a pid which is the fully qualified
> classname and looks for the value "cipherkey". If no value can be found
> a new value is created and saved using ConfigRepository:
>
> https://github.com/steinarb/authservice/blob/fc6c29fb5cede9d9f3e8d4212124adc815ac6cf6/authservice/authservice.web.security.cipherkey/src/main/java/no/priv/bang/authservice/web/security/cipherkey/ChipherKeyServiceProvider.java#L32
>
>

Re: code example that saves a value to a cfg file?

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

>>> Is there a code example somewhere of an SCR component that saves a
>>> value to a cfg file in karaf?

> Looks like injecting a ConfigRepository service may be the way to go...?
>  https://github.com/apache/karaf/blob/main/config/core/src/main/java/org/apache/karaf/config/core/ConfigRepository.java#L26

> I will try!

Using ConfigReposity worked.

This code gets config matching a pid which is the fully qualified
classname and looks for the value "cipherkey". If no value can be found
a new value is created and saved using ConfigRepository:
 https://github.com/steinarb/authservice/blob/fc6c29fb5cede9d9f3e8d4212124adc815ac6cf6/authservice/authservice.web.security.cipherkey/src/main/java/no/priv/bang/authservice/web/security/cipherkey/ChipherKeyServiceProvider.java#L32


Re: code example that saves a value to a cfg file?

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

>> Is there a code example somewhere of an SCR component that saves a value
>> to a cfg file in karaf?

Looks like injecting a ConfigRepository service may be the way to go...?
 https://github.com/apache/karaf/blob/main/config/core/src/main/java/org/apache/karaf/config/core/ConfigRepository.java#L26

I will try!

(I have no prior usages of ConfigRepository in any of my karaf projects,
so not something I have done before)


Re: code example that saves a value to a cfg file?

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

> Is there a code example somewhere of an SCR component that saves a value
> to a cfg file in karaf?

Pax exam has a class that looks like it has the appropriate
functionality:
 https://github.com/ops4j/org.ops4j.pax.exam/blob/v4.x/containers/pax-exam-container-karaf/src/main/java/org/ops4j/pax/exam/karaf/container/internal/KarafCfgFile.java#L26

But:
 1. It is in pax exam (which is a test framework for those that don't know)
 2. It is in a package with "internal" which means it's a hint it's
    probably not intended for use by others 

So... the search continues...