You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Federico Paparoni <fe...@gmail.com> on 2010/08/05 10:26:43 UTC

Properties file in a bundle

Hi all,

I would like to manage a properties file in my bundle and I'm trying to
understand which is the "Sling-way" to do this.
Surely it's possible to deploy a JSON/XML node but I don't really like this
way (and I think it isn't the only one).
Reading the "Configuration support" page (
http://sling.apache.org/site/configuration.html) I understood that I can
manage the properties file using OSGi mechanism (properties under
conf/config.properties right?). Is it the best way in Sling? Or can I use
something else?

-- 
Federico

Re: Properties file in a bundle

Posted by Felix Meschberger <fm...@gmail.com>.
Hi,

On 06.08.2010 12:21, Federico Paparoni wrote:
> Hi Vidar,
> 
> 2010/8/5 Vidar Ramdal <vi...@idium.no>
> 
>> On Thu, Aug 5, 2010 at 10:44 AM, Federico Paparoni
>> <fe...@gmail.com> wrote:
>>> 2010/8/5 Vidar Ramdal <vi...@idium.no>
>>>>
>>>>
>>>> What are the properties for? Configuration of your components?
>>>> If so, I'd use the OSGi configuration support, which will make the
>>>> configuration available in the Felix Web Console.
>>>>
>>>>
>>> Yes configuration for the components and anyway I would like to show in
>>> David the best way to handle properties (hard coded String are useful
>> only
>>> to show examples :P)
>>
>> Then OSGi configurations are definately the way to go.
>>
>>
> These properties are available through the BundleContext object I can
> retrieve from the ComponentContext.

Actually it is ComponentContext.getProperties()

The properties available from BundleContext.getProperty() are so-called
framework properties and are not backed by the Configuration Admin
service but are global-properties.

Regards
Felix

Re: Properties file in a bundle

Posted by Felix Meschberger <fm...@gmail.com>.
Hi,

On 06.08.2010 14:13, Vidar Ramdal wrote:
> On Fri, Aug 6, 2010 at 2:04 PM, Bertrand Delacretaz
> <bd...@apache.org> wrote:
>> On Fri, Aug 6, 2010 at 1:40 PM, Vidar Ramdal <vi...@idium.no> wrote:
>>> ...In principle, all you need to do is have a string constant annotated
>>> as @scr.property (or @Property if you're using the 'real'
>>> annotations). Then create local variables to store the property
>>> values. These values are fetchted from the Felix ConfigAdmin on
>>> component activation....
>>
>> I just did that this morning for the bgservlets module, if you look at
>> [1] you'll get an example of how to make things configurable, using
>> the "modern" annotations.
> 
> Yeah, use the modern annotations and not the old javadoc ones (that
> is, use @Property, not /** @scr.property */). The modern ones are
> easier to maintain, as you can use Java constants as values.
> Refactoring support in IDEs are also much better.
> 
> And, I forgot to mention, if you want a proper-looking configuration
> form in the Felix web console, you should supply a
> resources/OSGI-INF/metatype/metatypes.properties file, and specify
> metatype=true on your Component annotation. This also demonstrated in

Actually metatype is generated by default, so no need for
"metatype=true". You need the metatype attribute only if you want to
prevent this information from being generated.

Regards
Felix

Re: Properties file in a bundle

Posted by Vidar Ramdal <vi...@idium.no>.
On Fri, Aug 6, 2010 at 2:04 PM, Bertrand Delacretaz
<bd...@apache.org> wrote:
> On Fri, Aug 6, 2010 at 1:40 PM, Vidar Ramdal <vi...@idium.no> wrote:
>> ...In principle, all you need to do is have a string constant annotated
>> as @scr.property (or @Property if you're using the 'real'
>> annotations). Then create local variables to store the property
>> values. These values are fetchted from the Felix ConfigAdmin on
>> component activation....
>
> I just did that this morning for the bgservlets module, if you look at
> [1] you'll get an example of how to make things configurable, using
> the "modern" annotations.

Yeah, use the modern annotations and not the old javadoc ones (that
is, use @Property, not /** @scr.property */). The modern ones are
easier to maintain, as you can use Java constants as values.
Refactoring support in IDEs are also much better.

And, I forgot to mention, if you want a proper-looking configuration
form in the Felix web console, you should supply a
resources/OSGI-INF/metatype/metatypes.properties file, and specify
metatype=true on your Component annotation. This also demonstrated in
Bertrand's code.

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Sommerrogata 13-15, N-0255 Oslo, Norway
+ 47 22 00 84 00 / +47 22 00 84 76
Quando omni flunkus moritatus!

Re: Properties file in a bundle

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Fri, Aug 6, 2010 at 1:40 PM, Vidar Ramdal <vi...@idium.no> wrote:
> ...In principle, all you need to do is have a string constant annotated
> as @scr.property (or @Property if you're using the 'real'
> annotations). Then create local variables to store the property
> values. These values are fetchted from the Felix ConfigAdmin on
> component activation....

I just did that this morning for the bgservlets module, if you look at
[1] you'll get an example of how to make things configurable, using
the "modern" annotations. Again, you don't need properties files for
configs.

-Bertrand

[1] http://svn.apache.org/viewvc?view=revision&revision=982920

Re: Properties file in a bundle

Posted by Vidar Ramdal <vi...@idium.no>.
>> On Thu, Aug 5, 2010 at 10:44 AM, Federico Paparoni
>> <fe...@gmail.com> wrote:
>> > 2010/8/5 Vidar Ramdal <vi...@idium.no>
>> >>
>> >>
>> >> What are the properties for? Configuration of your components?
>> >> If so, I'd use the OSGi configuration support, which will make the
>> >> configuration available in the Felix Web Console.
>> >>
>> >>
>> > Yes configuration for the components and anyway I would like to show in
>> > David the best way to handle properties (hard coded String are useful
>> only
>> > to show examples :P)

> 2010/8/5 Vidar Ramdal <vi...@idium.no>
>> Then OSGi configurations are definately the way to go.

On Fri, Aug 6, 2010 at 12:21 PM, Federico Paparoni
<fe...@gmail.com> wrote:
> These properties are available through the BundleContext object I can
> retrieve from the ComponentContext.
> I tried to understand where I have to put the conf/config.properties file
> but I didn't find an example.
> Can you help me?

The easiest way is probably to specify configuration properties as
Java annotations.
Take a look at org.apache.sling.servlets.get.impl.DefaultGetServlet
for (one of many) examples.

In principle, all you need to do is have a string constant annotated
as @scr.property (or @Property if you're using the 'real'
annotations). Then create local variables to store the property
values. These values are fetchted from the Felix ConfigAdmin on
component activation.

Let's look at the 'aliases' config property in DefaultGetServlet:
/** @scr.property */
private static final String ALIAS_PROPERTY = "aliases"; // Name of the property
...
private String[] aliases; // Local variable to hold the value
...
protected void activate(ComponentContext ctx) {
  Dictionary props = ctx.getProperties();
  // Set the local 'aliases' variable to the value from ConfigAdmin
  this.aliases = OsgiUtil.toStringArray(props.get(ALIAS_PROPERTY));
  ...
}

protected void deactivate(ComponentContext ctx) {
  this.aliases = null;
 ...
}

In short, you don't need *.properties files. You can supply default
values in the Java code itself, or you can supply configuration as JCR
nodes. See http://sling.apache.org/site/jcr-installer-jcrjcrinstall-and-osgiinstaller.html#JCRInstaller%28jcr.jcrinstallandosgi.installer%29-Install%2Cmodifyandremoveaconfiguration
for the docs.

You might also find this mail thread useful:
http://web.archiveorange.com/archive/v/8X7ppYhYoN7CEPybNem7

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Sommerrogata 13-15, N-0255 Oslo, Norway
+ 47 22 00 84 00 / +47 22 00 84 76
Quando omni flunkus moritatus!

Re: Properties file in a bundle

Posted by Federico Paparoni <fe...@gmail.com>.
Hi Vidar,

2010/8/5 Vidar Ramdal <vi...@idium.no>

> On Thu, Aug 5, 2010 at 10:44 AM, Federico Paparoni
> <fe...@gmail.com> wrote:
> > 2010/8/5 Vidar Ramdal <vi...@idium.no>
> >>
> >>
> >> What are the properties for? Configuration of your components?
> >> If so, I'd use the OSGi configuration support, which will make the
> >> configuration available in the Felix Web Console.
> >>
> >>
> > Yes configuration for the components and anyway I would like to show in
> > David the best way to handle properties (hard coded String are useful
> only
> > to show examples :P)
>
> Then OSGi configurations are definately the way to go.
>
>
These properties are available through the BundleContext object I can
retrieve from the ComponentContext.
I tried to understand where I have to put the conf/config.properties file
but I didn't find an example.
Can you help me?

-- 
Federico

Re: Properties file in a bundle

Posted by Vidar Ramdal <vi...@idium.no>.
On Thu, Aug 5, 2010 at 10:44 AM, Federico Paparoni
<fe...@gmail.com> wrote:
> 2010/8/5 Vidar Ramdal <vi...@idium.no>
>>
>>
>> What are the properties for? Configuration of your components?
>> If so, I'd use the OSGi configuration support, which will make the
>> configuration available in the Felix Web Console.
>>
>>
> Yes configuration for the components and anyway I would like to show in
> David the best way to handle properties (hard coded String are useful only
> to show examples :P)

Then OSGi configurations are definately the way to go.

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Sommerrogata 13-15, N-0255 Oslo, Norway
+ 47 22 00 84 00 / +47 22 00 84 76
Quando omni flunkus moritatus!

Re: Properties file in a bundle

Posted by Federico Paparoni <fe...@gmail.com>.
2010/8/5 Vidar Ramdal <vi...@idium.no>
>
>
> What are the properties for? Configuration of your components?
> If so, I'd use the OSGi configuration support, which will make the
> configuration available in the Felix Web Console.
>
>
Yes configuration for the components and anyway I would like to show in
David the best way to handle properties (hard coded String are useful only
to show examples :P)


-- 
Federico

Re: Properties file in a bundle

Posted by Vidar Ramdal <vi...@idium.no>.
On Thu, Aug 5, 2010 at 10:26 AM, Federico Paparoni
<fe...@gmail.com> wrote:
> I would like to manage a properties file in my bundle and I'm trying to
> understand which is the "Sling-way" to do this.
> Surely it's possible to deploy a JSON/XML node but I don't really like this
> way (and I think it isn't the only one).
> Reading the "Configuration support" page (
> http://sling.apache.org/site/configuration.html) I understood that I can
> manage the properties file using OSGi mechanism (properties under
> conf/config.properties right?). Is it the best way in Sling? Or can I use
> something else?

What are the properties for? Configuration of your components?
If so, I'd use the OSGi configuration support, which will make the
configuration available in the Felix Web Console.

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Sommerrogata 13-15, N-0255 Oslo, Norway
+ 47 22 00 84 00 / +47 22 00 84 76
Quando omni flunkus moritatus!