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.sonatype.aether.RepositorySystemSession;
34 import org.sonatype.aether.repository.LocalRepository;
35 import org.sonatype.aether.repository.RemoteRepository;
36 import org.sonatype.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<Key, PluginDescriptor>( 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 private 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
92 clone.setPluginArtifact( ArtifactUtils.copyArtifactSafe( original.getPluginArtifact() ) );
93
94 clone.setComponents( clone( original.getMojos(), clone ) );
95 clone.setId( original.getId() );
96 clone.setIsolatedRealm( original.isIsolatedRealm() );
97 clone.setSource( original.getSource() );
98 }
99
100 return clone;
101 }
102
103 private static List<ComponentDescriptor<?>> clone( List<MojoDescriptor> mojos, PluginDescriptor pluginDescriptor )
104 {
105 List<ComponentDescriptor<?>> clones = null;
106
107 if ( mojos != null )
108 {
109 clones = new ArrayList<ComponentDescriptor<?>>( mojos.size() );
110
111 for ( MojoDescriptor mojo : mojos )
112 {
113 MojoDescriptor clone = mojo.clone();
114 clone.setPluginDescriptor( pluginDescriptor );
115 clones.add( clone );
116 }
117 }
118
119 return clones;
120 }
121
122 private static final class CacheKey
123 implements Key
124 {
125
126 private final String groupId;
127
128 private final String artifactId;
129
130 private final String version;
131
132 private final WorkspaceRepository workspace;
133
134 private final LocalRepository localRepo;
135
136 private final List<RemoteRepository> repositories;
137
138 private final int hashCode;
139
140 public CacheKey( Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session )
141 {
142 groupId = plugin.getGroupId();
143 artifactId = plugin.getArtifactId();
144 version = plugin.getVersion();
145
146 workspace = CacheUtils.getWorkspace( session );
147 localRepo = session.getLocalRepository();
148 this.repositories = new ArrayList<RemoteRepository>( repositories.size() );
149 for ( RemoteRepository repository : repositories )
150 {
151 if ( repository.isRepositoryManager() )
152 {
153 this.repositories.addAll( repository.getMirroredRepositories() );
154 }
155 else
156 {
157 this.repositories.add( repository );
158 }
159 }
160
161 int hash = 17;
162 hash = hash * 31 + groupId.hashCode();
163 hash = hash * 31 + artifactId.hashCode();
164 hash = hash * 31 + version.hashCode();
165 hash = hash * 31 + hash( workspace );
166 hash = hash * 31 + localRepo.hashCode();
167 hash = hash * 31 + CacheUtils.repositoriesHashCode( repositories );
168 this.hashCode = hash;
169 }
170
171 @Override
172 public int hashCode()
173 {
174 return hashCode;
175 }
176
177 @Override
178 public boolean equals( Object obj )
179 {
180 if ( this == obj )
181 {
182 return true;
183 }
184
185 if ( !( obj instanceof CacheKey ) )
186 {
187 return false;
188 }
189
190 CacheKey that = (CacheKey) obj;
191
192 return eq( this.artifactId, that.artifactId ) && eq( this.groupId, that.groupId )
193 && eq( this.version, that.version ) && eq( this.localRepo, that.localRepo )
194 && eq( this.workspace, that.workspace )
195 && CacheUtils.repositoriesEquals( this.repositories, that.repositories );
196 }
197
198 @Override
199 public String toString()
200 {
201 return groupId + ':' + artifactId + ':' + version;
202 }
203
204 private static int hash( Object obj )
205 {
206 return obj != null ? obj.hashCode() : 0;
207 }
208
209 private static <T> boolean eq( T s1, T s2 )
210 {
211 return s1 != null ? s1.equals( s2 ) : s2 == null;
212 }
213
214 }
215
216 }