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.Collections;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Objects;
29  import java.util.concurrent.ConcurrentHashMap;
30  
31  import org.apache.maven.RepositoryUtils;
32  import org.apache.maven.artifact.Artifact;
33  import org.apache.maven.model.Plugin;
34  import org.apache.maven.project.MavenProject;
35  import org.eclipse.aether.RepositorySystemSession;
36  import org.eclipse.aether.graph.DependencyFilter;
37  import org.eclipse.aether.repository.LocalRepository;
38  import org.eclipse.aether.repository.RemoteRepository;
39  import org.eclipse.aether.repository.WorkspaceRepository;
40  
41  /**
42   */
43  @Named
44  @Singleton
45  public class DefaultPluginArtifactsCache implements PluginArtifactsCache {
46      /**
47       * CacheKey
48       */
49      protected static class CacheKey implements Key {
50          private final Plugin plugin;
51  
52          private final WorkspaceRepository workspace;
53  
54          private final LocalRepository localRepo;
55  
56          private final List<RemoteRepository> repositories;
57  
58          private final DependencyFilter filter;
59  
60          private final int hashCode;
61  
62          public CacheKey(
63                  Plugin plugin,
64                  DependencyFilter extensionFilter,
65                  List<RemoteRepository> repositories,
66                  RepositorySystemSession session) {
67              this.plugin = plugin.clone();
68              workspace = RepositoryUtils.getWorkspace(session);
69              this.localRepo = session.getLocalRepository();
70              this.repositories = new ArrayList<>(repositories.size());
71              for (RemoteRepository repository : repositories) {
72                  if (repository.isRepositoryManager()) {
73                      this.repositories.addAll(repository.getMirroredRepositories());
74                  } else {
75                      this.repositories.add(repository);
76                  }
77              }
78              this.filter = extensionFilter;
79  
80              int hash = 17;
81              hash = hash * 31 + CacheUtils.pluginHashCode(plugin);
82              hash = hash * 31 + Objects.hashCode(workspace);
83              hash = hash * 31 + Objects.hashCode(localRepo);
84              hash = hash * 31 + RepositoryUtils.repositoriesHashCode(repositories);
85              hash = hash * 31 + Objects.hashCode(extensionFilter);
86              this.hashCode = hash;
87          }
88  
89          @Override
90          public String toString() {
91              return plugin.getId();
92          }
93  
94          @Override
95          public int hashCode() {
96              return hashCode;
97          }
98  
99          @Override
100         public boolean equals(Object o) {
101             if (o == this) {
102                 return true;
103             }
104 
105             if (o instanceof CacheKey that) {
106                 return CacheUtils.pluginEquals(plugin, that.plugin)
107                         && Objects.equals(workspace, that.workspace)
108                         && Objects.equals(localRepo, that.localRepo)
109                         && RepositoryUtils.repositoriesEquals(repositories, that.repositories)
110                         && Objects.equals(filter, that.filter);
111             } else {
112                 return false;
113             }
114         }
115     }
116 
117     protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<>();
118 
119     public Key createKey(
120             Plugin plugin,
121             DependencyFilter extensionFilter,
122             List<RemoteRepository> repositories,
123             RepositorySystemSession session) {
124         return new CacheKey(plugin, extensionFilter, repositories, session);
125     }
126 
127     public CacheRecord get(Key key) throws PluginResolutionException {
128         CacheRecord cacheRecord = cache.get(key);
129 
130         if (cacheRecord != null && cacheRecord.getException() != null) {
131             throw cacheRecord.getException();
132         }
133 
134         return cacheRecord;
135     }
136 
137     public CacheRecord put(Key key, List<Artifact> pluginArtifacts) {
138         Objects.requireNonNull(pluginArtifacts, "pluginArtifacts cannot be null");
139 
140         assertUniqueKey(key);
141 
142         CacheRecord record = new CacheRecord(Collections.unmodifiableList(new ArrayList<>(pluginArtifacts)));
143 
144         cache.put(key, record);
145 
146         return record;
147     }
148 
149     protected void assertUniqueKey(Key key) {
150         if (cache.containsKey(key)) {
151             throw new IllegalStateException("Duplicate artifact resolution result for plugin " + key);
152         }
153     }
154 
155     public CacheRecord put(Key key, PluginResolutionException exception) {
156         Objects.requireNonNull(exception, "exception cannot be null");
157 
158         assertUniqueKey(key);
159 
160         CacheRecord record = new CacheRecord(exception);
161 
162         cache.put(key, record);
163 
164         return record;
165     }
166 
167     public void flush() {
168         cache.clear();
169     }
170 
171     protected static int pluginHashCode(Plugin plugin) {
172         return CacheUtils.pluginHashCode(plugin);
173     }
174 
175     protected static boolean pluginEquals(Plugin a, Plugin b) {
176         return CacheUtils.pluginEquals(a, b);
177     }
178 
179     public void register(MavenProject project, Key cacheKey, CacheRecord record) {
180         // default cache does not track record usage
181     }
182 }