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