All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.sling.feature.builder.BuilderContext Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.sling.feature.builder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Builder context holds services used by {@link FeatureBuilder}.
 *
 * This class is not thread-safe.
 */
public class BuilderContext {

    /** The required feature provider */
    private final FeatureProvider provider;

    /** The optional artifact provider. */
    private ArtifactProvider artifactProvider;

    private final Map> extensionConfiguration = new HashMap<>();
    private final List mergeExtensions = new ArrayList<>();
    private final List postProcessExtensions = new ArrayList<>();
    private final List artifactsOverrides = new ArrayList<>();
    private final Map variables = new HashMap<>();
    private final Map frameworkProperties = new HashMap<>();


    /**
     * Create a new context
     *
     * @param provider A provider providing the included features
     * @throws IllegalArgumentException If feature provider is {@code null}
     */
    public BuilderContext(final FeatureProvider provider) {
        if ( provider == null ) {
            throw new IllegalArgumentException("Provider must not be null");
        }
        this.provider = provider;
    }

    /**
     * Set the artifact provider
     *
     * @param ap An ArtifactProvider to resolve artifact IDs to files
     * @return The builder context
     */
    public BuilderContext setArtifactProvider(final ArtifactProvider ap) {
        this.artifactProvider = ap;
        return this;
    }

    /**
     * Add overrides for the variables
     *
     * @param vars The overrides
     * @return The builder context
     */
    public BuilderContext addVariablesOverrides(final Map vars) {
        this.variables.putAll(vars);
        return this;
    }

    /**
     * Add overrides for the framework properties
     *
     * @param props The overrides
     * @return The builder context
     */
    public BuilderContext addFrameworkPropertiesOverrides(final Map props) {
        this.frameworkProperties.putAll(props);
        return this;
    }

    /**
     * Add overrides for artifact clashes
     *
     * @param overrides The overrides
     * @return The builder context
     */
    public BuilderContext addArtifactsOverrides(final List overrides) {
        this.artifactsOverrides.addAll(overrides);
        return this;
    }

    /**
     * Add merge extensions
     *
     * @param extensions A list of merge extensions
     * @return The builder context
     */
    public BuilderContext addMergeExtensions(final MergeHandler... extensions) {
        mergeExtensions.addAll(Arrays.asList(extensions));
        return this;
    }

    /**
     * Add post process extensions
     *
     * @param extensions A list of extensions
     * @return The builder context
     */
    public BuilderContext addPostProcessExtensions(final PostProcessHandler... extensions) {
        postProcessExtensions.addAll(Arrays.asList(extensions));
        return this;
    }

    /**
     * Set a handler configuration
     *
     * @param name The name of the handler
     * @param cfg The configuration for the handler
     * @return The builder context
     */
    public BuilderContext setHandlerConfiguration(final String name, final Map cfg) {
        this.extensionConfiguration.put(name, cfg);
        return this;
    }

    /**
     * Obtain the handler configuration.
     *
     * @return The current handler configuration object. The key is the handler name
     *         and the value is a map of configuration values.
     */
    Map> getHandlerConfigurations() {
        return this.extensionConfiguration;
    }

    ArtifactProvider getArtifactProvider() {
        return this.artifactProvider;
    }

    List getArtifactOverrides() {
        return this.artifactsOverrides;
    }

    Map getVariablesOverrides() {
        return this.variables;
    }

    Map getFrameworkPropertiesOverrides() {
        return this.frameworkProperties;
    }

    /**
     * Get the feature provider.
     * @return The feature provider
     */
    FeatureProvider getFeatureProvider() {
        return this.provider;
    }

    /**
     * Get the list of merge extensions
     * @return The list of merge extensions
     */
    List getMergeExtensions() {
        return this.mergeExtensions;
    }

    /**
     * Get the list of extension post processors
     * @return The list of post processors
     */
    List getPostProcessExtensions() {
        return this.postProcessExtensions;
    }

    /**
     * Clone the context and replace the feature provider
     * @param featureProvider The new feature provider
     * @return Cloned context
     */
    BuilderContext clone(final FeatureProvider featureProvider) {
        final BuilderContext ctx = new BuilderContext(featureProvider);
        ctx.setArtifactProvider(this.artifactProvider);
        ctx.artifactsOverrides.addAll(this.artifactsOverrides);
        ctx.variables.putAll(this.variables);
        ctx.frameworkProperties.putAll(this.frameworkProperties);
        ctx.mergeExtensions.addAll(mergeExtensions);
        ctx.postProcessExtensions.addAll(postProcessExtensions);
        return ctx;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy