View Javadoc
1   package org.apache.maven.cli;
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 java.io.IOException;
23  import java.io.InputStream;
24  import java.util.Date;
25  import java.util.Locale;
26  import java.util.Properties;
27  import java.util.TimeZone;
28  
29  import org.codehaus.plexus.util.IOUtil;
30  import org.codehaus.plexus.util.Os;
31  import org.slf4j.Logger;
32  
33  /**
34   * Utility class used to report errors, statistics, application version info, etc.
35   *
36   * @author jdcasey
37   *
38   */
39  public final class CLIReportingUtils
40  {
41  
42      public static final long MB = 1024 * 1024;
43  
44      private static final long ONE_SECOND = 1000L;
45      private static final long ONE_MINUTE = 60 * ONE_SECOND;
46      private static final long ONE_HOUR = 60 * ONE_MINUTE;
47      private static final long ONE_DAY = 24 * ONE_HOUR;
48  
49      public static final String BUILD_VERSION_PROPERTY = "version";
50  
51      public static String showVersion()
52      {
53          final String LS = System.getProperty( "line.separator" );
54          Properties properties = getBuildProperties();
55          StringBuilder version = new StringBuilder();
56          version.append( createMavenVersionString( properties ) ).append( LS );
57          version.append( reduce( properties.getProperty( "distributionShortName" ) + " home: "
58                              + System.getProperty( "maven.home", "<unknown maven home>" ) ) ).append( LS );
59          version.append( "Java version: " ).append(
60              System.getProperty( "java.version", "<unknown java version>" ) ).append( ", vendor: " ).append(
61              System.getProperty( "java.vendor", "<unknown vendor>" ) ).append( LS );
62          version.append( "Java home: " ).append( System.getProperty( "java.home", "<unknown java home>" ) ).append( LS );
63          version.append( "Default locale: " ).append( Locale.getDefault() ).append( ", platform encoding: " ).append(
64              System.getProperty( "file.encoding", "<unknown encoding>" ) ).append( LS );
65          version.append( "OS name: \"" ).append( Os.OS_NAME ).append( "\", version: \"" ).append( Os.OS_VERSION ).append(
66              "\", arch: \"" ).append( Os.OS_ARCH ).append( "\", family: \"" ).append( Os.OS_FAMILY ).append( "\"" );
67          return version.toString();
68      }
69  
70      /**
71       * Create a human readable string containing the Maven version, buildnumber, and time of build
72       *
73       * @param buildProperties The build properties
74       * @return Readable build info
75       */
76      static String createMavenVersionString( Properties buildProperties )
77      {
78          String timestamp = reduce( buildProperties.getProperty( "timestamp" ) );
79          String version = reduce( buildProperties.getProperty( BUILD_VERSION_PROPERTY ) );
80          String rev = reduce( buildProperties.getProperty( "buildNumber" ) );
81          String distributionName = reduce( buildProperties.getProperty( "distributionName" ) );
82  
83          String msg = distributionName + " ";
84          msg += ( version != null ? version : "<version unknown>" );
85          if ( rev != null || timestamp != null )
86          {
87              msg += " (";
88              msg += ( rev != null ? rev : "" );
89              if ( timestamp != null )
90              {
91                  String ts = formatTimestamp( Long.valueOf( timestamp ) );
92                  msg += ( rev != null ? "; " : "" ) + ts;
93              }
94              msg += ")";
95          }
96          return msg;
97      }
98  
99      private static String reduce( String s )
100     {
101         return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null );
102     }
103 
104     static Properties getBuildProperties()
105     {
106         Properties properties = new Properties();
107         InputStream resourceAsStream = null;
108         try
109         {
110             resourceAsStream = MavenCli.class.getResourceAsStream( "/org/apache/maven/messages/build.properties" );
111 
112             if ( resourceAsStream != null )
113             {
114                 properties.load( resourceAsStream );
115             }
116         }
117         catch ( IOException e )
118         {
119             System.err.println( "Unable determine version from JAR file: " + e.getMessage() );
120         }
121         finally
122         {
123             IOUtil.close( resourceAsStream );
124         }
125 
126         return properties;
127     }
128 
129     public static void showError( Logger logger, String message, Throwable e, boolean showStackTrace )
130     {
131         if ( showStackTrace )
132         {
133             logger.error( message, e );
134         }
135         else
136         {
137             logger.error( message );
138 
139             if ( e != null )
140             {
141                 logger.error( e.getMessage() );
142 
143                 for ( Throwable cause = e.getCause(); cause != null; cause = cause.getCause() )
144                 {
145                     logger.error( "Caused by: " + cause.getMessage() );
146                 }
147             }
148         }
149     }
150 
151     public static String formatTimestamp( long timestamp )
152     {
153         // Manual construction of the tz offset because only Java 7 is aware of ISO 8601 time zones
154         TimeZone tz = TimeZone.getDefault();
155         int offset = tz.getRawOffset();
156 
157         // Raw offset ignores DST, so check if we are in DST now and add the offset
158         if ( tz.inDaylightTime( new Date( timestamp ) ) )
159         {
160             offset += tz.getDSTSavings();
161         }
162 
163         long m = Math.abs( ( offset / ONE_MINUTE ) % 60 );
164         long h = Math.abs( ( offset / ONE_HOUR ) % 24 );
165 
166         int offsetDir = (int) Math.signum( (float) offset );
167         char offsetSign = offsetDir >= 0 ? '+' : '-';
168         return String.format( "%tFT%<tT%s%02d:%02d", timestamp, offsetSign, h, m );
169     }
170 
171     public static String formatDuration( long duration )
172     {
173         long ms = duration % 1000;
174         long s = ( duration / ONE_SECOND ) % 60;
175         long m = ( duration / ONE_MINUTE ) % 60;
176         long h = ( duration / ONE_HOUR ) % 24;
177         long d = duration / ONE_DAY;
178 
179         String format;
180         if ( d > 0 )
181         {
182             format = "%d d %02d:%02d h";
183         }
184         else if ( h > 0 )
185         {
186             format = "%2$02d:%3$02d h";
187         }
188         else if ( m > 0 )
189         {
190             format = "%3$02d:%4$02d min";
191         }
192         else
193         {
194             format = "%4$d.%5$03d s";
195         }
196 
197         return String.format( format, d, h, m, s, ms );
198     }
199 
200 }