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