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 java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Objects;
26  import java.util.concurrent.ConcurrentHashMap;
27  import javax.inject.Named;
28  import javax.inject.Singleton;
29  import org.apache.maven.RepositoryUtils;
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.model.Plugin;
32  import org.apache.maven.project.MavenProject;
33  import org.eclipse.aether.RepositorySystemSession;
34  import org.eclipse.aether.graph.DependencyFilter;
35  import org.eclipse.aether.repository.LocalRepository;
36  import org.eclipse.aether.repository.RemoteRepository;
37  import org.eclipse.aether.repository.WorkspaceRepository;
38  
39  
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)) {
106                 return false;
107             }
108 
109             CacheKey that = (CacheKey) o;
110 
111             return CacheUtils.pluginEquals(plugin, that.plugin)
112                     && Objects.equals(workspace, that.workspace)
113                     && Objects.equals(localRepo, that.localRepo)
114                     && RepositoryUtils.repositoriesEquals(repositories, that.repositories)
115                     && Objects.equals(filter, that.filter);
116         }
117     }
118 
119     protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<>();
120 
121     public Key createKey(
122             Plugin plugin,
123             DependencyFilter extensionFilter,
124             List<RemoteRepository> repositories,
125             RepositorySystemSession session) {
126         return new CacheKey(plugin, extensionFilter, repositories, session);
127     }
128 
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     public CacheRecord put(Key key, List<Artifact> pluginArtifacts) {
140         Objects.requireNonNull(pluginArtifacts, "pluginArtifacts cannot be null");
141 
142         assertUniqueKey(key);
143 
144         CacheRecord record = new CacheRecord(Collections.unmodifiableList(new ArrayList<>(pluginArtifacts)));
145 
146         cache.put(key, record);
147 
148         return record;
149     }
150 
151     protected void assertUniqueKey(Key key) {
152         if (cache.containsKey(key)) {
153             throw new IllegalStateException("Duplicate artifact resolution result for plugin " + key);
154         }
155     }
156 
157     public CacheRecord put(Key key, PluginResolutionException exception) {
158         Objects.requireNonNull(exception, "exception cannot be null");
159 
160         assertUniqueKey(key);
161 
162         CacheRecord record = new CacheRecord(exception);
163 
164         cache.put(key, record);
165 
166         return record;
167     }
168 
169     public void flush() {
170         cache.clear();
171     }
172 
173     protected static int pluginHashCode(Plugin plugin) {
174         return CacheUtils.pluginHashCode(plugin);
175     }
176 
177     protected static boolean pluginEquals(Plugin a, Plugin b) {
178         return CacheUtils.pluginEquals(a, b);
179     }
180 
181     public void register(MavenProject project, Key cacheKey, CacheRecord record) {
182         
183     }
184 }