View Javadoc
1   package org.apache.maven.shared.transfer.project;
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 org.codehaus.plexus.classworlds.realm.ClassRealm;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  /**
27   * This util class will import the Aether library available from the installed Maven distribution. It will do nothing if
28   * it is called from outside of a ClassRealm.
29   *
30   * @since 0.11.1
31   * @author Gabriel Belingueres <a href="mailto:belingueres@gmail.com">belingueres@gmail.com</a>
32   */
33  public class MavenAetherUtils
34  {
35  
36      private static final Logger LOGGER = LoggerFactory.getLogger( MavenAetherUtils.class );
37  
38      private static final String NO_SUCH_REALM_EXCEPTION = "org.codehaus.plexus.classworlds.realm.NoSuchRealmException";
39  
40      /**
41       * Import the core Aether library from the maven distribution.
42       */
43      public static void importAetherLibrary()
44      {
45          ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
46          if ( isClassRealm( classLoader ) )
47          {
48              importAether( classLoader );
49          }
50      }
51  
52      /**
53       * Imports aether-util library from the user's Maven distribution.
54       * <p>
55       * PRECONDITION: the classLoader parameter is an instance of ClassRealm.
56       * </p>
57       *
58       * @param classLoader the Classloader which needs to access aether-util.
59       */
60      private static void importAether( ClassLoader classLoader )
61      {
62          ClassRealm classRealm = (ClassRealm) classLoader;
63          try
64          {
65              classRealm.importFrom( "plexus.core", "org.eclipse.aether.util" );
66          }
67          catch ( Exception e )
68          {
69              if ( NO_SUCH_REALM_EXCEPTION.equals( e.getClass().getCanonicalName() ) )
70              {
71                  LOGGER.info( "'plexus.core' ClassRealm could not be found. "
72                      + "Ignore this message if you are using the library outside of a Maven execution.", e );
73              }
74              else
75              {
76                  // another exception
77                  LOGGER.error( "Unexpected exception when importing Aether library to the '{}' ClassRealm", classLoader,
78                                e );
79              }
80          }
81      }
82  
83      /**
84       * Using reflection, check if the Classloader is actually an instance of a ClassRealm.
85       *
86       * @param classLoader the Classloader to test.
87       * @return true if it an instance of ClassRealm; false otherwise.
88       */
89      private static boolean isClassRealm( ClassLoader classLoader )
90      {
91          for ( Class<?> clazz = classLoader.getClass(); clazz != null; clazz = clazz.getSuperclass() )
92          {
93              if ( "org.codehaus.plexus.classworlds.realm.ClassRealm".equals( clazz.getCanonicalName() ) )
94              {
95                  return true;
96              }
97          }
98          return false;
99      }
100 }