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.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   * @since 4.0.0
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                              // 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      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 }