View Javadoc
1   package org.codehaus.plexus.maven.plugin;
2   
3   /*
4    * Copyright (c) 2004-2005, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  import org.apache.maven.plugin.AbstractMojo;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.MojoFailureException;
33  import org.jdom.Document;
34  import org.jdom.Element;
35  import org.jdom.JDOMException;
36  import org.jdom.input.SAXBuilder;
37  
38  /**
39   * PlexusCheckRoleHintsMojo 
40   *
41   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
42   * @version $Id$
43   * 
44   * @goal check-role-hints
45   * 
46   * @phase test
47   */
48  public class PlexusCheckRoleHintsMojo extends AbstractMojo
49  {
50      /**
51       * The META-INF/plexus/ directory that contains the post-processed 
52       * plexus data files. [ components.xml, application.xml, and plexus.xml ]
53       * 
54       * @parameter expression="${project.build.outputDirectory}/META-INF/plexus"
55       * @required
56       */
57      private File plexusDirectory;
58  
59      public void execute() throws MojoExecutionException, MojoFailureException
60      {
61          File componentsXml = new File( plexusDirectory, "components.xml" );
62          if ( componentsXml.exists() )
63          {
64              checkPlexusRoleHints( componentsXml );
65          }
66  
67          File applicationXml = new File( plexusDirectory, "application.xml" );
68          if ( applicationXml.exists() )
69          {
70              checkPlexusRoleHints( applicationXml );
71          }
72  
73          File plexusXml = new File( plexusDirectory, "plexus.xml" );
74          if ( plexusXml.exists() )
75          {
76              checkPlexusRoleHints( plexusXml );
77          }
78      }
79  
80      /**
81       * Load document via jdom, perform some basic checks.
82       * 
83       * @param componentsXml
84       * @throws MojoFailureException 
85       */
86      private void checkPlexusRoleHints( File componentsXml ) throws MojoExecutionException, MojoFailureException
87      {
88          int violationCount = 0;
89          SAXBuilder builder = new SAXBuilder();
90  
91          try
92          {
93              Document doc = builder.build( componentsXml );
94  
95              Element root = doc.getRootElement();
96  
97              if ( !root.getName().equals( "component-set" ) )
98              {
99                  getLog().warn( "Not a plexus components.xml - doesn't start with <component-set>" );
100                 return;
101             }
102 
103             List componentsList = root.getChildren( "components" );
104             for ( Iterator itcomponents = componentsList.iterator(); itcomponents.hasNext(); )
105             {
106                 Element components = (Element) itcomponents.next();
107                 violationCount += countComponentRoleHintViolations( components );
108             }
109 
110             if ( violationCount > 0 )
111             {
112                 throw new MojoFailureException( componentsXml.getAbsolutePath() + " has " + violationCount
113                                 + " role-hint violation(s)." );
114             }
115         }
116         catch ( JDOMException e )
117         {
118             throw new MojoExecutionException( "Unable to load " + componentsXml.getAbsolutePath()
119                             + ", it is not valid.", e );
120         }
121         catch ( IOException e )
122         {
123             throw new MojoExecutionException( "Unable to load " + componentsXml.getAbsolutePath() + ": "
124                             + e.getMessage(), e );
125         }
126     }
127 
128     private int countComponentRoleHintViolations( Element components )
129     {
130         int violationCount = 0;
131 
132         List componentList = components.getChildren( "component" );
133         for ( Iterator itcomponent = componentList.iterator(); itcomponent.hasNext(); )
134         {
135             Element component = (Element) itcomponent.next();
136             String componentRole = component.getChildText( "role" );
137 
138             // Test component definition.
139             Element componentRoleHint = component.getChild( "role-hint" );
140             if ( componentRoleHint == null )
141             {
142                 violationCount++;
143                 getLog().error( "Missing <role-hint> on component definition for <role> " + componentRole );
144             }
145 
146             // Test requirements.
147             List requirementsList = component.getChildren( "requirements" );
148             for ( Iterator itrequirements = requirementsList.iterator(); itrequirements.hasNext(); )
149             {
150                 Element requirements = (Element) itrequirements.next();
151                 violationCount += countRequirementRoleHintViolations( requirements );
152             }
153         }
154 
155         return violationCount;
156     }
157 
158     private int countRequirementRoleHintViolations( Element requirements )
159     {
160         int violationCount = 0;
161 
162         List requirementList = requirements.getChildren( "requirement" );
163         for ( Iterator itrequirement = requirementList.iterator(); itrequirement.hasNext(); )
164         {
165             Element requirement = (Element) itrequirement.next();
166             String requirementRole = requirement.getChildText( "role" );
167 
168             // Test requirement definition.
169             Element requirementRoleHint = requirement.getChild( "role-hint" );
170             if ( requirementRoleHint == null )
171             {
172                 violationCount++;
173                 getLog().error( "Missing <role-hint> on <requirement> definition for <role> " + requirementRole );
174             }
175         }
176 
177         return violationCount;
178     }
179 }