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.io.File;
23 import java.util.ArrayList;
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.project.ExtensionDescriptor;
30 import org.apache.maven.project.MavenProject;
31 import org.codehaus.plexus.classworlds.realm.ClassRealm;
32 import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
33 import org.codehaus.plexus.component.annotations.Component;
34 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
35
36
37
38
39 @Component( role = ExtensionRealmCache.class )
40 public class DefaultExtensionRealmCache
41 implements ExtensionRealmCache, Disposable
42 {
43
44 protected static class CacheKey
45 implements Key
46 {
47
48 private final List<File> files;
49
50 private final List<Long> timestamps;
51
52 private final List<Long> sizes;
53
54 private final List<String> ids;
55
56 private final int hashCode;
57
58 public CacheKey( List<Artifact> extensionArtifacts )
59 {
60 this.files = new ArrayList<File>( extensionArtifacts.size() );
61 this.timestamps = new ArrayList<Long>( extensionArtifacts.size() );
62 this.sizes = new ArrayList<Long>( extensionArtifacts.size() );
63 this.ids = new ArrayList<String>( extensionArtifacts.size() );
64
65 for ( Artifact artifact : extensionArtifacts )
66 {
67 File file = artifact.getFile();
68 files.add( file );
69 timestamps.add( ( file != null ) ? Long.valueOf( file.lastModified() ) : Long.valueOf( 0 ) );
70 sizes.add( ( file != null ) ? Long.valueOf( file.length() ) : Long.valueOf( 0 ) );
71 ids.add( artifact.getVersion() );
72 }
73
74 this.hashCode =
75 31 * files.hashCode() + 31 * ids.hashCode() + 31 * timestamps.hashCode() + 31 * sizes.hashCode();
76 }
77
78 @Override
79 public int hashCode()
80 {
81 return hashCode;
82 }
83
84 @Override
85 public boolean equals( Object o )
86 {
87 if ( o == this )
88 {
89 return true;
90 }
91
92 if ( !( o instanceof CacheKey ) )
93 {
94 return false;
95 }
96
97 CacheKey other = (CacheKey) o;
98
99 return ids.equals( other.ids ) && files.equals( other.files ) && timestamps.equals( other.timestamps )
100 && sizes.equals( other.sizes );
101 }
102
103 @Override
104 public String toString()
105 {
106 return files.toString();
107 }
108 }
109
110 protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<Key, CacheRecord>();
111
112 @Override
113 public Key createKey( List<Artifact> extensionArtifacts )
114 {
115 return new CacheKey( extensionArtifacts );
116 }
117
118 public CacheRecord get( Key key )
119 {
120 return cache.get( key );
121 }
122
123 public CacheRecord put( Key key, ClassRealm extensionRealm, ExtensionDescriptor extensionDescriptor,
124 List<Artifact> artifacts )
125 {
126 if ( extensionRealm == null )
127 {
128 throw new NullPointerException();
129 }
130
131 if ( cache.containsKey( key ) )
132 {
133 throw new IllegalStateException( "Duplicate extension realm for extension " + key );
134 }
135
136 CacheRecord record = new CacheRecord( extensionRealm, extensionDescriptor, artifacts );
137
138 cache.put( key, record );
139
140 return record;
141 }
142
143 public void flush()
144 {
145 for ( CacheRecord record : cache.values() )
146 {
147 ClassRealm realm = record.realm;
148 try
149 {
150 realm.getWorld().disposeRealm( realm.getId() );
151 }
152 catch ( NoSuchRealmException e )
153 {
154
155 }
156 }
157 cache.clear();
158 }
159
160 public void register( MavenProject project, Key key, CacheRecord record )
161 {
162
163 }
164
165 public void dispose()
166 {
167 flush();
168 }
169
170 }