1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
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     @Override
120     public Key createKey(
121             Plugin plugin,
122             DependencyFilter extensionFilter,
123             List<RemoteRepository> repositories,
124             RepositorySystemSession session) {
125         return new CacheKey(plugin, extensionFilter, repositories, session);
126     }
127 
128     @Override
129     public CacheRecord get(Key key) throws PluginResolutionException {
130         CacheRecord cacheRecord = cache.get(key);
131 
132         if (cacheRecord != null && cacheRecord.getException() != null) {
133             throw cacheRecord.getException();
134         }
135 
136         return cacheRecord;
137     }
138 
139     @Override
140     public CacheRecord put(Key key, List<Artifact> pluginArtifacts) {
141         Objects.requireNonNull(pluginArtifacts, "pluginArtifacts cannot be null");
142 
143         assertUniqueKey(key);
144 
145         CacheRecord record = new CacheRecord(Collections.unmodifiableList(new ArrayList<>(pluginArtifacts)));
146 
147         cache.put(key, record);
148 
149         return record;
150     }
151 
152     protected void assertUniqueKey(Key key) {
153         if (cache.containsKey(key)) {
154             throw new IllegalStateException("Duplicate artifact resolution result for plugin " + key);
155         }
156     }
157 
158     @Override
159     public CacheRecord put(Key key, PluginResolutionException exception) {
160         Objects.requireNonNull(exception, "exception cannot be null");
161 
162         assertUniqueKey(key);
163 
164         CacheRecord record = new CacheRecord(exception);
165 
166         cache.put(key, record);
167 
168         return record;
169     }
170 
171     @Override
172     public void flush() {
173         cache.clear();
174     }
175 
176     protected static int pluginHashCode(Plugin plugin) {
177         return CacheUtils.pluginHashCode(plugin);
178     }
179 
180     protected static boolean pluginEquals(Plugin a, Plugin b) {
181         return CacheUtils.pluginEquals(a, b);
182     }
183 
184     @Override
185     public void register(MavenProject project, Key cacheKey, CacheRecord record) {
186         
187     }
188 }