Using Same OSGI Service at Multiple Times | Community
Skip to main content
Singaiah_Chintalapudi
New Participant
April 26, 2016
Solved

Using Same OSGI Service at Multiple Times

  • April 26, 2016
  • 13 replies
  • 5596 views

I want to use the same OSGI service to read config values from run modes instead of developing multiple OSGI services. For example, we can click on "+" button to configure multiple DB's in felix console. Is there a way in AEM to do that?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by leeasling
@Component(metatype = true, label = "Configuration Factory", description = "Configuration Factory", configurationFactory = true) @Service public class TestConfigurationFactory { @Property(label = "Property 1", propertyPrivate = false, description = "Property 1") private static final String PROPERTY_1 = "property.one"; @Property(label = "Property 2", propertyPrivate = false, description = "Property 2") private static final String PROPERTY_2 = "property.two"; private String property1; private String property2; @Activate protected void activate(ComponentContext context) throws RepositoryException { this.property1 = PropertiesUtil.toStringArray(context.getProperties().get(PROPERTY_1)); this.property2 = PropertiesUtil.toStringArray(context.getProperties().get(PROPERTY_2)); } }

13 replies

kautuk_sahni
Employee
April 27, 2016

Hi

Hi

Please have a look at this community article:-

Link:- http://www.wemblog.com/2012/10/how-to-work-with-configurations-in-cq.html

//You want to create configuration which can be edited at run time using OSGI console. 

 

Another reference link:- http://www.tothenew.com/blog/creating-osgi-factory-configurations-in-aem/

I hope this is what exactly your are trying to achieve. 

 

Thanks and Regards

Kautuk Sahni

Kautuk Sahni
edubey
New Participant
April 27, 2016

Take a look at these two well explained articles on osgi config and how you read them

OSGi Configuration Management: http://adobeaemclub.com/osgi-configuration-management-aem/

Reading OSGi Configuration properties: http://adobeaemclub.com/reading-osgi-configuration-properties/

Thanks

manoj_devapath
New Participant
April 27, 2016

noksc wrote...

leeasling wrote...

I'm a little lost as to what exactly you're looking for, but you cannot have an array OSGI configuration property with multiple fields.  You can implement an array of properties by using the property definition like this

  1. @Property(label = "Multi Property", unbounded = PropertyUnbounded.ARRAY, propertyPrivate = false, description = "A property that allows multiple values")
  2. private static final String RECIPIENT_PROPERTY = "property.multiple";

If you want to have multiple values for a single item then consider using a semi-colon or a pipe delimiter, then when you read the configuration you can separate the values.

The other concerning thing is that you mention authors authoring these values in the felix console.  Nobody other than the administrator should have access to the felix console.  I would consider a different method of managing these values.

 

Okay. In a simple words, we can add multiple factory configurations to configure multiple databases in Felix console (Please see attached screen shot). In the same way, can I configure my OSGI service to configure multiple factory configurations?

 

Do you want that property as editable each time. is that what you are looking for?

leeaslingAccepted solution
New Participant
April 26, 2016
@Component(metatype = true, label = "Configuration Factory", description = "Configuration Factory", configurationFactory = true) @Service public class TestConfigurationFactory { @Property(label = "Property 1", propertyPrivate = false, description = "Property 1") private static final String PROPERTY_1 = "property.one"; @Property(label = "Property 2", propertyPrivate = false, description = "Property 2") private static final String PROPERTY_2 = "property.two"; private String property1; private String property2; @Activate protected void activate(ComponentContext context) throws RepositoryException { this.property1 = PropertiesUtil.toStringArray(context.getProperties().get(PROPERTY_1)); this.property2 = PropertiesUtil.toStringArray(context.getProperties().get(PROPERTY_2)); } }
Singaiah_Chintalapudi
New Participant
April 26, 2016

leeasling wrote...

Yes you can.  Your service needs to be a component factory.  See below.

  1. @Component(metatype = true, label = "Configuration Factory", description = "Configuration Factory",
  2. configurationFactory = true)
  3. @Service

 

 

Thanks. Do you have any example?

New Participant
April 26, 2016

Yes you can.  Your service needs to be a component factory.  See below.

@Component(metatype = true, label = "Configuration Factory", description = "Configuration Factory", configurationFactory = true) @Service
Singaiah_Chintalapudi
New Participant
April 26, 2016

leeasling wrote...

I'm a little lost as to what exactly you're looking for, but you cannot have an array OSGI configuration property with multiple fields.  You can implement an array of properties by using the property definition like this

  1. @Property(label = "Multi Property", unbounded = PropertyUnbounded.ARRAY, propertyPrivate = false, description = "A property that allows multiple values")
  2. private static final String RECIPIENT_PROPERTY = "property.multiple";

If you want to have multiple values for a single item then consider using a semi-colon or a pipe delimiter, then when you read the configuration you can separate the values.

The other concerning thing is that you mention authors authoring these values in the felix console.  Nobody other than the administrator should have access to the felix console.  I would consider a different method of managing these values.

 

Okay. In a simple words, we can add multiple factory configurations to configure multiple databases in Felix console (Please see attached screen shot). In the same way, can I configure my OSGI service to configure multiple factory configurations?

New Participant
April 26, 2016

I think an Osgi configuration factory is what you are likely looking for. Here is a good reference on how to create those https://cqdump.wordpress.com/2014/08/05/managing-multiple-instances-of-services-osgi-service-factories/

And you should not give authors access to your Felix console. Admins should be doing that.

New Participant
April 26, 2016

I'm a little lost as to what exactly you're looking for, but you cannot have an array OSGI configuration property with multiple fields.  You can implement an array of properties by using the property definition like this

@Property(label = "Multi Property", unbounded = PropertyUnbounded.ARRAY, propertyPrivate = false, description = "A property that allows multiple values") private static final String RECIPIENT_PROPERTY = "property.multiple";

If you want to have multiple values for a single item then consider using a semi-colon or a pipe delimiter, then when you read the configuration you can separate the values.

The other concerning thing is that you mention authors authoring these values in the felix console.  Nobody other than the administrator should have access to the felix console.  I would consider a different method of managing these values.

Singaiah_Chintalapudi
New Participant
April 26, 2016

smacdonald2008 wrote...

API headers. I am not sure what you mean by this term . Do you mean a page header that is part of a web page?

 

Sorry for the confusion. Here is what I want to do. Authors want to configure below values in the felix console for one requirement.

endPointUrl

contentType

For the other requirement, they want to configure the same values. So instead of writing a new OSGI service, can I use the existing service to configure these values multiple times by giving a specific name to each configuration?

 

For example, we can give a specific name to a DB configuration in felix console and we can configure as many databases as we want. In the similar way, can we configure my OSGI service? So whenever we want, we will just click on "+" symbol and configure those values.