首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CQ5多场配置服务

CQ5多场配置服务
EN

Stack Overflow用户
提问于 2014-10-28 11:30:13
回答 2查看 567关注 0票数 0

我正在尝试创建一个具有多字段配置接口的CQ5服务。它将类似于,但是单击PLUS按钮,它不仅会添加一个新行,还会添加一组N行。

属性

  • Field1 +-
  • Field2
  • ……
  • FieldN

有什么建议吗?

EN

回答 2

Stack Overflow用户

发布于 2014-10-28 18:13:28

据我所知,Apache中没有这种可能性。

根据您的实际需求,我将考虑对配置进行分解。尝试将所有字段集(希望通过加号按钮添加的字段组)移动到单独的配置中。因此,与slf4j.Logger配置密切相关的是,您可以使用configuration方法。

一个简单的配置工厂可以如下所示

代码语言:javascript
复制
@Component(immediate = true, configurationFactory = true, metatype = true, policy = ConfigurationPolicy.OPTIONAL, name = "com.foo.bar.MyConfigurationProvider", label = "Multiple Configuration Provider")
@Service(serviceFactory = false, value = { MyConfigurationProvider.class })
@Properties({
        @Property(name = "propertyA", label = "Value for property A"),
        @Property(name = "propertyB", label = "Value for property B") })
public class MyConfigurationProvider {

    private String propertyA;
    private String propertyB;

    @Activate
    protected void activate(final Map<String, Object> properties, final ComponentContext componentContext) {
        propertyA = PropertiesUtil.toStringArray(properties.get("propertyA"), defaultValue);
        propertyB = PropertiesUtil.toStringArray(properties.get("propertyB"), defaultValue);
    }
}

使用它就像在任何@Component中添加引用一样简单

代码语言:javascript
复制
@Reference(cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, referenceInterface = MyConfigurationProvider.class, policy = ReferencePolicy.DYNAMIC)
private final List<MyConfigurationProvider> providers = new LinkedList<MyConfigurationProvider>();

protected void bindProviders(MyConfigurationProvider provider) {
    providers.add(provider);
}

protected void unbindProviders(MyConfigurationProvider provider) {
    providers.remove(provider);
}
票数 1
EN

Stack Overflow用户

发布于 2016-07-28 06:21:44

这是一种方法。

代码语言:javascript
复制
@Component(label = "My Service", metatype = true, immediate = true)
@Service(MyService.class)
@Properties({
    @Property(name = "my.property", description = "Provide details Eg: url=http://www.google.com|size=10|path=/content/project", value = "", unbounded = PropertyUnbounded.ARRAY) })

public class MyService {

private String[] myPropertyDetails;

@Activate
protected void activate(ComponentContext ctx) {
    this.myPropertyDetails = getPropertyAsArray(ctx.getProperties().get("my.property"));
    try {
        if (null != myPropertyDetails && myPropertyDetails.length > 0) {
            for(String myPropertyDetail : myPropertyDetails) {
                Map<String, String> map = new HashMap<String, String>();
                String[] propertyDetails = myPropertyDetails.split("|");
                for (String keyValuePair : propertyDetails) {
                    String[] keyValue = keyValuePair.split("=");
                    if (null != keyValue && keyValue.length > 1) {
                        map.put(keyValue[0], keyValue[1]);
                    }
                }                   
                /* the map now has all the properties in the form of key value pairs for single field 
                    use this for logic execution. when there are no multiple properties in the row, 
                    you can skip the  logic to split and add in the map */
            }
        }
    } catch (Exception e) {
        log.error( "Exception ", e.getMessage());
    }
}

private String[] getPropertyAsArray(Object obj) {
    String[] paths = { "" };
    if (obj != null) {
        if (obj instanceof String[]) {
            paths = (String[]) obj;
        } else {
            paths = new String[1];
            paths[0] = (String) obj;
        }
    }
    return paths;
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26607224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档