001package org.eclipse.aether.util.graph.manager;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.eclipse.aether.RepositorySystemSession;
023import org.eclipse.aether.graph.DependencyNode;
024
025/**
026 * A utility class assisting in analyzing the effects of dependency management.
027 */
028public final class DependencyManagerUtils
029{
030
031    /**
032     * The key in the repository session's {@link RepositorySystemSession#getConfigProperties() configuration
033     * properties} used to store a {@link Boolean} flag controlling the verbose mode for dependency management. If
034     * enabled, the original attributes of a dependency before its update due to dependency managemnent will be recorded
035     * in the node's {@link DependencyNode#getData() custom data} when building a dependency graph.
036     */
037    public static final String CONFIG_PROP_VERBOSE = "aether.dependencyManager.verbose";
038
039    /**
040     * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original version is
041     * stored.
042     */
043    public static final String NODE_DATA_PREMANAGED_VERSION = "premanaged.version";
044
045    /**
046     * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original scope is
047     * stored.
048     */
049    public static final String NODE_DATA_PREMANAGED_SCOPE = "premanaged.scope";
050
051    /**
052     * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original optional
053     * flag is stored.
054     */
055    public static final String NODE_DATA_PREMANAGED_OPTIONAL = "premanaged.optional";
056
057    /**
058     * Gets the version or version range of the specified dependency node before dependency management was applied (if
059     * any).
060     * 
061     * @param node The dependency node to retrieve the premanaged data for, must not be {@code null}.
062     * @return The node's dependency version before dependency management or {@code null} if the version was not managed
063     *         or if {@link #CONFIG_PROP_VERBOSE} was not enabled.
064     */
065    public static String getPremanagedVersion( DependencyNode node )
066    {
067        if ( ( node.getManagedBits() & DependencyNode.MANAGED_VERSION ) == 0 )
068        {
069            return null;
070        }
071        return cast( node.getData().get( NODE_DATA_PREMANAGED_VERSION ), String.class );
072    }
073
074    /**
075     * Gets the scope of the specified dependency node before dependency management was applied (if any).
076     * 
077     * @param node The dependency node to retrieve the premanaged data for, must not be {@code null}.
078     * @return The node's dependency scope before dependency management or {@code null} if the scope was not managed or
079     *         if {@link #CONFIG_PROP_VERBOSE} was not enabled.
080     */
081    public static String getPremanagedScope( DependencyNode node )
082    {
083        if ( ( node.getManagedBits() & DependencyNode.MANAGED_SCOPE ) == 0 )
084        {
085            return null;
086        }
087        return cast( node.getData().get( NODE_DATA_PREMANAGED_SCOPE ), String.class );
088    }
089
090    /**
091     * Gets the optional flag 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     * @return The node's optional flag before dependency management or {@code null} if the flag was not managed or if
095     *         {@link #CONFIG_PROP_VERBOSE} was not enabled.
096     */
097    public static Boolean getPremanagedOptional( DependencyNode node )
098    {
099        if ( ( node.getManagedBits() & DependencyNode.MANAGED_OPTIONAL ) == 0 )
100        {
101            return null;
102        }
103        return cast( node.getData().get( NODE_DATA_PREMANAGED_OPTIONAL ), Boolean.class );
104    }
105
106    private static <T> T cast( Object obj, Class<T> type )
107    {
108        return type.isInstance( obj ) ? type.cast( obj ) : null;
109    }
110
111}