View Javadoc
1   package org.apache.maven.plugins.help;
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.commons.lang3.time.DateFormatUtils;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.codehaus.plexus.util.StringUtils;
26  import org.codehaus.plexus.util.cli.CommandLineUtils;
27  
28  import java.io.IOException;
29  import java.util.Properties;
30  
31  /**
32   * Displays a list of the platform details like system properties and environment variables.
33   *
34   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
35   * @version $Id$
36   * @since 2.1
37   */
38  @Mojo( name = "system", requiresProject = false )
39  public class SystemMojo
40      extends AbstractHelpMojo
41  {
42      /** Magic number to beautify the output */
43      private static final int REPEAT = 25;
44  
45      // ----------------------------------------------------------------------
46      // Public methods
47      // ----------------------------------------------------------------------
48  
49      /** {@inheritDoc} */
50      public void execute()
51          throws MojoExecutionException
52      {
53          StringBuilder message = new StringBuilder();
54  
55          message.append( LS );
56          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
57          message.append( StringUtils.repeat( "=", REPEAT ) );
58          message.append( " Platform Properties Details " );
59          message.append( StringUtils.repeat( "=", REPEAT ) ).append( LS );
60          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
61          message.append( LS );
62  
63          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
64          message.append( "System Properties" ).append( LS );
65          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
66  
67          Properties systemProperties = System.getProperties();
68          for ( String key : systemProperties.stringPropertyNames() )
69          {
70              message.append( LS );
71              message.append( key ).append( "=" ).append( systemProperties.getProperty( key ) );
72          }
73  
74          message.append( LS ).append( LS );
75          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
76          message.append( "Environment Variables" ).append( LS );
77          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
78          try
79          {
80              Properties envVars = CommandLineUtils.getSystemEnvVars();
81              for ( String key : envVars.stringPropertyNames() )
82              {
83                  message.append( LS );
84                  message.append( key ).append( "=" ).append( envVars.getProperty( key ) );
85              }
86          }
87          catch ( IOException e )
88          {
89              getLog().warn( "Unable to get the environment variables: " + e.getMessage() );
90          }
91  
92          message.append( LS );
93  
94          if ( output != null )
95          {
96              String formattedDateTime = DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT
97                  .format( System.currentTimeMillis() );
98              StringBuilder sb = new StringBuilder();
99              sb.append( "Created by: " ).append( getClass().getName() ).append( LS );
100             sb.append( "Created on: " ).append( formattedDateTime ).append( LS ).append( LS );
101             sb.append( message.toString() );
102 
103             try
104             {
105                 writeFile( output, sb );
106             }
107             catch ( IOException e )
108             {
109                 throw new MojoExecutionException( "Cannot write system report to output: " + output, e );
110             }
111 
112             getLog().info( "System report written to: " + output );
113         }
114         else
115         {
116             getLog().info( message );
117         }
118     }
119 }