1 package org.apache.maven.project;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.concurrent.ConcurrentHashMap;
27
28 import org.codehaus.plexus.classworlds.realm.ClassRealm;
29 import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
30 import org.codehaus.plexus.component.annotations.Component;
31 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
32 import org.eclipse.aether.graph.DependencyFilter;
33
34
35
36
37 @Component( role = ProjectRealmCache.class )
38 public class DefaultProjectRealmCache
39 implements ProjectRealmCache, Disposable
40 {
41
42
43
44 protected static class CacheKey
45 implements Key
46 {
47
48 private final List<? extends ClassRealm> extensionRealms;
49
50 private final int hashCode;
51
52 public CacheKey( List<? extends ClassRealm> extensionRealms )
53 {
54 this.extensionRealms = ( extensionRealms != null )
55 ? Collections.unmodifiableList( extensionRealms )
56 : Collections.<ClassRealm>emptyList();
57
58 this.hashCode = this.extensionRealms.hashCode();
59 }
60
61 @Override
62 public int hashCode()
63 {
64 return hashCode;
65 }
66
67 @Override
68 public boolean equals( Object o )
69 {
70 if ( o == this )
71 {
72 return true;
73 }
74
75 if ( !( o instanceof CacheKey ) )
76 {
77 return false;
78 }
79
80 CacheKey other = (CacheKey) o;
81
82 return extensionRealms.equals( other.extensionRealms );
83 }
84
85 @Override
86 public String toString()
87 {
88 return extensionRealms.toString();
89 }
90 }
91
92 protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<>();
93
94 @Override
95 public Key createKey( List<? extends ClassRealm> extensionRealms )
96 {
97 return new CacheKey( extensionRealms );
98 }
99
100 public CacheRecord get( Key key )
101 {
102 return cache.get( key );
103 }
104
105 public CacheRecord put( Key key, ClassRealm projectRealm, DependencyFilter extensionArtifactFilter )
106 {
107 Objects.requireNonNull( projectRealm, "projectRealm cannot be null" );
108
109 if ( cache.containsKey( key ) )
110 {
111 throw new IllegalStateException( "Duplicate project realm for extensions " + key );
112 }
113
114 CacheRecord record = new CacheRecord( projectRealm, extensionArtifactFilter );
115
116 cache.put( key, record );
117
118 return record;
119 }
120
121 public void flush()
122 {
123 for ( CacheRecord record : cache.values() )
124 {
125 ClassRealm realm = record.getRealm();
126 try
127 {
128 realm.getWorld().disposeRealm( realm.getId() );
129 }
130 catch ( NoSuchRealmException e )
131 {
132
133 }
134 }
135 cache.clear();
136 }
137
138 public void register( MavenProject project, Key key, CacheRecord record )
139 {
140
141 }
142
143 @Override
144 public void dispose()
145 {
146 flush();
147 }
148
149 }