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