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.plugin;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.File;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Objects;
29  import java.util.concurrent.ConcurrentHashMap;
30  
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.project.ExtensionDescriptor;
33  import org.apache.maven.project.MavenProject;
34  import org.codehaus.plexus.classworlds.realm.ClassRealm;
35  import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
36  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
37  
38  /**
39   * Default extension realm cache implementation. Assumes cached data does not change.
40   */
41  @Named
42  @Singleton
43  public class DefaultExtensionRealmCache implements ExtensionRealmCache, Disposable {
44      /**
45       * CacheKey
46       */
47      protected static class CacheKey implements Key {
48  
49          private final List<File> files;
50  
51          private final List<Long> timestamps;
52  
53          private final List<Long> sizes;
54  
55          private final List<String> ids;
56  
57          private final int hashCode;
58  
59          public CacheKey(List<Artifact> extensionArtifacts) {
60              this.files = new ArrayList<>(extensionArtifacts.size());
61              this.timestamps = new ArrayList<>(extensionArtifacts.size());
62              this.sizes = new ArrayList<>(extensionArtifacts.size());
63              this.ids = new ArrayList<>(extensionArtifacts.size());
64  
65              for (Artifact artifact : extensionArtifacts) {
66                  File file = artifact.getFile();
67                  files.add(file);
68                  timestamps.add((file != null) ? Long.valueOf(file.lastModified()) : Long.valueOf(0));
69                  sizes.add((file != null) ? Long.valueOf(file.length()) : Long.valueOf(0));
70                  ids.add(artifact.getVersion());
71              }
72  
73              this.hashCode =
74                      31 * files.hashCode() + 31 * ids.hashCode() + 31 * timestamps.hashCode() + 31 * sizes.hashCode();
75          }
76  
77          @Override
78          public int hashCode() {
79              return hashCode;
80          }
81  
82          @Override
83          public boolean equals(Object o) {
84              if (o == this) {
85                  return true;
86              }
87  
88              if (o instanceof CacheKey other) {
89                  return ids.equals(other.ids)
90                          && files.equals(other.files)
91                          && timestamps.equals(other.timestamps)
92                          && sizes.equals(other.sizes);
93              } else {
94                  return false;
95              }
96          }
97  
98          @Override
99          public String toString() {
100             return files.toString();
101         }
102     }
103 
104     protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<>();
105 
106     @Override
107     public Key createKey(List<Artifact> extensionArtifacts) {
108         return new CacheKey(extensionArtifacts);
109     }
110 
111     public CacheRecord get(Key key) {
112         return cache.get(key);
113     }
114 
115     public CacheRecord put(
116             Key key, ClassRealm extensionRealm, ExtensionDescriptor extensionDescriptor, List<Artifact> artifacts) {
117         Objects.requireNonNull(extensionRealm, "extensionRealm cannot be null");
118 
119         if (cache.containsKey(key)) {
120             throw new IllegalStateException("Duplicate extension realm for extension " + key);
121         }
122 
123         CacheRecord record = new CacheRecord(extensionRealm, extensionDescriptor, artifacts);
124 
125         cache.put(key, record);
126 
127         return record;
128     }
129 
130     public void flush() {
131         for (CacheRecord record : cache.values()) {
132             ClassRealm realm = record.getRealm();
133             try {
134                 realm.getWorld().disposeRealm(realm.getId());
135             } catch (NoSuchRealmException e) {
136                 // ignore
137             }
138         }
139         cache.clear();
140     }
141 
142     public void register(MavenProject project, Key key, CacheRecord record) {
143         // default cache does not track extension usage
144     }
145 
146     public void dispose() {
147         flush();
148     }
149 }