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