1   package org.apache.maven.plugin;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.RepositoryUtils;
28  import org.apache.maven.artifact.ArtifactUtils;
29  import org.apache.maven.model.Plugin;
30  import org.apache.maven.plugin.descriptor.MojoDescriptor;
31  import org.apache.maven.plugin.descriptor.PluginDescriptor;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.component.repository.ComponentDescriptor;
34  import org.eclipse.aether.RepositorySystemSession;
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  
44  
45  
46  
47  
48  
49  @Component( role = PluginDescriptorCache.class )
50  public class DefaultPluginDescriptorCache
51      implements PluginDescriptorCache
52  {
53  
54      private Map<Key, PluginDescriptor> descriptors = new HashMap<>( 128 );
55  
56      public void flush()
57      {
58          descriptors.clear();
59      }
60  
61      public Key createKey( Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session )
62      {
63          return new CacheKey( plugin, repositories, session );
64      }
65  
66      public PluginDescriptor get( Key cacheKey )
67      {
68          return clone( descriptors.get( cacheKey ) );
69      }
70  
71      public void put( Key cacheKey, PluginDescriptor pluginDescriptor )
72      {
73          descriptors.put( cacheKey, clone( pluginDescriptor ) );
74      }
75  
76      protected static PluginDescriptor clone( PluginDescriptor original )
77      {
78          PluginDescriptor clone = null;
79  
80          if ( original != null )
81          {
82              clone = new PluginDescriptor();
83  
84              clone.setGroupId( original.getGroupId() );
85              clone.setArtifactId( original.getArtifactId() );
86              clone.setVersion( original.getVersion() );
87              clone.setGoalPrefix( original.getGoalPrefix() );
88              clone.setInheritedByDefault( original.isInheritedByDefault() );
89  
90              clone.setName( original.getName() );
91              clone.setDescription( original.getDescription() );
92              clone.setRequiredMavenVersion( original.getRequiredMavenVersion() );
93  
94              clone.setPluginArtifact( ArtifactUtils.copyArtifactSafe( original.getPluginArtifact() ) );
95  
96              clone.setComponents( clone( original.getMojos(), clone ) );
97              clone.setId( original.getId() );
98              clone.setIsolatedRealm( original.isIsolatedRealm() );
99              clone.setSource( original.getSource() );
100 
101             clone.setDependencies( original.getDependencies() );
102         }
103 
104         return clone;
105     }
106 
107     private static List<ComponentDescriptor<?>> clone( List<MojoDescriptor> mojos, PluginDescriptor pluginDescriptor )
108     {
109         List<ComponentDescriptor<?>> clones = null;
110 
111         if ( mojos != null )
112         {
113             clones = new ArrayList<>( mojos.size() );
114 
115             for ( MojoDescriptor mojo : mojos )
116             {
117                 MojoDescriptor clone = mojo.clone();
118                 clone.setPluginDescriptor( pluginDescriptor );
119                 clones.add( clone );
120             }
121         }
122 
123         return clones;
124     }
125 
126     private static final class CacheKey
127         implements Key
128     {
129 
130         private final String groupId;
131 
132         private final String artifactId;
133 
134         private final String version;
135 
136         private final WorkspaceRepository workspace;
137 
138         private final LocalRepository localRepo;
139 
140         private final List<RemoteRepository> repositories;
141 
142         private final int hashCode;
143 
144         CacheKey( Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session )
145         {
146             groupId = plugin.getGroupId();
147             artifactId = plugin.getArtifactId();
148             version = plugin.getVersion();
149 
150             workspace = RepositoryUtils.getWorkspace( session );
151             localRepo = session.getLocalRepository();
152             this.repositories = new ArrayList<>( repositories.size() );
153             for ( RemoteRepository repository : repositories )
154             {
155                 if ( repository.isRepositoryManager() )
156                 {
157                     this.repositories.addAll( repository.getMirroredRepositories() );
158                 }
159                 else
160                 {
161                     this.repositories.add( repository );
162                 }
163             }
164 
165             int hash = 17;
166             hash = hash * 31 + groupId.hashCode();
167             hash = hash * 31 + artifactId.hashCode();
168             hash = hash * 31 + version.hashCode();
169             hash = hash * 31 + hash( workspace );
170             hash = hash * 31 + localRepo.hashCode();
171             hash = hash * 31 + RepositoryUtils.repositoriesHashCode( repositories );
172             this.hashCode = hash;
173         }
174 
175         @Override
176         public int hashCode()
177         {
178             return hashCode;
179         }
180 
181         @Override
182         public boolean equals( Object obj )
183         {
184             if ( this == obj )
185             {
186                 return true;
187             }
188 
189             if ( !( obj instanceof CacheKey ) )
190             {
191                 return false;
192             }
193 
194             CacheKey that = (CacheKey) obj;
195 
196             return eq( this.artifactId, that.artifactId ) && eq( this.groupId, that.groupId )
197                 && eq( this.version, that.version ) && eq( this.localRepo, that.localRepo )
198                 && eq( this.workspace, that.workspace )
199                 && RepositoryUtils.repositoriesEquals( this.repositories, that.repositories );
200         }
201 
202         @Override
203         public String toString()
204         {
205             return groupId + ':' + artifactId + ':' + version;
206         }
207 
208         private static int hash( Object obj )
209         {
210             return obj != null ? obj.hashCode() : 0;
211         }
212 
213         private static <T> boolean eq( T s1, T s2 )
214         {
215             return s1 != null ? s1.equals( s2 ) : s2 == null;
216         }
217 
218     }
219 
220 }