001package org.apache.maven.plugin;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025import java.util.Map;
026import java.util.concurrent.ConcurrentHashMap;
027
028import org.apache.maven.model.Plugin;
029import org.apache.maven.project.MavenProject;
030import org.codehaus.plexus.component.annotations.Component;
031import org.eclipse.aether.RepositorySystemSession;
032import org.eclipse.aether.artifact.Artifact;
033import org.eclipse.aether.graph.DependencyFilter;
034import org.eclipse.aether.repository.LocalRepository;
035import org.eclipse.aether.repository.RemoteRepository;
036import org.eclipse.aether.repository.WorkspaceRepository;
037
038/**
039 * @author Igor Fedorenko
040 * @author Benjamin Bentmann
041 */
042@Component( role = PluginArtifactsCache.class )
043public class DefaultPluginArtifactsCache
044    implements PluginArtifactsCache
045{
046
047    protected static class CacheKey
048        implements Key
049    {
050
051        private final Plugin plugin;
052
053        private final WorkspaceRepository workspace;
054
055        private final LocalRepository localRepo;
056
057        private final List<RemoteRepository> repositories;
058
059        private final DependencyFilter filter;
060
061        private final int hashCode;
062
063        public CacheKey( Plugin plugin, DependencyFilter extensionFilter, List<RemoteRepository> repositories,
064                         RepositorySystemSession session )
065        {
066            this.plugin = plugin.clone();
067            workspace = CacheUtils.getWorkspace( session );
068            this.localRepo = session.getLocalRepository();
069            this.repositories = new ArrayList<RemoteRepository>( repositories.size() );
070            for ( RemoteRepository repository : repositories )
071            {
072                if ( repository.isRepositoryManager() )
073                {
074                    this.repositories.addAll( repository.getMirroredRepositories() );
075                }
076                else
077                {
078                    this.repositories.add( repository );
079                }
080            }
081            this.filter = extensionFilter;
082
083            int hash = 17;
084            hash = hash * 31 + CacheUtils.pluginHashCode( plugin );
085            hash = hash * 31 + hash( workspace );
086            hash = hash * 31 + hash( localRepo );
087            hash = hash * 31 + CacheUtils.repositoriesHashCode( repositories );
088            hash = hash * 31 + hash( extensionFilter );
089            this.hashCode = hash;
090        }
091
092        @Override
093        public String toString()
094        {
095            return plugin.getId();
096        }
097
098        @Override
099        public int hashCode()
100        {
101            return hashCode;
102        }
103
104        private static int hash( Object obj )
105        {
106            return obj != null ? obj.hashCode() : 0;
107        }
108
109        @Override
110        public boolean equals( Object o )
111        {
112            if ( o == this )
113            {
114                return true;
115            }
116
117            if ( !( o instanceof CacheKey ) )
118            {
119                return false;
120            }
121
122            CacheKey that = (CacheKey) o;
123
124            return CacheUtils.pluginEquals( plugin, that.plugin ) && eq( workspace, that.workspace )
125                && eq( localRepo, that.localRepo ) && CacheUtils.repositoriesEquals( repositories, that.repositories )
126                && eq( filter, that.filter );
127        }
128
129        private static <T> boolean eq( T s1, T s2 )
130        {
131            return s1 != null ? s1.equals( s2 ) : s2 == null;
132        }
133
134    }
135
136    protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<Key, CacheRecord>();
137
138    public Key createKey( Plugin plugin, DependencyFilter extensionFilter, List<RemoteRepository> repositories,
139                          RepositorySystemSession session )
140    {
141        return new CacheKey( plugin, extensionFilter, repositories, session );
142    }
143
144    public CacheRecord get( Key key )
145        throws PluginResolutionException
146    {
147        CacheRecord cacheRecord = cache.get( key );
148
149        if ( cacheRecord != null && cacheRecord.exception != null )
150        {
151            throw cacheRecord.exception;
152        }
153
154        return cacheRecord;
155    }
156
157    public CacheRecord put( Key key, List<Artifact> pluginArtifacts )
158    {
159        if ( pluginArtifacts == null )
160        {
161            throw new NullPointerException();
162        }
163
164        assertUniqueKey( key );
165
166        CacheRecord record =
167            new CacheRecord( Collections.unmodifiableList( new ArrayList<Artifact>( pluginArtifacts ) ) );
168
169        cache.put( key, record );
170
171        return record;
172    }
173
174    protected void assertUniqueKey( Key key )
175    {
176        if ( cache.containsKey( key ) )
177        {
178            throw new IllegalStateException( "Duplicate artifact resolution result for plugin " + key );
179        }
180    }
181
182    public CacheRecord put( Key key, PluginResolutionException exception )
183    {
184        if ( exception == null )
185        {
186            throw new NullPointerException();
187        }
188
189        assertUniqueKey( key );
190
191        CacheRecord record = new CacheRecord( exception );
192
193        cache.put( key, record );
194
195        return record;
196    }
197
198    public void flush()
199    {
200        cache.clear();
201    }
202
203    protected static int pluginHashCode( Plugin plugin )
204    {
205        return CacheUtils.pluginHashCode( plugin );
206    }
207
208    protected static boolean pluginEquals( Plugin a, Plugin b )
209    {
210        return CacheUtils.pluginEquals( a, b );
211    }
212
213    public void register( MavenProject project, Key cacheKey, CacheRecord record )
214    {
215        // default cache does not track record usage
216    }
217
218}