CQ5多字段配置服务

时间:2014-10-28 11:30:13

标签: cq5 aem

我尝试使用多字段配置界面创建CQ5服务。它类似this,但点击PLUS按钮,它不仅会添加一个新行,还会添加一组N行。

  

财产

     
      
  • Field1 + -
  •   
  • 字段2
  •   
  • ....
  •   
  • 字段N
  •   

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

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

根据您的实际要求,我会考虑分解配置。尝试将所有字段集(您希望通过加号按钮添加的字段组)移动到单独的配置中。因此,与slf4j.Logger配置紧密相关,您将拥有配置工厂方法。

简单的配置工厂可能如下所示

@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

中添加引用一样简单
@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 :(得分:0)

这是一种做法。

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

}