1 package org.eclipse.aether.util.graph.manager;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.Collection;
23 import java.util.Map;
24
25 import org.eclipse.aether.graph.DependencyNode;
26 import org.eclipse.aether.graph.Exclusion;
27
28 /**
29 * A utility class assisting in analyzing the effects of dependency management.
30 */
31 public final class DependencyManagerUtils
32 {
33
34 /**
35 * The key in the repository session's {@link org.eclipse.aether.RepositorySystemSession#getConfigProperties()
36 * configuration properties} used to store a {@link Boolean} flag controlling the verbose mode for dependency
37 * management. If enabled, the original attributes of a dependency before its update due to dependency managemnent
38 * will be recorded * in the node's {@link DependencyNode#getData() custom data} when building a dependency graph.
39 */
40 public static final String CONFIG_PROP_VERBOSE = "aether.dependencyManager.verbose";
41
42 /**
43 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original version is
44 * stored.
45 */
46 public static final String NODE_DATA_PREMANAGED_VERSION = "premanaged.version";
47
48 /**
49 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original scope is
50 * stored.
51 */
52 public static final String NODE_DATA_PREMANAGED_SCOPE = "premanaged.scope";
53
54 /**
55 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original optional
56 * flag is stored.
57 */
58 public static final String NODE_DATA_PREMANAGED_OPTIONAL = "premanaged.optional";
59
60 /**
61 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original exclusions
62 * are stored.
63 *
64 * @since 1.1.0
65 */
66 public static final String NODE_DATA_PREMANAGED_EXCLUSIONS = "premanaged.exclusions";
67
68 /**
69 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original properties
70 * are stored.
71 *
72 * @since 1.1.0
73 */
74 public static final String NODE_DATA_PREMANAGED_PROPERTIES = "premanaged.properties";
75
76 /**
77 * Gets the version or version range of the specified dependency node before dependency management was applied (if
78 * any).
79 *
80 * @param node The dependency node to retrieve the premanaged data for, must not be {@code null}.
81 *
82 * @return The node's dependency version before dependency management or {@code null} if the version was not managed
83 * or if {@link #CONFIG_PROP_VERBOSE} was not enabled.
84 */
85 public static String getPremanagedVersion( DependencyNode node )
86 {
87 if ( ( node.getManagedBits() & DependencyNode.MANAGED_VERSION ) == 0 )
88 {
89 return null;
90 }
91 return cast( node.getData().get( NODE_DATA_PREMANAGED_VERSION ), String.class );
92 }
93
94 /**
95 * Gets the scope of the specified dependency node before dependency management was applied (if any).
96 *
97 * @param node The dependency node to retrieve the premanaged data for, must not be {@code null}.
98 *
99 * @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 }