View Javadoc
1   package org.apache.maven.reporting.exec;
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.lang.reflect.InvocationTargetException;
23  import java.lang.reflect.Method;
24  import java.util.List;
25  
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.model.Plugin;
28  import org.apache.maven.plugin.InvalidPluginDescriptorException;
29  import org.apache.maven.plugin.MavenPluginManager;
30  import org.apache.maven.plugin.PluginContainerException;
31  import org.apache.maven.plugin.PluginDescriptorParsingException;
32  import org.apache.maven.plugin.PluginResolutionException;
33  import org.apache.maven.plugin.descriptor.PluginDescriptor;
34  import org.codehaus.plexus.component.annotations.Component;
35  import org.codehaus.plexus.component.annotations.Requirement;
36  import org.codehaus.plexus.logging.Logger;
37  
38  /**
39   * 
40   */
41  @Component( role = MavenPluginManagerHelper.class )
42  public class DefaultMavenPluginManagerHelper
43      implements MavenPluginManagerHelper
44  {
45      @Requirement
46      private Logger logger;
47  
48      @Requirement
49      protected MavenPluginManager mavenPluginManager;
50  
51      private Boolean isEclipseAether;
52  
53      private Method setupPluginRealm;
54  
55      private Method getPluginDescriptor;
56  
57      private Method getRepositorySession;
58  
59      public DefaultMavenPluginManagerHelper()
60      {
61          try
62          {
63              for ( Method m : MavenPluginManager.class.getMethods() )
64              {
65                  if ( "setupPluginRealm".equals( m.getName() ) )
66                  {
67                      setupPluginRealm = m;
68                  }
69                  else if ( "getPluginDescriptor".equals( m.getName() ) )
70                  {
71                      getPluginDescriptor = m;
72                  }
73              }
74          }
75          catch ( SecurityException e )
76          {
77              logger.warn( "unable to find MavenPluginManager.setupPluginRealm() method", e );
78          }
79  
80          try
81          {
82              for ( Method m : MavenSession.class.getMethods() )
83              {
84                  if ( "getRepositorySession".equals( m.getName() ) )
85                  {
86                      getRepositorySession = m;
87                      break;
88                  }
89              }
90          }
91          catch ( SecurityException e )
92          {
93              logger.warn( "unable to find MavenSession.getRepositorySession() method", e );
94          }
95      }
96  
97      private boolean isEclipseAether()
98      {
99          if ( isEclipseAether == null )
100         {
101             try
102             {
103                 ClassLoader cl = Thread.currentThread().getContextClassLoader();
104                 cl.loadClass( "org.sonatype.aether.graph.DependencyFilter" );
105                 isEclipseAether = false;
106             }
107             catch ( ClassNotFoundException e )
108             {
109                 isEclipseAether = true;
110             }
111         }
112 
113         return isEclipseAether;
114     }
115 
116     private Object createExclusionsDependencyFilter( List<String> artifactIdsList )
117     {
118         if ( isEclipseAether() )
119         {
120             return new org.eclipse.aether.util.filter.ExclusionsDependencyFilter( artifactIdsList );
121         }
122         else
123         {
124             return new org.sonatype.aether.util.filter.ExclusionsDependencyFilter( artifactIdsList );
125         }
126     }
127 
128     @Override
129     public PluginDescriptor getPluginDescriptor( Plugin plugin, MavenSession session )
130         throws PluginResolutionException, PluginDescriptorParsingException, InvalidPluginDescriptorException
131     {
132         try
133         {
134             Object repositorySession = getRepositorySession.invoke( session );
135             List<?> remoteRepositories = session.getCurrentProject().getRemotePluginRepositories();
136 
137             return (PluginDescriptor) getPluginDescriptor.invoke( mavenPluginManager, plugin, remoteRepositories,
138                                                                   repositorySession );
139         }
140         catch ( IllegalArgumentException e )
141         {
142             logger.warn( "IllegalArgumentException during MavenPluginManager.getPluginDescriptor() call", e );
143         }
144         catch ( IllegalAccessException e )
145         {
146             logger.warn( "IllegalAccessException during MavenPluginManager.getPluginDescriptor() call", e );
147         }
148         catch ( InvocationTargetException e )
149         {
150             Throwable target = e.getTargetException();
151             if ( target instanceof PluginResolutionException )
152             {
153                 throw (PluginResolutionException) target;
154             }
155             if ( target instanceof PluginDescriptorParsingException )
156             {
157                 throw (PluginDescriptorParsingException) target;
158             }
159             if ( target instanceof InvalidPluginDescriptorException )
160             {
161                 throw (InvalidPluginDescriptorException) target;
162             }
163             if ( target instanceof RuntimeException )
164             {
165                 throw (RuntimeException) target;
166             }
167             if ( target instanceof Error )
168             {
169                 throw (Error) target;
170             }
171             logger.warn( "Exception during MavenPluginManager.getPluginDescriptor() call", e );
172         }
173 
174         return null;
175     }
176 
177     @Override
178     public void setupPluginRealm( PluginDescriptor pluginDescriptor, MavenSession session, ClassLoader parent,
179                                   List<String> imports, List<String> excludeArtifactIds )
180         throws PluginResolutionException, PluginContainerException
181     {
182         try
183         {
184             setupPluginRealm.invoke( mavenPluginManager, pluginDescriptor, session, parent, imports,
185                                      createExclusionsDependencyFilter( excludeArtifactIds ) );
186         }
187         catch ( IllegalArgumentException e )
188         {
189             logger.warn( "IllegalArgumentException during MavenPluginManager.setupPluginRealm() call", e );
190         }
191         catch ( IllegalAccessException e )
192         {
193             logger.warn( "IllegalAccessException during MavenPluginManager.setupPluginRealm() call", e );
194         }
195         catch ( InvocationTargetException e )
196         {
197             Throwable target = e.getTargetException();
198             if ( target instanceof PluginResolutionException )
199             {
200                 throw (PluginResolutionException) target;
201             }
202             if ( target instanceof PluginContainerException )
203             {
204                 throw (PluginContainerException) target;
205             }
206             if ( target instanceof RuntimeException )
207             {
208                 throw (RuntimeException) target;
209             }
210             if ( target instanceof Error )
211             {
212                 throw (Error) target;
213             }
214             logger.warn( "Exception during MavenPluginManager.setupPluginRealm() call", e );
215         }
216     }
217 }