View Javadoc
1   package org.apache.maven.model.building;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
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  
30  /**
31   *
32   * @author Robert Scholte
33   * @since 4.0.0
34   */
35  class DefaultTransformerContext implements TransformerContext
36  {
37      final Map<String, String> userProperties = new ConcurrentHashMap<>();
38  
39      final Map<Path, Holder> modelByPath = new ConcurrentHashMap<>();
40  
41      final Map<GAKey, Holder> modelByGA = new ConcurrentHashMap<>();
42  
43      public static class Holder
44      {
45          private volatile boolean set;
46          private volatile Model model;
47  
48          Holder()
49          {
50          }
51  
52          public static Model deref( Holder holder )
53          {
54              return holder != null ? holder.get() : null;
55          }
56  
57          public Model get()
58          {
59              if ( !set )
60              {
61                  synchronized ( this )
62                  {
63                      if ( !set )
64                      {
65                          try
66                          {
67                              this.wait();
68                          }
69                          catch ( InterruptedException e )
70                          {
71                              // Ignore
72                          }
73                      }
74                  }
75              }
76              return model;
77          }
78  
79          public Model computeIfAbsent( Supplier<Model> supplier )
80          {
81              if ( !set )
82              {
83                  synchronized ( this )
84                  {
85                      if ( !set )
86                      {
87                          this.set = true;
88                          this.model = supplier.get();
89                          this.notifyAll();
90                      }
91                  }
92              }
93              return model;
94          }
95  
96      }
97  
98      @Override
99      public String getUserProperty( String key )
100     {
101         return userProperties.get( key );
102     }
103 
104     @Override
105     public Model getRawModel( Path p )
106     {
107         return Holder.deref( modelByPath.get( p ) );
108     }
109 
110     @Override
111     public Model getRawModel( String groupId, String artifactId )
112     {
113         return Holder.deref( modelByGA.get( new GAKey( groupId, artifactId ) ) );
114     }
115 
116     static class GAKey
117     {
118         private final String groupId;
119         private final String artifactId;
120         private final int hashCode;
121 
122         GAKey( String groupId, String artifactId )
123         {
124             this.groupId = groupId;
125             this.artifactId = artifactId;
126             this.hashCode = Objects.hash( groupId, artifactId );
127         }
128 
129         @Override
130         public int hashCode()
131         {
132             return hashCode;
133         }
134 
135         @Override
136         public boolean equals( Object obj )
137         {
138             if ( this == obj )
139             {
140                 return true;
141             }
142             if ( !( obj instanceof GAKey ) )
143             {
144                 return false;
145             }
146 
147             GAKey other = (GAKey) obj;
148             return Objects.equals( artifactId, other.artifactId ) && Objects.equals( groupId, other.groupId );
149         }
150     }
151 }