1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.project;
20  
21  import java.nio.file.Files;
22  import java.nio.file.Path;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.Map;
27  import java.util.Objects;
28  import java.util.Set;
29  
30  import org.apache.maven.model.Model;
31  
32  
33  
34  
35  
36  
37  
38  
39  class ReactorModelPool {
40      private final Map<GAKey, Set<Model>> modelsByGa = new HashMap<>();
41  
42      private final Map<Path, Model> modelsByPath = new HashMap<>();
43  
44      
45  
46  
47  
48  
49  
50  
51  
52  
53      public Model get(String groupId, String artifactId, String version) {
54          return modelsByGa.getOrDefault(new GAKey(groupId, artifactId), Collections.emptySet()).stream()
55                  .filter(m -> version == null || version.equals(getVersion(m)))
56                  .reduce((a, b) -> {
57                      throw new IllegalStateException(
58                              "Multiple modules with key " + a.getGroupId() + ':' + a.getArtifactId());
59                  })
60                  .orElse(null);
61      }
62  
63      
64  
65  
66  
67  
68  
69  
70      public Model get(Path path) {
71          final Path pomFile;
72          if (Files.isDirectory(path)) {
73              pomFile = path.resolve("pom.xml");
74          } else {
75              pomFile = path;
76          }
77          return modelsByPath.get(pomFile);
78      }
79  
80      private String getVersion(Model model) {
81          String version = model.getVersion();
82          if (version == null && model.getParent() != null) {
83              version = model.getParent().getVersion();
84          }
85          return version;
86      }
87  
88      static class Builder {
89          private ReactorModelPool pool = new ReactorModelPool();
90  
91          Builder put(Path pomFile, Model model) {
92              pool.modelsByPath.put(pomFile, model);
93              pool.modelsByGa
94                      .computeIfAbsent(new GAKey(getGroupId(model), model.getArtifactId()), k -> new HashSet<Model>())
95                      .add(model);
96              return this;
97          }
98  
99          ReactorModelPool build() {
100             return pool;
101         }
102 
103         private static String getGroupId(Model model) {
104             String groupId = model.getGroupId();
105             if (groupId == null && model.getParent() != null) {
106                 groupId = model.getParent().getGroupId();
107             }
108             return groupId;
109         }
110     }
111 
112     private static final class GAKey {
113 
114         private final String groupId;
115 
116         private final String artifactId;
117 
118         private final int hashCode;
119 
120         GAKey(String groupId, String artifactId) {
121             this.groupId = (groupId != null) ? groupId : "";
122             this.artifactId = (artifactId != null) ? artifactId : "";
123 
124             hashCode = Objects.hash(this.groupId, this.artifactId);
125         }
126 
127         @Override
128         public boolean equals(Object obj) {
129             if (this == obj) {
130                 return true;
131             }
132 
133             GAKey that = (GAKey) obj;
134 
135             return artifactId.equals(that.artifactId) && groupId.equals(that.groupId);
136         }
137 
138         @Override
139         public int hashCode() {
140             return hashCode;
141         }
142 
143         @Override
144         public String toString() {
145             StringBuilder buffer = new StringBuilder(128);
146             buffer.append(groupId).append(':').append(artifactId);
147             return buffer.toString();
148         }
149     }
150 }