View Javadoc
1   package org.apache.maven.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Objects;
27  import java.util.concurrent.ConcurrentHashMap;
28  
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.component.annotations.Component;
35  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
36  
37  /**
38   * Default extension realm cache implementation. Assumes cached data does not change.
39   */
40  @Component( role = ExtensionRealmCache.class )
41  public class DefaultExtensionRealmCache
42      implements ExtensionRealmCache, Disposable
43  {
44      /**
45       * CacheKey
46       */
47      protected static class CacheKey
48          implements Key
49      {
50  
51          private final List<File> files;
52  
53          private final List<Long> timestamps;
54  
55          private final List<Long> sizes;
56  
57          private final List<String> ids;
58  
59          private final int hashCode;
60  
61          public CacheKey( List<Artifact> extensionArtifacts )
62          {
63              this.files = new ArrayList<>( extensionArtifacts.size() );
64              this.timestamps = new ArrayList<>( extensionArtifacts.size() );
65              this.sizes = new ArrayList<>( extensionArtifacts.size() );
66              this.ids = new ArrayList<>( extensionArtifacts.size() );
67  
68              for ( Artifact artifact : extensionArtifacts )
69              {
70                  File file = artifact.getFile();
71                  files.add( file );
72                  timestamps.add( ( file != null ) ? Long.valueOf( file.lastModified() ) : Long.valueOf( 0 ) );
73                  sizes.add( ( file != null ) ? Long.valueOf( file.length() ) : Long.valueOf( 0 ) );
74                  ids.add( artifact.getVersion() );
75              }
76  
77              this.hashCode =
78                  31 * files.hashCode() + 31 * ids.hashCode() + 31 * timestamps.hashCode() + 31 * sizes.hashCode();
79          }
80  
81          @Override
82          public int hashCode()
83          {
84              return hashCode;
85          }
86  
87          @Override
88          public boolean equals( Object o )
89          {
90              if ( o == this )
91              {
92                  return true;
93              }
94  
95              if ( !( o instanceof CacheKey ) )
96              {
97                  return false;
98              }
99  
100             CacheKey other = (CacheKey) o;
101 
102             return ids.equals( other.ids ) && files.equals( other.files ) && timestamps.equals( other.timestamps )
103                 && sizes.equals( other.sizes );
104         }
105 
106         @Override
107         public String toString()
108         {
109             return files.toString();
110         }
111     }
112 
113     protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<>();
114 
115     @Override
116     public Key createKey( List<Artifact> extensionArtifacts )
117     {
118         return new CacheKey( extensionArtifacts );
119     }
120 
121     public CacheRecord get( Key key )
122     {
123         return cache.get( key );
124     }
125 
126     public CacheRecord put( Key key, ClassRealm extensionRealm, ExtensionDescriptor extensionDescriptor,
127                             List<Artifact> artifacts )
128     {
129         Objects.requireNonNull( extensionRealm, "extensionRealm cannot be null" );
130 
131         if ( cache.containsKey( key ) )
132         {
133             throw new IllegalStateException( "Duplicate extension realm for extension " + key );
134         }
135 
136         CacheRecord record = new CacheRecord( extensionRealm, extensionDescriptor, artifacts );
137 
138         cache.put( key, record );
139 
140         return record;
141     }
142 
143     public void flush()
144     {
145         for ( CacheRecord record : cache.values() )
146         {
147             ClassRealm realm = record.getRealm();
148             try
149             {
150                 realm.getWorld().disposeRealm( realm.getId() );
151             }
152             catch ( NoSuchRealmException e )
153             {
154                 // ignore
155             }
156         }
157         cache.clear();
158     }
159 
160     public void register( MavenProject project, Key key, CacheRecord record )
161     {
162         // default cache does not track extension usage
163     }
164 
165     public void dispose()
166     {
167         flush();
168     }
169 
170 }