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.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Objects;
27  
28  import org.apache.maven.RepositoryUtils;
29  import org.apache.maven.artifact.ArtifactUtils;
30  import org.apache.maven.model.Plugin;
31  import org.apache.maven.plugin.descriptor.MojoDescriptor;
32  import org.apache.maven.plugin.descriptor.PluginDescriptor;
33  import org.codehaus.plexus.component.annotations.Component;
34  import org.codehaus.plexus.component.repository.ComponentDescriptor;
35  import org.eclipse.aether.RepositorySystemSession;
36  import org.eclipse.aether.repository.LocalRepository;
37  import org.eclipse.aether.repository.RemoteRepository;
38  import org.eclipse.aether.repository.WorkspaceRepository;
39  
40  /**
41   * Caches raw plugin descriptors. A raw plugin descriptor is a descriptor that has just been extracted from the plugin
42   * artifact and does not contain any runtime specific data. The cache must not be used for descriptors that hold runtime
43   * data like the plugin realm. <strong>Warning:</strong> This is an internal utility interface that is only public for
44   * technical reasons, it is not part of the public API. In particular, this interface can be changed or deleted without
45   * prior notice.
46   *
47   * @since 3.0
48   * @author Benjamin Bentmann
49   */
50  @Component( role = PluginDescriptorCache.class )
51  public class DefaultPluginDescriptorCache
52      implements PluginDescriptorCache
53  {
54  
55      private Map<Key, PluginDescriptor> descriptors = new HashMap<>( 128 );
56  
57      public void flush()
58      {
59          descriptors.clear();
60      }
61  
62      public Key createKey( Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session )
63      {
64          return new CacheKey( plugin, repositories, session );
65      }
66  
67      public PluginDescriptor get( Key cacheKey )
68      {
69          return clone( descriptors.get( cacheKey ) );
70      }
71  
72      public void put( Key cacheKey, PluginDescriptor pluginDescriptor )
73      {
74          descriptors.put( cacheKey, clone( pluginDescriptor ) );
75      }
76  
77      protected static PluginDescriptor clone( PluginDescriptor original )
78      {
79          PluginDescriptor clone = null;
80  
81          if ( original != null )
82          {
83              clone = new PluginDescriptor();
84  
85              clone.setGroupId( original.getGroupId() );
86              clone.setArtifactId( original.getArtifactId() );
87              clone.setVersion( original.getVersion() );
88              clone.setGoalPrefix( original.getGoalPrefix() );
89              clone.setInheritedByDefault( original.isInheritedByDefault() );
90  
91              clone.setName( original.getName() );
92              clone.setDescription( original.getDescription() );
93              clone.setRequiredMavenVersion( original.getRequiredMavenVersion() );
94  
95              clone.setPluginArtifact( ArtifactUtils.copyArtifactSafe( original.getPluginArtifact() ) );
96  
97              clone.setComponents( clone( original.getMojos(), clone ) );
98              clone.setId( original.getId() );
99              clone.setIsolatedRealm( original.isIsolatedRealm() );
100             clone.setSource( original.getSource() );
101 
102             clone.setDependencies( original.getDependencies() );
103         }
104 
105         return clone;
106     }
107 
108     private static List<ComponentDescriptor<?>> clone( List<MojoDescriptor> mojos, PluginDescriptor pluginDescriptor )
109     {
110         List<ComponentDescriptor<?>> clones = null;
111 
112         if ( mojos != null )
113         {
114             clones = new ArrayList<>( mojos.size() );
115 
116             for ( MojoDescriptor mojo : mojos )
117             {
118                 MojoDescriptor clone = mojo.clone();
119                 clone.setPluginDescriptor( pluginDescriptor );
120                 clones.add( clone );
121             }
122         }
123 
124         return clones;
125     }
126 
127     private static final class CacheKey
128         implements Key
129     {
130 
131         private final String groupId;
132 
133         private final String artifactId;
134 
135         private final String version;
136 
137         private final WorkspaceRepository workspace;
138 
139         private final LocalRepository localRepo;
140 
141         private final List<RemoteRepository> repositories;
142 
143         private final int hashCode;
144 
145         CacheKey( Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session )
146         {
147             groupId = plugin.getGroupId();
148             artifactId = plugin.getArtifactId();
149             version = plugin.getVersion();
150 
151             workspace = RepositoryUtils.getWorkspace( session );
152             localRepo = session.getLocalRepository();
153             this.repositories = new ArrayList<>( repositories.size() );
154             for ( RemoteRepository repository : repositories )
155             {
156                 if ( repository.isRepositoryManager() )
157                 {
158                     this.repositories.addAll( repository.getMirroredRepositories() );
159                 }
160                 else
161                 {
162                     this.repositories.add( repository );
163                 }
164             }
165 
166             int hash = 17;
167             hash = hash * 31 + groupId.hashCode();
168             hash = hash * 31 + artifactId.hashCode();
169             hash = hash * 31 + version.hashCode();
170             hash = hash * 31 + hash( workspace );
171             hash = hash * 31 + localRepo.hashCode();
172             hash = hash * 31 + RepositoryUtils.repositoriesHashCode( repositories );
173             this.hashCode = hash;
174         }
175 
176         @Override
177         public int hashCode()
178         {
179             return hashCode;
180         }
181 
182         @Override
183         public boolean equals( Object obj )
184         {
185             if ( this == obj )
186             {
187                 return true;
188             }
189 
190             if ( !( obj instanceof CacheKey ) )
191             {
192                 return false;
193             }
194 
195             CacheKey that = (CacheKey) obj;
196 
197             return Objects.equals( this.artifactId, that.artifactId ) 
198                 && Objects.equals( this.groupId, that.groupId )
199                 && Objects.equals( this.version, that.version ) 
200                 && Objects.equals( this.localRepo, that.localRepo )
201                 && Objects.equals( this.workspace, that.workspace )
202                 && RepositoryUtils.repositoriesEquals( this.repositories, that.repositories );
203         }
204 
205         @Override
206         public String toString()
207         {
208             return groupId + ':' + artifactId + ':' + version;
209         }
210 
211         private static int hash( Object obj )
212         {
213             return obj != null ? obj.hashCode() : 0;
214         }
215 
216     }
217 
218 }