001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl.resolution;
020
021import java.util.List;
022
023import org.eclipse.aether.RepositorySystemSession;
024import org.eclipse.aether.resolution.ArtifactResult;
025import org.eclipse.aether.spi.resolution.ArtifactResolverPostProcessor;
026import org.eclipse.aether.util.ConfigUtils;
027
028import static java.util.Objects.requireNonNull;
029
030/**
031 * Support class to implement {@link ArtifactResolverPostProcessor}.
032 *
033 * @since 1.9.0
034 */
035public abstract class ArtifactResolverPostProcessorSupport implements ArtifactResolverPostProcessor {
036    private static final String CONFIG_PROP_PREFIX = "aether.artifactResolver.postProcessor.";
037
038    private final String name;
039
040    protected ArtifactResolverPostProcessorSupport(String name) {
041        this.name = requireNonNull(name);
042    }
043
044    /**
045     * This implementation will call into underlying code only if enabled.
046     */
047    @Override
048    public void postProcess(RepositorySystemSession session, List<ArtifactResult> artifactResults) {
049        if (isEnabled(session)) {
050            doPostProcess(session, artifactResults);
051        }
052    }
053
054    protected abstract void doPostProcess(RepositorySystemSession session, List<ArtifactResult> artifactResults);
055
056    /**
057     * To be used by underlying implementations to form configuration property keys properly scoped.
058     */
059    protected String configPropKey(String name) {
060        requireNonNull(name);
061        return CONFIG_PROP_PREFIX + this.name + "." + name;
062    }
063
064    /**
065     * Returns {@code true} if session configuration marks this instance as enabled.
066     * <p>
067     * Default value is {@code false}.
068     */
069    protected boolean isEnabled(RepositorySystemSession session) {
070        return ConfigUtils.getBoolean(session, false, CONFIG_PROP_PREFIX + this.name);
071    }
072}