1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl.model;
20
21 import java.nio.file.Path;
22 import java.util.Map;
23 import java.util.Objects;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.function.Supplier;
26
27 import org.apache.maven.api.model.Model;
28 import org.apache.maven.api.services.ModelTransformerContext;
29 import org.apache.maven.api.services.model.ModelProcessor;
30
31
32
33
34
35 class DefaultModelTransformerContext implements ModelTransformerContext {
36 final ModelProcessor modelLocator;
37
38 final Map<String, String> userProperties = new ConcurrentHashMap<>();
39
40 final Map<Path, Holder> modelByPath = new ConcurrentHashMap<>();
41
42 final Map<GAKey, Holder> modelByGA = new ConcurrentHashMap<>();
43
44 public static class Holder {
45 private volatile boolean set;
46 private volatile Model model;
47
48 Holder() {}
49
50 Holder(Model model) {
51 this.model = Objects.requireNonNull(model);
52 this.set = true;
53 }
54
55 public static Model deref(Holder holder) {
56 return holder != null ? holder.get() : null;
57 }
58
59 public Model get() {
60 if (!set) {
61 synchronized (this) {
62 if (!set) {
63 try {
64 this.wait();
65 } catch (InterruptedException e) {
66
67 }
68 }
69 }
70 }
71 return model;
72 }
73
74 public Model computeIfAbsent(Supplier<Model> supplier) {
75 if (!set) {
76 synchronized (this) {
77 if (!set) {
78 this.set = true;
79 this.model = supplier.get();
80 this.notifyAll();
81 }
82 }
83 }
84 return model;
85 }
86 }
87
88 DefaultModelTransformerContext(ModelProcessor modelLocator) {
89 this.modelLocator = modelLocator;
90 }
91
92 @Override
93 public String getUserProperty(String key) {
94 return userProperties.get(key);
95 }
96
97 @Override
98 public Model getRawModel(Path from, Path p) {
99 return Holder.deref(modelByPath.get(p));
100 }
101
102 @Override
103 public Model getRawModel(Path from, String groupId, String artifactId) {
104 return Holder.deref(modelByGA.get(new GAKey(groupId, artifactId)));
105 }
106
107 @Override
108 public Path locate(Path path) {
109 return modelLocator.locateExistingPom(path);
110 }
111
112 static class GAKey {
113 private final String groupId;
114 private final String artifactId;
115 private final int hashCode;
116
117 GAKey(String groupId, String artifactId) {
118 this.groupId = groupId;
119 this.artifactId = artifactId;
120 this.hashCode = Objects.hash(groupId, artifactId);
121 }
122
123 @Override
124 public int hashCode() {
125 return hashCode;
126 }
127
128 @Override
129 public boolean equals(Object obj) {
130 if (this == obj) {
131 return true;
132 }
133 if (!(obj instanceof GAKey)) {
134 return false;
135 }
136
137 GAKey other = (GAKey) obj;
138 return Objects.equals(artifactId, other.artifactId) && Objects.equals(groupId, other.groupId);
139 }
140 }
141 }