001package org.apache.maven.plugin.internal;
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.Map;
023
024import org.apache.maven.artifact.repository.ArtifactRepository;
025import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
026import org.apache.maven.artifact.resolver.ArtifactResolutionException;
027import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
028import org.apache.maven.execution.MavenSession;
029import org.apache.maven.model.Plugin;
030import org.apache.maven.plugin.InvalidPluginDescriptorException;
031import org.apache.maven.plugin.InvalidPluginException;
032import org.apache.maven.plugin.LegacySupport;
033import org.apache.maven.plugin.MavenPluginManager;
034import org.apache.maven.plugin.MojoExecution;
035import org.apache.maven.plugin.MojoExecutionException;
036import org.apache.maven.plugin.MojoFailureException;
037import org.apache.maven.plugin.PluginConfigurationException;
038import org.apache.maven.plugin.PluginDescriptorParsingException;
039import org.apache.maven.plugin.PluginManager;
040import org.apache.maven.plugin.PluginManagerException;
041import org.apache.maven.plugin.PluginNotFoundException;
042import org.apache.maven.plugin.PluginResolutionException;
043import org.apache.maven.plugin.descriptor.PluginDescriptor;
044import org.apache.maven.plugin.prefix.DefaultPluginPrefixRequest;
045import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
046import org.apache.maven.plugin.prefix.PluginPrefixRequest;
047import org.apache.maven.plugin.prefix.PluginPrefixResolver;
048import org.apache.maven.plugin.prefix.PluginPrefixResult;
049import org.apache.maven.plugin.version.DefaultPluginVersionRequest;
050import org.apache.maven.plugin.version.PluginVersionNotFoundException;
051import org.apache.maven.plugin.version.PluginVersionRequest;
052import org.apache.maven.plugin.version.PluginVersionResolutionException;
053import org.apache.maven.plugin.version.PluginVersionResolver;
054import org.apache.maven.project.MavenProject;
055import org.apache.maven.project.artifact.InvalidDependencyVersionException;
056import org.apache.maven.settings.Settings;
057import org.codehaus.plexus.PlexusContainer;
058import org.codehaus.plexus.component.annotations.Component;
059import org.codehaus.plexus.component.annotations.Requirement;
060import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
061
062/**
063 * @author Benjamin Bentmann
064 */
065@Component( role = PluginManager.class )
066public class DefaultPluginManager
067    implements PluginManager
068{
069
070    @Requirement
071    private PlexusContainer container;
072
073    @Requirement
074    private MavenPluginManager pluginManager;
075
076    @Requirement
077    private PluginVersionResolver pluginVersionResolver;
078
079    @Requirement
080    private PluginPrefixResolver pluginPrefixResolver;
081
082    @Requirement
083    private LegacySupport legacySupport;
084
085    public void executeMojo( MavenProject project, MojoExecution execution, MavenSession session )
086        throws MojoExecutionException, ArtifactResolutionException, MojoFailureException, ArtifactNotFoundException,
087        InvalidDependencyVersionException, PluginManagerException, PluginConfigurationException
088    {
089        throw new UnsupportedOperationException();
090    }
091
092    public Object getPluginComponent( Plugin plugin, String role, String roleHint )
093        throws PluginManagerException, ComponentLookupException
094    {
095        MavenSession session = legacySupport.getSession();
096
097        PluginDescriptor pluginDescriptor;
098        try
099        {
100            pluginDescriptor =
101                pluginManager.getPluginDescriptor( plugin, session.getCurrentProject().getRemotePluginRepositories(),
102                                                   session.getRepositorySession() );
103
104            pluginManager.setupPluginRealm( pluginDescriptor, session, null, null, null );
105        }
106        catch ( Exception e )
107        {
108            throw new PluginManagerException( plugin, e.getMessage(), e );
109        }
110
111        ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
112        try
113        {
114            Thread.currentThread().setContextClassLoader( pluginDescriptor.getClassRealm() );
115
116            return container.lookup( role, roleHint );
117        }
118        finally
119        {
120            Thread.currentThread().setContextClassLoader( oldClassLoader );
121        }
122    }
123
124    public Map getPluginComponents( Plugin plugin, String role )
125        throws ComponentLookupException, PluginManagerException
126    {
127        MavenSession session = legacySupport.getSession();
128
129        PluginDescriptor pluginDescriptor;
130        try
131        {
132            pluginDescriptor =
133                pluginManager.getPluginDescriptor( plugin, session.getCurrentProject().getRemotePluginRepositories(),
134                                                   session.getRepositorySession() );
135
136            pluginManager.setupPluginRealm( pluginDescriptor, session, null, null, null );
137        }
138        catch ( Exception e )
139        {
140            throw new PluginManagerException( plugin, e.getMessage(), e );
141        }
142
143        ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
144        try
145        {
146            Thread.currentThread().setContextClassLoader( pluginDescriptor.getClassRealm() );
147
148            return container.lookupMap( role );
149        }
150        finally
151        {
152            Thread.currentThread().setContextClassLoader( oldClassLoader );
153        }
154    }
155
156    public Plugin getPluginDefinitionForPrefix( String prefix, MavenSession session, MavenProject project )
157    {
158        PluginPrefixRequest request = new DefaultPluginPrefixRequest( prefix, session );
159        request.setPom( project.getModel() );
160
161        try
162        {
163            PluginPrefixResult result = pluginPrefixResolver.resolve( request );
164
165            Plugin plugin = new Plugin();
166            plugin.setGroupId( result.getGroupId() );
167            plugin.setArtifactId( result.getArtifactId() );
168
169            return plugin;
170        }
171        catch ( NoPluginFoundForPrefixException e )
172        {
173            return null;
174        }
175    }
176
177    public PluginDescriptor getPluginDescriptorForPrefix( String prefix )
178    {
179        MavenSession session = legacySupport.getSession();
180
181        PluginPrefixRequest request = new DefaultPluginPrefixRequest( prefix, session );
182
183        try
184        {
185            PluginPrefixResult result = pluginPrefixResolver.resolve( request );
186
187            Plugin plugin = new Plugin();
188            plugin.setGroupId( result.getGroupId() );
189            plugin.setArtifactId( result.getArtifactId() );
190
191            return loadPluginDescriptor( plugin, session.getCurrentProject(), session );
192        }
193        catch ( Exception e )
194        {
195            return null;
196        }
197    }
198
199    public PluginDescriptor loadPluginDescriptor( Plugin plugin, MavenProject project, MavenSession session )
200        throws ArtifactResolutionException, PluginVersionResolutionException, ArtifactNotFoundException,
201        InvalidVersionSpecificationException, InvalidPluginException, PluginManagerException, PluginNotFoundException,
202        PluginVersionNotFoundException
203    {
204        return verifyPlugin( plugin, project, session.getSettings(), session.getLocalRepository() );
205    }
206
207    public PluginDescriptor loadPluginFully( Plugin plugin, MavenProject project, MavenSession session )
208        throws ArtifactResolutionException, PluginVersionResolutionException, ArtifactNotFoundException,
209        InvalidVersionSpecificationException, InvalidPluginException, PluginManagerException, PluginNotFoundException,
210        PluginVersionNotFoundException
211    {
212        PluginDescriptor pluginDescriptor = loadPluginDescriptor( plugin, project, session );
213
214        try
215        {
216            pluginManager.setupPluginRealm( pluginDescriptor, session, null, null, null );
217        }
218        catch ( PluginResolutionException e )
219        {
220            throw new PluginManagerException( plugin, e.getMessage(), e );
221        }
222
223        return pluginDescriptor;
224    }
225
226    public PluginDescriptor verifyPlugin( Plugin plugin, MavenProject project, Settings settings,
227                                          ArtifactRepository localRepository )
228        throws ArtifactResolutionException, PluginVersionResolutionException, ArtifactNotFoundException,
229        InvalidVersionSpecificationException, InvalidPluginException, PluginManagerException, PluginNotFoundException,
230        PluginVersionNotFoundException
231    {
232        MavenSession session = legacySupport.getSession();
233
234        if ( plugin.getVersion() == null )
235        {
236            PluginVersionRequest versionRequest =
237                new DefaultPluginVersionRequest( plugin, session.getRepositorySession(),
238                                                 project.getRemotePluginRepositories() );
239            plugin.setVersion( pluginVersionResolver.resolve( versionRequest ).getVersion() );
240        }
241
242        try
243        {
244            return pluginManager.getPluginDescriptor( plugin, project.getRemotePluginRepositories(),
245                                                      session.getRepositorySession() );
246        }
247        catch ( PluginResolutionException e )
248        {
249            throw new PluginNotFoundException( plugin, project.getPluginArtifactRepositories() );
250        }
251        catch ( PluginDescriptorParsingException e )
252        {
253            throw new PluginManagerException( plugin, e.getMessage(), e );
254        }
255        catch ( InvalidPluginDescriptorException e )
256        {
257            throw new PluginManagerException( plugin, e.getMessage(), e );
258        }
259    }
260
261}