View Javadoc

1   package org.apache.maven.plugin.coreit;
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.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.Mojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  
26  import java.io.File;
27  import java.util.Properties;
28  
29  /**
30   * Checks whether API classes exported by the Maven core are assignment-compatible with types loaded from the plugin
31   * class loader. In other words, checks that types shared with the core realm are imported into the plugin realm.
32   * 
33   * @goal assignment-compatible
34   * @phase initialize
35   * 
36   * @author Benjamin Bentmann
37   */
38  public class AssignmentCompatibleMojo
39      extends AbstractMojo
40  {
41  
42      /**
43       * The path to the properties file used to track the results of the assignment compatibility tests.
44       * 
45       * @parameter expression="${clsldr.assigncompatPropertiesFile}"
46       */
47      private File assigncompatPropertiesFile;
48  
49      /**
50       * The qualified names of the types to check.
51       * 
52       * @parameter
53       */
54      private String[] classNames;
55  
56      /**
57       * Runs this mojo.
58       * 
59       * @throws MojoExecutionException If the output file could not be created.
60       */
61      public void execute()
62          throws MojoExecutionException
63      {
64          ClassLoader coreRealm = Mojo.class.getClassLoader();
65          ClassLoader pluginRealm = getClass().getClassLoader();
66  
67          if ( coreRealm == pluginRealm )
68          {
69              throw new MojoExecutionException( "Unexpected class loader hierarchy, could not detect core realm" );
70          }
71  
72          Properties properties = new Properties();
73  
74          if ( classNames != null )
75          {
76              for ( int i = 0; i < classNames.length; i++ )
77              {
78                  String className = classNames[i];
79                  String result;
80  
81                  getLog().info( "[MAVEN-CORE-IT-LOG] Loading class " + className );
82  
83                  try
84                  {
85                      Class type = pluginRealm.loadClass( className );
86                      result = getKey( type );
87                  }
88                  catch ( ClassNotFoundException e )
89                  {
90                      result = "";
91                  }
92                  properties.setProperty( "plugin." + className, result );
93                  getLog().info( "[MAVEN-CORE-IT-LOG]   plugin: " + result );
94  
95                  try
96                  {
97                      Class type = coreRealm.loadClass( className );
98                      result = getKey( type );
99                  }
100                 catch ( ClassNotFoundException e )
101                 {
102                     result = "";
103                 }
104                 properties.setProperty( "core." + className, result );
105                 getLog().info( "[MAVEN-CORE-IT-LOG]   core  : " + result );
106             }
107         }
108 
109         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + assigncompatPropertiesFile );
110 
111         PropertiesUtil.write( assigncompatPropertiesFile, properties );
112 
113         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + assigncompatPropertiesFile );
114     }
115 
116     private String getKey( Class type )
117     {
118         return type.hashCode() + "@" + type.getClassLoader().hashCode();
119     }
120 
121 }