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?
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
@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/
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
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
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="\{"apiKey":"KEY_123455","apiPath":"https://adb/api"}"/>
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="["1","2"]"/>
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="["[{\"name\":\"john\"},{\"name\":\"carole\"}]"]"/>
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Replies
Total Likes
@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/
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.
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies