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.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.maven.project.ExtensionDescriptor;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.classworlds.realm.ClassRealm;
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.sonatype.aether.artifact.Artifact;
33  
34  /**
35   * Default extension realm cache implementation. Assumes cached data does not change.
36   */
37  @Component( role = ExtensionRealmCache.class )
38  public class DefaultExtensionRealmCache
39      implements ExtensionRealmCache
40  {
41  
42      private static class CacheKey
43      {
44  
45          private final List<File> files;
46  
47          private final List<Long> timestamps;
48  
49          private final List<Long> sizes;
50  
51          private final List<String> ids;
52  
53          private final int hashCode;
54  
55          public CacheKey( List<? extends Artifact> extensionArtifacts )
56          {
57              this.files = new ArrayList<File>( extensionArtifacts.size() );
58              this.timestamps = new ArrayList<Long>( extensionArtifacts.size() );
59              this.sizes = new ArrayList<Long>( extensionArtifacts.size() );
60              this.ids = new ArrayList<String>( extensionArtifacts.size() );
61  
62              for ( Artifact artifact : extensionArtifacts )
63              {
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          {
78              return hashCode;
79          }
80  
81          @Override
82          public boolean equals( Object o )
83          {
84              if ( o == this )
85              {
86                  return true;
87              }
88  
89              if ( !( o instanceof CacheKey ) )
90              {
91                  return false;
92              }
93  
94              CacheKey other = (CacheKey) o;
95  
96              return ids.equals( other.ids ) && files.equals( other.files ) && timestamps.equals( other.timestamps )
97                  && sizes.equals( other.sizes );
98          }
99  
100     }
101 
102     private final Map<CacheKey, CacheRecord> cache = new HashMap<CacheKey, CacheRecord>();
103 
104     public CacheRecord get( List<? extends Artifact> extensionArtifacts )
105     {
106         return cache.get( new CacheKey( extensionArtifacts ) );
107     }
108 
109     public CacheRecord put( List<? extends Artifact> extensionArtifacts, ClassRealm extensionRealm,
110                             ExtensionDescriptor extensionDescriptor )
111     {
112         if ( extensionRealm == null )
113         {
114             throw new NullPointerException();
115         }
116 
117         CacheKey key = new CacheKey( extensionArtifacts );
118 
119         if ( cache.containsKey( key ) )
120         {
121             throw new IllegalStateException( "Duplicate extension realm for extension " + extensionArtifacts );
122         }
123 
124         CacheRecord record = new CacheRecord( extensionRealm, extensionDescriptor );
125 
126         cache.put( key, record );
127 
128         return record;
129     }
130 
131     public void flush()
132     {
133         cache.clear();
134     }
135 
136     public void register( MavenProject project, CacheRecord record )
137     {
138         // default cache does not track extension usage
139     }
140 
141 }