1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.util.graph.manager;
20
21 import java.util.Collection;
22 import java.util.Map;
23
24 import org.eclipse.aether.ConfigurationProperties;
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 * 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 management
38 * will be recorded in the node's {@link DependencyNode#getData() custom data} when building a dependency graph.
39 *
40 * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
41 * @configurationType {@link java.lang.Boolean}
42 * @configurationDefaultValue false
43 */
44 public static final String CONFIG_PROP_VERBOSE =
45 ConfigurationProperties.PREFIX_AETHER + "dependencyManager.verbose";
46
47 /**
48 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original version is
49 * stored.
50 */
51 public static final String NODE_DATA_PREMANAGED_VERSION = "premanaged.version";
52
53 /**
54 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original scope is
55 * stored.
56 */
57 public static final String NODE_DATA_PREMANAGED_SCOPE = "premanaged.scope";
58
59 /**
60 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original optional
61 * flag is stored.
62 */
63 public static final String NODE_DATA_PREMANAGED_OPTIONAL = "premanaged.optional";
64
65 /**
66 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original exclusions
67 * are stored.
68 *
69 * @since 1.1.0
70 */
71 public static final String NODE_DATA_PREMANAGED_EXCLUSIONS = "premanaged.exclusions";
72
73 /**
74 * The key in the dependency node's {@link DependencyNode#getData() custom data} under which the original properties
75 * are stored.
76 *
77 * @since 1.1.0
78 */
79 public static final String NODE_DATA_PREMANAGED_PROPERTIES = "premanaged.properties";
80
81 /**
82 * Gets the version or version range of the specified dependency node before dependency management was applied (if
83 * any).
84 *
85 * @param node the dependency node to retrieve the premanaged data for, must not be {@code null}
86 * @return the node's dependency version before dependency management or {@code null} if the version was not managed
87 * or if {@link #CONFIG_PROP_VERBOSE} was not enabled
88 */
89 public static String getPremanagedVersion(DependencyNode node) {
90 if ((node.getManagedBits() & DependencyNode.MANAGED_VERSION) == 0) {
91 return null;
92 }
93 return cast(node.getData().get(NODE_DATA_PREMANAGED_VERSION), String.class);
94 }
95
96 /**
97 * Gets the scope of the specified dependency node before dependency management was applied (if any).
98 *
99 * @param node the dependency node to retrieve the premanaged data for, must not be {@code null}
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 if ((node.getManagedBits() & DependencyNode.MANAGED_SCOPE) == 0) {
105 return null;
106 }
107 return cast(node.getData().get(NODE_DATA_PREMANAGED_SCOPE), String.class);
108 }
109
110 /**
111 * Gets the optional flag of the specified dependency node before dependency management was applied (if any).
112 *
113 * @param node the dependency node to retrieve the premanaged data for, must not be {@code null}
114 * @return the node's optional flag before dependency management or {@code null} if the flag was not managed or if
115 * {@link #CONFIG_PROP_VERBOSE} was not enabled
116 */
117 public static Boolean getPremanagedOptional(DependencyNode node) {
118 if ((node.getManagedBits() & DependencyNode.MANAGED_OPTIONAL) == 0) {
119 return null;
120 }
121 return cast(node.getData().get(NODE_DATA_PREMANAGED_OPTIONAL), Boolean.class);
122 }
123
124 /**
125 * Gets the {@code Exclusion}s of the specified dependency node before dependency management was applied (if any).
126 *
127 * @param node the dependency node to retrieve the premanaged data for, must not be {@code null}
128 * @return the nodes' {@code Exclusion}s before dependency management or {@code null} if exclusions were not managed
129 * or if {@link #CONFIG_PROP_VERBOSE} was not enabled
130 * @since 1.1.0
131 */
132 @SuppressWarnings("unchecked")
133 public static Collection<Exclusion> getPremanagedExclusions(DependencyNode node) {
134 if ((node.getManagedBits() & DependencyNode.MANAGED_EXCLUSIONS) == 0) {
135 return null;
136 }
137 return cast(node.getData().get(NODE_DATA_PREMANAGED_EXCLUSIONS), Collection.class);
138 }
139
140 /**
141 * Gets the properties of the specified dependency node before dependency management was applied (if any).
142 *
143 * @param node the dependency node to retrieve the premanaged data for, must not be {@code null}
144 * @return the nodes' properties before dependency management or {@code null} if properties were not managed or if
145 * {@link #CONFIG_PROP_VERBOSE} was not enabled
146 * @since 1.1.0
147 */
148 @SuppressWarnings("unchecked")
149 public static Map<String, String> getPremanagedProperties(DependencyNode node) {
150 if ((node.getManagedBits() & DependencyNode.MANAGED_PROPERTIES) == 0) {
151 return null;
152 }
153 return cast(node.getData().get(NODE_DATA_PREMANAGED_PROPERTIES), Map.class);
154 }
155
156 private static <T> T cast(Object obj, Class<T> type) {
157 return type.isInstance(obj) ? type.cast(obj) : null;
158 }
159 }