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