1 package org.codehaus.plexus.maven.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
40
41
42
43
44
45
46
47
48 public class PlexusCheckRoleHintsMojo extends AbstractMojo
49 {
50
51
52
53
54
55
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
82
83
84
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
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
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
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 }