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.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   * Checks the general retrieval of components from active component collections.
37   *
38   * @goal check
39   * @phase validate
40   *
41   * @author Benjamin Bentmann
42   */
43  public class CheckMojo
44      extends AbstractMojo
45  {
46  
47      /**
48       * Project base directory used for manual path alignment.
49       *
50       * @parameter default-value="${basedir}"
51       * @readonly
52       */
53      private File basedir;
54  
55      /**
56       * The available components, as a map.
57       *
58       * @component role="org.apache.maven.plugin.coreit.Component"
59       */
60      private Map componentMap;
61  
62      /**
63       * The available components, as a list.
64       *
65       * @component role="org.apache.maven.plugin.coreit.Component"
66       */
67      private List componentList;
68  
69      /**
70       * The path to the properties file to create.
71       *
72       * @parameter property="collections.outputFile"
73       */
74      private File outputFile;
75  
76      /**
77       * Runs this mojo.
78       *
79       * @throws MojoFailureException If the output file could not be created.
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                     // just ignore
157                 }
158             }
159         }
160 
161         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + outputFile );
162     }
163 
164 }