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