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