private static void processPrototype()

in src/main/java/org/apache/sling/feature/builder/FeatureBuilder.java [400:517]


    private static void processPrototype(final Feature feature, final Prototype prototype) {
        // process bundles removals
        for (final ArtifactId a : prototype.getBundleRemovals()) {
            boolean removed = false;
            final boolean ignoreVersion = a.getOSGiVersion().equals(Version.emptyVersion);
            if ( ignoreVersion ) {
                // remove any version of that bundle
                while (feature.getBundles().removeSame(a)) {
                    // continue to remove
                    removed = true;
                }
            } else {
                // remove exact version
                removed = feature.getBundles().removeExact(a);
            }
            if ( !removed ) {
                throw new IllegalStateException("Bundle " + a + " can't be removed from feature " + feature.getId()
                        + " as it is not part of that feature.");
            }
            final Iterator<Configuration> iter = feature.getConfigurations().iterator();
            while ( iter.hasNext() ) {
                final Configuration cfg = iter.next();
                final String bundleId = (String)cfg.getProperties().get(Configuration.PROP_ARTIFACT_ID);
                if (bundleId != null) {
                    final ArtifactId bundleArtifactId = ArtifactId.fromMvnId(bundleId);
                    boolean remove = false;
                    if ( ignoreVersion ) {
                        remove = bundleArtifactId.isSame(a);
                    } else {
                        remove = bundleArtifactId.equals(a);
                    }
                    if (  remove) {
                        iter.remove();
                    }
                }
            }
        }

        // process configuration removals
        for (final String c : prototype.getConfigurationRemovals()) {
            final int attrPos = c.indexOf('@');
            final String pid = (attrPos == -1 ? c : c.substring(0, attrPos));
            final String attr = (attrPos == -1 ? null : c.substring(attrPos + 1));

            final Configuration found = feature.getConfigurations().getConfiguration(pid);
            if ( found != null ) {
                if ( attr == null ) {
                    feature.getConfigurations().remove(found);
                } else {
                    found.getProperties().remove(attr);
                }
            }
        }

        // process framework properties removals
        for (final String p : prototype.getFrameworkPropertiesRemovals()) {
            feature.getFrameworkProperties().remove(p);
        }

        // process extensions removals
        for (final String name : prototype.getExtensionRemovals()) {
            for (final Extension ext : feature.getExtensions()) {
                if ( ext.getName().equals(name) ) {
                    feature.getExtensions().remove(ext);
                    break;
                }
            }
        }
        // process artifact extensions removals
        for (final Map.Entry<String, List<ArtifactId>> entry : prototype.getArtifactExtensionRemovals().entrySet()) {
            for (final Extension ext : feature.getExtensions()) {
                if ( ext.getName().equals(entry.getKey()) ) {
                    for(final ArtifactId toRemove : entry.getValue() ) {
                        boolean removed = false;
                        final boolean ignoreVersion = toRemove.getOSGiVersion().equals(Version.emptyVersion);
                        final Iterator<Artifact> iter = ext.getArtifacts().iterator();
                        while ( iter.hasNext() ) {
                            final Artifact a = iter.next();

                            boolean remove = false;
                            if ( ignoreVersion ) {
                                // remove any version of that bundle
                                if ( a.getId().isSame(toRemove) ) {
                                    remove = true;
                                }
                            } else {
                                // remove exact version

                                remove = a.getId().equals(toRemove);
                            }
                            if ( remove ) {
                                iter.remove();
                                removed = true;
                            }
                            if ( remove && !ignoreVersion ) {
                                break;
                            }
                        }
                        if ( !removed ) {
                            throw new IllegalStateException("Artifact " + toRemove + " can't be removed from feature "
                                    + feature.getId() + " as it is not part of that feature.");
                        }
                    }
                    break;
                }
            }
        }

        // process requirement removals
        for (final MatchingRequirement req : prototype.getRequirementRemovals()) {
            feature.getRequirements().remove(req);
        }

        // process capability removals
        for (final Capability cap : prototype.getCapabilityRemovals()) {
            feature.getCapabilities().remove(cap);
        }
    }