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 javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Objects;
28  import java.util.concurrent.ConcurrentHashMap;
29  
30  import org.codehaus.plexus.classworlds.realm.ClassRealm;
31  import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
32  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
33  import org.eclipse.aether.graph.DependencyFilter;
34  
35  /**
36   * Default project realm cache implementation. Assumes cached data does not change.
37   */
38  @Named
39  @Singleton
40  public class DefaultProjectRealmCache implements ProjectRealmCache, Disposable {
41      /**
42       * CacheKey
43       */
44      protected static class CacheKey implements Key {
45  
46          private final List<? extends ClassRealm> extensionRealms;
47  
48          private final int hashCode;
49  
50          public CacheKey(List<? extends ClassRealm> extensionRealms) {
51              this.extensionRealms =
52                      (extensionRealms != null) ? Collections.unmodifiableList(extensionRealms) : Collections.emptyList();
53  
54              this.hashCode = this.extensionRealms.hashCode();
55          }
56  
57          @Override
58          public int hashCode() {
59              return hashCode;
60          }
61  
62          @Override
63          public boolean equals(Object o) {
64              if (o == this) {
65                  return true;
66              }
67  
68              if (!(o instanceof CacheKey)) {
69                  return false;
70              }
71  
72              CacheKey other = (CacheKey) o;
73  
74              return extensionRealms.equals(other.extensionRealms);
75          }
76  
77          @Override
78          public String toString() {
79              return extensionRealms.toString();
80          }
81      }
82  
83      protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<>();
84  
85      @Override
86      public Key createKey(List<? extends ClassRealm> extensionRealms) {
87          return new CacheKey(extensionRealms);
88      }
89  
90      public CacheRecord get(Key key) {
91          return cache.get(key);
92      }
93  
94      public CacheRecord put(Key key, ClassRealm projectRealm, DependencyFilter extensionArtifactFilter) {
95          Objects.requireNonNull(projectRealm, "projectRealm cannot be null");
96  
97          if (cache.containsKey(key)) {
98              throw new IllegalStateException("Duplicate project realm for extensions " + key);
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         for (CacheRecord record : cache.values()) {
110             ClassRealm realm = record.getRealm();
111             try {
112                 realm.getWorld().disposeRealm(realm.getId());
113             } catch (NoSuchRealmException e) {
114                 // ignore
115             }
116         }
117         cache.clear();
118     }
119 
120     public void register(MavenProject project, Key key, CacheRecord record) {
121         // default cache does not track record usage
122     }
123 
124     @Override
125     public void dispose() {
126         flush();
127     }
128 }