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