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 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
42
43
44
45
46
47
48
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 }