Hi Juanjo,

Basically, you access the properties from the ComponentContext you get
in the activate method by calling the ComponentContext.getProperties()
method. This returns you all the properties, which you have set
"programmatically", i.e. the default values, as well as the configured
values, where the configured values overwrite the default
(programmatically set values).

See inline comments for more information.

Juanjo Vázquez schrieb:
> Hi,
> 
> I´m trying  to get to work a custom OSGi service with configurable
> properties over Sling. My goal is using the Felix web console to admin the
> OSGi service but supplying an initial configuration programatically. The
> initial configuration could be retrieved from bundle context or maybe an
> external file.
> 
> The service is basically defined like this:
> 
> /**
>  * @scr.component label="%custom.name" description="%custom.description"
>  * @scr.property name="service.description" value="Custom Service"
>  * @scr.property name="service.vendor" value="The Apache Software
> Foundation"
> *  @scr.service
>  */
> public class CustomOsgiService implements ManagedService {
> 
>     /**
>      * @scr.property value=""
>      */
>     public static final String PROP_CUSTOM = "prop.custom";
> 
>     public void activate(ComponentContext context).......
> 
>     public void deactivate(ComponentContext context).......
> 
>     public void update(Dictionary props).......
> 
> }
> 
> How could I initialize PROP_CUSTOM programatically?

The easiest way is to the set "value" attribute of the @scr.property tag
to the programmatically set value. Alternatively you may define a
constant in you class and place a reference to that value in the
@scr.property tag:

     /** @scr.property valueRef="PROP_CUSTOM_DEFAULT" */
     public static final String PROP_CUSTOM = "prop.custom";
     public static final String PROP_CUSTOM_DEFAULT = "Some Value";

> 
> How could I manage PROP_CUSTOM changes? (using Felix console SAVE button).

You don't have to care. As soon as the configuration is updated the
Service Component Runtime, the thing managing the declarative services
components, will deactivate the component and reactivate it with the new
configuration data. Nothing special to do on your part.

Very easy and cool.

> 
> Is it necessary CustomService to be a managed service?.

No, a CustomService should not generally implement a managed service,
otherwise the configuration may not be provided.

> 
> Should I register CustomService with the Configuration Admin Service?.

No, not needed either. (see above).

> Any help will be appreciate.

I hope this helps. Otherwise you might find more information at [1] and
[2] and of course the Declarative Services Specificaiton in the OSGi
Compendium.

Regards
Felix

[1] http://felix.apache.org/site/apache-felix-service-component-runtime.html
[2] http://felix.apache.org/site/apache-felix-maven-scr-plugin.html

Reply via email to