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.eclipse.aether.graph.DependencyFilter;
33  import org.eclipse.sisu.PreDestroy;
34  
35  /**
36   * Default project realm cache implementation. Assumes cached data does not change.
37   */
38  @Singleton
39  @Named
40  public class DefaultProjectRealmCache implements ProjectRealmCache {
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 = (extensionRealms != null)
52                      ? Collections.unmodifiableList(extensionRealms)
53                      : Collections.<ClassRealm>emptyList();
54  
55              this.hashCode = this.extensionRealms.hashCode();
56          }
57  
58          @Override
59          public int hashCode() {
60              return hashCode;
61          }
62  
63          @Override
64          public boolean equals(Object o) {
65              if (o == this) {
66                  return true;
67              }
68  
69              if (!(o instanceof CacheKey)) {
70                  return false;
71              }
72  
73              CacheKey other = (CacheKey) o;
74  
75              return extensionRealms.equals(other.extensionRealms);
76          }
77  
78          @Override
79          public String toString() {
80              return extensionRealms.toString();
81          }
82      }
83  
84      protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<>();
85  
86      @Override
87      public Key createKey(List<? extends ClassRealm> extensionRealms) {
88          return new CacheKey(extensionRealms);
89      }
90  
91      public CacheRecord get(Key key) {
92          return cache.get(key);
93      }
94  
95      public CacheRecord put(Key key, ClassRealm projectRealm, DependencyFilter extensionArtifactFilter) {
96          Objects.requireNonNull(projectRealm, "projectRealm cannot be null");
97  
98          if (cache.containsKey(key)) {
99              throw new IllegalStateException("Duplicate project realm for extensions " + key);
100         }
101 
102         CacheRecord record = new CacheRecord(projectRealm, extensionArtifactFilter);
103 
104         cache.put(key, record);
105 
106         return record;
107     }
108 
109     public void flush() {
110         for (CacheRecord record : cache.values()) {
111             ClassRealm realm = record.getRealm();
112             try {
113                 realm.getWorld().disposeRealm(realm.getId());
114             } catch (NoSuchRealmException e) {
115                 // ignore
116             }
117         }
118         cache.clear();
119     }
120 
121     public void register(MavenProject project, Key key, CacheRecord record) {
122         // default cache does not track record usage
123     }
124 
125     @PreDestroy
126     public void dispose() {
127         flush();
128     }
129 }