从OSGI配置读取值到servlet

时间:2016-06-07 20:11:16

标签: servlets configuration osgi cq5 aem

我已经创建了一个OSGI配置,如下所示

@Property(label = "Social Media", value = "", unbounded = PropertyUnbounded.ARRAY, description = "Enter a social media string configuration")

这是一个数组属性。我想将它读入servlet,使其可以在页面上显示。请帮帮我。

2 个答案:

答案 0 :(得分:1)

一种方法是使用org.apache.sling.commons.osgi.PropertiesUtil#toStringArray方法:

package com.foo.bar.service.impl;

import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;

@Service( value = SampleService.class )
@Component( metatype = true )
@Properties({
  @Property(
      label       = "Sample Service",
      description = "Sample service demonstrating an array setting",
      name        = SampleServiceImpl.MY_PROPERTY_NAME,
      unbounded   = PropertyUnbounded.ARRAY)
})
public class SampleServiceImpl implements SampleService {

    public static final String MY_PROPERTY_NAME = "sample.myarray";

    private String[] myArraySetting;

    @Activate
    protected void activate(Map<String, Object> properties) {
        myArraySetting = PropertiesUtil.toStringArray(properties.get(MY_PROPERTY_NAME), ArrayUtils.EMPTY_STRING_ARRAY);
    }

    @Deactivate
    protected void deactivate() {
    }
}

答案 1 :(得分:0)

请查看此Adobe Helpx文章: - https://helpx.adobe.com/experience-manager/using/osgi_config.html

//读取AEM OSGi配置值

代码: -

package org.testsite.core.service.impl;

import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testsite.core.service.ConfigurationService;

@Service({ConfigurationServiceImpl.class})
@Component(immediate=true, metatype=true, label="Example Configuration Service")
public class ConfigurationServiceImpl
  implements ConfigurationService
{
  private static final Logger LOG = LoggerFactory.getLogger(ConfigurationServiceImpl.class);
  private static final String CLASS_NAME = "[ConfigurationService]: ";
  @Property(unbounded=PropertyUnbounded.ARRAY, label="Multi String", cardinality=50, description="Example for Multi field config")
  private static final String MULTI_FIELD = "multifield";
  @Property(unbounded=PropertyUnbounded.DEFAULT, label="Simple String", description="Example for Simple text field config")
  private static final String SIMPLE_FIELD = "simplefield";
  private String[] multiString;
  private String simpleString;

  public String[] getMultiString()
  {
    return this.multiString;
  }

  public String getSimpleString()
  {
    return this.simpleString;
  }

  @Activate
  protected void activate(Map<String, Object> properties)
  {
    LOG.info("[*** AEM ConfigurationService]: activating configuration service");
    readProperties(properties);
  }

  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);
  }
}

另一篇文章涉及同样的问题: - http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.topic.html/forum__pg8f-i_am_extendingmymo.html

我希望这会对你有所帮助。

谢谢和问候 Kautuk Sahni