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 java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Objects;
26  import java.util.concurrent.ConcurrentHashMap;
27  import javax.inject.Named;
28  import javax.inject.Singleton;
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.project.ExtensionDescriptor;
31  import org.apache.maven.project.MavenProject;
32  import org.codehaus.plexus.classworlds.realm.ClassRealm;
33  import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
34  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
35  
36  /**
37   * Default extension realm cache implementation. Assumes cached data does not change.
38   */
39  @Named
40  @Singleton
41  public class DefaultExtensionRealmCache implements ExtensionRealmCache, Disposable {
42      /**
43       * CacheKey
44       */
45      protected static class CacheKey implements Key {
46  
47          private final List<File> files;
48  
49          private final List<Long> timestamps;
50  
51          private final List<Long> sizes;
52  
53          private final List<String> ids;
54  
55          private final int hashCode;
56  
57          public CacheKey(List<Artifact> extensionArtifacts) {
58              this.files = new ArrayList<>(extensionArtifacts.size());
59              this.timestamps = new ArrayList<>(extensionArtifacts.size());
60              this.sizes = new ArrayList<>(extensionArtifacts.size());
61              this.ids = new ArrayList<>(extensionArtifacts.size());
62  
63              for (Artifact artifact : extensionArtifacts) {
64                  File file = artifact.getFile();
65                  files.add(file);
66                  timestamps.add((file != null) ? Long.valueOf(file.lastModified()) : Long.valueOf(0));
67                  sizes.add((file != null) ? Long.valueOf(file.length()) : Long.valueOf(0));
68                  ids.add(artifact.getVersion());
69              }
70  
71              this.hashCode =
72                      31 * files.hashCode() + 31 * ids.hashCode() + 31 * timestamps.hashCode() + 31 * sizes.hashCode();
73          }
74  
75          @Override
76          public int hashCode() {
77              return hashCode;
78          }
79  
80          @Override
81          public boolean equals(Object o) {
82              if (o == this) {
83                  return true;
84              }
85  
86              if (!(o instanceof CacheKey)) {
87                  return false;
88              }
89  
90              CacheKey other = (CacheKey) o;
91  
92              return ids.equals(other.ids)
93                      && files.equals(other.files)
94                      && timestamps.equals(other.timestamps)
95                      && sizes.equals(other.sizes);
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 }