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.Collections;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.concurrent.ConcurrentHashMap;
27
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.model.Plugin;
30 import org.apache.maven.project.MavenProject;
31 import org.codehaus.plexus.classworlds.realm.ClassRealm;
32 import org.codehaus.plexus.component.annotations.Component;
33 import org.sonatype.aether.RepositorySystemSession;
34 import org.sonatype.aether.graph.DependencyFilter;
35 import org.sonatype.aether.repository.LocalRepository;
36 import org.sonatype.aether.repository.RemoteRepository;
37 import org.sonatype.aether.repository.WorkspaceRepository;
38
39
40
41
42 @Component( role = PluginRealmCache.class )
43 public class DefaultPluginRealmCache
44 implements PluginRealmCache
45 {
46
47 protected static class CacheKey
48 implements Key
49 {
50
51 private final Plugin plugin;
52
53 private final WorkspaceRepository workspace;
54
55 private final LocalRepository localRepo;
56
57 private final List<RemoteRepository> repositories;
58
59 private final ClassLoader parentRealm;
60
61 private final Map<String, ClassLoader> foreignImports;
62
63 private final DependencyFilter filter;
64
65 private final int hashCode;
66
67 public CacheKey( Plugin plugin, ClassLoader parentRealm, Map<String, ClassLoader> foreignImports,
68 DependencyFilter dependencyFilter, List<RemoteRepository> repositories,
69 RepositorySystemSession session )
70 {
71 this.plugin = plugin.clone();
72 this.workspace = CacheUtils.getWorkspace( session );
73 this.localRepo = session.getLocalRepository();
74 this.repositories = new ArrayList<RemoteRepository>( repositories.size() );
75 for ( RemoteRepository repository : repositories )
76 {
77 if ( repository.isRepositoryManager() )
78 {
79 this.repositories.addAll( repository.getMirroredRepositories() );
80 }
81 else
82 {
83 this.repositories.add( repository );
84 }
85 }
86 this.parentRealm = parentRealm;
87 this.foreignImports =
88 ( foreignImports != null ) ? foreignImports : Collections.<String, ClassLoader> emptyMap();
89 this.filter = dependencyFilter;
90
91 int hash = 17;
92 hash = hash * 31 + CacheUtils.pluginHashCode( plugin );
93 hash = hash * 31 + hash( workspace );
94 hash = hash * 31 + hash( localRepo );
95 hash = hash * 31 + CacheUtils.repositoriesHashCode( repositories );
96 hash = hash * 31 + hash( parentRealm );
97 hash = hash * 31 + this.foreignImports.hashCode();
98 hash = hash * 31 + hash( dependencyFilter );
99 this.hashCode = hash;
100 }
101
102 @Override
103 public String toString()
104 {
105 return plugin.getId();
106 }
107
108 @Override
109 public int hashCode()
110 {
111 return hashCode;
112 }
113
114 private static int hash( Object obj )
115 {
116 return obj != null ? obj.hashCode() : 0;
117 }
118
119 @Override
120 public boolean equals( Object o )
121 {
122 if ( o == this )
123 {
124 return true;
125 }
126
127 if ( !( o instanceof CacheKey ) )
128 {
129 return false;
130 }
131
132 CacheKey that = (CacheKey) o;
133
134 return parentRealm == that.parentRealm && CacheUtils.pluginEquals( plugin, that.plugin )
135 && eq( workspace, that.workspace ) && eq( localRepo, that.localRepo )
136 && CacheUtils.repositoriesEquals( this.repositories, that.repositories ) && eq( filter, that.filter )
137 && eq( foreignImports, that.foreignImports );
138 }
139
140 private static <T> boolean eq( T s1, T s2 )
141 {
142 return s1 != null ? s1.equals( s2 ) : s2 == null;
143 }
144
145 }
146
147 protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<Key, CacheRecord>();
148
149 public Key createKey( Plugin plugin, ClassLoader parentRealm, Map<String, ClassLoader> foreignImports,
150 DependencyFilter dependencyFilter, List<RemoteRepository> repositories,
151 RepositorySystemSession session )
152 {
153 return new CacheKey( plugin, parentRealm, foreignImports, dependencyFilter, repositories, session );
154 }
155
156 public CacheRecord get( Key key )
157 {
158 return cache.get( key );
159 }
160
161 public CacheRecord put( Key key, ClassRealm pluginRealm, List<Artifact> pluginArtifacts )
162 {
163 if ( pluginRealm == null || pluginArtifacts == null )
164 {
165 throw new IllegalArgumentException();
166 }
167
168 if ( cache.containsKey( key ) )
169 {
170 throw new IllegalStateException( "Duplicate plugin realm for plugin " + key );
171 }
172
173 CacheRecord record = new CacheRecord( pluginRealm, pluginArtifacts );
174
175 cache.put( key, record );
176
177 return record;
178 }
179
180 public void flush()
181 {
182 cache.clear();
183 }
184
185 protected static int pluginHashCode( Plugin plugin )
186 {
187 return CacheUtils.pluginHashCode( plugin );
188 }
189
190 protected static boolean pluginEquals( Plugin a, Plugin b )
191 {
192 return CacheUtils.pluginEquals( a, b );
193 }
194
195 public void register( MavenProject project, CacheRecord record )
196 {
197
198 }
199
200 }