001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl.collect;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.Map;
026
027import org.eclipse.aether.artifact.Artifact;
028import org.eclipse.aether.collection.DependencyManagement;
029import org.eclipse.aether.collection.DependencyManager;
030import org.eclipse.aether.graph.DefaultDependencyNode;
031import org.eclipse.aether.graph.Dependency;
032import org.eclipse.aether.graph.DependencyNode;
033import org.eclipse.aether.graph.Exclusion;
034import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
035
036/**
037 * Helper class used during collection.
038 */
039public class PremanagedDependency {
040
041    final String premanagedVersion;
042
043    final String premanagedScope;
044
045    final Boolean premanagedOptional;
046
047    /**
048     * @since 1.1.0
049     */
050    final Collection<Exclusion> premanagedExclusions;
051
052    /**
053     * @since 1.1.0
054     */
055    final Map<String, String> premanagedProperties;
056
057    final int managedBits;
058
059    final Dependency managedDependency;
060
061    final boolean premanagedState;
062
063    @SuppressWarnings("checkstyle:parameternumber")
064    PremanagedDependency(
065            String premanagedVersion,
066            String premanagedScope,
067            Boolean premanagedOptional,
068            Collection<Exclusion> premanagedExclusions,
069            Map<String, String> premanagedProperties,
070            int managedBits,
071            Dependency managedDependency,
072            boolean premanagedState) {
073        this.premanagedVersion = premanagedVersion;
074        this.premanagedScope = premanagedScope;
075        this.premanagedOptional = premanagedOptional;
076        this.premanagedExclusions = premanagedExclusions != null
077                ? Collections.unmodifiableCollection(new ArrayList<>(premanagedExclusions))
078                : null;
079
080        this.premanagedProperties =
081                premanagedProperties != null ? Collections.unmodifiableMap(new HashMap<>(premanagedProperties)) : null;
082
083        this.managedBits = managedBits;
084        this.managedDependency = managedDependency;
085        this.premanagedState = premanagedState;
086    }
087
088    public static PremanagedDependency create(
089            DependencyManager depManager,
090            Dependency dependency,
091            boolean disableVersionManagement,
092            boolean premanagedState) {
093        DependencyManagement depMngt = depManager != null ? depManager.manageDependency(dependency) : null;
094
095        int managedBits = 0;
096        String premanagedVersion = null;
097        String premanagedScope = null;
098        Boolean premanagedOptional = null;
099        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}