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.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   * Default project realm cache implementation. Assumes cached data does not change.
38   */
39  @Named
40  @Singleton
41  public class DefaultProjectRealmCache
42      implements ProjectRealmCache, Disposable
43  {
44      /**
45       * CacheKey
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                 // ignore
136             }
137         }
138         cache.clear();
139     }
140 
141     public void register( MavenProject project, Key key, CacheRecord record )
142     {
143         // default cache does not track record usage
144     }
145 
146     @Override
147     public void dispose()
148     {
149         flush();
150     }
151 
152 }