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.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  
26  import java.io.File;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Properties;
32  
33  /**
34   * Checks whether objects obtained from the Maven core are assignment-compatible with types loaded from the plugin class
35   * loader. In other words, checks that types shared with the core realm are imported into the plugin realm.
36   * 
37   * @goal instanceof
38   * @phase initialize
39   * 
40   * @author Benjamin Bentmann
41   * @version $Id: InstanceofMojo.java 799883 2009-08-01 15:06:37Z bentmann $
42   */
43  public class InstanceofMojo
44      extends AbstractMojo
45  {
46  
47      /**
48       * The path to the properties file used to track the results of the instanceof tests.
49       * 
50       * @parameter expression="${clsldr.instanceofPropertiesFile}"
51       */
52      private File instanceofPropertiesFile;
53  
54      /**
55       * The qualified name of the type to which the objects should be assignment-compatible. This type will be loaded
56       * from the plugin class loader, just like as if it was imported in the plugin source code.
57       * 
58       * @parameter expression="${clsldr.className}"
59       */
60      private String className;
61  
62      /**
63       * A list of expressions that denote the object instances that should be type-checked.
64       * 
65       * @parameter
66       */
67      private String[] objectExpressions;
68  
69      /**
70       * A list of injected component instances that should be type-checked.
71       * 
72       * @component role="org.apache.maven.plugin.coreit.Component"
73       */
74      private List components;
75  
76      /**
77       * The current Maven project against which expressions are evaluated.
78       * 
79       * @parameter default-value="${project}"
80       * @readonly
81       */
82      private Object project;
83  
84      /**
85       * Runs this mojo.
86       * 
87       * @throws MojoExecutionException If the output file could not be created.
88       */
89      public void execute()
90          throws MojoExecutionException, MojoFailureException
91      {
92          Class type;
93          try
94          {
95              getLog().info( "[MAVEN-CORE-IT-LOG] Loading class " + className );
96              type = getClass().getClassLoader().loadClass( className );
97              getLog().info( "[MAVEN-CORE-IT-LOG] Loaded class from " + type.getClassLoader() );
98          }
99          catch ( ClassNotFoundException e )
100         {
101             throw new MojoExecutionException( "Failed to load type " + className, e );
102         }
103 
104         Properties instanceofProperties = new Properties();
105 
106         if ( objectExpressions != null && objectExpressions.length > 0 )
107         {
108             Map contexts = new HashMap();
109             contexts.put( "project", project );
110             contexts.put( "pom", project );
111 
112             for ( int i = 0; i < objectExpressions.length; i++ )
113             {
114                 String expression = objectExpressions[i];
115                 getLog().info( "[MAVEN-CORE-IT-LOG] Evaluating expression " + expression );
116                 Object object = ExpressionUtil.evaluate( expression, contexts );
117                 getLog().info( "[MAVEN-CORE-IT-LOG] Checking object " + object );
118                 if ( object != null )
119                 {
120                     getLog().info( "[MAVEN-CORE-IT-LOG]   Loaded class " + object.getClass().getName() );
121                     getLog().info( "[MAVEN-CORE-IT-LOG]   Loaded class from " + object.getClass().getClassLoader() );
122                 }
123                 instanceofProperties.setProperty( expression.replace( '/', '.' ),
124                                                   Boolean.toString( type.isInstance( object ) ) );
125             }
126         }
127 
128         if ( components != null && !components.isEmpty() )
129         {
130             for ( Iterator it = components.iterator(); it.hasNext(); )
131             {
132                 Object object = it.next();
133                 getLog().info( "[MAVEN-CORE-IT-LOG] Checking component " + object );
134                 getLog().info( "[MAVEN-CORE-IT-LOG]   Loaded class " + object.getClass().getName() );
135                 getLog().info( "[MAVEN-CORE-IT-LOG]   Loaded class from " + object.getClass().getClassLoader() );
136                 instanceofProperties.setProperty( object.getClass().getName(),
137                                                   Boolean.toString( type.isInstance( object ) ) );
138             }
139         }
140 
141         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + instanceofPropertiesFile );
142 
143         PropertiesUtil.write( instanceofPropertiesFile, instanceofProperties );
144 
145         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + instanceofPropertiesFile );
146     }
147 
148 }