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.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.codehaus.plexus.classworlds.realm.ClassRealm;
28  import org.codehaus.plexus.component.annotations.Component;
29  import org.eclipse.aether.graph.DependencyFilter;
30  
31  /**
32   * Default project realm cache implementation. Assumes cached data does not change.
33   */
34  @Component( role = ProjectRealmCache.class )
35  public class DefaultProjectRealmCache
36      implements ProjectRealmCache
37  {
38  
39      private static class CacheKey
40      {
41  
42          private final List<? extends ClassRealm> extensionRealms;
43  
44          private final int hashCode;
45  
46          public CacheKey( List<? extends ClassRealm> extensionRealms )
47          {
48              this.extensionRealms = ( extensionRealms != null ) ? extensionRealms : Collections.<ClassRealm> emptyList();
49  
50              this.hashCode = this.extensionRealms.hashCode();
51          }
52  
53          @Override
54          public int hashCode()
55          {
56              return hashCode;
57          }
58  
59          @Override
60          public boolean equals( Object o )
61          {
62              if ( o == this )
63              {
64                  return true;
65              }
66  
67              if ( !( o instanceof CacheKey ) )
68              {
69                  return false;
70              }
71  
72              CacheKey other = (CacheKey) o;
73  
74              return extensionRealms.equals( other.extensionRealms );
75          }
76  
77      }
78  
79      private final Map<CacheKey, CacheRecord> cache = new HashMap<CacheKey, CacheRecord>();
80  
81      public CacheRecord get( List<? extends ClassRealm> extensionRealms )
82      {
83          return cache.get( new CacheKey( extensionRealms ) );
84      }
85  
86      public CacheRecord put( List<? extends ClassRealm> extensionRealms, ClassRealm projectRealm,
87                              DependencyFilter extensionArtifactFilter )
88      {
89          if ( projectRealm == null )
90          {
91              throw new NullPointerException();
92          }
93  
94          CacheKey key = new CacheKey( extensionRealms );
95  
96          if ( cache.containsKey( key ) )
97          {
98              throw new IllegalStateException( "Duplicate project realm for extensions " + extensionRealms );
99          }
100 
101         CacheRecord record = new CacheRecord( projectRealm, extensionArtifactFilter );
102 
103         cache.put( key, record );
104 
105         return record;
106     }
107 
108     public void flush()
109     {
110         cache.clear();
111     }
112 
113     public void register( MavenProject project, CacheRecord record )
114     {
115         // default cache does not track record usage
116     }
117 
118 }