forked from jeromebenois/ivy
-
Notifications
You must be signed in to change notification settings - Fork 20
/
LatestConflictManager.java
172 lines (150 loc) · 5.92 KB
/
LatestConflictManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.ivy.plugins.conflict;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
import org.apache.ivy.core.module.id.ModuleRevisionId;
import org.apache.ivy.core.resolve.IvyNode;
import org.apache.ivy.plugins.latest.ArtifactInfo;
import org.apache.ivy.plugins.latest.LatestStrategy;
import org.apache.ivy.util.Message;
public class LatestConflictManager extends AbstractConflictManager {
public static class NoConflictResolvedYetException extends RuntimeException {
}
protected static final class IvyNodeArtifactInfo implements ArtifactInfo {
private final IvyNode node;
private IvyNodeArtifactInfo(IvyNode dep) {
node = dep;
}
public long getLastModified() {
long lastModified = node.getLastModified();
if (lastModified == 0) {
// if the last modified timestamp is unknown, we can't resolve
// the conflicts now, and trigger an exception which will be catched
// in the main resolveConflicts method
throw new NoConflictResolvedYetException();
} else {
return lastModified;
}
}
public String getRevision() {
return node.getResolvedId().getRevision();
}
public IvyNode getNode() {
return node;
}
}
private LatestStrategy strategy;
private String strategyName;
public LatestConflictManager() {
}
public LatestConflictManager(LatestStrategy strategy) {
this.strategy = strategy;
}
public LatestConflictManager(String name, LatestStrategy strategy) {
setName(name);
this.strategy = strategy;
}
public Collection resolveConflicts(IvyNode parent, Collection conflicts) {
if (conflicts.size() < 2) {
return conflicts;
}
for (Iterator iter = conflicts.iterator(); iter.hasNext();) {
IvyNode node = (IvyNode) iter.next();
DependencyDescriptor dd = node.getDependencyDescriptor(parent);
if (dd != null && dd.isForce()
&& parent.getResolvedId().equals(dd.getParentRevisionId())) {
return Collections.singleton(node);
}
}
/*
* If the list of conflicts contains dynamic revisions, delay the conflict
* calculation until they are resolved.
* TODO: we probably could already evict some of the dynamic revisions!
*/
for (Iterator iter = conflicts.iterator(); iter.hasNext();) {
IvyNode node = (IvyNode) iter.next();
ModuleRevisionId modRev = node.getResolvedId();
if (getSettings().getVersionMatcher().isDynamic(modRev)) {
return null;
}
}
ArrayList unevicted = new ArrayList();
for (Iterator iter = conflicts.iterator(); iter.hasNext();) {
IvyNode node = (IvyNode) iter.next();
if (!node.isCompletelyEvicted())
unevicted.add(node);
}
if (unevicted.size() > 0) {
conflicts = unevicted;
}
try {
IvyNodeArtifactInfo latest = (IvyNodeArtifactInfo)
getStrategy().findLatest(toArtifactInfo(conflicts), null);
if (latest != null) {
return Collections.singleton(latest.getNode());
} else {
return conflicts;
}
} catch (NoConflictResolvedYetException ex) {
// we have not enough informations in the nodes to resolve conflict
// according to the resolveConflicts contract, we must return null
return null;
}
}
protected ArtifactInfo[] toArtifactInfo(Collection conflicts) {
List artifacts = new ArrayList(conflicts.size());
for (Iterator iter = conflicts.iterator(); iter.hasNext();) {
IvyNode node = (IvyNode) iter.next();
artifacts.add(new IvyNodeArtifactInfo(node));
}
return (ArtifactInfo[]) artifacts.toArray(new ArtifactInfo[artifacts.size()]);
}
public LatestStrategy getStrategy() {
if (strategy == null) {
if (strategyName != null) {
strategy = getSettings().getLatestStrategy(strategyName);
if (strategy == null) {
Message.error("unknown latest strategy: " + strategyName);
strategy = getSettings().getDefaultLatestStrategy();
}
} else {
strategy = getSettings().getDefaultLatestStrategy();
}
}
return strategy;
}
/**
* To conform to configurator API
*
* @param strategyName
*/
public void setLatest(String strategyName) {
this.strategyName = strategyName;
}
public void setStrategy(LatestStrategy strategy) {
this.strategy = strategy;
}
public String toString() {
return strategy != null ? String.valueOf(strategy) : strategyName;
}
}