1 package org.apache.maven.plugin.coreit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Properties;
34
35
36
37
38
39
40
41
42
43 public class CheckMojo
44 extends AbstractMojo
45 {
46
47
48
49
50
51
52
53 private File basedir;
54
55
56
57
58
59
60 private Map componentMap;
61
62
63
64
65
66
67 private List componentList;
68
69
70
71
72
73
74 private File outputFile;
75
76
77
78
79
80
81 public void execute()
82 throws MojoExecutionException, MojoFailureException
83 {
84 Properties componentProperties = new Properties();
85
86 getLog().info( "[MAVEN-CORE-IT-LOG] Dumping component info" );
87
88 componentProperties.setProperty( "count", Integer.toString( componentList.size() ) );
89
90 if ( componentList.size() != componentMap.size() )
91 {
92 throw new MojoExecutionException( "Inconsistent collection: " + componentList + " vs " + componentMap );
93 }
94
95 for ( int i = componentList.size() - 1; i >= 0; i-- )
96 {
97 Object component = componentList.get( i );
98
99 if ( component != componentList.get( i ) )
100 {
101 throw new MojoExecutionException( "Invalid re-lookup of component from list: " + i );
102 }
103 }
104
105 int i = 0;
106 for ( Iterator it = componentMap.keySet().iterator(); it.hasNext(); i++ )
107 {
108 String roleHint = (String) it.next();
109 componentProperties.setProperty( "component." + i + ".hint", roleHint );
110
111 Object component = componentMap.get( roleHint );
112
113 if ( component != null )
114 {
115 String hash = Integer.toString( System.identityHashCode( component ) );
116 componentProperties.setProperty( "component." + i + ".hash", hash );
117 componentProperties.setProperty( "component." + roleHint + ".hash", hash );
118 }
119
120 if ( component != componentMap.get( roleHint ) )
121 {
122 throw new MojoExecutionException( "Invalid re-lookup of component from map: " + roleHint );
123 }
124
125 getLog().info( "[MAVEN-CORE-IT-LOG] " + roleHint + " = " + component );
126 }
127
128 if ( !outputFile.isAbsolute() )
129 {
130 outputFile = new File( basedir, outputFile.getPath() );
131 }
132
133 getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + outputFile );
134
135 OutputStream out = null;
136 try
137 {
138 outputFile.getParentFile().mkdirs();
139 out = new FileOutputStream( outputFile );
140 componentProperties.store( out, "MAVEN-CORE-IT-LOG" );
141 }
142 catch ( IOException e )
143 {
144 throw new MojoExecutionException( "Output file could not be created: " + outputFile, e );
145 }
146 finally
147 {
148 if ( out != null )
149 {
150 try
151 {
152 out.close();
153 }
154 catch ( IOException e )
155 {
156
157 }
158 }
159 }
160
161 getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + outputFile );
162 }
163
164 }