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