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.RepositorySystemSession;
26 import org.eclipse.aether.graph.DependencyNode;
27 import org.eclipse.aether.graph.Exclusion;
28
29 /**
30 * A utility class assisting in analyzing the effects of dependency management.
31 */
32 public final class DependencyManagerUtils
33 {
34
35 /**
36 * The key in the repository session's {@link RepositorySystemSession#getConfigProperties() configuration
37 * properties} used to store a {@link Boolean} flag controlling the verbose mode for dependency management. If
38 * enabled, the original attributes of a dependency before its update due to dependency managemnent will be recorded
39 * in the node's {@link DependencyNode#getData() custom data} when building a dependency graph.
40 */
41 public static final String CONFIG_PROP_VERBOSE = "aether.dependencyManager.verbose";
42
43 /**
44 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original version is
45 * stored.
46 */
47 public static final String NODE_DATA_PREMANAGED_VERSION = "premanaged.version";
48
49 /**
50 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original scope is
51 * stored.
52 */
53 public static final String NODE_DATA_PREMANAGED_SCOPE = "premanaged.scope";
54
55 /**
56 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original optional
57 * flag is stored.
58 */
59 public static final String NODE_DATA_PREMANAGED_OPTIONAL = "premanaged.optional";
60
61 /**
62 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original exclusions
63 * are stored.
64 *
65 * @since 1.1.0
66 */
67 public static final String NODE_DATA_PREMANAGED_EXCLUSIONS = "premanaged.exclusions";
68
69 /**
70 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original properties
71 * are stored.
72 *
73 * @since 1.1.0
74 */
75 public static final String NODE_DATA_PREMANAGED_PROPERTIES = "premanaged.properties";
76
77 /**
78 * Gets the version or version range of the specified dependency node before dependency management was applied (if
79 * any).
80 *
81 * @param node The dependency node to retrieve the premanaged data for, must not be {@code null}.
82 *
83 * @return The node's dependency version before dependency management or {@code null} if the version was not managed
84 * or if {@link #CONFIG_PROP_VERBOSE} was not enabled.
85 */
86 public static String getPremanagedVersion( DependencyNode node )
87 {
88 if ( ( node.getManagedBits() & DependencyNode.MANAGED_VERSION ) == 0 )
89 {
90 return null;
91 }
92 return cast( node.getData().get( NODE_DATA_PREMANAGED_VERSION ), String.class );
93 }
94
95 /**
96 * Gets the scope of the specified dependency node before dependency management was applied (if any).
97 *
98 * @param node The dependency node to retrieve the premanaged data for, must not be {@code null}.
99 *
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 }