View Javadoc
1   package org.apache.maven.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Objects;
27  import java.util.concurrent.ConcurrentHashMap;
28  
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.codehaus.plexus.component.annotations.Component;
34  import org.eclipse.aether.RepositorySystemSession;
35  import org.eclipse.aether.graph.DependencyFilter;
36  import org.eclipse.aether.repository.LocalRepository;
37  import org.eclipse.aether.repository.RemoteRepository;
38  import org.eclipse.aether.repository.WorkspaceRepository;
39  
40  /**
41   * @author Igor Fedorenko
42   * @author Benjamin Bentmann
43   */
44  @Component( role = PluginArtifactsCache.class )
45  public class DefaultPluginArtifactsCache
46      implements PluginArtifactsCache
47  {
48      /**
49       * CacheKey
50       */
51      protected static class CacheKey
52          implements Key
53      {
54          private final Plugin plugin;
55  
56          private final WorkspaceRepository workspace;
57  
58          private final LocalRepository localRepo;
59  
60          private final List<RemoteRepository> repositories;
61  
62          private final DependencyFilter filter;
63  
64          private final int hashCode;
65  
66          public CacheKey( Plugin plugin, DependencyFilter extensionFilter, List<RemoteRepository> repositories,
67                           RepositorySystemSession session )
68          {
69              this.plugin = plugin.clone();
70              workspace = RepositoryUtils.getWorkspace( session );
71              this.localRepo = session.getLocalRepository();
72              this.repositories = new ArrayList<>( repositories.size() );
73              for ( RemoteRepository repository : repositories )
74              {
75                  if ( repository.isRepositoryManager() )
76                  {
77                      this.repositories.addAll( repository.getMirroredRepositories() );
78                  }
79                  else
80                  {
81                      this.repositories.add( repository );
82                  }
83              }
84              this.filter = extensionFilter;
85  
86              int hash = 17;
87              hash = hash * 31 + CacheUtils.pluginHashCode( plugin );
88              hash = hash * 31 + Objects.hashCode( workspace );
89              hash = hash * 31 + Objects.hashCode( localRepo );
90              hash = hash * 31 + RepositoryUtils.repositoriesHashCode( repositories );
91              hash = hash * 31 + Objects.hashCode( extensionFilter );
92              this.hashCode = hash;
93          }
94  
95          @Override
96          public String toString()
97          {
98              return plugin.getId();
99          }
100 
101         @Override
102         public int hashCode()
103         {
104             return hashCode;
105         }
106 
107         @Override
108         public boolean equals( Object o )
109         {
110             if ( o == this )
111             {
112                 return true;
113             }
114 
115             if ( !( o instanceof CacheKey ) )
116             {
117                 return false;
118             }
119 
120             CacheKey that = (CacheKey) o;
121 
122             return CacheUtils.pluginEquals( plugin, that.plugin ) 
123                 && Objects.equals( workspace, that.workspace )
124                 && Objects.equals( localRepo, that.localRepo ) 
125                 && RepositoryUtils.repositoriesEquals( repositories, that.repositories )
126                 && Objects.equals( filter, that.filter );
127         }
128     }
129 
130     protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<>();
131 
132     public Key createKey( Plugin plugin, DependencyFilter extensionFilter, List<RemoteRepository> repositories,
133                           RepositorySystemSession session )
134     {
135         return new CacheKey( plugin, extensionFilter, repositories, session );
136     }
137 
138     public CacheRecord get( Key key )
139         throws PluginResolutionException
140     {
141         CacheRecord cacheRecord = cache.get( key );
142 
143         if ( cacheRecord != null && cacheRecord.getException() != null )
144         {
145             throw cacheRecord.getException();
146         }
147 
148         return cacheRecord;
149     }
150 
151     public CacheRecord put( Key key, List<Artifact> pluginArtifacts )
152     {
153         Objects.requireNonNull( pluginArtifacts, "pluginArtifacts cannot be null" );
154 
155         assertUniqueKey( key );
156 
157         CacheRecord record =
158             new CacheRecord( Collections.unmodifiableList( new ArrayList<>( pluginArtifacts ) ) );
159 
160         cache.put( key, record );
161 
162         return record;
163     }
164 
165     protected void assertUniqueKey( Key key )
166     {
167         if ( cache.containsKey( key ) )
168         {
169             throw new IllegalStateException( "Duplicate artifact resolution result for plugin " + key );
170         }
171     }
172 
173     public CacheRecord put( Key key, PluginResolutionException exception )
174     {
175         Objects.requireNonNull( exception, "exception cannot be null" );
176 
177         assertUniqueKey( key );
178 
179         CacheRecord record = new CacheRecord( exception );
180 
181         cache.put( key, record );
182 
183         return record;
184     }
185 
186     public void flush()
187     {
188         cache.clear();
189     }
190 
191     protected static int pluginHashCode( Plugin plugin )
192     {
193         return CacheUtils.pluginHashCode( plugin );
194     }
195 
196     protected static boolean pluginEquals( Plugin a, Plugin b )
197     {
198         return CacheUtils.pluginEquals( a, b );
199     }
200 
201     public void register( MavenProject project, Key cacheKey, CacheRecord record )
202     {
203         // default cache does not track record usage
204     }
205 
206 }