View Javadoc
1   package org.apache.maven.project;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.codehaus.plexus.classworlds.realm.ClassRealm;
28  import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
29  import org.codehaus.plexus.component.annotations.Component;
30  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
31  import org.eclipse.aether.graph.DependencyFilter;
32  
33  /**
34   * Default project realm cache implementation. Assumes cached data does not change.
35   */
36  @Component( role = ProjectRealmCache.class )
37  public class DefaultProjectRealmCache
38      implements ProjectRealmCache, Disposable
39  {
40  
41      protected static class CacheKey
42          implements Key
43      {
44  
45          private final List<? extends ClassRealm> extensionRealms;
46  
47          private final int hashCode;
48  
49          public CacheKey( List<? extends ClassRealm> extensionRealms )
50          {
51              this.extensionRealms = ( extensionRealms != null ) ? extensionRealms : Collections.<ClassRealm> emptyList();
52  
53              this.hashCode = this.extensionRealms.hashCode();
54          }
55  
56          @Override
57          public int hashCode()
58          {
59              return hashCode;
60          }
61  
62          @Override
63          public boolean equals( Object o )
64          {
65              if ( o == this )
66              {
67                  return true;
68              }
69  
70              if ( !( o instanceof CacheKey ) )
71              {
72                  return false;
73              }
74  
75              CacheKey other = (CacheKey) o;
76  
77              return extensionRealms.equals( other.extensionRealms );
78          }
79  
80          @Override
81          public String toString()
82          {
83              return extensionRealms.toString();
84          }
85      }
86  
87      protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<Key, CacheRecord>();
88  
89      @Override
90      public Key createKey( List<? extends ClassRealm> extensionRealms )
91      {
92          return new CacheKey( extensionRealms );
93      }
94  
95      public CacheRecord get( Key key )
96      {
97          return cache.get( key );
98      }
99  
100     public CacheRecord put( Key key, ClassRealm projectRealm, DependencyFilter extensionArtifactFilter )
101     {
102         if ( projectRealm == null )
103         {
104             throw new NullPointerException();
105         }
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.realm;
124             try
125             {
126                 realm.getWorld().disposeRealm( realm.getId() );
127             }
128             catch ( NoSuchRealmException e )
129             {
130                 // ignore
131             }
132         }
133         cache.clear();
134     }
135 
136     public void register( MavenProject project, Key key, CacheRecord record )
137     {
138         // default cache does not track record usage
139     }
140 
141     @Override
142     public void dispose()
143     {
144         flush();
145     }
146 
147 }