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