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