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