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.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.concurrent.ConcurrentHashMap;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.model.Plugin;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.classworlds.realm.ClassRealm;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.sonatype.aether.RepositorySystemSession;
34  import org.sonatype.aether.graph.DependencyFilter;
35  import org.sonatype.aether.repository.LocalRepository;
36  import org.sonatype.aether.repository.RemoteRepository;
37  import org.sonatype.aether.repository.WorkspaceRepository;
38  
39  /**
40   * Default PluginCache implementation. Assumes cached data does not change.
41   */
42  @Component( role = PluginRealmCache.class )
43  public class DefaultPluginRealmCache
44      implements PluginRealmCache
45  {
46  
47      protected static class CacheKey
48          implements Key
49      {
50  
51          private final Plugin plugin;
52  
53          private final WorkspaceRepository workspace;
54  
55          private final LocalRepository localRepo;
56  
57          private final List<RemoteRepository> repositories;
58  
59          private final ClassLoader parentRealm;
60  
61          private final Map<String, ClassLoader> foreignImports;
62  
63          private final DependencyFilter filter;
64  
65          private final int hashCode;
66  
67          public CacheKey( Plugin plugin, ClassLoader parentRealm, Map<String, ClassLoader> foreignImports,
68                           DependencyFilter dependencyFilter, List<RemoteRepository> repositories,
69                           RepositorySystemSession session )
70          {
71              this.plugin = plugin.clone();
72              this.workspace = CacheUtils.getWorkspace( session );
73              this.localRepo = session.getLocalRepository();
74              this.repositories = new ArrayList<RemoteRepository>( repositories.size() );
75              for ( RemoteRepository repository : repositories )
76              {
77                  if ( repository.isRepositoryManager() )
78                  {
79                      this.repositories.addAll( repository.getMirroredRepositories() );
80                  }
81                  else
82                  {
83                      this.repositories.add( repository );
84                  }
85              }
86              this.parentRealm = parentRealm;
87              this.foreignImports =
88                  ( foreignImports != null ) ? foreignImports : Collections.<String, ClassLoader> emptyMap();
89              this.filter = dependencyFilter;
90  
91              int hash = 17;
92              hash = hash * 31 + CacheUtils.pluginHashCode( plugin );
93              hash = hash * 31 + hash( workspace );
94              hash = hash * 31 + hash( localRepo );
95              hash = hash * 31 + CacheUtils.repositoriesHashCode( repositories );
96              hash = hash * 31 + hash( parentRealm );
97              hash = hash * 31 + this.foreignImports.hashCode();
98              hash = hash * 31 + hash( dependencyFilter );
99              this.hashCode = hash;
100         }
101 
102         @Override
103         public String toString()
104         {
105             return plugin.getId();
106         }
107 
108         @Override
109         public int hashCode()
110         {
111             return hashCode;
112         }
113 
114         private static int hash( Object obj )
115         {
116             return obj != null ? obj.hashCode() : 0;
117         }
118 
119         @Override
120         public boolean equals( Object o )
121         {
122             if ( o == this )
123             {
124                 return true;
125             }
126 
127             if ( !( o instanceof CacheKey ) )
128             {
129                 return false;
130             }
131 
132             CacheKey that = (CacheKey) o;
133 
134             return parentRealm == that.parentRealm && CacheUtils.pluginEquals( plugin, that.plugin )
135                 && eq( workspace, that.workspace ) && eq( localRepo, that.localRepo )
136                 && CacheUtils.repositoriesEquals( this.repositories, that.repositories ) && eq( filter, that.filter )
137                 && eq( foreignImports, that.foreignImports );
138         }
139 
140         private static <T> boolean eq( T s1, T s2 )
141         {
142             return s1 != null ? s1.equals( s2 ) : s2 == null;
143         }
144 
145     }
146 
147     protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<Key, CacheRecord>();
148 
149     public Key createKey( Plugin plugin, ClassLoader parentRealm, Map<String, ClassLoader> foreignImports,
150                           DependencyFilter dependencyFilter, List<RemoteRepository> repositories,
151                           RepositorySystemSession session )
152     {
153         return new CacheKey( plugin, parentRealm, foreignImports, dependencyFilter, repositories, session );
154     }
155 
156     public CacheRecord get( Key key )
157     {
158         return cache.get( key );
159     }
160 
161     public CacheRecord put( Key key, ClassRealm pluginRealm, List<Artifact> pluginArtifacts )
162     {
163         if ( pluginRealm == null || pluginArtifacts == null )
164         {
165             throw new IllegalArgumentException();
166         }
167 
168         if ( cache.containsKey( key ) )
169         {
170             throw new IllegalStateException( "Duplicate plugin realm for plugin " + key );
171         }
172 
173         CacheRecord record = new CacheRecord( pluginRealm, pluginArtifacts );
174 
175         cache.put( key, record );
176 
177         return record;
178     }
179 
180     public void flush()
181     {
182         cache.clear();
183     }
184 
185     protected static int pluginHashCode( Plugin plugin )
186     {
187         return CacheUtils.pluginHashCode( plugin );
188     }
189 
190     protected static boolean pluginEquals( Plugin a, Plugin b )
191     {
192         return CacheUtils.pluginEquals( a, b );
193     }
194 
195     public void register( MavenProject project, CacheRecord record )
196     {
197         // default cache does not track plugin usage
198     }
199 
200 }