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.util.graph.manager;
020
021import java.util.Collection;
022import java.util.Map;
023
024import org.eclipse.aether.ConfigurationProperties;
025import org.eclipse.aether.RepositorySystemSession;
026import org.eclipse.aether.graph.DependencyNode;
027import org.eclipse.aether.graph.Exclusion;
028
029/**
030 * A utility class assisting in analyzing the effects of dependency management.
031 */
032public final class DependencyManagerUtils {
033
034    /**
035     * The key in the repository session's {@link org.eclipse.aether.RepositorySystemSession#getConfigProperties()
036     * configuration properties} used to store a {@link Boolean} flag controlling the verbose mode for dependency
037     * management. If enabled, the original attributes of a dependency before its update due to dependency management
038     * will be recorded * in the node's {@link DependencyNode#getData() custom data} when building a dependency graph.
039     *
040     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
041     * @configurationType {@link java.lang.Boolean}
042     * @configurationDefaultValue false
043     */
044    public static final String CONFIG_PROP_VERBOSE =
045            ConfigurationProperties.PREFIX_AETHER + "dependencyManager.verbose";
046
047    /**
048     * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original version is
049     * stored.
050     */
051    public static final String NODE_DATA_PREMANAGED_VERSION = "premanaged.version";
052
053    /**
054     * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original scope is
055     * stored.
056     */
057    public static final String NODE_DATA_PREMANAGED_SCOPE = "premanaged.scope";
058
059    /**
060     * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original optional
061     * flag is stored.
062     */
063    public static final String NODE_DATA_PREMANAGED_OPTIONAL = "premanaged.optional";
064
065    /**
066     * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original exclusions
067     * are stored.
068     *
069     * @since 1.1.0
070     */
071    public static final String NODE_DATA_PREMANAGED_EXCLUSIONS = "premanaged.exclusions";
072
073    /**
074     * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original properties
075     * are stored.
076     *
077     * @since 1.1.0
078     */
079    public static final String NODE_DATA_PREMANAGED_PROPERTIES = "premanaged.properties";
080
081    /**
082     * Gets the version or version range of the specified dependency node before dependency management was applied (if
083     * any).
084     *
085     * @param node The dependency node to retrieve the premanaged data for, must not be {@code null}.
086     *
087     * @return The node's dependency version before dependency management or {@code null} if the version was not managed
088     *         or if {@link #CONFIG_PROP_VERBOSE} was not enabled.
089     */
090    public static String getPremanagedVersion(DependencyNode node) {
091        if ((node.getManagedBits() & DependencyNode.MANAGED_VERSION) == 0) {
092            return null;
093        }
094        return cast(node.getData().get(NODE_DATA_PREMANAGED_VERSION), String.class);
095    }
096
097    /**
098     * Gets the scope of the specified dependency node before dependency management was applied (if any).
099     *
100     * @param node The dependency node to retrieve the premanaged data for, must not be {@code null}.
101     *
102     * @return The node's dependency scope before dependency management or {@code null} if the scope was not managed or
103     *         if {@link #CONFIG_PROP_VERBOSE} was not enabled.
104     */
105    public static String getPremanagedScope(DependencyNode node) {
106        if ((node.getManagedBits() & DependencyNode.MANAGED_SCOPE) == 0) {
107            return null;
108        }
109        return cast(node.getData().get(NODE_DATA_PREMANAGED_SCOPE), String.class);
110    }
111
112    /**
113     * Gets the optional flag of the specified dependency node before dependency management was applied (if any).
114     *
115     * @param node The dependency node to retrieve the premanaged data for, must not be {@code null}.
116     *
117     * @return The node's optional flag before dependency management or {@code null} if the flag was not managed or if
118     *         {@link #CONFIG_PROP_VERBOSE} was not enabled.
119     */
120    public static Boolean getPremanagedOptional(DependencyNode node) {
121        if ((node.getManagedBits() & DependencyNode.MANAGED_OPTIONAL) == 0) {
122            return null;
123        }
124        return cast(node.getData().get(NODE_DATA_PREMANAGED_OPTIONAL), Boolean.class);
125    }
126
127    /**
128     * Gets the {@code Exclusion}s of the specified dependency node before dependency management was applied (if any).
129     *
130     * @param node The dependency node to retrieve the premanaged data for, must not be {@code null}.
131     *
132     * @return The nodes' {@code Exclusion}s before dependency management or {@code null} if exclusions were not managed
133     *         or if {@link #CONFIG_PROP_VERBOSE} was not enabled.
134     *
135     * @since 1.1.0
136     */
137    @SuppressWarnings("unchecked")
138    public static Collection<Exclusion> getPremanagedExclusions(DependencyNode node) {
139        if ((node.getManagedBits() & DependencyNode.MANAGED_EXCLUSIONS) == 0) {
140            return null;
141        }
142        return cast(node.getData().get(NODE_DATA_PREMANAGED_EXCLUSIONS), Collection.class);
143    }
144
145    /**
146     * Gets the properties of the specified dependency node before dependency management was applied (if any).
147     *
148     * @param node The dependency node to retrieve the premanaged data for, must not be {@code null}.
149     *
150     * @return The nodes' properties before dependency management or {@code null} if properties were not managed or if
151     *         {@link #CONFIG_PROP_VERBOSE} was not enabled.
152     *
153     * @since 1.1.0
154     */
155    @SuppressWarnings("unchecked")
156    public static Map<String, String> getPremanagedProperties(DependencyNode node) {
157        if ((node.getManagedBits() & DependencyNode.MANAGED_PROPERTIES) == 0) {
158            return null;
159        }
160        return cast(node.getData().get(NODE_DATA_PREMANAGED_PROPERTIES), Map.class);
161    }
162
163    private static <T> T cast(Object obj, Class<T> type) {
164        return type.isInstance(obj) ? type.cast(obj) : null;
165    }
166}