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