View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project;
20  
21  import java.util.Collections;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Objects;
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 implements ProjectRealmCache, Disposable {
38      /**
39       * CacheKey
40       */
41      protected static class CacheKey implements Key {
42  
43          private final List<? extends ClassRealm> extensionRealms;
44  
45          private final int hashCode;
46  
47          public CacheKey(List<? extends ClassRealm> extensionRealms) {
48              this.extensionRealms = (extensionRealms != null)
49                      ? Collections.unmodifiableList(extensionRealms)
50                      : Collections.<ClassRealm>emptyList();
51  
52              this.hashCode = this.extensionRealms.hashCode();
53          }
54  
55          @Override
56          public int hashCode() {
57              return hashCode;
58          }
59  
60          @Override
61          public boolean equals(Object o) {
62              if (o == this) {
63                  return true;
64              }
65  
66              if (!(o instanceof CacheKey)) {
67                  return false;
68              }
69  
70              CacheKey other = (CacheKey) o;
71  
72              return extensionRealms.equals(other.extensionRealms);
73          }
74  
75          @Override
76          public String toString() {
77              return extensionRealms.toString();
78          }
79      }
80  
81      protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<>();
82  
83      @Override
84      public Key createKey(List<? extends ClassRealm> extensionRealms) {
85          return new CacheKey(extensionRealms);
86      }
87  
88      public CacheRecord get(Key key) {
89          return cache.get(key);
90      }
91  
92      public CacheRecord put(Key key, ClassRealm projectRealm, DependencyFilter extensionArtifactFilter) {
93          Objects.requireNonNull(projectRealm, "projectRealm cannot be null");
94  
95          if (cache.containsKey(key)) {
96              throw new IllegalStateException("Duplicate project realm for extensions " + key);
97          }
98  
99          CacheRecord record = new CacheRecord(projectRealm, extensionArtifactFilter);
100 
101         cache.put(key, record);
102 
103         return record;
104     }
105 
106     public void flush() {
107         for (CacheRecord record : cache.values()) {
108             ClassRealm realm = record.getRealm();
109             try {
110                 realm.getWorld().disposeRealm(realm.getId());
111             } catch (NoSuchRealmException e) {
112                 // ignore
113             }
114         }
115         cache.clear();
116     }
117 
118     public void register(MavenProject project, Key key, CacheRecord record) {
119         // default cache does not track record usage
120     }
121 
122     @Override
123     public void dispose() {
124         flush();
125     }
126 }