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   * @deprecated use {@link org.apache.maven.api.services.ModelBuilder} instead
35   */
36  @Deprecated(since = "4.0.0")
37  class DefaultTransformerContext implements TransformerContext {
38      final ModelLocator modelLocator;
39  
40      final Map<String, String> userProperties = new ConcurrentHashMap<>();
41  
42      final Map<Path, Holder> modelByPath = new ConcurrentHashMap<>();
43  
44      final Map<GAKey, Holder> modelByGA = new ConcurrentHashMap<>();
45  
46      public static class Holder {
47          private volatile boolean set;
48          private volatile Model model;
49  
50          Holder() {}
51  
52          Holder(Model model) {
53              this.model = Objects.requireNonNull(model);
54              this.set = true;
55          }
56  
57          public static Model deref(Holder holder) {
58              return holder != null ? holder.get() : null;
59          }
60  
61          public Model get() {
62              if (!set) {
63                  synchronized (this) {
64                      if (!set) {
65                          try {
66                              this.wait();
67                          } catch (InterruptedException e) {
68                              // Ignore
69                          }
70                      }
71                  }
72              }
73              return model;
74          }
75  
76          public Model computeIfAbsent(Supplier<Model> supplier) {
77              if (!set) {
78                  synchronized (this) {
79                      if (!set) {
80                          this.set = true;
81                          this.model = supplier.get();
82                          this.notifyAll();
83                      }
84                  }
85              }
86              return model;
87          }
88      }
89  
90      DefaultTransformerContext(ModelLocator modelLocator) {
91          this.modelLocator = modelLocator;
92      }
93  
94      @Override
95      public String getUserProperty(String key) {
96          return userProperties.get(key);
97      }
98  
99      @Override
100     public Model getRawModel(Path from, Path p) {
101         return Holder.deref(modelByPath.get(p));
102     }
103 
104     @Override
105     public Model getRawModel(Path from, String groupId, String artifactId) {
106         return Holder.deref(modelByGA.get(new GAKey(groupId, artifactId)));
107     }
108 
109     @Override
110     public Path locate(Path path) {
111         File file = modelLocator.locateExistingPom(path.toFile());
112         return file != null ? file.toPath() : null;
113     }
114 
115     static class GAKey {
116         private final String groupId;
117         private final String artifactId;
118         private final int hashCode;
119 
120         GAKey(String groupId, String artifactId) {
121             this.groupId = groupId;
122             this.artifactId = artifactId;
123             this.hashCode = Objects.hash(groupId, artifactId);
124         }
125 
126         @Override
127         public int hashCode() {
128             return hashCode;
129         }
130 
131         @Override
132         public boolean equals(Object obj) {
133             if (this == obj) {
134                 return true;
135             }
136             if (!(obj instanceof GAKey)) {
137                 return false;
138             }
139 
140             GAKey other = (GAKey) obj;
141             return Objects.equals(artifactId, other.artifactId) && Objects.equals(groupId, other.groupId);
142         }
143     }
144 }