static void mergeConfigurations()

in src/main/java/org/apache/sling/feature/builder/BuilderUtil.java [434:509]


    static void mergeConfigurations(final Configurations target, 
        final Configurations source, 
        final Map<String, String> overrides,
        final ArtifactId sourceFeatureId) {

        for(final Configuration cfg : source) {
            final List<ArtifactId> sourceOrigins = cfg.getFeatureOrigins().isEmpty() ? Collections.singletonList(sourceFeatureId) : cfg.getFeatureOrigins();
            Configuration found = target.getConfiguration(cfg.getPid());

            if ( found != null ) {
                boolean handled = false;
                outer:
                for (Map.Entry<String, String> override : overrides.entrySet()) {
                    if (match(cfg, override.getKey())) {
                        if (BuilderContext.CONFIG_USE_LATEST.equals(override.getValue())) {
                            final int idx = target.indexOf(found);
                            target.remove(found);
                            found = cfg.copy(cfg.getPid());
                            target.add(idx, found);
                            setPropertyFeatureOrigins(found, sourceFeatureId);
                            handled = true;
                        } else if (BuilderContext.CONFIG_FAIL_ON_PROPERTY_CLASH.equals(override.getValue())){
                            for (final String key : listProperties(cfg)) {
                                if (found.getProperties().get(key) != null) {
                                    break outer;
                                } else {
                                    found.getProperties().put(key, cfg.getProperties().get(key));
                                    found.setFeatureOrigins(key, cfg.getFeatureOrigins(key, sourceFeatureId));
                                }
                            }
                            handled = true;
                        } else if (BuilderContext.CONFIG_MERGE_LATEST.equals(override.getValue())) {
                            for (final String key : listProperties(cfg)) {
                                found.getProperties().put(key, cfg.getProperties().get(key));
                                final List<ArtifactId> propOrigins = new ArrayList<>(found.getFeatureOrigins(key));
                                propOrigins.addAll(cfg.getFeatureOrigins(key, sourceFeatureId));
                                found.setFeatureOrigins(key, propOrigins);
                            }
                            handled = true;
                        } else if (BuilderContext.CONFIG_USE_FIRST.equals(override.getValue())) {
                            // no need to update property origins
                            handled = true;
                            found = null;
                        } else if (BuilderContext.CONFIG_MERGE_FIRST.equals(override.getValue())) {
                            for (final String key : listProperties(cfg)) {
                                if (found.getProperties().get(key) == null) {
                                    found.getProperties().put(key, cfg.getProperties().get(key));
                                    found.setFeatureOrigins(key, cfg.getFeatureOrigins(key, sourceFeatureId));
                                }
                            }
                            handled = true;
                        }
                        break outer;
                    }
                }
                if (!handled) {
                    throw new IllegalStateException("Configuration override rule required to select between configurations for " +
                        cfg.getPid());
                }
            } else {
                // create new configuration
                found = cfg.copy(cfg.getPid());
                target.add(found);
                setPropertyFeatureOrigins(found, sourceFeatureId);
                if ( !found.getFeatureOrigins().isEmpty() ) {
                    found = null;
                }
            }
            if ( found != null ) {
                // update origin
                final List<ArtifactId> origins = new ArrayList<>(found.getFeatureOrigins());
                origins.addAll(sourceOrigins);
                found.setFeatureOrigins(origins);    
            }
        }
    }