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)) {
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         // default cache does not track record usage
183     }
184 }