How to create a OSGI Configuration holding multi dimensional array value

Avatar

Avatar

pn_2007

Avatar

pn_2007

pn_2007

29-05-2020

I have a string array as below. 

String[][] group = {{"A","B","C","D"},{"E","F","G","H"},{"I","J","K","L"},{"M","N","O","P"},
{"Q","R","S"},{"T","U","V"}};

 

I need to create a OSGI configuration property holding this value. Is it possible? Does OSGI support this?

Can we create this kind of array configuration in OSGI?

 

Configuration osgi

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

raghavc

Avatar

raghavc

raghavc

29-05-2020

@pn_2007 create a factory OSGI Configuration, and each factory item config can have an array property (@Property(unbounded=PropertyUnbounded.ARRAY)). This would allow you to have 2 dimensional array 

 

You can refer to the below link on how to create a factory config

https://www.tothenew.com/blog/creating-osgi-factory-configurations-in-aem/

Answers (2)

Answers (2)

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

916

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

916

Correct Answer

1.0K
Jörg_Hoh
Employee

29-05-2020

OSGI does not support multi-dimensional arrays of values, but just simple lists.

 

When you need to provide more complex data as OSGI parameter, you should consider to package the configuration in a different format (e.g. as JSON file in the repository, or a node structure), and use OSGI to configure the path to it.

Avatar

Avatar

BrianKasingli

MVP

Avatar

BrianKasingli

MVP

BrianKasingli
MVP

29-05-2020

@pn_2007,

Definitely, OSGI configurations can contain string values for a configuration. For the specific string value, you can configure a String representation of a JSON Object. When your OSGI service is obtaining the configuration, you will expect to get the String value. With the String value, you can use a common JSON library like GSON, org.json, Jackson, or any other JSON library serialise the String to a JSON Object. Then you can use the JSON Object to get values within the JSON String.

Example:

 

 

import org.json.JSONObject;
...
@Activate
protected void activate(final Config config) {
    String config = config.jsonConfig();
    JSONObject jsonObject = new JSONObject(config);
    String apiKey = jsonObject.has("apiKey") ? jsonObject.get("apiKey") : "";
}

 

 

 

Just make sure your configured value is a valid String formatted JSON object. Maintaining these configurations can be a nightmare.

I won't recommend Complex structures of JSON or XML, String formatted, because it is be hard to maintain in OSGI configurations. It might be better to create a configuration of String[] (OSGI will show multi-field string configurable input fields) rather than using complex structures like JSON or XML in a String format.
Example:

 

@AttributeDefinition(name = "MultipleValues", description = "Multi Configuration values")
String[] getStringValues();
// from example, https://helpx.adobe.com/experience-manager/using/osgi_config63.html

 

 

Thanks Brian for your answer. I understand we have string array representation as below in the osgi xml file like a single array

      group="[A,B,C,D,E,F]"    . But could not get something like an array representation in my question...I am looking for a 2D array representation in the xml file.
 
if it's a xml format, then it would be the similar solution of the code example listed above.
complex structures of JSON or XML might be hard to maintain. It might be better to create configuration of String[] (OSGI will show multi-field string configurable input fields) rather than using complex structures like JSON or XML in a String format.
with something setup like "[A,B,C,D,E,F]", you can compute the configuration by using Java logic.... "[A,B,C,D,E,F]".split[","]
I think, there is a misunderstanding here. We have a config node in our apps folder for each OSGI configuration right. That is basically a xml file where we define our OSGI properties which is then picked by our component or service. How we can define a 2D array in this osgi config node is my question here.

I won't recommend this, but if you really need to store String formatted XML or JSON, then  here are some examples of how configurations can be stored in your OSGI XML files.

JSON Example:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"
config="\{&quot;apiKey&quot;:&quot;KEY_123455&quot;,&quot;apiPath&quot;:&quot;https://adb/api&quot;}"/>

String Example:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"
config="[&quot;1&quot;,&quot;2&quot;]"/>

2D: [{},{},{}] example:

 

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="sling:OsgiConfig"
    config="[&quot;[{\&quot;name\&quot;:\&quot;john\&quot;},{\&quot;name\&quot;:\&quot;carole\&quot;}]&quot;]"/>

 

 

Got another solution. Created 6 different single dimensional string arrays which is supported by OSGI. Then added all these 6 string array to a list and then used that list in my logic and that seems to be working.
Thank you Briankasingli