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