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     {
146         return cache.get( key );
147     }
148 
149     public CacheRecord put( Key key, List<Artifact> pluginArtifacts )
150     {
151         if ( pluginArtifacts == null )
152         {
153             throw new NullPointerException();
154         }
155 
156         if ( cache.containsKey( key ) )
157         {
158             throw new IllegalStateException( "Duplicate artifact resolution result for plugin " + key );
159         }
160 
161         CacheRecord record =
162             new CacheRecord( Collections.unmodifiableList( new ArrayList<Artifact>( pluginArtifacts ) ) );
163 
164         cache.put( key, record );
165 
166         return record;
167     }
168 
169     public void flush()
170     {
171         cache.clear();
172     }
173 
174     protected static int pluginHashCode( Plugin plugin )
175     {
176         return CacheUtils.pluginHashCode( plugin );
177     }
178 
179     protected static boolean pluginEquals( Plugin a, Plugin b )
180     {
181         return CacheUtils.pluginEquals( a, b );
182     }
183 
184     public void register( MavenProject project, CacheRecord record )
185     {
186         // default cache does not track record usage
187     }
188 
189 }