View Javadoc
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.internal.impl.collect;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.collection.DependencyManagement;
29  import org.eclipse.aether.collection.DependencyManager;
30  import org.eclipse.aether.graph.DefaultDependencyNode;
31  import org.eclipse.aether.graph.Dependency;
32  import org.eclipse.aether.graph.DependencyNode;
33  import org.eclipse.aether.graph.Exclusion;
34  import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
35  
36  /**
37   * Helper class used during collection.
38   */
39  public class PremanagedDependency {
40  
41      final String premanagedVersion;
42  
43      final String premanagedScope;
44  
45      final Boolean premanagedOptional;
46  
47      /**
48       * @since 1.1.0
49       */
50      final Collection<Exclusion> premanagedExclusions;
51  
52      /**
53       * @since 1.1.0
54       */
55      final Map<String, String> premanagedProperties;
56  
57      final int managedBits;
58  
59      final Dependency managedDependency;
60  
61      final boolean premanagedState;
62  
63      @SuppressWarnings("checkstyle:parameternumber")
64      PremanagedDependency(
65              String premanagedVersion,
66              String premanagedScope,
67              Boolean premanagedOptional,
68              Collection<Exclusion> premanagedExclusions,
69              Map<String, String> premanagedProperties,
70              int managedBits,
71              Dependency managedDependency,
72              boolean premanagedState) {
73          this.premanagedVersion = premanagedVersion;
74          this.premanagedScope = premanagedScope;
75          this.premanagedOptional = premanagedOptional;
76          this.premanagedExclusions = premanagedExclusions != null
77                  ? Collections.unmodifiableCollection(new ArrayList<>(premanagedExclusions))
78                  : null;
79  
80          this.premanagedProperties =
81                  premanagedProperties != null ? Collections.unmodifiableMap(new HashMap<>(premanagedProperties)) : null;
82  
83          this.managedBits = managedBits;
84          this.managedDependency = managedDependency;
85          this.premanagedState = premanagedState;
86      }
87  
88      public static PremanagedDependency create(
89              DependencyManager depManager,
90              Dependency dependency,
91              boolean disableVersionManagement,
92              boolean premanagedState) {
93          DependencyManagement depMngt = depManager != null ? depManager.manageDependency(dependency) : null;
94  
95          int managedBits = 0;
96          String premanagedVersion = null;
97          String premanagedScope = null;
98          Boolean premanagedOptional = null;
99          Collection<Exclusion> premanagedExclusions = null;
100         Map<String, String> premanagedProperties = null;
101 
102         if (depMngt != null) {
103             if (depMngt.getVersion() != null && !disableVersionManagement) {
104                 Artifact artifact = dependency.getArtifact();
105                 premanagedVersion = artifact.getVersion();
106                 dependency = dependency.setArtifact(artifact.setVersion(depMngt.getVersion()));
107                 managedBits |= DependencyNode.MANAGED_VERSION;
108             }
109             if (depMngt.getProperties() != null) {
110                 Artifact artifact = dependency.getArtifact();
111                 premanagedProperties = artifact.getProperties();
112                 dependency = dependency.setArtifact(artifact.setProperties(depMngt.getProperties()));
113                 managedBits |= DependencyNode.MANAGED_PROPERTIES;
114             }
115             if (depMngt.getScope() != null) {
116                 premanagedScope = dependency.getScope();
117                 dependency = dependency.setScope(depMngt.getScope());
118                 managedBits |= DependencyNode.MANAGED_SCOPE;
119             }
120             if (depMngt.getOptional() != null) {
121                 premanagedOptional = dependency.isOptional();
122                 dependency = dependency.setOptional(depMngt.getOptional());
123                 managedBits |= DependencyNode.MANAGED_OPTIONAL;
124             }
125             if (depMngt.getExclusions() != null) {
126                 premanagedExclusions = dependency.getExclusions();
127                 dependency = dependency.setExclusions(depMngt.getExclusions());
128                 managedBits |= DependencyNode.MANAGED_EXCLUSIONS;
129             }
130         }
131         return new PremanagedDependency(
132                 premanagedVersion,
133                 premanagedScope,
134                 premanagedOptional,
135                 premanagedExclusions,
136                 premanagedProperties,
137                 managedBits,
138                 dependency,
139                 premanagedState);
140     }
141 
142     public Dependency getManagedDependency() {
143         return managedDependency;
144     }
145 
146     public void applyTo(DefaultDependencyNode child) {
147         child.setManagedBits(managedBits);
148         if (premanagedState) {
149             child.setData(DependencyManagerUtils.NODE_DATA_PREMANAGED_VERSION, premanagedVersion);
150             child.setData(DependencyManagerUtils.NODE_DATA_PREMANAGED_SCOPE, premanagedScope);
151             child.setData(DependencyManagerUtils.NODE_DATA_PREMANAGED_OPTIONAL, premanagedOptional);
152             child.setData(DependencyManagerUtils.NODE_DATA_PREMANAGED_EXCLUSIONS, premanagedExclusions);
153             child.setData(DependencyManagerUtils.NODE_DATA_PREMANAGED_PROPERTIES, premanagedProperties);
154         }
155     }
156 }