Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to create a OSGI Configuration holding multi dimensional array value

Avatar

Level 3

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?

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 5

@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/

View solution in original post

11 Replies

Avatar

Community Advisor

@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

 

 

Avatar

Level 3

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.
 

Avatar

Community Advisor
if it's a xml format, then it would be the similar solution of the code example listed above.

Avatar

Community Advisor
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.

Avatar

Community Advisor
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[","]

Avatar

Level 3
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.

Avatar

Community Advisor

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;]"/>

 

 

Avatar

Level 3
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.

Avatar

Correct answer by
Level 5

@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/

Avatar

Employee Advisor

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.