2014-10-28 2 views
0

multifield 구성 인터페이스를 사용하여 CQ5 서비스를 만들려고합니다. this과 같을 수 있지만 PLUS 버튼을 클릭하면 새로운 행뿐만 아니라 N 행의 그룹이 추가됩니다.CQ5 multifield 구성 서비스

재산권

  • 필드 1 + -
  • 필드 2
  • ....

  • FieldN 어떤 조언을?

  • 답변

    1

    내가 아는 한 Apache Felix에는 그러한 가능성이 없습니다.

    실제 요구 사항에 따라 구성을 분해하는 것을 고려할 것입니다. 더하기 버튼을 통해 추가하려는 모든 필드 세트를 분리 된 구성으로 이동해보십시오. 따라서 slf4j.Logger 구성과 밀접하게 관련하여 구성 팩토리 접근 방식을 사용하게됩니다.

    간단한 구성 공장은 어떤 @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); 
    } 
    
    0

    에 대한 참조를 추가하는 것만 큼 간단 사용

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

    }