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.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.model.Model;
28  
29  /**
30   *
31   * @author Robert Scholte
32   * @since 4.0.0
33   */
34  class DefaultTransformerContext implements TransformerContext {
35      final Map<String, String> userProperties = new ConcurrentHashMap<>();
36  
37      final Map<Path, Holder> modelByPath = new ConcurrentHashMap<>();
38  
39      final Map<GAKey, Holder> modelByGA = new ConcurrentHashMap<>();
40  
41      public static class Holder {
42          private volatile boolean set;
43          private volatile Model model;
44  
45          Holder() {}
46  
47          public static Model deref(Holder holder) {
48              return holder != null ? holder.get() : null;
49          }
50  
51          public Model get() {
52              if (!set) {
53                  synchronized (this) {
54                      if (!set) {
55                          try {
56                              this.wait();
57                          } catch (InterruptedException e) {
58                              // Ignore
59                          }
60                      }
61                  }
62              }
63              return model;
64          }
65  
66          public Model computeIfAbsent(Supplier<Model> supplier) {
67              if (!set) {
68                  synchronized (this) {
69                      if (!set) {
70                          this.set = true;
71                          this.model = supplier.get();
72                          this.notifyAll();
73                      }
74                  }
75              }
76              return model;
77          }
78      }
79  
80      @Override
81      public String getUserProperty(String key) {
82          return userProperties.get(key);
83      }
84  
85      @Override
86      public Model getRawModel(Path p) {
87          return Holder.deref(modelByPath.get(p));
88      }
89  
90      @Override
91      public Model getRawModel(String groupId, String artifactId) {
92          return Holder.deref(modelByGA.get(new GAKey(groupId, artifactId)));
93      }
94  
95      static class GAKey {
96          private final String groupId;
97          private final String artifactId;
98          private final int hashCode;
99  
100         GAKey(String groupId, String artifactId) {
101             this.groupId = groupId;
102             this.artifactId = artifactId;
103             this.hashCode = Objects.hash(groupId, artifactId);
104         }
105 
106         @Override
107         public int hashCode() {
108             return hashCode;
109         }
110 
111         @Override
112         public boolean equals(Object obj) {
113             if (this == obj) {
114                 return true;
115             }
116             if (!(obj instanceof GAKey)) {
117                 return false;
118             }
119 
120             GAKey other = (GAKey) obj;
121             return Objects.equals(artifactId, other.artifactId) && Objects.equals(groupId, other.groupId);
122         }
123     }
124 }