private static void buildFromFeature()

in src/main/java/org/apache/sling/feature/modelconverter/ProvisioningToFeature.java [306:460]


    private static void buildFromFeature(final Feature feature,
            final Map<String,String> variables,
            final List<String> dropVariables,
            final Bundles bundles,
            final List<String> excludeBundles,
            final Configurations configurations,
            final List<String> currentRunModes,
            final Extensions extensions,
            final Map<String,String> properties) {
        for (Iterator<Map.Entry<String, String>> it = feature.getVariables().iterator(); it.hasNext(); ) {
            Entry<String, String> entry = it.next();
            boolean found = false;
            if(dropVariables != null) {
                for (String variableName : dropVariables) {
                    // Look if variable is in the list of Variables to be dropped
                    if (entry.getKey().equals(variableName)) {
                        found = true;
                    }
                }
            }
            if(!found) { variables.put(entry.getKey(), entry.getValue()); }
        }

        Extension cpExtension = extensions.getByName(Extension.EXTENSION_NAME_CONTENT_PACKAGES);
        for(final RunMode runMode : feature.getRunModes() ) {
            int runModelFilteringMode = 0; // Default behavior with no filtering
            String[] runModeNames = runMode.getNames();
            if(!currentRunModes.isEmpty()) {
                if(runModeNames == null || runModeNames.length == 0) {
                    runModelFilteringMode = 1; // No Runmode configured -> write as usual
                } else {
                    for(String runModeName: runModeNames) {
                        if(currentRunModes.contains(runModeName)) {
                            runModelFilteringMode = 2; // Matching Runmode -> write out w/o run mode suffix
                        }
                    }
                    if(runModelFilteringMode != 2) {
                        runModelFilteringMode = -1; // Ignore this runmode as it does not have a match
                    }
                }
            }
            if(runModelFilteringMode < 0) {
                continue;
            }
            for(final ArtifactGroup group : runMode.getArtifactGroups()) {
                for(final Artifact artifact : group) {
                    ArtifactId id = ArtifactId.fromMvnUrl(artifact.toMvnUrl());
                    String artifactId = id.getArtifactId();
                    LOGGER.info("Check Artitfact Id: '{}' if excluded by: '{}'", artifactId, excludeBundles);
                    if(excludeBundles.contains(artifactId)) {
                        // If bundle is excluded then go to the next one
                        continue;
                    }
                    String version = id.getVersion();
                    if(version.startsWith("${") && version.endsWith("}")) {
                        // Replace Variable with value if found
                        String versionFromVariable = variables.get(version.substring(2, version.length() - 1));
                        if (versionFromVariable != null && !versionFromVariable.isEmpty()) {
                            id = new ArtifactId(id.getGroupId(), id.getArtifactId(), versionFromVariable, id.getClassifier(), id.getType());
                        }
                    }
                    final org.apache.sling.feature.Artifact newArtifact = new org.apache.sling.feature.Artifact(id);

                    for(final Map.Entry<String, String> entry : artifact.getMetadata().entrySet()) {
                        newArtifact.getMetadata().put(entry.getKey(), entry.getValue());
                    }

                    if ( newArtifact.getId().getType().equals("zip") ) {
                        if ( cpExtension == null ) {
                            cpExtension = new Extension(ExtensionType.ARTIFACTS,
                                    Extension.EXTENSION_NAME_CONTENT_PACKAGES, ExtensionState.REQUIRED);
                            extensions.add(cpExtension);
                        }
                        cpExtension.getArtifacts().add(newArtifact);
                    } else {
                        int startLevel = group.getStartLevel();
                        if ( startLevel == 0) {
                            if ( ModelConstants.FEATURE_BOOT.equals(feature.getName()) ) {
                                startLevel = 1;
                            } else if ( startLevel == 0 ) {
                                startLevel = 20;
                            }
                        }
                        newArtifact.getMetadata().put("start-order", String.valueOf(startLevel));

                        bundles.add(newArtifact);
                    }
                }
            }

            for(final Configuration cfg : runMode.getConfigurations()) {
                String pid = cfg.getPid();
                if (pid.startsWith(":")) {
                    // The configurator doesn't accept colons ':' in it's keys, so replace these
                    pid = ".." + pid.substring(1);
                }

                LOGGER.info("Check Configuration Id: '{}' if excluded by: '{}'", pid, excludeBundles);
                if(excludeBundles.contains(pid)) {
                    // If configuration is excluded then go to the next one
                    continue;
                }

                if (runModeNames != null && runModelFilteringMode != 2) {
                    pid = pid + ".runmodes." + String.join(".", runModeNames);
                    pid = pid.replaceAll("[:]", "..");
                }

                final org.apache.sling.feature.Configuration newCfg;
                if ( cfg.getFactoryPid() != null ) {
                    newCfg = new org.apache.sling.feature.Configuration(cfg.getFactoryPid() + '~' + pid);
                } else {
                    newCfg = new org.apache.sling.feature.Configuration(pid);
                }
                final Enumeration<String> keys = cfg.getProperties().keys();
                while ( keys.hasMoreElements() ) {
                    String key = keys.nextElement();
                    Object value = cfg.getProperties().get(key);

                    if (key.startsWith(":")) {
                        key = ".." + key.substring(1);
                    }
                    newCfg.getProperties().put(key, value);
                }

                configurations.add(newCfg);
            }

            for(final Map.Entry<String, String> prop : runMode.getSettings()) {
                if (runModeNames == null && runModelFilteringMode != 2) {
                    properties.put(prop.getKey(), prop.getValue());
                } else {
                    properties.put(prop.getKey() + ".runmodes:" + String.join(",", runModeNames),
                            prop.getValue());
                }
            }
        }

        final StringBuilder repoinitText = new StringBuilder();
        for(final Section sect : feature.getAdditionalSections("repoinit")) {
            repoinitText.append(sect.getContents()).append("\n");
        }

        if(repoinitText.length() > 0) {
            Extension repoExtension = extensions.getByName(Extension.EXTENSION_NAME_REPOINIT);

            if ( repoExtension == null ) {
                repoExtension = new Extension(ExtensionType.TEXT, Extension.EXTENSION_NAME_REPOINIT, ExtensionState.REQUIRED);
                extensions.add(repoExtension);
                repoExtension.setText(repoinitText.toString());
            } else {
                throw new IllegalStateException("Repoinit sections already processed");
            }
        }
    }