Expand my Community achievements bar.

SOLVED

OSGI configuration with multi-field option

Avatar

Former Community Member

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

1 Accepted Solution

Avatar

Correct answer by
Administrator

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



Kautuk Sahni

View solution in original post

4 Replies

Avatar

Level 4

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/

Avatar

Correct answer by
Administrator

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



Kautuk Sahni

Avatar

Level 1

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.?

Avatar

Community Advisor

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