View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.building;
20  
21  import java.io.File;
22  import java.nio.file.Path;
23  import java.util.Map;
24  import java.util.Objects;
25  import java.util.concurrent.ConcurrentHashMap;
26  import java.util.function.Supplier;
27  
28  import org.apache.maven.model.Model;
29  import org.apache.maven.model.locator.ModelLocator;
30  
31  /**
32   *
33   * @since 4.0.0
34   */
35  class DefaultTransformerContext implements TransformerContext {
36      final ModelLocator 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                              // Ignore
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      DefaultTransformerContext(ModelLocator 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         File file = modelLocator.locateExistingPom(path.toFile());
110         return file != null ? file.toPath() : null;
111     }
112 
113     static class GAKey {
114         private final String groupId;
115         private final String artifactId;
116         private final int hashCode;
117 
118         GAKey(String groupId, String artifactId) {
119             this.groupId = groupId;
120             this.artifactId = artifactId;
121             this.hashCode = Objects.hash(groupId, artifactId);
122         }
123 
124         @Override
125         public int hashCode() {
126             return hashCode;
127         }
128 
129         @Override
130         public boolean equals(Object obj) {
131             if (this == obj) {
132                 return true;
133             }
134             if (!(obj instanceof GAKey)) {
135                 return false;
136             }
137 
138             GAKey other = (GAKey) obj;
139             return Objects.equals(artifactId, other.artifactId) && Objects.equals(groupId, other.groupId);
140         }
141     }
142 }