-
-
Notifications
You must be signed in to change notification settings - Fork 545
/
DependencyResolver.java
377 lines (314 loc) · 12.9 KB
/
DependencyResolver.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Copyright (C) 2012-present the original author or authors.
*
* Licensed 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.pf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.pf4j.util.DirectedGraph;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This class builds a dependency graph for a list of plugins (descriptors).
* The entry point is the {@link #resolve(List)} method, method that returns a {@link Result} object.
* The {@code Result} class contains nice information about the result of resolve operation (if it's a cyclic dependency,
* they are not found dependencies, they are dependencies with wrong version).
* This class is very useful for if-else scenarios.
* <p>
* Only some attributes (pluginId, dependencies and pluginVersion) from {@link PluginDescriptor} are used in
* the process of {@code resolve} operation.
*
* @author Decebal Suiu
*/
public class DependencyResolver {
private static final Logger log = LoggerFactory.getLogger(DependencyResolver.class);
private final VersionManager versionManager;
private DirectedGraph<String> dependenciesGraph; // the value is 'pluginId'
private DirectedGraph<String> dependentsGraph; // the value is 'pluginId'
private boolean resolved;
public DependencyResolver(VersionManager versionManager) {
this.versionManager = versionManager;
}
/**
* Resolve the dependencies for the given plugins.
*
* @param plugins the list of plugins
* @return a {@link Result} object
*/
public Result resolve(List<PluginDescriptor> plugins) {
// create graphs
dependenciesGraph = new DirectedGraph<>();
dependentsGraph = new DirectedGraph<>();
// populate graphs
Map<String, PluginDescriptor> pluginByIds = new HashMap<>();
for (PluginDescriptor plugin : plugins) {
addPlugin(plugin);
pluginByIds.put(plugin.getPluginId(), plugin);
}
log.debug("Graph: {}", dependenciesGraph);
// get a sorted list of dependencies
List<String> sortedPlugins = dependenciesGraph.reverseTopologicalSort();
log.debug("Plugins order: {}", sortedPlugins);
// create the result object
Result result = new Result(sortedPlugins);
resolved = true;
if (sortedPlugins != null) { // no cyclic dependency
// detect not found dependencies
for (String pluginId : sortedPlugins) {
if (!pluginByIds.containsKey(pluginId)) {
result.addNotFoundDependency(pluginId);
}
}
}
// check dependencies versions
for (PluginDescriptor plugin : plugins) {
String pluginId = plugin.getPluginId();
String existingVersion = plugin.getVersion();
List<String> dependents = getDependents(pluginId);
while (!dependents.isEmpty()) {
String dependentId = dependents.remove(0);
PluginDescriptor dependent = pluginByIds.get(dependentId);
String requiredVersion = getDependencyVersionSupport(dependent, pluginId);
boolean ok = checkDependencyVersion(requiredVersion, existingVersion);
if (!ok) {
result.addWrongDependencyVersion(new WrongDependencyVersion(pluginId, dependentId, existingVersion, requiredVersion));
}
}
}
return result;
}
/**
* Retrieves the plugins ids that the given plugin id directly depends on.
*
* @param pluginId the unique plugin identifier, specified in its metadata
* @return an immutable list of dependencies (new list for each call)
*/
public List<String> getDependencies(String pluginId) {
checkResolved();
return new ArrayList<>(dependenciesGraph.getNeighbors(pluginId));
}
/**
* Retrieves the plugins ids that the given content is a direct dependency of.
*
* @param pluginId the unique plugin identifier, specified in its metadata
* @return an immutable list of dependents (new list for each call)
*/
public List<String> getDependents(String pluginId) {
checkResolved();
return new ArrayList<>(dependentsGraph.getNeighbors(pluginId));
}
/**
* Check if an existing version of dependency is compatible with the required version (from plugin descriptor).
*
* @param requiredVersion the required version
* @param existingVersion the existing version
* @return {@code true} if the existing version is compatible with the required version, {@code false} otherwise
*/
protected boolean checkDependencyVersion(String requiredVersion, String existingVersion) {
return versionManager.checkVersionConstraint(existingVersion, requiredVersion);
}
private void addPlugin(PluginDescriptor descriptor) {
String pluginId = descriptor.getPluginId();
List<PluginDependency> dependencies = descriptor.getDependencies();
if (dependencies.isEmpty()) {
dependenciesGraph.addVertex(pluginId);
dependentsGraph.addVertex(pluginId);
} else {
boolean edgeAdded = false;
for (PluginDependency dependency : dependencies) {
// Don't register optional plugins in the dependency graph to avoid automatic disabling of the plugin,
// if an optional dependency is missing.
if (!dependency.isOptional()) {
edgeAdded = true;
dependenciesGraph.addEdge(pluginId, dependency.getPluginId());
dependentsGraph.addEdge(dependency.getPluginId(), pluginId);
}
}
// Register the plugin without dependencies, if all of its dependencies are optional.
if (!edgeAdded) {
dependenciesGraph.addVertex(pluginId);
dependentsGraph.addVertex(pluginId);
}
}
}
private void checkResolved() {
if (!resolved) {
throw new IllegalStateException("Call 'resolve' method first");
}
}
private String getDependencyVersionSupport(PluginDescriptor dependent, String dependencyId) {
List<PluginDependency> dependencies = dependent.getDependencies();
for (PluginDependency dependency : dependencies) {
if (dependencyId.equals(dependency.getPluginId())) {
return dependency.getPluginVersionSupport();
}
}
throw new IllegalStateException("Cannot find a dependency with id '" + dependencyId +
"' for plugin '" + dependent.getPluginId() + "'");
}
/**
* The result of the {@link #resolve(List)} operation.
*/
public static class Result {
private boolean cyclicDependency;
private final List<String> notFoundDependencies; // value is "pluginId"
private final List<String> sortedPlugins; // value is "pluginId"
private final List<WrongDependencyVersion> wrongVersionDependencies;
Result(List<String> sortedPlugins) {
if (sortedPlugins == null) {
cyclicDependency = true;
this.sortedPlugins = Collections.emptyList();
} else {
this.sortedPlugins = new ArrayList<>(sortedPlugins);
}
notFoundDependencies = new ArrayList<>();
wrongVersionDependencies = new ArrayList<>();
}
/**
* Returns true is a cyclic dependency was detected.
*
* @return true is a cyclic dependency was detected
*/
public boolean hasCyclicDependency() {
return cyclicDependency;
}
/**
* Returns true if there are dependencies required that were not found.
*
* @return true if there are dependencies required that were not found
*/
public boolean hasNotFoundDependencies() {
return !notFoundDependencies.isEmpty();
}
/**
* Returns a list with dependencies required that were not found.
*
* @return a list with dependencies required that were not found
*/
public List<String> getNotFoundDependencies() {
return notFoundDependencies;
}
/**
* Returns true if there are dependencies with wrong version.
*
* @return true if there are dependencies with wrong version
*/
public boolean hasWrongVersionDependencies() {
return !wrongVersionDependencies.isEmpty();
}
/**
* Returns a list with dependencies with wrong version.
*
* @return a list with dependencies with wrong version
*/
public List<WrongDependencyVersion> getWrongVersionDependencies() {
return wrongVersionDependencies;
}
/**
* Returns true if the result is OK (no cyclic dependency, no not found dependencies, no wrong version dependencies).
*
* @return true if the result is OK
*/
public boolean isOK() {
return !hasCyclicDependency() && !hasNotFoundDependencies() && !hasWrongVersionDependencies();
}
/**
* Get the list of plugins in dependency sorted order.
*
* @return the list of plugins in dependency sorted order
*/
public List<String> getSortedPlugins() {
return sortedPlugins;
}
void addNotFoundDependency(String pluginId) {
notFoundDependencies.add(pluginId);
}
void addWrongDependencyVersion(WrongDependencyVersion wrongDependencyVersion) {
wrongVersionDependencies.add(wrongDependencyVersion);
}
}
/**
* Represents a wrong dependency version.
*/
public static class WrongDependencyVersion {
private final String dependencyId; // value is "pluginId"
private final String dependentId; // value is "pluginId"
private final String existingVersion;
private final String requiredVersion;
WrongDependencyVersion(String dependencyId, String dependentId, String existingVersion, String requiredVersion) {
this.dependencyId = dependencyId;
this.dependentId = dependentId;
this.existingVersion = existingVersion;
this.requiredVersion = requiredVersion;
}
public String getDependencyId() {
return dependencyId;
}
public String getDependentId() {
return dependentId;
}
public String getExistingVersion() {
return existingVersion;
}
public String getRequiredVersion() {
return requiredVersion;
}
@Override
public String toString() {
return "WrongDependencyVersion{" +
"dependencyId='" + dependencyId + '\'' +
", dependentId='" + dependentId + '\'' +
", existingVersion='" + existingVersion + '\'' +
", requiredVersion='" + requiredVersion + '\'' +
'}';
}
}
/**
* It will be thrown if a cyclic dependency is detected.
*/
public static class CyclicDependencyException extends PluginRuntimeException {
public CyclicDependencyException() {
super("Cyclic dependencies");
}
}
/**
* Indicates that the dependencies required were not found.
*/
public static class DependenciesNotFoundException extends PluginRuntimeException {
private final List<String> dependencies;
public DependenciesNotFoundException(List<String> dependencies) {
super("Dependencies '{}' not found", dependencies);
this.dependencies = dependencies;
}
public List<String> getDependencies() {
return dependencies;
}
}
/**
* Indicates that some dependencies have wrong version.
*/
public static class DependenciesWrongVersionException extends PluginRuntimeException {
private final List<WrongDependencyVersion> dependencies;
public DependenciesWrongVersionException(List<WrongDependencyVersion> dependencies) {
super("Dependencies '{}' have wrong version", dependencies);
this.dependencies = dependencies;
}
public List<WrongDependencyVersion> getDependencies() {
return dependencies;
}
}
}