OSGI configuration with multi-field option | Community
Skip to main content
November 9, 2017
Solved

OSGI configuration with multi-field option

  • November 9, 2017
  • 4 replies
  • 8343 views

Hi,

I have a requirement to create OSGI configuration which can allow to add multiple fields dynamically.  I could create configuration with fixed number of fields and provided the code snippet below which is put into meta xml. Can this be modified to add the fields dynamically ? Please assist.

<OCD description="Store Content Configuration" name="com.testosgi.config.storecontent" id="com.testosgi.config.storecontent"">

    <AD name="Author Host Name"  id="authorHost" required="true" type="String" default="localhost"/>

    <AD name="Author Port"  id="authorPort" required="true" type="String" default="4502"/>

    <AD name="User Name"  id="userName" required="false" type="String" default="admin"/>

</OCD>

    <Designate pid="com.testosgi.config.storecontent">

<Object ocdref="com.testosgi.config.storecontent"/>

    </Designate>

Thanks,

Suresh

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 kautuk_sahni

Adding two more references to further help you:

1. Adobe Experience Manager Help | Reading Adobe Experience Manager OSGi Configuration Values

// You can download package directly and see it working

ConfigurationServiceImpl class

The ConfigurationServiceImpl class reads the OSGi configuration values defined in CRXDE Lite by using org.apache.sling.commons.osgi.PropertiesUtil.

This class contains this method that reads the configuration values.

protected void readProperties(Map<String, Object> properties)
{
LOG.info(properties.toString());
this.multiString = PropertiesUtil.toStringArray(properties.get("multifield"));
LOG.info("Mutli String Size: " + this.multiString.length);
this.simpleString = PropertiesUtil.toString(properties.get("simplefield"), "default");
LOG.info("Simple String: " + this.simpleString);
}

2. Programmatically updating OSGi configurations in AEM and Sling - Adobe Experience Manager | AEM/CQ | Apache Sling

4 replies

Level 3
November 9, 2017

I am sure this is what you are looking it? Please refer to this beautiful article which talks about the same.

http://www.aemcq5tutorials.com/tutorials/custom-osgi-configuration-aem/

kautuk_sahni
Community Manager
kautuk_sahniCommunity ManagerAccepted solution
Community Manager
November 10, 2017

Adding two more references to further help you:

1. Adobe Experience Manager Help | Reading Adobe Experience Manager OSGi Configuration Values

// You can download package directly and see it working

ConfigurationServiceImpl class

The ConfigurationServiceImpl class reads the OSGi configuration values defined in CRXDE Lite by using org.apache.sling.commons.osgi.PropertiesUtil.

This class contains this method that reads the configuration values.

protected void readProperties(Map<String, Object> properties)
{
LOG.info(properties.toString());
this.multiString = PropertiesUtil.toStringArray(properties.get("multifield"));
LOG.info("Mutli String Size: " + this.multiString.length);
this.simpleString = PropertiesUtil.toString(properties.get("simplefield"), "default");
LOG.info("Simple String: " + this.simpleString);
}

2. Programmatically updating OSGi configurations in AEM and Sling - Adobe Experience Manager | AEM/CQ | Apache Sling

Kautuk Sahni
sachind88574992
August 10, 2018

How can we do this using the R6 annotations?

Code snippet

@Component(

  immediate = true,

  name = OrderStatusLineStatus.NAME,

  configurationPid = OrderStatusLineStatus.CONFIG_PID

  )

@Designate(ocd = OrderStatusLineStatus.Configuration.class)

public class OrderStatusLineStatus {

  public static final String NAME = "OM - Line Status Description";

  private static final Logger LOG = LoggerFactory.getLogger(OrderStatusLineStatus.class);

  public static final String CONFIG_PID = "com.common.services.impl.OrderStatusLineStatus";

  protected Configuration configuration;

  protected ResourceResolver resourceResolver;

  protected Resource resource;

  protected Session session;

  protected String[] lineStatusCode;

  protected String[] getLineStatusCode() {

  return this.lineStatusCode;

  }

  @Activate

  @Modified

  protected final void activate(final Configuration config) {

  this.configuration = config;

  lineStatusCode = config.lineStatus();

  LOG.info("[*** AEM ConfigurationService" + lineStatusCode);

  }

  @ObjectClassDefinition(name=OrderStatusLineStatus.NAME)

  public @interface Configuration {

  @AttributeDefinition(

  name = "Line Status",

  description = "Add Line Status",

  type = AttributeType.STRING)

  public String[] lineStatus() default {};

  }

  protected void readProperties(Configuration config)

  {

  LOG.info("Mutli String Size: " + config.toString());

  }

}

How can retreive the values of the multifield in the activate method.?

arunpatidar
Community Advisor
Community Advisor
August 11, 2018

Hi,

Yes did it right to get value from config to lineStatusCode array, but you can't captures those values in log like that you need to change your code a bit e.g.

protected final void activate(final Configuration config) {

    this.configuration = config;

    lineStatusCode = config.lineStatus();

    LOG.info("[*** AEM ConfigurationService" + java.util.Arrays.toString(lineStatusCode));

  }

Thanks

Arun

Arun Patidar