从 OSGI 配置读取值到 servlet

Read values from OSGI configuration to servlet

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

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

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

一种方法是使用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() {
    }
}

请查看这篇 Adob​​e 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);
  }
}

另一个 Post 涵盖同样的问题 :- http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.topic.html/forum__pg8f-i_am_extendingmymo.html

希望对您有所帮助。

感谢和问候 考图克萨尼