1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.resolver;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Objects;
29 import java.util.stream.Collectors;
30
31 import org.apache.maven.api.model.Model;
32 import org.apache.maven.internal.impl.resolver.MavenWorkspaceReader;
33 import org.eclipse.aether.artifact.Artifact;
34 import org.eclipse.aether.repository.WorkspaceReader;
35 import org.eclipse.aether.repository.WorkspaceRepository;
36
37 import static java.util.Objects.requireNonNull;
38
39
40
41
42
43
44
45 public class MavenChainedWorkspaceReader implements MavenWorkspaceReader {
46
47 protected List<WorkspaceReader> readers;
48 protected WorkspaceRepository repository;
49
50
51
52
53
54
55 public MavenChainedWorkspaceReader(WorkspaceReader... readers) {
56 setReaders(Arrays.asList(readers));
57 }
58
59 @Override
60 public WorkspaceRepository getRepository() {
61 return this.repository;
62 }
63
64 @Override
65 public Model findModel(Artifact artifact) {
66 requireNonNull(artifact, "artifact cannot be null");
67 Model model = null;
68
69 for (WorkspaceReader workspaceReader : readers) {
70 if (workspaceReader instanceof MavenWorkspaceReader) {
71 model = ((MavenWorkspaceReader) workspaceReader).findModel(artifact);
72 if (model != null) {
73 break;
74 }
75 }
76 }
77
78 return model;
79 }
80
81 @Override
82 public File findArtifact(Artifact artifact) {
83 requireNonNull(artifact, "artifact cannot be null");
84 File file = null;
85
86 for (WorkspaceReader reader : readers) {
87 file = reader.findArtifact(artifact);
88 if (file != null) {
89 break;
90 }
91 }
92
93 return file;
94 }
95
96 @Override
97 public List<String> findVersions(Artifact artifact) {
98 requireNonNull(artifact, "artifact cannot be null");
99 Collection<String> versions = new LinkedHashSet<>();
100
101 for (WorkspaceReader reader : readers) {
102 versions.addAll(reader.findVersions(artifact));
103 }
104
105 return Collections.unmodifiableList(new ArrayList<>(versions));
106 }
107
108 public void setReaders(Collection<WorkspaceReader> readers) {
109 requireNonNull(readers, "readers");
110
111 this.readers = Collections.unmodifiableList(
112 new ArrayList<>(readers.stream().filter(Objects::nonNull).collect(Collectors.toList())));
113 Key key = new Key(this.readers);
114 this.repository = new WorkspaceRepository(key.getContentType(), key);
115 }
116
117 public List<WorkspaceReader> getReaders() {
118 return readers;
119 }
120
121 public void addReader(WorkspaceReader workspaceReader) {
122 requireNonNull(workspaceReader, "workspaceReader");
123 ArrayList<WorkspaceReader> newReaders = new ArrayList<>(this.readers);
124 newReaders.add(workspaceReader);
125 setReaders(newReaders);
126 }
127
128 private static class Key {
129 private final List<Object> keys;
130 private final String type;
131
132 Key(Collection<WorkspaceReader> readers) {
133 keys = readers.stream().map(r -> r.getRepository().getKey()).collect(Collectors.toList());
134 type = readers.stream().map(r -> r.getRepository().getContentType()).collect(Collectors.joining("+"));
135 }
136
137 public String getContentType() {
138 return type;
139 }
140
141 public boolean equals(Object obj) {
142 if (this == obj) {
143 return true;
144 } else {
145 return obj != null && this.getClass().equals(obj.getClass()) && this.keys.equals(((Key) obj).keys);
146 }
147 }
148
149 public int hashCode() {
150 return this.keys.hashCode();
151 }
152 }
153
154
155
156
157
158
159
160 public static WorkspaceReader of(Collection<WorkspaceReader> workspaceReaderCollection) {
161 WorkspaceReader[] readers = workspaceReaderCollection.toArray(new WorkspaceReader[0]);
162 if (readers.length == 1) {
163 return readers[0];
164 }
165 return new MavenChainedWorkspaceReader(readers);
166 }
167 }