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.plugin;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Objects;
28  import java.util.concurrent.ConcurrentHashMap;
29  
30  import org.apache.maven.RepositoryUtils;
31  import org.apache.maven.artifact.ArtifactUtils;
32  import org.apache.maven.model.Plugin;
33  import org.apache.maven.plugin.descriptor.MojoDescriptor;
34  import org.apache.maven.plugin.descriptor.PluginDescriptor;
35  import org.codehaus.plexus.component.repository.ComponentDescriptor;
36  import org.eclipse.aether.RepositorySystemSession;
37  import org.eclipse.aether.repository.LocalRepository;
38  import org.eclipse.aether.repository.RemoteRepository;
39  import org.eclipse.aether.repository.WorkspaceRepository;
40  
41  /**
42   * Caches raw plugin descriptors. A raw plugin descriptor is a descriptor that has just been extracted from the plugin
43   * artifact and does not contain any runtime specific data. The cache must not be used for descriptors that hold runtime
44   * data like the plugin realm. <strong>Warning:</strong> This is an internal utility interface that is only public for
45   * technical reasons, it is not part of the public API. In particular, this interface can be changed or deleted without
46   * prior notice.
47   *
48   * @since 3.0
49   * @author Benjamin Bentmann
50   */
51  @Singleton
52  @Named
53  public class DefaultPluginDescriptorCache implements PluginDescriptorCache {
54  
55      private Map<Key, PluginDescriptor> descriptors = new ConcurrentHashMap<>(128);
56      private Map<Key, Key> keys = new ConcurrentHashMap<>();
57  
58      public void flush() {
59          descriptors.clear();
60      }
61  
62      public Key createKey(Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session) {
63          return keys.computeIfAbsent(new CacheKey(plugin, repositories, session), k -> k);
64      }
65  
66      public PluginDescriptor get(Key cacheKey) {
67          return clone(descriptors.get(cacheKey));
68      }
69  
70      @Override
71      public PluginDescriptor get(Key key, PluginDescriptorSupplier supplier)
72              throws PluginDescriptorParsingException, PluginResolutionException, InvalidPluginDescriptorException {
73  
74          try {
75              PluginDescriptor desc = descriptors.get(key);
76              if (desc == null) {
77                  synchronized (key) {
78                      desc = descriptors.get(key);
79                      if (desc == null) {
80                          desc = supplier.load();
81                          descriptors.putIfAbsent(key, clone(desc));
82                      }
83                  }
84              }
85              return clone(desc);
86          } catch (PluginDescriptorParsingException | PluginResolutionException | InvalidPluginDescriptorException e) {
87              throw e;
88          }
89      }
90  
91      public void put(Key cacheKey, PluginDescriptor pluginDescriptor) {
92          descriptors.put(cacheKey, clone(pluginDescriptor));
93      }
94  
95      protected static PluginDescriptor clone(PluginDescriptor original) {
96          PluginDescriptor clone = null;
97  
98          if (original != null) {
99              clone = new PluginDescriptor();
100 
101             clone.setGroupId(original.getGroupId());
102             clone.setArtifactId(original.getArtifactId());
103             clone.setVersion(original.getVersion());
104             clone.setGoalPrefix(original.getGoalPrefix());
105             clone.setInheritedByDefault(original.isInheritedByDefault());
106 
107             clone.setName(original.getName());
108             clone.setDescription(original.getDescription());
109             clone.setRequiredMavenVersion(original.getRequiredMavenVersion());
110             clone.setRequiredJavaVersion(original.getRequiredJavaVersion());
111 
112             clone.setPluginArtifact(ArtifactUtils.copyArtifactSafe(original.getPluginArtifact()));
113 
114             clone.setComponents(clone(original.getMojos(), clone));
115             clone.setId(original.getId());
116             clone.setIsolatedRealm(original.isIsolatedRealm());
117             clone.setSource(original.getSource());
118 
119             clone.setDependencies(original.getDependencies());
120         }
121 
122         return clone;
123     }
124 
125     private static List<ComponentDescriptor<?>> clone(List<MojoDescriptor> mojos, PluginDescriptor pluginDescriptor) {
126         List<ComponentDescriptor<?>> clones = null;
127 
128         if (mojos != null) {
129             clones = new ArrayList<>(mojos.size());
130 
131             for (MojoDescriptor mojo : mojos) {
132                 MojoDescriptor clone = mojo.clone();
133                 clone.setPluginDescriptor(pluginDescriptor);
134                 clones.add(clone);
135             }
136         }
137 
138         return clones;
139     }
140 
141     private static final class CacheKey implements Key {
142 
143         private final String groupId;
144 
145         private final String artifactId;
146 
147         private final String version;
148 
149         private final WorkspaceRepository workspace;
150 
151         private final LocalRepository localRepo;
152 
153         private final List<RemoteRepository> repositories;
154 
155         private final int hashCode;
156 
157         CacheKey(Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session) {
158             groupId = plugin.getGroupId();
159             artifactId = plugin.getArtifactId();
160             version = plugin.getVersion();
161 
162             workspace = RepositoryUtils.getWorkspace(session);
163             localRepo = session.getLocalRepository();
164             this.repositories = new ArrayList<>(repositories.size());
165             for (RemoteRepository repository : repositories) {
166                 if (repository.isRepositoryManager()) {
167                     this.repositories.addAll(repository.getMirroredRepositories());
168                 } else {
169                     this.repositories.add(repository);
170                 }
171             }
172 
173             int hash = 17;
174             hash = hash * 31 + groupId.hashCode();
175             hash = hash * 31 + artifactId.hashCode();
176             hash = hash * 31 + version.hashCode();
177             hash = hash * 31 + hash(workspace);
178             hash = hash * 31 + localRepo.hashCode();
179             hash = hash * 31 + RepositoryUtils.repositoriesHashCode(repositories);
180             this.hashCode = hash;
181         }
182 
183         @Override
184         public int hashCode() {
185             return hashCode;
186         }
187 
188         @Override
189         public boolean equals(Object obj) {
190             if (this == obj) {
191                 return true;
192             }
193 
194             if (!(obj instanceof CacheKey)) {
195                 return false;
196             }
197 
198             CacheKey that = (CacheKey) obj;
199 
200             return Objects.equals(this.artifactId, that.artifactId)
201                     && Objects.equals(this.groupId, that.groupId)
202                     && Objects.equals(this.version, that.version)
203                     && Objects.equals(this.localRepo, that.localRepo)
204                     && Objects.equals(this.workspace, that.workspace)
205                     && RepositoryUtils.repositoriesEquals(this.repositories, that.repositories);
206         }
207 
208         @Override
209         public String toString() {
210             return groupId + ':' + artifactId + ':' + version;
211         }
212 
213         private static int hash(Object obj) {
214             return obj != null ? obj.hashCode() : 0;
215         }
216     }
217 }