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 List<String> parentImports;
62  
63          private final DependencyFilter filter;
64  
65          private final int hashCode;
66  
67          public CacheKey( Plugin plugin, ClassLoader parentRealm, List<String> parentImports,
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.parentImports = ( parentImports != null ) ? parentImports : Collections.<String> emptyList();
88              this.filter = dependencyFilter;
89  
90              int hash = 17;
91              hash = hash * 31 + CacheUtils.pluginHashCode( plugin );
92              hash = hash * 31 + hash( workspace );
93              hash = hash * 31 + hash( localRepo );
94              hash = hash * 31 + CacheUtils.repositoriesHashCode( repositories );
95              hash = hash * 31 + hash( parentRealm );
96              hash = hash * 31 + this.parentImports.hashCode();
97              hash = hash * 31 + hash( dependencyFilter );
98              this.hashCode = hash;
99          }
100 
101         @Override
102         public String toString()
103         {
104             return plugin.getId();
105         }
106 
107         @Override
108         public int hashCode()
109         {
110             return hashCode;
111         }
112 
113         private static int hash( Object obj )
114         {
115             return obj != null ? obj.hashCode() : 0;
116         }
117 
118         @Override
119         public boolean equals( Object o )
120         {
121             if ( o == this )
122             {
123                 return true;
124             }
125 
126             if ( !( o instanceof CacheKey ) )
127             {
128                 return false;
129             }
130 
131             CacheKey that = (CacheKey) o;
132 
133             return parentRealm == that.parentRealm && CacheUtils.pluginEquals( plugin, that.plugin )
134                 && eq( workspace, that.workspace ) && eq( localRepo, that.localRepo )
135                 && CacheUtils.repositoriesEquals( this.repositories, that.repositories ) && eq( filter, that.filter )
136                 && eq( parentImports, that.parentImports );
137         }
138 
139         private static <T> boolean eq( T s1, T s2 )
140         {
141             return s1 != null ? s1.equals( s2 ) : s2 == null;
142         }
143 
144     }
145 
146     protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<Key, CacheRecord>();
147 
148     public Key createKey( Plugin plugin, ClassLoader parentRealm, List<String> parentImports,
149                           DependencyFilter dependencyFilter, List<RemoteRepository> repositories,
150                           RepositorySystemSession session )
151     {
152         return new CacheKey( plugin, parentRealm, parentImports, dependencyFilter, repositories, session );
153     }
154 
155     public CacheRecord get( Key key )
156     {
157         return cache.get( key );
158     }
159 
160     public CacheRecord put( Key key, ClassRealm pluginRealm, List<Artifact> pluginArtifacts )
161     {
162         if ( pluginRealm == null || pluginArtifacts == null )
163         {
164             throw new IllegalArgumentException();
165         }
166 
167         if ( cache.containsKey( key ) )
168         {
169             throw new IllegalStateException( "Duplicate plugin realm for plugin " + key );
170         }
171 
172         CacheRecord record = new CacheRecord( pluginRealm, pluginArtifacts );
173 
174         cache.put( key, record );
175 
176         return record;
177     }
178 
179     public void flush()
180     {
181         cache.clear();
182     }
183 
184     protected static int pluginHashCode( Plugin plugin )
185     {
186         return CacheUtils.pluginHashCode( plugin );
187     }
188 
189     protected static boolean pluginEquals( Plugin a, Plugin b )
190     {
191         return CacheUtils.pluginEquals( a, b );
192     }
193 
194     public void register( MavenProject project, CacheRecord record )
195     {
196         // default cache does not track plugin usage
197     }
198 
199 }