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   */
50  @Named
51  @Singleton
52  public class DefaultPluginDescriptorCache implements PluginDescriptorCache {
53  
54      private Map<Key, PluginDescriptor> descriptors = new ConcurrentHashMap<>(128);
55  
56      public void flush() {
57          descriptors.clear();
58      }
59  
60      public Key createKey(Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session) {
61          return new CacheKey(plugin, repositories, session);
62      }
63  
64      public PluginDescriptor get(Key cacheKey) {
65          return clone(descriptors.get(cacheKey));
66      }
67  
68      @Override
69      public PluginDescriptor get(Key key, PluginDescriptorSupplier supplier)
70              throws PluginDescriptorParsingException, PluginResolutionException, InvalidPluginDescriptorException {
71          try {
72              return clone(descriptors.computeIfAbsent(key, k -> {
73                  try {
74                      return clone(supplier.load());
75                  } catch (PluginDescriptorParsingException
76                          | PluginResolutionException
77                          | InvalidPluginDescriptorException e) {
78                      throw new RuntimeException(e);
79                  }
80              }));
81          } catch (RuntimeException e) {
82              if (e.getCause() instanceof PluginDescriptorParsingException) {
83                  throw (PluginDescriptorParsingException) e.getCause();
84              }
85              if (e.getCause() instanceof PluginResolutionException) {
86                  throw (PluginResolutionException) e.getCause();
87              }
88              if (e.getCause() instanceof InvalidPluginDescriptorException) {
89                  throw (InvalidPluginDescriptorException) e.getCause();
90              }
91              throw e;
92          }
93      }
94  
95      public void put(Key cacheKey, PluginDescriptor pluginDescriptor) {
96          descriptors.put(cacheKey, clone(pluginDescriptor));
97      }
98  
99      protected static PluginDescriptor clone(PluginDescriptor original) {
100         PluginDescriptor clone = null;
101 
102         if (original != null) {
103             clone = new PluginDescriptor();
104 
105             clone.setGroupId(original.getGroupId());
106             clone.setArtifactId(original.getArtifactId());
107             clone.setVersion(original.getVersion());
108             clone.setGoalPrefix(original.getGoalPrefix());
109             clone.setInheritedByDefault(original.isInheritedByDefault());
110 
111             clone.setName(original.getName());
112             clone.setDescription(original.getDescription());
113             clone.setRequiredMavenVersion(original.getRequiredMavenVersion());
114             clone.setRequiredJavaVersion(original.getRequiredJavaVersion());
115 
116             clone.setPluginArtifact(ArtifactUtils.copyArtifactSafe(original.getPluginArtifact()));
117 
118             clone.setComponents(clone(original.getMojos(), clone));
119             clone.setId(original.getId());
120             clone.setIsolatedRealm(original.isIsolatedRealm());
121             clone.setSource(original.getSource());
122 
123             clone.setDependencies(original.getDependencies());
124         }
125 
126         return clone;
127     }
128 
129     private static List<ComponentDescriptor<?>> clone(List<MojoDescriptor> mojos, PluginDescriptor pluginDescriptor) {
130         List<ComponentDescriptor<?>> clones = null;
131 
132         if (mojos != null) {
133             clones = new ArrayList<>(mojos.size());
134 
135             for (MojoDescriptor mojo : mojos) {
136                 MojoDescriptor clone = mojo.clone();
137                 clone.setPluginDescriptor(pluginDescriptor);
138                 clones.add(clone);
139             }
140         }
141 
142         return clones;
143     }
144 
145     private static final class CacheKey implements Key {
146 
147         private final String groupId;
148 
149         private final String artifactId;
150 
151         private final String version;
152 
153         private final WorkspaceRepository workspace;
154 
155         private final LocalRepository localRepo;
156 
157         private final List<RemoteRepository> repositories;
158 
159         private final int hashCode;
160 
161         CacheKey(Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session) {
162             groupId = plugin.getGroupId();
163             artifactId = plugin.getArtifactId();
164             version = plugin.getVersion();
165 
166             workspace = RepositoryUtils.getWorkspace(session);
167             localRepo = session.getLocalRepository();
168             this.repositories = new ArrayList<>(repositories.size());
169             for (RemoteRepository repository : repositories) {
170                 if (repository.isRepositoryManager()) {
171                     this.repositories.addAll(repository.getMirroredRepositories());
172                 } else {
173                     this.repositories.add(repository);
174                 }
175             }
176 
177             int hash = 17;
178             hash = hash * 31 + groupId.hashCode();
179             hash = hash * 31 + artifactId.hashCode();
180             hash = hash * 31 + version.hashCode();
181             hash = hash * 31 + hash(workspace);
182             hash = hash * 31 + localRepo.hashCode();
183             hash = hash * 31 + RepositoryUtils.repositoriesHashCode(repositories);
184             this.hashCode = hash;
185         }
186 
187         @Override
188         public int hashCode() {
189             return hashCode;
190         }
191 
192         @Override
193         public boolean equals(Object obj) {
194             if (this == obj) {
195                 return true;
196             }
197 
198             if (!(obj instanceof CacheKey)) {
199                 return false;
200             }
201 
202             CacheKey that = (CacheKey) obj;
203 
204             return Objects.equals(this.artifactId, that.artifactId)
205                     && Objects.equals(this.groupId, that.groupId)
206                     && Objects.equals(this.version, that.version)
207                     && Objects.equals(this.localRepo, that.localRepo)
208                     && Objects.equals(this.workspace, that.workspace)
209                     && RepositoryUtils.repositoriesEquals(this.repositories, that.repositories);
210         }
211 
212         @Override
213         public String toString() {
214             return groupId + ':' + artifactId + ':' + version;
215         }
216 
217         private static int hash(Object obj) {
218             return obj != null ? obj.hashCode() : 0;
219         }
220     }
221 }